Skip to content

Commit

Permalink
expose method
Browse files Browse the repository at this point in the history
  • Loading branch information
pshihn committed Mar 1, 2018
1 parent 12c6bed commit 253d5b2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 33 deletions.
23 changes: 13 additions & 10 deletions dist/workly.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var workly = (function () {
var workly = (function (exports) {
'use strict';

function workly(obj) {
function proxy(obj) {
let url, ourl;
if (typeof obj === 'function') {
const tos = Function.prototype.toString;
url = ourl = URL.createObjectURL(new Blob([`${tos.call(randomInt)}\n(${tos.call(worklyExport)})(${tos.call(obj)})`]));
url = ourl = URL.createObjectURL(new Blob([`${tos.call(randomInt)}\n(${tos.call(expose)})(${tos.call(obj)})`]));
} else if (typeof obj === 'string') {
url = obj;
}
if (url) {
let wrkr = new Worker(url);
if (ourl) wrkr.oURL = ourl;
return proxy(new WorklyProxy(wrkr));
return _proxy(new WorklyProxy(wrkr));
}
throw "Workly only supports functions, classes, urls";
}

function proxy(worker, path) {
function _proxy(worker, path) {
path = path || [];
return new Proxy(function () { }, {
get(_, prop, receiver) {
Expand All @@ -28,7 +28,7 @@ function proxy(worker, path) {
const p = worker.remote({ type: 'GET', path });
return p.then.bind(p);
}
return proxy(worker, path.concat(prop));
return _proxy(worker, path.concat(prop));
},
set(_, prop, value) {
return worker.remote({ type: 'SET', path: path.concat(prop), value });
Expand Down Expand Up @@ -59,7 +59,7 @@ class WorklyProxy {
if (event.data.error) {
cb[1](new Error(event.data.error));
} else {
cb[0](event.data.targetId ? proxy(new WorklyProxy(worker, event.data.targetId)) : event.data.value);
cb[0](event.data.targetId ? _proxy(new WorklyProxy(worker, event.data.targetId)) : event.data.value);
}
}
});
Expand All @@ -75,7 +75,7 @@ class WorklyProxy {
}

function randomInt() { return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); }
function worklyExport(target) {
function expose(target) {
const _target = target, _tmap = {};
const expObj = obj => {
const tid = `${Date.now()}-${randomInt()}`;
Expand Down Expand Up @@ -124,6 +124,9 @@ function worklyExport(target) {
});
}

return workly;
exports.proxy = proxy;
exports.expose = expose;

}());
return exports;

}({}));
2 changes: 1 addition & 1 deletion dist/workly.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
}
async function init() {
let WAdder = workly(Adder);
let WAdder = workly.proxy(Adder);
let instance = await new WAdder();
console.log(await instance.count);
console.log(await instance.add(23, 16));
Expand Down
2 changes: 1 addition & 1 deletion examples/function.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
return a + b;
}
async function init() {
let workerAdd = workly(busyAdd);
let workerAdd = workly.proxy(busyAdd);
console.log(await workerAdd(23, 16));
}
init();
Expand Down
18 changes: 16 additions & 2 deletions examples/worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

<head>
<title>Workly custom worker sample</title>
<script src="../dist/workly.min.js"></script>
<script src="../dist/workly.js"></script>
</head>

<body>
<script>
async function init() {
let w = workly("./worker.js");
let w = workly.proxy("./worker.js");
setTimeout(async () => {
let now = Date.now();
console.log(now);
console.log(await w.friendlyTime(now));
}, 1000);

// setTimeout(() => {

// console.log(await w.friendlyTime(now));
// }, 2000);


// console.log(friendlyTime(now - (24 * 60 * 60 * 1000)));
// console.log(friendlyTime(now - (4 * 24 * 60 * 60 * 1000)));
}
init();
</script>
Expand Down
13 changes: 2 additions & 11 deletions examples/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
importScripts('https://cdn.jsdelivr.net/npm/[email protected]/moment.min.js', '../dist/workly.min.js');
importScripts('https://cdn.jsdelivr.net/npm/[email protected]/moment.min.js', '../dist/workly.js');

function friendlyTime(value) {
return moment(value).calendar(null, {
Expand All @@ -14,13 +14,4 @@ function friendlyTime(value) {
});
}

function test() {
let now = Date.now();
console.log(friendlyTime(now));
console.log(friendlyTime(now - (24 * 60 * 60 * 1000)));
console.log(friendlyTime(now - (4 * 24 * 60 * 60 * 1000)));
}

test();

console.log(workly);
workly.expose(friendlyTime);
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export default function workly(obj) {
export function proxy(obj) {
let url, ourl;
if (typeof obj === 'function') {
const tos = Function.prototype.toString;
url = ourl = URL.createObjectURL(new Blob([`${tos.call(randomInt)}\n(${tos.call(worklyExport)})(${tos.call(obj)})`]));
url = ourl = URL.createObjectURL(new Blob([`${tos.call(randomInt)}\n(${tos.call(expose)})(${tos.call(obj)})`]));
} else if (typeof obj === 'string') {
url = obj;
}
if (url) {
let wrkr = new Worker(url);
if (ourl) wrkr.oURL = ourl;
return proxy(new WorklyProxy(wrkr));
return _proxy(new WorklyProxy(wrkr));
}
throw "Workly only supports functions, classes, urls";
}

function proxy(worker, path) {
function _proxy(worker, path) {
path = path || [];
return new Proxy(function () { }, {
get(_, prop, receiver) {
Expand All @@ -25,7 +25,7 @@ function proxy(worker, path) {
const p = worker.remote({ type: 'GET', path });
return p.then.bind(p);
}
return proxy(worker, path.concat(prop));
return _proxy(worker, path.concat(prop));
},
set(_, prop, value) {
return worker.remote({ type: 'SET', path: path.concat(prop), value });
Expand Down Expand Up @@ -56,7 +56,7 @@ class WorklyProxy {
if (event.data.error) {
cb[1](new Error(event.data.error));
} else {
cb[0](event.data.targetId ? proxy(new WorklyProxy(worker, event.data.targetId)) : event.data.value);
cb[0](event.data.targetId ? _proxy(new WorklyProxy(worker, event.data.targetId)) : event.data.value);
}
}
});
Expand All @@ -72,7 +72,7 @@ class WorklyProxy {
}

function randomInt() { return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); }
function worklyExport(target) {
export function expose(target) {
const _target = target, _tmap = {};
const expObj = obj => {
const tid = `${Date.now()}-${randomInt()}`;
Expand Down

0 comments on commit 253d5b2

Please sign in to comment.