Skip to content

Commit

Permalink
add a getInitialState method to store
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Mar 14, 2024
1 parent 348c155 commit 512372d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function createStore<
let currentState: S | PreloadedState | undefined = preloadedState as
| PreloadedState
| undefined
let initialState: S | PreloadedState | undefined = currentState
let currentListeners: Map<number, ListenerCallback> | null = new Map()
let nextListeners = currentListeners
let listenerIdCounter = 0
Expand Down Expand Up @@ -176,6 +177,15 @@ export function createStore<
return currentState as S
}

/**
* Reads the initial state tree managed by the store.
*
* @returns The initial state tree of your application.
*/
function getInitialState(): S {
return initialState as S
}

/**
* Adds a change listener. It will be called any time an action is dispatched,
* and some part of the state tree may potentially have changed. You may then
Expand Down Expand Up @@ -385,10 +395,14 @@ export function createStore<
// the initial state tree.
dispatch({ type: ActionTypes.INIT } as A)

// initial state should be the state *after* the "INIT" action is dispatched
initialState = currentState

const store = {
dispatch: dispatch as Dispatch<A>,
subscribe,
getState,
getInitialState,
replaceReducer,
[$$observable]: observable
} as unknown as Store<S, A, StateExt> & Ext
Expand Down
2 changes: 2 additions & 0 deletions src/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export interface Store<
*/
getState(): S & StateExt

getInitialState(): S & StateExt

/**
* Adds a change listener. It will be called any time an action is
* dispatched, and some part of the state tree may potentially have changed.
Expand Down
54 changes: 53 additions & 1 deletion test/createStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ describe('createStore', () => {
// So we filter it out
const methods = Object.keys(store).filter(key => key !== $$observable)

expect(methods.length).toBe(4)
expect(methods.length).toBe(5)
expect(methods).toContain('subscribe')
expect(methods).toContain('dispatch')
expect(methods).toContain('getState')
expect(methods).toContain('getInitialState')
expect(methods).toContain('replaceReducer')
})

Expand Down Expand Up @@ -926,4 +927,55 @@ describe('createStore', () => {
)
).toThrow()
})

it("exposes getInitialState which returns the store's initial state", () => {
const unPreloadedStore = createStore(reducers.todos)

expect(unPreloadedStore.getInitialState()).toEqual([])

unPreloadedStore.dispatch(addTodo('Hello'))

expect(unPreloadedStore.getInitialState()).toEqual([])

expect(unPreloadedStore.getState()).toEqual([
{
id: 1,
text: 'Hello'
}
])

const preloadedStore = createStore(reducers.todos, [
{
id: 1,
text: 'Hello'
}
])

expect(preloadedStore.getInitialState()).toEqual([
{
id: 1,
text: 'Hello'
}
])

preloadedStore.dispatch(addTodo('World'))

expect(preloadedStore.getInitialState()).toEqual([
{
id: 1,
text: 'Hello'
}
])

expect(preloadedStore.getState()).toEqual([
{
id: 1,
text: 'Hello'
},
{
id: 2,
text: 'World'
}
])
})
})

0 comments on commit 512372d

Please sign in to comment.