forked from senecajs/seneca-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseneca-browser-src.js
51 lines (41 loc) · 1.35 KB
/
seneca-browser-src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var Timers = require('timers')
var SenecaModule = require('seneca')
global.setImmediate = global.setImmediate || Timers.setImmediate
var SenecaExport = function(options, more_options) {
options = options || {}
options.legacy = options.legacy || {}
options.legacy.transport = false
var seneca = SenecaModule(options, more_options)
seneca.use(function browser() {
this.add('role:transport,hook:client,type:browser', hook_client_browser)
var tu = this.export('transport/utils')
function hook_client_browser(msg, reply) {
var seneca = this
reply({
send: function(msg, reply, meta) {
fetch('/seneca', {
credentials: 'same-origin',
method: 'post',
body: tu.stringifyJSON(tu.externalize_msg(seneca, msg, meta))
})
.then(function(response) {
if (response.ok) {
return response.json()
} else {
// FIX: handle transport errors
return null
}
})
.then(function(json) {
// FIX: seneca.reply broken in browser
var rep = tu.internalize_reply(seneca, json)
reply(rep.err, rep.out)
})
}
})
}
})
return seneca
}
SenecaExport.prototype = SenecaModule.prototype
module.exports = SenecaExport