1 Star 0 Fork 0

nenge/libunrar-js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rpc.js 3.42 KB
一键复制 编辑 原始数据 按行查看 历史
Choi Wai Chung 提交于 2015-09-02 17:55 . Init commit
// Worker side
if (typeof document === typeof void 0) {
importScripts("Promise.min.js")
var RPC = {}
RPC.init = function(proxy) {
var rs
var loadP = new Promise(function(res, rej) {
rs = res
})
var sender = {}
sender.supportTransferable = true
sender.transferables = []
// register worker's function to main thread, and test for support of transferable objects
var ab = new ArrayBuffer(1);
try {
postMessage({
ts: 'reg',
fns: Object.keys(proxy),
dummy: ab
}, [ab])
// transferables not supported
if (ab.byteLength) {
sender.supportTransferable = false
}
} catch(e) {
// transferables not supported
// also need to re-register the functions
sender.supportTransferable = false
postMessage({
ts: 'reg',
fns: Object.keys(proxy)
})
}
addEventListener('message', function onmessage(evt) {
// register sender's function to sender obj
if (evt.data.ts === 'reg') {
evt.data.fns.forEach(function(fn) {
sender[fn] = function() {
postMessage({
ts: 'w2s',
fn: fn,
args: Array.prototype.slice.call(arguments)
}, sender.supportTransferable ? sender.transferables : null)
sender.transferables = []
}
})
rs(sender)
return
}
try {
var result = proxy[evt.data.fn].apply(proxy, evt.data.args)
postMessage({
ts: evt.data.ts,
result: result
}, sender.supportTransferable ? sender.transferables : null)
sender.transferables = []
} catch (error) {
postMessage({
ts: evt.data.ts,
error: error.toString()
})
}
})
return loadP
}
} else {
// Main thread side:
var RPC = {}
RPC.new = function(workersrc, senderProxy) {
senderProxy = senderProxy || {}
var rs
var loadP = new Promise(function(res, rej) {
rs = res
})
var obj = {}
obj.supportTransferable = true
obj.worker = new Worker(workersrc)
var ab = new ArrayBuffer(1);
// reg fns and test for transferable support
try {
obj.worker.postMessage({
ts: 'reg',
fns: Object.keys(senderProxy),
dummy: ab
}, [ab])
// transferables not supported
if (ab.byteLength) {
obj.supportTransferable = false
}
} catch(e) {
// transferables not supported
// also need to re-register the functions
obj.supportTransferable = false
obj.worker.postMessage({
ts: 'reg',
fns: Object.keys(senderProxy)
})
}
obj.pendingPromises = {}
obj.transferables = []
var that = obj
obj.worker.onmessage = function(e) {
var ts = e.data.ts
if (!ts) throw new Error("Unknown timestamp")
else if (ts === "reg") {
e.data.fns.forEach(function(fn) {
that[fn] = function() {
var ts = Date.now()
var p = new Promise(function(resolve, reject) {
that.pendingPromises[ts] = {
resolve: resolve,
reject: reject
}
})
that.worker.postMessage({
fn: fn,
args: Array.prototype.slice.call(arguments),
ts: ts
}, that.supportTransferable ? that.transferables : null)
that.transferables = []
return p
}
})
rs(obj)
return
} else if (ts === 'w2s') {
senderProxy[e.data.fn].apply(senderProxy, e.data.args)
return
}
var p = that.pendingPromises[ts]
if (!p) throw new Error("Unknown response")
var error = e.data.error
if (error) {
p.reject(error)
} else {
p.resolve(e.data.result)
}
delete that.pendingPromises[ts]
}
return loadP
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/nenge/libunrar-js.git
git@gitee.com:nenge/libunrar-js.git
nenge
libunrar-js
libunrar-js
gh-pages

搜索帮助