]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: add rootStore.ts
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 17:52:10 +0000 (18:52 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:46 +0000 (19:21 +0100)
src/index.ts
src/rootStore.ts [new file with mode: 0644]
src/store.ts
src/types.ts

index 7743f44705d5d4b30f0b510caa9b949ce8df1ac4..4ca79b37ccef4b34524f292b501232d5ea0e68b5 100644 (file)
@@ -1,8 +1,3 @@
-export {
-  createStore,
-  Store,
-  setActiveReq,
-  setStateProvider,
-  getRootState,
-} from './store'
-export { StateTree, StoreGetter } from './types'
+export { createStore } from './store'
+export { setActiveReq, setStateProvider, getRootState } from './rootStore'
+export { StateTree, StoreGetter, Store } from './types'
diff --git a/src/rootStore.ts b/src/rootStore.ts
new file mode 100644 (file)
index 0000000..2314245
--- /dev/null
@@ -0,0 +1,65 @@
+import { NonNullObject, StateTree, GenericStore } from './types'
+
+/**
+ * setActiveReq must be called to handle SSR at the top of functions like `fetch`, `setup`, `serverPrefetch` and others
+ */
+export let activeReq: NonNullObject = {}
+export const setActiveReq = (req: NonNullObject | undefined) =>
+  req && (activeReq = req)
+
+export const getActiveReq = () => activeReq
+
+/**
+ * The api needs more work we must be able to use the store easily in any
+ * function by calling `useStore` to get the store Instance and we also need to
+ * be able to reset the store instance between requests on the server
+ */
+
+export const storesMap = new WeakMap<
+  NonNullObject,
+  Record<string, GenericStore>
+>()
+
+/**
+ * A state provider allows to set how states are stored for hydration. e.g. setting a property on a context, getting a property from window
+ */
+interface StateProvider {
+  get(): Record<string, StateTree>
+  set(store: GenericStore): any
+}
+
+/**
+ * Map of initial states used for hydration
+ */
+export const stateProviders = new WeakMap<NonNullObject, StateProvider>()
+
+export function setStateProvider(stateProvider: StateProvider) {
+  stateProviders.set(getActiveReq(), stateProvider)
+}
+
+export function getInitialState(id: string): StateTree | undefined {
+  const provider = stateProviders.get(getActiveReq())
+  return provider && provider.get()[id]
+}
+
+export function setInitialState(store: GenericStore): void {
+  const provider = stateProviders.get(getActiveReq())
+  if (provider) provider.set(store)
+}
+
+/**
+ * Gets the root state of all active stores. This is useful when reporting an application crash by
+ * retrieving the problematic state and send it to your error tracking service.
+ * @param req request key
+ */
+export function getRootState(req: NonNullObject): Record<string, StateTree> {
+  const stores = storesMap.get(req)
+  if (!stores) return {}
+  const rootState = {} as Record<string, StateTree>
+
+  for (const store of Object.values(stores)) {
+    rootState[store.id] = store.state
+  }
+
+  return rootState
+}
index d33989cda8942c076f2c99a9181a33932c63a186..7781332e414bd5b4071ee7ff4768338a9f813349 100644 (file)
@@ -7,11 +7,18 @@ import {
   isPlainObject,
   StoreWithGetters,
   StoreGetter,
-  NonNullObject,
   StoreAction,
+  Store,
   StoreWithActions,
 } from './types'
 import { useStoreDevtools } from './devtools'
+import {
+  getActiveReq,
+  setActiveReq,
+  storesMap,
+  getInitialState,
+  setInitialState,
+} from './rootStore'
 
 const isClient = typeof window != 'undefined'
 
@@ -34,23 +41,6 @@ function innerPatch<T extends StateTree>(
   return target
 }
 
-/**
- * setActiveReq must be called to handle SSR at the top of functions like `fetch`, `setup`, `serverPrefetch` and others
- */
-export let activeReq: NonNullObject = {}
-export const setActiveReq = (req: NonNullObject | undefined) =>
-  req && (activeReq = req)
-
-export const getActiveReq = () => activeReq
-
-// has the actions without the context (this) for typings
-export type Store<
-  Id extends string,
-  S extends StateTree,
-  G extends Record<string, StoreGetter<S>>,
-  A extends Record<string, StoreAction>
-> = StoreWithState<Id, S> & StoreWithGetters<S, G> & StoreWithActions<A>
-
 /**
  * Creates a store instance
  * @param id unique identifier of the store, like a name. eg: main, cart, user
@@ -166,61 +156,6 @@ export function buildStore<
   return store
 }
 
-/**
- * The api needs more work we must be able to use the store easily in any
- * function by calling `useStore` to get the store Instance and we also need to
- * be able to reset the store instance between requests on the server
- */
-
-export const storesMap = new WeakMap<
-  NonNullObject,
-  Record<string, Store<any, any, any, any>>
->()
-
-/**
- * A state provider allows to set how states are stored for hydration. e.g. setting a property on a context, getting a property from window
- */
-interface StateProvider {
-  get(): Record<string, StateTree>
-  set(store: Store<string, StateTree, any, any>): any
-}
-
-/**
- * Map of initial states used for hydration
- */
-export const stateProviders = new WeakMap<NonNullObject, StateProvider>()
-
-export function setStateProvider(stateProvider: StateProvider) {
-  stateProviders.set(getActiveReq(), stateProvider)
-}
-
-function getInitialState(id: string): StateTree | undefined {
-  const provider = stateProviders.get(getActiveReq())
-  return provider && provider.get()[id]
-}
-
-function setInitialState(store: Store<string, StateTree, any, any>): void {
-  const provider = stateProviders.get(getActiveReq())
-  if (provider) provider.set(store)
-}
-
-/**
- * Gets the root state of all active stores. This is useful when reporting an application crash by
- * retrieving the problematic state and send it to your error tracking service.
- * @param req request key
- */
-export function getRootState(req: NonNullObject): Record<string, StateTree> {
-  const stores = storesMap.get(req)
-  if (!stores) return {}
-  const rootState = {} as Record<string, StateTree>
-
-  for (const store of Object.values(stores)) {
-    rootState[store.id] = store.state
-  }
-
-  return rootState
-}
-
 /**
  * Creates a `useStore` function that retrieves the store instance
  * @param options
@@ -245,7 +180,7 @@ export function createStore<
     let stores = storesMap.get(req)
     if (!stores) storesMap.set(req, (stores = {}))
 
-    let store = stores[id]
+    let store = stores[id] as Store<Id, S, G, A>
     if (!store) {
       stores[id] = store = buildStore(
         id,
@@ -254,9 +189,7 @@ export function createStore<
         actions,
         getInitialState(id)
       )
-      // save a reference to the initial state
-      // TODO: this implies that replacing the store cannot be done by the user on the server
-      setInitialState(store)
+
       if (isClient) useStoreDevtools(store)
     }
 
index f2cbd8ce95f194158a330431e88b653b172321d1..8a59e1112d345673dff9ca7865db032121f3c975 100644 (file)
@@ -91,6 +91,21 @@ export type StoreWithActions<A extends Record<string, StoreAction>> = {
     : never
 }
 
+// has the actions without the context (this) for typings
+export type Store<
+  Id extends string,
+  S extends StateTree,
+  G extends Record<string, StoreGetter<S>>,
+  A extends Record<string, StoreAction>
+> = StoreWithState<Id, S> & StoreWithGetters<S, G> & StoreWithActions<A>
+
+export type GenericStore = Store<
+  string,
+  StateTree,
+  Record<string, StoreGetter<StateTree>>,
+  Record<string, StoreAction>
+>
+
 export interface DevtoolHook {
   on(
     event: string,