Skip to content

Commit f827719

Browse files
committed
feat: expose internal helper functions
1 parent e19cde6 commit f827719

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

src/workflow.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ export type WorkflowWorkerOptions<Input> = Except<
4747
}
4848

4949
export class Workflow<RunInput, Input, Output> {
50-
id
51-
private opts
52-
private queue?: WorkflowQueueInternal<Input>
50+
'id'
51+
private 'opts'
52+
private 'queue'?: WorkflowQueueInternal<Input>
53+
'~internal' = {
54+
getOrCreateQueue: this.getOrCreateQueue.bind(this),
55+
}
5356

54-
constructor(opts: WorkflowOptions<RunInput, Input, Output>) {
57+
'constructor'(opts: WorkflowOptions<RunInput, Input, Output>) {
5558
this.id = opts.id
5659
this.opts = opts
5760
}
5861

59-
async work(opts?: WorkflowWorkerOptions<Input>) {
62+
async 'work'(opts?: WorkflowWorkerOptions<Input>) {
6063
const queue = await this.getOrCreateQueue()
6164

6265
const worker = new Worker<WorkflowJobPayloadInternal<Input>>({
@@ -127,10 +130,10 @@ export class Workflow<RunInput, Input, Output> {
127130
{ wait: 10_000 },
128131
)
129132

130-
return this
133+
return worker
131134
}
132135

133-
async run(input: RunInput, opts?: WorkflowJobRunOptions<Input>) {
136+
async 'run'(input: RunInput, opts?: WorkflowJobRunOptions<Input>) {
134137
const parsedInput = this.opts.schema && (await this.opts.schema['~standard'].validate(input))
135138
if (parsedInput?.issues) throw new Error('Invalid workflow input')
136139

@@ -175,21 +178,21 @@ export class Workflow<RunInput, Input, Output> {
175178
)
176179
}
177180

178-
async runIn(input: RunInput, delayMs: number, opts?: WorkflowJobRunOptions<Input>) {
181+
async 'runIn'(input: RunInput, delayMs: number, opts?: WorkflowJobRunOptions<Input>) {
179182
return this.run(input, {
180183
delay: delayMs,
181184
...opts,
182185
})
183186
}
184187

185-
async runAt(input: RunInput, date: Date, opts?: WorkflowJobRunOptions<Input>) {
188+
async 'runAt'(input: RunInput, date: Date, opts?: WorkflowJobRunOptions<Input>) {
186189
return this.run(input, {
187190
runAt: date,
188191
...opts,
189192
})
190193
}
191194

192-
async runCron(
195+
async 'runCron'(
193196
scheduleId: string,
194197
cron: string,
195198
input: RunInput,
@@ -204,7 +207,7 @@ export class Workflow<RunInput, Input, Output> {
204207
})
205208
}
206209

207-
async runEvery(
210+
async 'runEvery'(
208211
scheduleId: string,
209212
everyMs: number,
210213
input: RunInput,
@@ -219,7 +222,7 @@ export class Workflow<RunInput, Input, Output> {
219222
})
220223
}
221224

222-
private async getOrCreateQueue() {
225+
private async 'getOrCreateQueue'() {
223226
if (!this.queue) {
224227
this.queue = new Queue({
225228
namespace: this.opts.id,
@@ -232,7 +235,7 @@ export class Workflow<RunInput, Input, Output> {
232235
return this.queue
233236
}
234237

235-
private async setupMetrics({ meter, prefix }: { meter: Meter; prefix: string }) {
238+
private async 'setupMetrics'({ meter, prefix }: { meter: Meter; prefix: string }) {
236239
const attributes = {
237240
workflow_id: this.opts.id,
238241
}

tests/workflow.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ beforeAll(() => {
1414
describe('input', () => {
1515
test('primitive', async () => {
1616
const handler = vi.fn()
17-
const workflow = await new Workflow({
17+
const workflow = new Workflow({
1818
id: randomUUID(),
1919
schema: type({
2020
name: 'string',
2121
}),
2222
run: handler,
23-
}).work()
23+
})
24+
await workflow.work()
2425
await workflow.run({ name: 'A' })
2526

2627
await vi.waitFor(() =>
@@ -33,13 +34,14 @@ describe('input', () => {
3334
})
3435
test('non-pojos', async () => {
3536
const handler = vi.fn()
36-
const workflow = await new Workflow({
37+
const workflow = new Workflow({
3738
id: randomUUID(),
3839
schema: type({
3940
date: 'Date',
4041
}),
4142
run: handler,
42-
}).work()
43+
})
44+
await workflow.work()
4345
await workflow.run({ date: new Date('2024-01-01T00:00:00.000Z') })
4446

4547
await vi.waitFor(() =>
@@ -57,14 +59,15 @@ describe('step', () => {
5759
test('only runs once', async () => {
5860
const stepHandler = vi.fn()
5961
const handler = vi.fn()
60-
const workflow = await new Workflow({
62+
const workflow = new Workflow({
6163
id: randomUUID(),
6264
run: async ({ step }) => {
6365
await step.do('test-step', stepHandler)
6466
await handler()
6567
throw new Error('error')
6668
},
67-
}).work({
69+
})
70+
await workflow.work({
6871
backoff: () => 0,
6972
})
7073

@@ -81,14 +84,15 @@ describe('step', () => {
8184
date: new Date(),
8285
}))
8386
const handler = vi.fn()
84-
const workflow = await new Workflow({
87+
const workflow = new Workflow({
8588
id: randomUUID(),
8689
run: async ({ step }) => {
8790
const result = await step.do('test-step', stepHandler)
8891
await handler(result)
8992
throw new Error('error')
9093
},
91-
}).work({
94+
})
95+
await workflow.work({
9296
backoff: () => 0,
9397
})
9498

@@ -108,20 +112,21 @@ describe('step', () => {
108112

109113
describe('groups', () => {
110114
test('random id if not specified', async () => {
111-
const workflow = await new Workflow({
115+
const workflow = new Workflow({
112116
id: randomUUID(),
113117
schema: type({
114118
name: 'string',
115119
}),
116120
run: async () => {},
117-
}).work()
121+
})
122+
await workflow.work()
118123
const job = await workflow.run({ name: 'A' })
119124

120125
expect(job.groupId).toMatch(/^[0-9a-f-]{36}$/i)
121126
})
122127

123128
test('uses specified groupId getter', async () => {
124-
const workflow = await new Workflow({
129+
const workflow = new Workflow({
125130
id: randomUUID(),
126131
schema: type({
127132
name: 'string',
@@ -130,7 +135,8 @@ describe('groups', () => {
130135
return `group-for-${input.name}`
131136
},
132137
run: async () => {},
133-
}).work()
138+
})
139+
await workflow.work()
134140
const job = await workflow.run({ name: 'A' })
135141

136142
expect(job.groupId).toBe('group-for-A')

0 commit comments

Comments
 (0)