Skip to content

Commit

Permalink
Fix type generics
Browse files Browse the repository at this point in the history
  • Loading branch information
krispya committed Jun 1, 2024
1 parent 7ab7ed7 commit a272e64
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
36 changes: 18 additions & 18 deletions packages/scheduler/src/class/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runnable<any>>;
export class Scheduler<T extends Scheduler.Context = Scheduler.Context> {
dag: DirectedGraph<Runnable<T>>;
tags: Map<symbol | string, Tag<any>>;
symbols: Map<symbol | string, Runnable<any>>;
symbols: Map<symbol | string, Runnable<T>>;

constructor() {
this.dag = new DirectedGraph<Runnable<any>>();
this.tags = new Map<symbol | string, Tag<any>>();
this.symbols = new Map<symbol | string, Runnable<any>>();
this.dag = new DirectedGraph<Runnable<T>>();
this.tags = new Map<symbol | string, Tag<T>>();
this.symbols = new Map<symbol | string, Runnable<T>>();
}

add(runnable: Runnable<any>, options?: OptionsObject): Scheduler {
const optionsFns = createOptionsFns(options);
add(runnable: Runnable<T>, options?: OptionsObject): Scheduler<T> {
const optionsFns = createOptionsFns<T>(options);
add(this, runnable, ...optionsFns);

return this;
}

run(context: Scheduler.Context): Scheduler {
run(context: T): Scheduler<T> {
run(this, context);
return this;
}

build(): Scheduler {
build(): Scheduler<T> {
build(this);
return this;
}

remove(runnable: Runnable): Scheduler {
remove(runnable: Runnable): Scheduler<T> {
remove(this, runnable);
return this;
}

debug(): Scheduler {
debug(): Scheduler<T> {
debug(this);
return this;
}

createTag(id: symbol | string, options?: OptionsObject): Scheduler {
const optionsFns = createOptionsFns(options);
createTag(id: symbol | string, options?: OptionsObject): Scheduler<T> {
const optionsFns = createOptionsFns<T>(options);
createTag(this, id, ...optionsFns);
return this;
}

removeTag(id: symbol | string): Scheduler {
removeTag(id: symbol | string): Scheduler<T> {
removeTag(this, id);
return this;
}
Expand All @@ -65,7 +65,7 @@ export class Scheduler {
return this.tags.has(id);
}

getRunnable(id: symbol | string): Runnable<any> | undefined {
getRunnable(id: symbol | string): Runnable<T> | undefined {
return this.symbols.get(id);
}
}
6 changes: 4 additions & 2 deletions packages/scheduler/src/class/utils/create-options-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>[] {
const optionsFns: OptionsFn[] = [];

if (options?.id) {
Expand Down Expand Up @@ -39,5 +41,5 @@ export function createOptionsFns(options: OptionsObject | undefined) {
optionsFns.push(tagFn(options.tag));
}
}
return optionsFns;
return optionsFns as OptionsFn<T>[];
}
58 changes: 40 additions & 18 deletions packages/scheduler/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
context: T
) {
for (let i = 0; i < schedule.dag.sorted.length; i++) {
const runnable = schedule.dag.sorted[i];
runnable(context);
Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
id: symbol | string
) {
const tag = schedule.tags.get(id);

if (!tag) {
Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
id: symbol | string
) {
return schedule.tags.has(id);
}

Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
id: symbol | string,
...options: OptionsFn[]
...options: OptionsFn<T>[]
): Tag {
if (hasTag(schedule, id)) {
if (hasTag<T>(schedule, id)) {
throw new Error(`Tag with id ${String(id)} already exists`);
}

Expand All @@ -256,7 +265,7 @@ export function createTag(

const tag = { id, before, after };

const optionParams: Options = {
const optionParams: Options<T> = {
dag: schedule.dag,
tag,
schedule,
Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
runnable: Runnable<T>,
...options: OptionsFn<T>[]
) {
if (schedule.dag.exists(runnable)) {
throw new Error('Runnable already exists in schedule');
Expand All @@ -293,7 +302,7 @@ export function add(
// add the runnable to the graph
schedule.dag.addVertex(runnable, {});

const optionParams: Options = {
const optionParams: Options<T> = {
dag: schedule.dag,
runnable,
schedule,
Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>
) {
schedule.dag.topSort();
}

Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
runnable: Runnable<T>
) {
schedule.dag.removeVertex(runnable);
}

Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
id: symbol | string
) {
return schedule.symbols.get(id);
}

Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>,
id: symbol | string
) {
return schedule.tags.get(id);
}

Expand All @@ -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<T extends Scheduler.Context = Scheduler.Context>(
schedule: Schedule<T>
) {
schedule.dag.asciiVisualize();
}

0 comments on commit a272e64

Please sign in to comment.