]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: use function to build state
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 23 Nov 2019 12:23:26 +0000 (13:23 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 23 Nov 2019 12:23:26 +0000 (13:23 +0100)
__tests__/createStore.spec.ts
__tests__/store.patch.spec.ts [new file with mode: 0644]
src/index.ts

index cc416bc329616585fe932c44f6d5d52f61a62224..8a6c04343616a381dc1b6c7c1ffb4f25b554dabc 100644 (file)
@@ -2,12 +2,13 @@ import { createStore } from '../src'
 
 describe('createStore', () => {
   it('sets the initial state', () => {
-    const state = {
+    const state = () => ({
       a: true,
       nested: {
         a: { b: 'string' },
       },
-    }
+    })
+
     const store = createStore('main', state)
     expect(store.state).toEqual({
       a: true,
diff --git a/__tests__/store.patch.spec.ts b/__tests__/store.patch.spec.ts
new file mode 100644 (file)
index 0000000..bbdde42
--- /dev/null
@@ -0,0 +1,59 @@
+import { createStore } from '../src'
+
+describe('store.patch', () => {
+  function buildStore() {
+    return createStore('main', () => ({
+      // TODO: the boolean cas shouldn't be necessary
+      // https://www.typescriptlang.org/play/#code/MYewdgzgLgBCMF4YG8CwAoGWYEMBcMUATgK4CmGAvhhiAHQ6IwBmOANhBehqJLMETI4oZJgAoAlIgB8MMclwFi5GJQk10vaDGBMBQkZI3AGTVhzJA
+      a: true as boolean,
+      nested: {
+        foo: 'foo',
+        a: { b: 'string' },
+      },
+    }))
+  }
+
+  it('patches a property without touching the rest', () => {
+    const store = buildStore()
+    store.patch({ a: false })
+    expect(store.state).toEqual({
+      a: false,
+      nested: {
+        foo: 'foo',
+        a: { b: 'string' },
+      },
+    })
+  })
+
+  it('patches a nested property without touching the rest', () => {
+    const store = buildStore()
+    store.patch({ nested: { foo: 'bar' } })
+    expect(store.state).toEqual({
+      a: true,
+      nested: {
+        foo: 'bar',
+        a: { b: 'string' },
+      },
+    })
+    store.patch({ nested: { a: { b: 'hello' } } })
+    expect(store.state).toEqual({
+      a: true,
+      nested: {
+        foo: 'bar',
+        a: { b: 'hello' },
+      },
+    })
+  })
+
+  it('patches multiple properties at the same time', () => {
+    const store = buildStore()
+    store.patch({ a: false, nested: { foo: 'hello' } })
+    expect(store.state).toEqual({
+      a: false,
+      nested: {
+        foo: 'hello',
+        a: { b: 'string' },
+      },
+    })
+  })
+})
index bca3d75b022d6664e058b5de1feda60d69b41df3..27babaf124a6dc3655290d7982f9bd18ef65f93d 100644 (file)
@@ -9,21 +9,6 @@ import {
 } from './types'
 import { devtoolPlugin } from './devtools'
 
-function createState<S extends StateTree>(initialState: S) {
-  const state: Ref<S> = ref(initialState)
-
-  // type State = UnwrapRef<typeof state>
-
-  function replaceState(newState: S) {
-    state.value = newState
-  }
-
-  return {
-    state,
-    replaceState,
-  }
-}
-
 function innerPatch<T extends StateTree>(
   target: T,
   patchToApply: DeepPartial<T>
@@ -56,10 +41,13 @@ function innerPatch<T extends StateTree>(
 
 export function createStore<Id extends string, S extends StateTree>(
   id: Id,
-  initialState: S
+  buildState: () => S
   // methods: Record<string | symbol, StoreMethod>
 ): Store<Id, S> {
-  const { state, replaceState } = createState(initialState)
+  const state: Ref<S> = ref(buildState())
+  function replaceState(newState: S) {
+    state.value = newState
+  }
 
   let isListening = true
   const subscriptions: SubscriptionCallback<S>[] = []
@@ -123,12 +111,12 @@ export function createStore<Id extends string, S extends StateTree>(
 
 function makeStore<Id extends string, S extends StateTree>(
   id: Id,
-  initialState: S
+  buildState: () => S
 ) {
   let store: Store<Id, S> | undefined
 
   function useStore(): Store<Id, S> {
-    if (!store) store = createStore(id, initialState)
+    if (!store) store = createStore(id, buildState)
 
     return store
   }