Last active
May 30, 2022 06:25
-
-
Save victorporof/360caf41f7442c1e4829484af082affb to your computer and use it in GitHub Desktop.
Async Stack Tagging API (Non-Recurring)
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
/* --- Userland --- */ | |
function someTask() { | |
console.trace("completeWork: someTask"); | |
} | |
function someOtherTask() { | |
console.trace("completeWork: someOtherTask"); | |
} | |
function businessLogic() { | |
scheduler.scheduleUnitOfWork(someTask); | |
scheduler.scheduleUnitOfWork(someOtherTask); | |
} | |
businessLogic(); | |
scheduler.workLoop(); |
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
/* --- Framework --- */ | |
function makeScheduler() { | |
let tasks = []; | |
return { | |
scheduleUnitOfWork(f) { | |
const id = console.scheduleAsyncTask(f.name); | |
tasks.push({ id, f }); | |
}, | |
workLoop() { | |
while (tasks.length) { | |
const { id, f } = tasks.shift(); | |
console.startAsyncTask(id); | |
f(); | |
console.finishAsyncTask(id); | |
} | |
}, | |
}; | |
} | |
const scheduler = makeScheduler(); |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Async Tasks API</title> | |
</head> | |
<body> | |
<script src="framework.js"></script> | |
<script src="async-stack-tagging-non-recurring.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment