Skip to content

Commit

Permalink
feat(Worker): Supporting callback functions like setTimeout, setInterval
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
gja committed Feb 11, 2019
1 parent 31cb10b commit 880045d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/__tests__/worker_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ describe("Workers", () => {
expect(decoded).toBe("test");
});

test("It has support for delayed promises with setTimeout", async () => {
const worker = new Worker(
"foo.com",
`addEventListener('test', () => new Promise(resolve => setTimeout(() => resolve(42), 100)))`
);
const result = await worker.triggerEvent("test");
expect(result).toBe(42);
})

test("It has support for crypto and Text encoding APIs", async () => {
const worker = new Worker(
"foo.com",
Expand Down
18 changes: 17 additions & 1 deletion app/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,33 @@ class Worker {

evaluateWorkerContents(workerContents, kvStores) {
const context = {
// From fetch
Request,
Response,
Headers,

// URL Standards
URL,
URLSearchParams,

// bas64
atob,
btoa,

// Crypto
crypto,
TextDecoder,
TextEncoder,
console

// Debugging
console,

// Async
setTimeout,
setInterval,
clearTimeout,
clearInterval,

};
const script = new Script(workerContents);
script.runInContext(
Expand Down

0 comments on commit 880045d

Please sign in to comment.