git ssb

0+

cel / pull-postmsg



Commit 5f8f97e14246f9235578d60cff40ce76625f9b5b

Be a duplex stream instead of a through stream

Charles Lehner committed on 7/24/2016, 1:47:07 AM
Parent: 3483d9b13e2262c88a5b0f30ad66e581d4e8a555

Files changed

README.mdchanged
index.jschanged
README.mdView
@@ -7,12 +7,12 @@
77 var Postmsg = require('pull-postsg')
88 ```
99
1010 ```
11-var through = Postmsg(win, origin)
11+var duplex = Postmsg(win, origin)
1212 ```
1313
14-- `through`: through-stream for communication between `win` and the current
14+- `duplex`: duplex-stream for communication between `win` and the current
1515 window
1616 - `win`: window object to post messages to. If not specified, the source
1717 window for the first message received on the current window will be used.
1818 - `origin`: origin URL or path. defaults to `'*'`. This value is used when
index.jsView
@@ -19,9 +19,9 @@
1919 nextEnd: 'N',
2020 }
2121
2222 module.exports = function (win, origin) {
23- var _cb, _msg
23+ var _read, _cb, _msg
2424
2525 if(origin && origin[0] === '/') {
2626 origin = location.origin + origin
2727 }
@@ -40,43 +40,50 @@
4040 post(msg)
4141 }
4242 }
4343
44- return function (read) {
45- function onMessage(e) {
46- if(isOriginCompatible(origin, e.origin)) {
47- if(!win) gotWin(e.source)
48- if(!origin) origin = e.origin
49- switch (e.data[0]) {
50- case cmds.read: return onRead(null)
51- case cmds.readEnd: return onRead(JSON.parse(e.data.substr(1)))
52- case cmds.next: return onNext(null, e.data.substr(1))
53- case cmds.nextEnd: return onNext(JSON.parse(e.data.substr(1)))
54- }
55- }
56- }
44+ function onRead(end) {
45+ if (!_read) return post(cmds.nextEnd,
46+ JSON.stringify(new Error('missing source'), fixJSON))
47+ _read(end, function (end, data) {
48+ if (end) post(cmds.nextEnd, JSON.stringify(end, fixJSON))
49+ else post(cmds.next, data)
50+ })
51+ }
5752
58- function onRead(end) {
59- read(end, function (end, data) {
60- if (end) post(cmds.nextEnd, JSON.stringify(end, fixJSON))
61- else post(cmds.next, data)
62- })
63- }
53+ function onNext(end, data) {
54+ if (end) window.removeEventListener('message', onMessage, false)
55+ if (!_cb) return console.error('unexpected data', end, data)
56+ var cb = _cb
57+ _cb = null
58+ return cb(end, data)
59+ }
6460
65- function onNext(end, data) {
66- if (end) window.removeEventListener('message', onMessage, false)
67- if (!_cb) return console.error('unexpected data', end, data)
68- var cb = _cb
69- _cb = null
70- return cb(end, data)
61+ function onMessage(e) {
62+ if(isOriginCompatible(origin, e.origin)) {
63+ if(!win) gotWin(e.source)
64+ if(!origin) origin = e.origin
65+ switch (e.data[0]) {
66+ case cmds.read: return onRead(null)
67+ case cmds.readEnd: return onRead(JSON.parse(e.data.substr(1)))
68+ case cmds.next: return onNext(null, e.data.substr(1))
69+ case cmds.nextEnd: return onNext(JSON.parse(e.data.substr(1)))
70+ }
7171 }
72+ }
7273
73- window.addEventListener('message', onMessage, false)
74+ window.addEventListener('message', onMessage, false)
7475
75- return function (end, cb) {
76- if(_cb) return cb(new Error('read too soon'))
76+ return {
77+ source: function (end, cb) {
78+ if(_cb) return
7779 _cb = cb
7880 if (end) post(cmds.readEnd, JSON.stringify(end, fixJSON))
7981 else post(cmds.read)
82+ },
83+
84+ sink: function (read) {
85+ _read = read
86+ onRead()
8087 }
8188 }
8289 }

Built with git-ssb-web