]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: remove replaceState
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 26 Nov 2019 19:59:36 +0000 (20:59 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 26 Nov 2019 20:00:07 +0000 (21:00 +0100)
__tests__/store.spec.ts
src/devtools.ts
src/index.ts
src/types.ts

index 6e91a94beced71edca87c670ab40f04899be582d..c042a6ca327732ec787b5e3bb82f0e18af377a0f 100644 (file)
@@ -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: {
index d2b92347102f43175ea9a2379c95a6d4e9ab4cab..1804245eba4b2e38193cb41b8cba7b4d75bc272f 100644 (file)
@@ -64,7 +64,7 @@ export function devtoolPlugin(store: Store<string, StateTree>) {
 
   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<string, StateTree>) {
   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) => {
index 1bcead4c2b43eafd1a15262aa6b870d3455c0c66..fafbb23835c3b95301a7cea98a1af4116817e2c9 100644 (file)
@@ -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<string | symbol, StoreMethod>
 ): CombinedStore<Id, S, G> {
   const state: Ref<S> = ref(buildState())
+  // TODO: do we need this?
   function replaceState(newState: S) {
     state.value = newState
   }
 
   let isListening = true
-  const subscriptions: SubscriptionCallback<S>[] = []
+  let subscriptions: SubscriptionCallback<S>[] = []
 
   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, S> = {
     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<string, StoreGetter<S>>
->(
-  id: Id,
-  buildState: () => S,
-  // @ts-ignore
-  getters: G = {}
-) {
+>(id: Id, buildState: () => S, getters: G = {} as G) {
   let store: CombinedStore<Id, S, G> | undefined
 
   function useStore(): CombinedStore<Id, S, G> {
index 431de4329aa782d031800a23da59a30bd57026b7..f3c6f51d3426c6c249b43ba54fed40d5b0c72726 100644 (file)
@@ -81,10 +81,11 @@ export interface Store<Id extends string, S extends StateTree> {
   patch(partialState: DeepPartial<S>): 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