summaryrefslogtreecommitdiff
path: root/examples/03-scheduler/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'examples/03-scheduler/index.ts')
-rw-r--r--examples/03-scheduler/index.ts47
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/03-scheduler/index.ts b/examples/03-scheduler/index.ts
new file mode 100644
index 0000000..40268fc
--- /dev/null
+++ b/examples/03-scheduler/index.ts
@@ -0,0 +1,47 @@
+import CancellationToken from 'cancellationtoken'
+
+class Scheduler {
+ private readonly _tasksAndTokens: {task: Task; token: CancellationToken}[]
+
+ public constructor() {
+ this._tasksAndTokens = []
+ this._executeNextTask()
+ }
+
+ private _executeNextTask(): void {
+ setTimeout(() => this._executeNextTask(), 1000)
+ let nextTask: Task | undefined
+ while (!nextTask && this._tasksAndTokens.length > 0) {
+ const {task, token} = this._tasksAndTokens.shift()!
+ if (!token.isCancelled) {
+ nextTask = task
+ }
+ }
+ if (nextTask) {
+ setTimeout(nextTask, 0)
+ }
+ }
+
+ public schedule(task: Task, token: CancellationToken = CancellationToken.CONTINUE): void {
+ this._tasksAndTokens.push({task, token})
+ }
+}
+
+interface Task {
+ (): void
+}
+
+const scheduler = new Scheduler()
+const {cancel, token} = CancellationToken.create()
+scheduler.schedule(task1)
+scheduler.schedule(task2, token)
+scheduler.schedule(() => process.exit(0))
+
+function task1() {
+ console.log('yay! (:')
+ cancel()
+}
+
+function task2() {
+ throw new Error('nay! ):')
+}