keajs / kea

Batteries Included State Management for React
https://keajs.org/
MIT License
1.94k stars 51 forks source link

Please specify the recommended order in which actions, listeners, shared listeners must be written in kea([]) #158

Closed karthik-hr closed 7 months ago

karthik-hr commented 1 year ago

Can you share us recommended way of writing following inside kea([])? 1) events 2) afterMount 3) beforeUnmount 4) actions 5) listeners 6) sharedListeners 7) reducers 8) selectors 9) loaders from kea-loaders 10) defaults 11) path 12) connect 13) keys etc

kea([
    afterMount(({actions}) => {
       actions.init()
    }),

    listeners(({actions, sharedListeners}) => ({
        init: async () => {
           ...
        },
        doSomething: sharedListeners.doSomething
    }),

    sharedListeners(() => ({
       doSomething: () => {
          ....
       }
    }))
])

I encountered init inside listeners() not been called when afterMount is above listeners(). similarly sharedListeners.doSomething is undefined inside listeners() when sharedListeners() placed after listeners()

mariusandra commented 1 year ago

Hey @karthik-hr , usually the logic is simple: whatever is declared higher up, is accessible lower down.

That said, this list is in the order the Kea v2 -> v3 automatic codegen arranges things, and can act as a guide:

const supportedProperties = {
    props: 'kea',
    key: 'kea',
    path: 'kea',
    connect: 'kea',
    actions: 'kea',
    defaults: 'kea',
    loaders: 'kea-loaders',
    forms: 'kea-forms',
    subscriptions: 'kea-subscriptions',
    windowValues: 'kea-window-values',
    reducers: 'kea',
    selectors: 'kea',
    sharedListeners: 'kea',
    thunks: 'kea-thunk',
    listeners: 'kea',
    start: ['kea-saga', 'saga'],
    stop: ['kea-saga', 'cancelled'],
    saga: 'kea-saga',
    workers: 'kea-saga',
    takeEvery: 'kea-saga',
    takeLatest: 'kea-saga',
    actionToUrl: 'kea-router',
    urlToAction: 'kea-router',
    events: 'kea',
}