From: Eduardo San Martin Morote Date: Tue, 26 Nov 2019 19:59:36 +0000 (+0100) Subject: refactor: remove replaceState X-Git-Tag: v0.0.1~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=24bbce02f471f408dc6d5f29338c3f7a4492e4c8;p=thirdparty%2Fvuejs%2Fpinia.git refactor: remove replaceState --- diff --git a/__tests__/store.spec.ts b/__tests__/store.spec.ts index 6e91a94b..c042a6ca 100644 --- a/__tests__/store.spec.ts +++ b/__tests__/store.spec.ts @@ -24,7 +24,7 @@ describe('Store', () => { it('can replace its state', () => { const store = buildStore() - store.replaceState({ + store.state = { a: false, nested: { foo: 'bar', @@ -32,7 +32,7 @@ describe('Store', () => { b: 'hey', }, }, - }) + } expect(store.state).toEqual({ a: false, nested: { diff --git a/src/devtools.ts b/src/devtools.ts index d2b92347..1804245e 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -64,7 +64,7 @@ export function devtoolPlugin(store: Store) { Object.defineProperty(rootStore.state, store.id, { get: () => store.state, - set: state => store.replaceState(state), + set: state => (store.state = state), }) // Vue.set(rootStore.state, store.name, store.state) @@ -72,7 +72,7 @@ export function devtoolPlugin(store: Store) { rootStore._modulesNamespaceMap[store.id + '/'] = true devtoolHook.on('vuex:travel-to-state', targetState => { - store.replaceState(targetState[store.id]) + store.state = targetState[store.id] }) store.subscribe((mutation, state) => { diff --git a/src/index.ts b/src/index.ts index 1bcead4c..fafbb238 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,17 +54,17 @@ export function createStore< >( id: Id, buildState: () => S, - // @ts-ignore an empty object is valid for Record - getters: G = {} + getters: G = {} as G // methods: Record ): CombinedStore { const state: Ref = ref(buildState()) + // TODO: do we need this? function replaceState(newState: S) { state.value = newState } let isListening = true - const subscriptions: SubscriptionCallback[] = [] + let subscriptions: SubscriptionCallback[] = [] watch( () => state.value, @@ -98,6 +98,11 @@ export function createStore< // TODO: return function to remove subscription } + function reset() { + subscriptions = [] + state.value = buildState() + } + const storeWithState: Store = { id, // it is replaced below by a getter @@ -105,11 +110,7 @@ export function createStore< patch, subscribe, - replaceState: (newState: S) => { - isListening = false - replaceState(newState) - isListening = true - }, + reset, } // @ts-ignore we have to build it @@ -130,6 +131,11 @@ export function createStore< // make state access invisible Object.defineProperty(store, 'state', { get: () => state.value, + set: (newState: S) => { + isListening = false + state.value = newState + isListening = true + }, }) // Devtools injection hue hue @@ -154,12 +160,7 @@ export function makeStore< Id extends string, S extends StateTree, G extends Record> ->( - id: Id, - buildState: () => S, - // @ts-ignore - getters: G = {} -) { +>(id: Id, buildState: () => S, getters: G = {} as G) { let store: CombinedStore | undefined function useStore(): CombinedStore { diff --git a/src/types.ts b/src/types.ts index 431de432..f3c6f51d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,10 +81,11 @@ export interface Store { patch(partialState: DeepPartial): void /** - * Replaces current state with a completely new version. - * @param newState state object to replace current state + * Resets the store to its initial state by removing all subscriptions and + * building a new state object */ - replaceState(newState: S): void + reset(): void + /** * Setups a callback to be called whenever the state changes. * @param callback callback that is called whenever the state changes