Skip to content

Commit

Permalink
feat: add init cb to wf start
Browse files Browse the repository at this point in the history
  • Loading branch information
mav-rik committed Jul 28, 2023
1 parent ecd510f commit 840a718
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/event-wf/src/composables/wf-state.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useWFContext } from '../event-wf'

export function useWfState() {
const { store } = useWFContext()
const { store, getCtx } = useWFContext()
const event = store('event')
return {
ctx: <T>() => event.get('inputContext') as T,
input: <I>() => event.get('input') as I | undefined,
schemaId: event.get('schemaId'),
indexes: () => event.get('indexes'),
resume: getCtx().resume,
}
}
2 changes: 2 additions & 0 deletions packages/event-wf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './event-wf'
export * from './wf-adapter'
export * from './types'
export * from './composables'
export { TStepHandler, TWorkflowSchema } from '@prostojs/wf'

14 changes: 9 additions & 5 deletions packages/event-wf/src/wf-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export class WooksWf<T> extends WooksAdapterBase {
return this.on<string>('WF_FLOW', id, () => id)
}

public start<I>(schemaId: string, inputContext: T, input?: I) {
return this._start(schemaId, inputContext, undefined, input)
public start<I>(schemaId: string, inputContext: T, input?: I, init?: () => void | Promise<void>) {
return this._start(schemaId, inputContext, undefined, input, init)
}

public resume<I>(schemaId: string, inputContext: T, indexes: number[], input?: I) {
return this._start(schemaId, inputContext, indexes, input)
public resume<I>(schemaId: string, inputContext: T, indexes: number[], input?: I, init?: () => void | Promise<void>) {
return this._start(schemaId, inputContext, indexes, input, init)
}

protected async _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I) {
protected async _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I, init?: () => void | Promise<void>) {
const resume = !!indexes?.length
const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
inputContext,
Expand All @@ -71,6 +71,10 @@ export class WooksWf<T> extends WooksAdapterBase {
for (const handler of handlers) {
restoreCtx()
const schemaId = (await handler()) as string
if (init) {
await init()
restoreCtx()
}
if (resume) {
result = await this.wf.resume<I>(schemaId, { context: inputContext, indexes }, input as I)
break
Expand Down

0 comments on commit 840a718

Please sign in to comment.