]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: export types, support state hydration
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 13 Jan 2020 19:20:04 +0000 (20:20 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 13 Jan 2020 19:21:08 +0000 (20:21 +0100)
src/index.ts
src/store.ts

index 97b1efdcb59636f0d547c211740fe09588c03437..7c7dc06710ecbfee260aba0c8b3cc15a40b97a17 100644 (file)
@@ -1 +1,2 @@
-export { createStore } from './store'
+export { createStore, CombinedStore } from './store'
+export { StateTree, StoreGetter } from './types'
index 9cabaef7365cba5406059931a0f688e1a4aedad5..49d91cbcb82da73a10647f3de0fe18cffa2407da 100644 (file)
@@ -148,13 +148,17 @@ export function buildStore<
  * be able to reset the store instance between requests on the server
  */
 
+const storesMap = new WeakMap<
+  object,
+  Record<string, CombinedStore<any, any, any>>
+>()
+
 /**
  * Creates a `useStore` function that retrieves the store instance
  * @param id id of the store we are creating
  * @param buildState function that returns a state
  * @param getters optional object of getters
  */
-
 export function createStore<
   Id extends string,
   S extends StateTree,
@@ -162,10 +166,22 @@ export function createStore<
 >(id: Id, buildState: () => S, getters: G = {} as G) {
   let store: CombinedStore<Id, S, G> | undefined
 
-  return function useStore(forceNewStore = false): CombinedStore<Id, S, G> {
-    if (!store || forceNewStore) store = buildStore(id, buildState, getters)
-
-    useStoreDevtools(store)
+  // TODO: do we really need the boolean version for SSR? Using the request would be better
+  // as it allows async code like pending requests to use the correct store version.
+  return function useStore(
+    req?: object | boolean,
+    initialStates?: Record<Id, S>
+  ): CombinedStore<Id, S, G> {
+    if (!req || typeof req === 'boolean') {
+      if (!store || req) store = buildStore(id, buildState, getters)
+      if (initialStates && initialStates[id]) store.state = initialStates[id]
+      useStoreDevtools(store)
+    } else {
+      let stores = storesMap.get(req)
+      if (!stores) storesMap.set(req, (stores = {}))
+      store = stores[id]
+      if (!store) stores[id] = store = buildStore(id, buildState, getters)
+    }
 
     return store
   }