Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove legacy mode and bind api to emitter #83

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 5 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const initStream = require('./stream')
const createRemoteApi = require('./remote-api')
const createLocalApi = require('./local-api')

function createMuxrpc (remoteManifest, localManifest, localApi, id, perms, codec, legacy) {
function createMuxrpc (remoteManifest, localManifest, localApi, perms, codec) {
let bootstrapCB
if (typeof remoteManifest === 'function') {
bootstrapCB = remoteManifest
Expand All @@ -17,29 +17,13 @@ function createMuxrpc (remoteManifest, localManifest, localApi, id, perms, codec
const emitter = new EventEmitter()
if (!codec) codec = PacketStreamCodec

// pass the manifest to the permissions so that it can know
// what something should be.
let _cb
const context = {
_emit (event, value) {
if (emitter) emitter._emit(event, value)
return context
},
id
}

const ws = initStream(
createLocalApi(localApi, localManifest, perms).bind(context),
createLocalApi(localApi, localManifest, perms).bind(emitter),
codec,
(err) => {
() => {
if (emitter.closed) return
emitter.closed = true
emitter.emit('closed')
if (_cb) {
const cb = _cb
_cb = null
cb(err)
}
}
)

Expand All @@ -53,26 +37,7 @@ function createMuxrpc (remoteManifest, localManifest, localApi, id, perms, codec
bootstrapCB
)

// legacy local emit, from when remote emit was supported.
emitter._emit = emitter.emit

if (legacy) {
Object.__defineGetter__.call(emitter, 'id', () => context.id)
Object.__defineSetter__.call(emitter, 'id', (value) => { context.id = value })

let first = true
emitter.createStream = (cb) => {
_cb = cb
if (first) {
first = false
return ws
} else {
throw new Error('one stream per rpc')
}
}
} else {
emitter.stream = ws
}
emitter.stream = ws

emitter.closed = false

Expand All @@ -84,11 +49,4 @@ function createMuxrpc (remoteManifest, localManifest, localApi, id, perms, codec
return emitter
}

module.exports = function (remoteManifest, localManifest, codec) {
if (arguments.length > 3) {
return createMuxrpc.apply(this, arguments)
}
return function (local, perms, id) {
return createMuxrpc(remoteManifest, localManifest, local, id, perms, codec, true)
}
}
module.exports = createMuxrpc
13 changes: 5 additions & 8 deletions test/abort.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module.exports = function (serializer) {
drainAbort: 'sink'
}

const A = mux(client, null, serializer)()
const B = mux(null, client, serializer)({
const A = mux(client, null, null, null, serializer)
const B = mux(null, client, {
drainAbort: (n) => {
return pull(
pull.through(() => {
Expand All @@ -28,20 +28,17 @@ module.exports = function (serializer) {
})
)
}
})
}, null, serializer)

const as = A.createStream()
const bs = B.createStream()

pull(as, abortable, bs, as)
pull(A.stream, abortable, B.stream, A.stream)

const sent = []

pull(
pull.values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (abort) => {
if (process.env.TEST_VERBOSE) console.log(abort)
t.ok(sent.length < 10, 'sent is correct')
t.end()
// t.end()
}),
pull.asyncMap((data, cb) => {
setImmediate(() => {
Expand Down
Loading
Loading