Skip to content

[WIP] feat(core): waitFor condition receives app state #154

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions packages/stapp/src/core/createApp/createApp.h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export type EventEpic<Payload, Meta, State> = (
*/
export type Epic<State> = EventEpic<any, any, State>

export type WaitFor = Array<
export type WaitFor<State> = Array<
| AnyEventCreator
| string
| {
event: AnyEventCreator | string
timeout?: number
condition?: () => boolean
condition?: (state: Partial<State>) => boolean
}
>

Expand All @@ -60,7 +60,7 @@ export type Module<Api, State, Full extends Partial<State> = State> = {
// Api
events?: Api
api?: Api
waitFor?: WaitFor
waitFor?: WaitFor<Full>

// State
reducers?: ReducersMap<State>
Expand Down
28 changes: 28 additions & 0 deletions packages/stapp/src/core/createApp/createApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,34 @@ describe('createApp', () => {
await null
expect(resolved).toBe(true)
})

it('condition function should receive state', async () => {
let state: any
const module: Module<any, any> = {
name: 'test',
state: {
t: createReducer('test')
},
waitFor: [
{
event: fire1,
condition(s) {
state = s
return true
}
}
]
}

createApp({
modules: [module]
})

await null
expect(state).toEqual({
t: 'test'
})
})
})

describe('app compatibility', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/stapp/src/core/createApp/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const createApp: CreateApp = <Api, State, Extra>(config: {
const moduleDependencies = new Set<string>()
const reducers: any = {}
const api: any = {}
let waitFor: WaitFor = []
let waitFor: WaitFor<State> = []

const subscriptions: Subscription[] = []

Expand Down
5 changes: 3 additions & 2 deletions packages/stapp/src/core/createApp/getReadyPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const isEvent = (ev: any): ev is AnyEventCreator | string => {
export const getReadyPromise = <State>(
event$: Observable<Event<any, any>>,
getState: () => Partial<State>,
waitFor: WaitFor
waitFor: WaitFor<State>
): Promise<Partial<State>> => {
const initialState = getState()
const [promise, resolve] = controlledPromise<Partial<State>>()
const toWait = new Set<string>(
waitFor.reduce(
Expand All @@ -24,7 +25,7 @@ export const getReadyPromise = <State>(
return result
}

if (event.condition && !event.condition()) {
if (event.condition && !event.condition(initialState)) {
return result
}

Expand Down