index.jsView |
---|
19 | 19 | nextEnd: 'N', |
20 | 20 | } |
21 | 21 | |
22 | 22 | module.exports = function (win, origin) { |
23 | | - var _cb, _msg |
| 23 | + var _read, _cb, _msg |
24 | 24 | |
25 | 25 | if(origin && origin[0] === '/') { |
26 | 26 | origin = location.origin + origin |
27 | 27 | } |
40 | 40 | post(msg) |
41 | 41 | } |
42 | 42 | } |
43 | 43 | |
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 | + } |
57 | 52 | |
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 | + } |
64 | 60 | |
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 | + } |
71 | 71 | } |
| 72 | + } |
72 | 73 | |
73 | | - window.addEventListener('message', onMessage, false) |
| 74 | + window.addEventListener('message', onMessage, false) |
74 | 75 | |
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 |
77 | 79 | _cb = cb |
78 | 80 | if (end) post(cmds.readEnd, JSON.stringify(end, fixJSON)) |
79 | 81 | else post(cmds.read) |
| 82 | + }, |
| 83 | + |
| 84 | + sink: function (read) { |
| 85 | + _read = read |
| 86 | + onRead() |
80 | 87 | } |
81 | 88 | } |
82 | 89 | } |