Skip to content

Commit

Permalink
remove legacy mode and bind api to emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Nov 21, 2023
1 parent 5138b9d commit 66810fe
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 324 deletions.
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

0 comments on commit 66810fe

Please sign in to comment.