From 4cabca73450058e1fcf8e033e33cc06b0aa1a807 Mon Sep 17 00:00:00 2001 From: Kris Baumgartner Date: Thu, 30 May 2024 15:28:03 -0700 Subject: [PATCH] Create tags automatically when invoked --- packages/scheduler/src/scheduler.ts | 3 +- .../scheduler/src/tests/scheduler.test.ts | 46 ++++++------------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/packages/scheduler/src/scheduler.ts b/packages/scheduler/src/scheduler.ts index b721fb1..c83105a 100644 --- a/packages/scheduler/src/scheduler.ts +++ b/packages/scheduler/src/scheduler.ts @@ -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); diff --git a/packages/scheduler/src/tests/scheduler.test.ts b/packages/scheduler/src/tests/scheduler.test.ts index 8ab112d..eeebc9e 100644 --- a/packages/scheduler/src/tests/scheduler.test.ts +++ b/packages/scheduler/src/tests/scheduler.test.ts @@ -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[] = []; @@ -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')); @@ -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')); @@ -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')); @@ -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, {}); @@ -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']); + } + ); });