Skip to content

Commit 512372d

Browse files
committed
add a getInitialState method to store
1 parent 348c155 commit 512372d

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/createStore.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export function createStore<
138138
let currentState: S | PreloadedState | undefined = preloadedState as
139139
| PreloadedState
140140
| undefined
141+
let initialState: S | PreloadedState | undefined = currentState
141142
let currentListeners: Map<number, ListenerCallback> | null = new Map()
142143
let nextListeners = currentListeners
143144
let listenerIdCounter = 0
@@ -176,6 +177,15 @@ export function createStore<
176177
return currentState as S
177178
}
178179

180+
/**
181+
* Reads the initial state tree managed by the store.
182+
*
183+
* @returns The initial state tree of your application.
184+
*/
185+
function getInitialState(): S {
186+
return initialState as S
187+
}
188+
179189
/**
180190
* Adds a change listener. It will be called any time an action is dispatched,
181191
* and some part of the state tree may potentially have changed. You may then
@@ -385,10 +395,14 @@ export function createStore<
385395
// the initial state tree.
386396
dispatch({ type: ActionTypes.INIT } as A)
387397

398+
// initial state should be the state *after* the "INIT" action is dispatched
399+
initialState = currentState
400+
388401
const store = {
389402
dispatch: dispatch as Dispatch<A>,
390403
subscribe,
391404
getState,
405+
getInitialState,
392406
replaceReducer,
393407
[$$observable]: observable
394408
} as unknown as Store<S, A, StateExt> & Ext

src/types/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export interface Store<
118118
*/
119119
getState(): S & StateExt
120120

121+
getInitialState(): S & StateExt
122+
121123
/**
122124
* Adds a change listener. It will be called any time an action is
123125
* dispatched, and some part of the state tree may potentially have changed.

test/createStore.spec.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ describe('createStore', () => {
2424
// So we filter it out
2525
const methods = Object.keys(store).filter(key => key !== $$observable)
2626

27-
expect(methods.length).toBe(4)
27+
expect(methods.length).toBe(5)
2828
expect(methods).toContain('subscribe')
2929
expect(methods).toContain('dispatch')
3030
expect(methods).toContain('getState')
31+
expect(methods).toContain('getInitialState')
3132
expect(methods).toContain('replaceReducer')
3233
})
3334

@@ -926,4 +927,55 @@ describe('createStore', () => {
926927
)
927928
).toThrow()
928929
})
930+
931+
it("exposes getInitialState which returns the store's initial state", () => {
932+
const unPreloadedStore = createStore(reducers.todos)
933+
934+
expect(unPreloadedStore.getInitialState()).toEqual([])
935+
936+
unPreloadedStore.dispatch(addTodo('Hello'))
937+
938+
expect(unPreloadedStore.getInitialState()).toEqual([])
939+
940+
expect(unPreloadedStore.getState()).toEqual([
941+
{
942+
id: 1,
943+
text: 'Hello'
944+
}
945+
])
946+
947+
const preloadedStore = createStore(reducers.todos, [
948+
{
949+
id: 1,
950+
text: 'Hello'
951+
}
952+
])
953+
954+
expect(preloadedStore.getInitialState()).toEqual([
955+
{
956+
id: 1,
957+
text: 'Hello'
958+
}
959+
])
960+
961+
preloadedStore.dispatch(addTodo('World'))
962+
963+
expect(preloadedStore.getInitialState()).toEqual([
964+
{
965+
id: 1,
966+
text: 'Hello'
967+
}
968+
])
969+
970+
expect(preloadedStore.getState()).toEqual([
971+
{
972+
id: 1,
973+
text: 'Hello'
974+
},
975+
{
976+
id: 2,
977+
text: 'World'
978+
}
979+
])
980+
})
929981
})

0 commit comments

Comments
 (0)