-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
47 lines (37 loc) · 1.41 KB
/
server.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
export default function expose (funcName, func, opts) {
opts = opts || {}
const addListener = opts.addListener || window.addEventListener
const removeListener = opts.removeListener || window.removeEventListener
const postMessage = opts.postMessage || window.postMessage
const targetOrigin = opts.targetOrigin || '*'
const getMessageData = opts.getMessageData || ((event) => event.data)
const isCallback = opts.isCallback || false
const handler = function () {
const data = getMessageData.apply(null, arguments)
if (!data) return
if (data.sender !== 'postmsg-rpc/client' || data.func !== funcName) return
const msg = { sender: 'postmsg-rpc/server', id: data.id }
const onSuccess = (res) => {
msg.res = res
postMessage(msg, targetOrigin)
}
const onError = (err) => {
msg.err = Object.assign({ message: err.message }, err.output && err.output.payload)
if (process.env.NODE_ENV !== 'production') {
msg.err.stack = msg.err.stack || err.stack
}
postMessage(msg, targetOrigin)
}
if (isCallback) {
func.apply(null, data.args.concat((err, res) => {
if (err) return onError(err)
onSuccess(res)
}))
} else {
const res = func.apply(null, data.args)
Promise.resolve(res).then(onSuccess).catch(onError)
}
}
addListener('message', handler)
return { close: () => removeListener('message', handler) }
}