Skip to content

Commit

Permalink
ensure that noAutoDestroy() functions next to the original
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuhn committed Nov 23, 2023
1 parent 6b2c829 commit df0516d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
34 changes: 22 additions & 12 deletions machine-runner/src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,22 @@ export const createMachineRunnerInternal = <
// AsyncIterator part
// ==================

const nextValueAwaiter = NextValueAwaiter.make<
StateOpaque<SwarmProtocolName, MachineName, string, StateUnion>
>({
topLevelDestruction: internals.destruction,
failure: () => internals.failure,
})
type S = StateOpaque<SwarmProtocolName, MachineName, string, StateUnion>
const nextValueAwaiter = (state?: S | undefined) => {
const nva = NextValueAwaiter.make<S>({
topLevelDestruction: internals.destruction,
failure: () => internals.failure,
cloneFrom: state,
})

emitter.on('next', nva.push)
emitter.on('failure', nva.fail)
internals.destruction.addDestroyHook(nva.kill)

return nva
}

emitter.on('next', nextValueAwaiter.push)
emitter.on('failure', nextValueAwaiter.fail)
internals.destruction.addDestroyHook(nextValueAwaiter.kill)
const defaultNextValueAwaiter = nextValueAwaiter()

// Self API construction

Expand All @@ -531,7 +537,7 @@ export const createMachineRunnerInternal = <
isDestroyed: internals.destruction.isDestroyed,
noAutoDestroy: () =>
MachineRunnerIterableIterator.make({
nextValueAwaiter,
nextValueAwaiter: nextValueAwaiter(defaultNextValueAwaiter.state()),
destruction: (() => {
const childDestruction = Destruction.make()

Expand All @@ -548,7 +554,7 @@ export const createMachineRunnerInternal = <

const defaultIterator: MachineRunnerIterableIterator<SwarmProtocolName, MachineName, StateUnion> =
MachineRunnerIterableIterator.make({
nextValueAwaiter,
nextValueAwaiter: defaultNextValueAwaiter,
destruction: internals.destruction,
})

Expand Down Expand Up @@ -677,14 +683,17 @@ namespace NextValueAwaiter {
export const make = <S extends any>({
topLevelDestruction,
failure,
cloneFrom,
}: {
topLevelDestruction: Destruction
failure: () => MachineRunnerFailure | null
cloneFrom?: S
}) => {
const Done: IteratorResult<S, null> = { done: true, value: null }
const wrapAsIteratorResult = (value: S): IteratorResult<S, null> => ({ done: false, value })

let store: null | { state: S } | RequestedPromisePair<IteratorResult<S, null>> = null
let store: null | { state: S } | RequestedPromisePair<IteratorResult<S, null>> =
cloneFrom === undefined ? null : { state: cloneFrom }

/**
* Allows different level of destructions
Expand Down Expand Up @@ -743,6 +752,7 @@ namespace NextValueAwaiter {
store = { state }
}
},
state: () => (store && 'state' in store ? store.state : undefined),
generateConsumeAPI,
}
}
Expand Down
7 changes: 6 additions & 1 deletion machine-runner/tests/esm/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,12 @@ describe('machine as async generator', () => {
r1.feed([], true)

const peekResult = (await machine.peekNext()).value
for await (const state of machine.noAutoDestroy()) {
const copy = machine.noAutoDestroy()
expect(await Promise.race([machine.next(), sleep(1)])).toEqual({
done: false,
value: peekResult,
})
for await (const state of copy) {
expect(state).toBe(peekResult)
break
}
Expand Down

0 comments on commit df0516d

Please sign in to comment.