Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete test case for #84 #85

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively we could also use the .get() value here — this would mean that a .noAutoDestroy() AsyncIterable always immediately yields the current state if there is one, then waits for the next update

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
18 changes: 18 additions & 0 deletions machine-runner/tests/esm/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,24 @@ describe('machine as async generator', () => {
})

describe('non-destroying cloned async generator', () => {
it('should not wait after main generator peek', async () => {
const r1 = new Runner(On, { toggleCount: 0 })
const machine = r1.machine

r1.feed([], true)

const peekResult = (await machine.peekNext()).value
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
}
})

it('should generate the same snapshot as parent', async () => {
const r = new Runner(On, { toggleCount: 0 })
const machine = r.machine
Expand Down