-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support waiting for background tasks on the
per_worker
policy (…
…#451) * chore: update deno manifest * chore: update `.gitignore` * feat: support waiting for background tasks on the `per_worker` policy * chore: add integration tests * chore: add an example * chore: add `global.d.ts`
- Loading branch information
1 parent
388d2ea
commit 3cddc61
Showing
17 changed files
with
410 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ scripts/debug.sh | |
|
||
node_modules/ | ||
.DS_Store | ||
eszip.bin | ||
eszip.bin | ||
deno.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
function sleep(ms: number): Promise<string> { | ||
return new Promise(res => { | ||
setTimeout(() => { | ||
res("meow"); | ||
}, ms) | ||
}); | ||
} | ||
|
||
function mySlowFunction(baseNumber: number) { | ||
const now = Date.now(); | ||
let result = 0; | ||
for (let i = Math.pow(baseNumber, 7); i >= 0; i--) { | ||
result += Math.atan(i) * Math.tan(i); | ||
} | ||
const duration = Date.now() - now; | ||
return { result: result, duration: duration }; | ||
} | ||
|
||
class MyBackgroundTaskEvent extends Event { | ||
readonly taskPromise: Promise<string> | ||
|
||
constructor(taskPromise: Promise<string>) { | ||
super('myBackgroundTask') | ||
this.taskPromise = taskPromise | ||
} | ||
} | ||
|
||
globalThis.addEventListener('myBackgroundTask', async (event) => { | ||
const str = await (event as MyBackgroundTaskEvent).taskPromise | ||
console.log(str); | ||
}); | ||
|
||
|
||
export default { | ||
fetch() { | ||
// consumes lots of cpu time | ||
mySlowFunction(10); | ||
// however, this time we did not notify the runtime that it should wait for this promise. | ||
// therefore, the above console.log(str) will not be output and the worker will terminate. | ||
dispatchEvent(new MyBackgroundTaskEvent(sleep(5000))); | ||
return new Response(); | ||
} | ||
} |
Oops, something went wrong.