Last active
March 16, 2021 10:29
-
-
Save loreanvictor/0e82671b3f158de51b749ca35e0bdbc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* I think garbage collection with this scheme is definitely not enough. | |
* However, I need to double check with the protocol, and perhaps with | |
* this minimalist garbage collection scheme and a simple echoing of terminate signals | |
* we will achieve proper dereferencing. | |
* | |
*/ | |
const makeStreamLayer = (receive) => { | |
const __sent__ = {} | |
const __received__ = {} | |
const serialize = callback => { | |
callback.__id__ = callback.__id__ || makeId() | |
if (!(callback.__id__ in __sent__)) | |
__sent__[callback.__id__] = { fn: callback, greeted: [] } | |
return callback.__id__ | |
} | |
receive((id, t, d) => { | |
if (msg in __sent__) { | |
const entry = __sent__[id] | |
entry.fn(t, d) | |
if (t === 0) | |
entry.greeted.push(d) | |
if (t === 2 || t === -2) { | |
entry.greeted.forEach(fn => fn(-2, d)) | |
delete __sent__[id] | |
} | |
} | |
}) | |
const parse = (id, send) => { | |
if (!(id in __received__)) { | |
const entry = { | |
fn: (t, d) => { | |
if (t === 2 || t === -2) { | |
entry.ref-- | |
if (entry.ref === 0) { | |
__sent__.forEach(e => { | |
e.greeted = e.greeted.filter(f => f !== entry.fn) | |
}) | |
delete __received__[id] | |
} | |
} | |
send(id, t, d, serialize) | |
}, | |
ref: 1, | |
} | |
__sent__[id] = entry | |
} | |
return __received__[id] | |
} | |
return { serialize, parse } | |
} | |
/******* ANALYSIS ******** | |
A --[src]--> B | |
A.sent = [{src, []}] | |
B.rec = [{src, 1}] | |
B: src(0, sink) | |
B --[sink]--> A | |
A.sent = [{src, [sink]}] | |
A.rec = [{sink, 1}] | |
B.sent = [{sink, []}] | |
B.rec = [{src, 1}] | |
B: sink(0, talkback) | |
A --[talkback]--> B | |
A.sent = [{src, [sink]}, {talkback, []}] | |
A.rec = [{sink, 1}] | |
B.reg = [{sink, [talkback]}] | |
B.rec = [{src, 1}, {talkback, 1}] | |
--- | |
B: talkback(2) | |
A.sent = [{src, [sink]}] | |
A.rec = [{sink, 1}] | |
B.sent = [{sink, []}] | |
B.rec = [{src, 1}] | |
// --> sink(2) is missing | |
--- | |
A: sink(2) --> B: talkback(-2) | |
A.sent = [{src, []}] | |
A.rec = [] | |
B.sent = [] | |
B.rec = [] | |
--- | |
A: src(2) --> A: sink(2) | sink(-2) --> B: talkback(-2) | |
A.sent = [{src, []}] | |
A.rec = [] | |
B.sent = [] | |
B.rec = [] | |
// --> src(2) is not observed locally | |
--- | |
B: src(2) --> A: sink(2) | sink(-2) --> B: talkback(-2) | |
A.sent = [] | |
A.rec = [] | |
B.sent = [] | |
B.rec = [] | |
**********/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment