Skip to content

Commit

Permalink
fix: initializing a store without a rootSlice nor preloaded state res…
Browse files Browse the repository at this point in the history
…ulting in error
  • Loading branch information
carere committed Oct 19, 2022
1 parent f841410 commit b83134e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export const configureStore = <S extends object, C>({
You may read the docs in order to understand how to use Solux and its architecture.
`)

const [state, setState] = createStore(preloadedState ?? rootSlice.getInitialState())
const [state, setState] = createStore(
preloadedState ?? (rootSlice ? rootSlice.getInitialState() : {}),
)

const store$ = new ReplaySubject<{ state: S; event: Event }>(1)
const event$ = new Subject<Event>()
const isDevtoolsAvailable =
Expand All @@ -58,17 +61,17 @@ export const configureStore = <S extends object, C>({
console.log('DevTools requested to change the state to', state)
}
})
devTools.init(state)
devTools.init(state as S)
}

const getState: Store<S>['getState'] = () => {
return state
return state as S
}

const dispatch: Store<S>['dispatch'] = event => {
setState(produce((state: S) => rootSlice.handler(state, event)))
store$.next({ state: state, event })
if (isDevtoolsAvailable) devTools.send(event, state)
store$.next({ state: state as S, event })
if (isDevtoolsAvailable) devTools.send(event, state as S)
event$.next(event)
}

Expand All @@ -79,7 +82,7 @@ export const configureStore = <S extends object, C>({
}

if (rootEpic) {
event$.pipe(mergeMap(event => rootEpic(of(event), state, container))).subscribe(dispatch)
event$.pipe(mergeMap(event => rootEpic(of(event), state as S, container))).subscribe(dispatch)
}

return { dispatch, getState, subscribe }
Expand Down

0 comments on commit b83134e

Please sign in to comment.