Skip to content

Commit

Permalink
fix: restoreCtx before init
Browse files Browse the repository at this point in the history
  • Loading branch information
mav-rik committed Jul 28, 2023
1 parent d1e99bf commit 176969e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/event-wf/src/wf-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ export class WooksWf<T> extends WooksAdapterBase {
restoreCtx()
const { id, init } = (await handler()) as { init?: () => void | Promise<void>, id: string }
if (init) {
await init()
restoreCtx()
await init()
}
restoreCtx()
if (resume) {
result = await this.wf.resume<I>(id, { context: inputContext, indexes }, input as I)
break
Expand Down
15 changes: 14 additions & 1 deletion packages/event-wf/src/wf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRouteParams } from '@wooksjs/event-core'
import { createWfApp } from './wf-adapter'
import { useWfState } from './composables'

const app = createWfApp<{ result: number }>()
const app = createWfApp<{ result?: number }>()

app.step('add', {
input: 'number',
Expand Down Expand Up @@ -42,6 +42,14 @@ app.flow('parametric/*', [
},
])

let c = -100

app.flow('init', ['add/1'], () => {
const ctx = useWfState().ctx<{ result: number }>()
c = ctx.result
ctx.result = 0
})

describe('event-wf', () => {
it('must run simple wf', async () => {
const result = await app.start('adding', { result: 0 })
Expand All @@ -61,4 +69,9 @@ describe('event-wf', () => {
const result2 = await app.start('parametric/asdf/asdf12', { result: 10 })
expect(result2.state.context.result).toBe(17)
})
it('must call init fn', async () => {
const result = await app.start('init', {})
expect(c).toBeUndefined()
expect(result.state.context.result).toBe(1)
})
})

0 comments on commit 176969e

Please sign in to comment.