diff --git a/packages/scheduler/src/class/scheduler.ts b/packages/scheduler/src/class/scheduler.ts index 44d0cdd..f355c7b 100644 --- a/packages/scheduler/src/class/scheduler.ts +++ b/packages/scheduler/src/class/scheduler.ts @@ -8,55 +8,55 @@ import { removeTag, run, } from '../scheduler'; -import { Runnable, Tag } from '../scheduler-types'; -import { OptionsObject } from './types'; +import type { Runnable, Tag } from '../scheduler-types'; +import type { OptionsObject } from './types'; import { createOptionsFns } from './utils/create-options-fns'; -export class Scheduler { - dag: DirectedGraph>; +export class Scheduler { + dag: DirectedGraph>; tags: Map>; - symbols: Map>; + symbols: Map>; constructor() { - this.dag = new DirectedGraph>(); - this.tags = new Map>(); - this.symbols = new Map>(); + this.dag = new DirectedGraph>(); + this.tags = new Map>(); + this.symbols = new Map>(); } - add(runnable: Runnable, options?: OptionsObject): Scheduler { - const optionsFns = createOptionsFns(options); + add(runnable: Runnable, options?: OptionsObject): Scheduler { + const optionsFns = createOptionsFns(options); add(this, runnable, ...optionsFns); return this; } - run(context: Scheduler.Context): Scheduler { + run(context: T): Scheduler { run(this, context); return this; } - build(): Scheduler { + build(): Scheduler { build(this); return this; } - remove(runnable: Runnable): Scheduler { + remove(runnable: Runnable): Scheduler { remove(this, runnable); return this; } - debug(): Scheduler { + debug(): Scheduler { debug(this); return this; } - createTag(id: symbol | string, options?: OptionsObject): Scheduler { - const optionsFns = createOptionsFns(options); + createTag(id: symbol | string, options?: OptionsObject): Scheduler { + const optionsFns = createOptionsFns(options); createTag(this, id, ...optionsFns); return this; } - removeTag(id: symbol | string): Scheduler { + removeTag(id: symbol | string): Scheduler { removeTag(this, id); return this; } @@ -65,7 +65,7 @@ export class Scheduler { return this.tags.has(id); } - getRunnable(id: symbol | string): Runnable | undefined { + getRunnable(id: symbol | string): Runnable | undefined { return this.symbols.get(id); } } diff --git a/packages/scheduler/src/class/utils/create-options-fns.ts b/packages/scheduler/src/class/utils/create-options-fns.ts index a6c81cd..557bbc4 100644 --- a/packages/scheduler/src/class/utils/create-options-fns.ts +++ b/packages/scheduler/src/class/utils/create-options-fns.ts @@ -7,7 +7,9 @@ import { import { OptionsFn } from '../../scheduler-types'; import { OptionsObject } from '../types'; -export function createOptionsFns(options: OptionsObject | undefined) { +export function createOptionsFns< + T extends Scheduler.Context = Scheduler.Context +>(options: OptionsObject | undefined): OptionsFn[] { const optionsFns: OptionsFn[] = []; if (options?.id) { @@ -39,5 +41,5 @@ export function createOptionsFns(options: OptionsObject | undefined) { optionsFns.push(tagFn(options.tag)); } } - return optionsFns; + return optionsFns as OptionsFn[]; } diff --git a/packages/scheduler/src/scheduler.ts b/packages/scheduler/src/scheduler.ts index b721fb1..bbcc6d9 100644 --- a/packages/scheduler/src/scheduler.ts +++ b/packages/scheduler/src/scheduler.ts @@ -181,7 +181,10 @@ export function create< * @param {Schedule} schedule - The schedule containing the runnables to execute. * @param {Context} context - The context to be passed to each runnable. */ -export function run(schedule: Schedule, context: Scheduler.Context) { +export function run( + schedule: Schedule, + context: T +) { for (let i = 0; i < schedule.dag.sorted.length; i++) { const runnable = schedule.dag.sorted[i]; runnable(context); @@ -195,7 +198,10 @@ export function run(schedule: Schedule, context: Scheduler.Context) { * @param {symbol | string} id - The ID of the tag to remove. * @return {void} This function does not return anything. */ -export function removeTag(schedule: Schedule, id: symbol | string) { +export function removeTag( + schedule: Schedule, + id: symbol | string +) { const tag = schedule.tags.get(id); if (!tag) { @@ -215,7 +221,10 @@ export function removeTag(schedule: Schedule, id: symbol | string) { * @param {symbol | string} id - The ID of the tag to check. * @return {boolean} Returns true if the tag exists, false otherwise. */ -export function hasTag(schedule: Schedule, id: symbol | string) { +export function hasTag( + schedule: Schedule, + id: symbol | string +) { return schedule.tags.has(id); } @@ -228,12 +237,12 @@ export function hasTag(schedule: Schedule, id: symbol | string) { * @param {...OptionsFn[]} options - Additional options to customize the tag. * @return {Tag} The newly created tag. */ -export function createTag( - schedule: Schedule, +export function createTag( + schedule: Schedule, id: symbol | string, - ...options: OptionsFn[] + ...options: OptionsFn[] ): Tag { - if (hasTag(schedule, id)) { + if (hasTag(schedule, id)) { throw new Error(`Tag with id ${String(id)} already exists`); } @@ -256,7 +265,7 @@ export function createTag( const tag = { id, before, after }; - const optionParams: Options = { + const optionParams: Options = { dag: schedule.dag, tag, schedule, @@ -281,10 +290,10 @@ export function createTag( * @throws {Error} If the runnable already exists in the schedule. * @return {void} */ -export function add( - schedule: Schedule, - runnable: Runnable, - ...options: OptionsFn[] +export function add( + schedule: Schedule, + runnable: Runnable, + ...options: OptionsFn[] ) { if (schedule.dag.exists(runnable)) { throw new Error('Runnable already exists in schedule'); @@ -293,7 +302,7 @@ export function add( // add the runnable to the graph schedule.dag.addVertex(runnable, {}); - const optionParams: Options = { + const optionParams: Options = { dag: schedule.dag, runnable, schedule, @@ -314,7 +323,9 @@ export function add( * @param {Schedule} schedule - The schedule to be built. * @return {void} This function does not return anything. */ -export function build(schedule: Schedule) { +export function build( + schedule: Schedule +) { schedule.dag.topSort(); } @@ -325,7 +336,10 @@ export function build(schedule: Schedule) { * @param {Runnable} runnable - The runnable to remove from the schedule. * @return {void} This function does not return anything. */ -export function remove(schedule: Schedule, runnable: Runnable) { +export function remove( + schedule: Schedule, + runnable: Runnable +) { schedule.dag.removeVertex(runnable); } @@ -336,7 +350,10 @@ export function remove(schedule: Schedule, runnable: Runnable) { * @param {symbol | string} id - The ID of the runnable to retrieve. * @return {Runnable | undefined} The retrieved runnable or undefined if not found. */ -export function getRunnable(schedule: Schedule, id: symbol | string) { +export function getRunnable( + schedule: Schedule, + id: symbol | string +) { return schedule.symbols.get(id); } @@ -347,7 +364,10 @@ export function getRunnable(schedule: Schedule, id: symbol | string) { * @param {symbol | string} id - The ID of the tag to retrieve. * @return {Tag | undefined} The retrieved tag or undefined if not found. */ -export function getTag(schedule: Schedule, id: symbol | string) { +export function getTag( + schedule: Schedule, + id: symbol | string +) { return schedule.tags.get(id); } @@ -357,6 +377,8 @@ export function getTag(schedule: Schedule, id: symbol | string) { * @param {Schedule} schedule - The schedule containing the DAG to visualize. * @return {void} This function does not return anything. */ -export function debug(schedule: Schedule) { +export function debug( + schedule: Schedule +) { schedule.dag.asciiVisualize(); }