Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tags automatically when invoked #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/scheduler/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ export function tag(id: symbol | string): OptionsFn {
// apply the tag
let tag = getTag(schedule, id);

// create the tag if it doesn't exist
if (!tag) {
throw new Error(`Could not find tag with id ${String(id)}`);
tag = createTag(schedule, id);
}

dag.addEdge(tag.before, runnable);
Expand Down
46 changes: 15 additions & 31 deletions packages/scheduler/src/tests/scheduler.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';
import {
add,
after,
before,
id,
create,
createTag,
run,
tag,
} from '../scheduler';
import { add, after, before, id, create, run, tag } from '../scheduler';

describe('Scheduler', () => {
let order: string[] = [];
Expand Down Expand Up @@ -110,8 +101,6 @@ describe('Scheduler', () => {
const group1 = Symbol();
const schedule = create();

createTag(schedule, group1);

add(schedule, aFn, tag(group1), id('A'));
add(schedule, bFn, after('A'), tag(group1), id('B'));

Expand All @@ -127,8 +116,6 @@ describe('Scheduler', () => {
const group1 = Symbol();
const schedule = create();

createTag(schedule, group1);

add(schedule, aFn, tag(group1), id('A'));
add(schedule, bFn, after('A'), tag(group1), id('B'));
add(schedule, cFn, before(group1), id('C'));
Expand All @@ -148,8 +135,6 @@ describe('Scheduler', () => {
const group1 = Symbol();
const schedule = create();

createTag(schedule, group1);

add(schedule, aFn, id('A'), tag(group1), id('A'));
add(schedule, bFn, after('A'), tag(group1), id('B'));

Expand Down Expand Up @@ -194,13 +179,9 @@ describe('Scheduler', () => {

const schedule = create();

createTag(schedule, group1);
createTag(schedule, group2, before(group1));
createTag(schedule, group3, after(group1));

add(schedule, aFn, tag(group1), id('A'));
add(schedule, bFn, tag(group2), id('B'));
add(schedule, cFn, tag(group3), id('C'));
add(schedule, bFn, tag(group2), id('B'), before(group1));
add(schedule, cFn, tag(group3), id('C'), after(group1));

run(schedule, {});

Expand All @@ -211,17 +192,20 @@ describe('Scheduler', () => {
expect(order).toEqual(['B', 'A', 'C']);
});

test.fails('scheduling the same runnable multiple times does not run it multiple times', () => {
const schedule = create();
test.fails(
'scheduling the same runnable multiple times does not run it multiple times',
() => {
const schedule = create();

add(schedule, aFn, id('A'));
add(schedule, aFn, id('A'));
add(schedule, aFn, id('A'));
add(schedule, aFn, id('A'));
add(schedule, aFn, id('A'));
add(schedule, aFn, id('A'));

run(schedule, {});
run(schedule, {});

expect(aFn).toBeCalledTimes(1);
expect(aFn).toBeCalledTimes(1);

expect(order).toEqual(['A']);
});
expect(order).toEqual(['A']);
}
);
});