@sqlrooms/room-store 0.25.0-rc.0 → 0.26.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/RoomStore.d.ts +3 -3
- package/dist/RoomStore.d.ts.map +1 -1
- package/dist/RoomStore.js +2 -1
- package/dist/RoomStore.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/RoomStore.d.ts
CHANGED
|
@@ -40,9 +40,9 @@ export type RoomState<PC> = {
|
|
|
40
40
|
config: PC;
|
|
41
41
|
room: RoomStateProps<PC> & RoomStateActions<PC>;
|
|
42
42
|
};
|
|
43
|
-
export declare function createRoomSlice<PC>(props
|
|
44
|
-
config
|
|
45
|
-
room
|
|
43
|
+
export declare function createRoomSlice<PC>(props?: {
|
|
44
|
+
config?: PC;
|
|
45
|
+
room?: Partial<Omit<RoomStateProps<PC>, 'config'>>;
|
|
46
46
|
}): StateCreator<RoomState<PC>>;
|
|
47
47
|
export declare function createBaseSlice<PC, S>(sliceCreator: (...args: Parameters<StateCreator<S & RoomState<PC>>>) => S): StateCreator<S>;
|
|
48
48
|
/**
|
package/dist/RoomStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RoomStore.d.ts","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"RoomStore.d.ts","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,YAAY,EAAE,QAAQ,EAAwB,MAAM,SAAS,CAAC;AAEtE,MAAM,MAAM,SAAS,CAAC,EAAE,IAAI,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;AAEpD,MAAM,MAAM,cAAc,CAAC,EAAE,IAAI;IAC/B,WAAW,EAAE,OAAO,CAAC;IACrB,eAAe,EAAE,EA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
|
|
1
2
|
import { castDraft, produce } from 'immer';
|
|
2
3
|
import { createStore, useStore } from 'zustand';
|
|
3
4
|
export function createRoomSlice(props) {
|
|
4
|
-
const { config: initialConfig, room: roomStateProps, ...restState } = props;
|
|
5
|
+
const { config: initialConfig = createDefaultBaseRoomConfig(), room: roomStateProps, ...restState } = props ?? {};
|
|
5
6
|
const initialRoomState = {
|
|
6
7
|
...roomStateProps,
|
|
7
8
|
initialized: false,
|
package/dist/RoomStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RoomStore.js","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AACzC,OAAO,EAAyB,WAAW,EAAE,QAAQ,EAAC,MAAM,SAAS,CAAC;AAkDtE,MAAM,UAAU,eAAe,CAAK,KAGnC;IACC,MAAM,EAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,SAAS,EAAC,GAAG,KAAK,CAAC;IAC1E,MAAM,gBAAgB,GAAuB;QAC3C,GAAG,cAAc;QACjB,WAAW,EAAE,KAAK;QAClB,eAAe,EAAE,SAAS;QAC1B,aAAa,EAAE,EAAE;QACjB,gBAAgB,EAAE,CAAC,SAAkB,EAAE,EAAE;YACvC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;KACF,CAAC;IACF,MAAM,KAAK,GAAgC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACtD,MAAM,SAAS,GAAkB;YAC/B,MAAM,EAAE,aAAa;YACrB,IAAI,EAAE;gBACJ,GAAG,gBAAgB;gBACnB,UAAU,EAAE,KAAK,IAAI,EAAE;oBACrB,qCAAqC;gBACvC,CAAC;gBAED,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE,CACxB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACZ,OAAO,CAAC,KAAK,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';\n\nexport type RoomStore<PC> = StoreApi<RoomState<PC>>;\n\nexport type RoomStateProps<PC> = {\n initialized: boolean;\n lastSavedConfig: PC | undefined;\n tasksProgress: Record<string, TaskProgress>;\n captureException: (exception: unknown, captureContext?: unknown) => void;\n};\n\nexport type TaskProgress = {\n progress?: number | undefined;\n message: string;\n};\n\nexport type RoomStateActions<PC> = {\n initialize: () => Promise<void>;\n\n /**\n * Set the room config.\n * @param config - The room config to set.\n */\n setRoomConfig: (config: PC) => void;\n /**\n * Set the last saved room config. This can be used to check if the room has unsaved changes.\n * @param config - The room config to set.\n */\n setLastSavedConfig: (config: PC) => void;\n /**\n * Check if the room has unsaved changes.\n * @returns True if the room has unsaved changes, false otherwise.\n */\n hasUnsavedChanges(): boolean; // since last save\n /**\n * Called when the project config gets changed. Can be used for saving.\n * To be overridden by the custom project state.\n * @param config - The project config to save.\n */\n onSaveConfig?: (config: PC) => Promise<void> | undefined;\n\n setTaskProgress: (id: string, taskProgress: TaskProgress | undefined) => void;\n getLoadingProgress: () => TaskProgress | undefined;\n};\n\nexport type RoomState<PC> = {\n config: PC;\n room: RoomStateProps<PC> & RoomStateActions<PC>;\n};\n\nexport function createRoomSlice<PC>(props: {\n config: PC;\n room: Partial<Omit<RoomStateProps<PC>, 'config'>>;\n}): StateCreator<RoomState<PC>> {\n const {config: initialConfig, room: roomStateProps, ...restState} = props;\n const initialRoomState: RoomStateProps<PC> = {\n ...roomStateProps,\n initialized: false,\n lastSavedConfig: undefined,\n tasksProgress: {},\n captureException: (exception: unknown) => {\n console.error(exception);\n },\n };\n const slice: StateCreator<RoomState<PC>> = (set, get) => {\n const roomState: RoomState<PC> = {\n config: initialConfig,\n room: {\n ...initialRoomState,\n initialize: async () => {\n // To be overridden by the room shell\n },\n\n setRoomConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.config = castDraft(config);\n }),\n ),\n setLastSavedConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.room.lastSavedConfig = castDraft(config);\n }),\n ),\n\n hasUnsavedChanges: () => {\n const {lastSavedConfig} = get().room;\n const {config} = get();\n return config !== lastSavedConfig;\n },\n\n /** Returns the progress of the last task */\n getLoadingProgress() {\n const {tasksProgress} = get().room;\n const keys = Object.keys(tasksProgress);\n const lastKey = keys[keys.length - 1];\n if (lastKey) {\n return tasksProgress[lastKey];\n }\n return undefined;\n },\n\n setTaskProgress(id, taskProgress) {\n set((state) =>\n produce(state, (draft) => {\n if (taskProgress) {\n draft.room.tasksProgress[id] = taskProgress;\n } else {\n delete draft.room.tasksProgress[id];\n }\n }),\n );\n },\n },\n ...restState,\n };\n\n return roomState;\n };\n\n return slice;\n}\n\nexport function createBaseSlice<PC, S>(\n sliceCreator: (...args: Parameters<StateCreator<S & RoomState<PC>>>) => S,\n): StateCreator<S> {\n return (set, get, store) =>\n sliceCreator(\n set,\n get as () => S & RoomState<PC>,\n store as StoreApi<S & RoomState<PC>>,\n );\n}\n\n/**\n * Create a room store with custom fields and methods\n * @param initialState - The initial state and config for the room\n * @param sliceCreators - The slices to add to the room store\n * @returns The room store and a hook for accessing the room store\n */\nexport function createRoomStore<PC, RS extends RoomState<PC>>(\n stateCreator: StateCreator<RS>,\n) {\n const factory = createRoomStoreCreator<RS>();\n const storeCreator = factory(() => stateCreator);\n const roomStore = storeCreator.createRoomStore();\n\n function useRoomStore<T>(selector: (state: RS) => T): T {\n return storeCreator.useRoomStore(selector);\n }\n\n return {roomStore, useRoomStore};\n}\n\nexport interface RoomSlice {\n initialize?: () => Promise<void>;\n}\n\nexport function isRoomSliceWithInitialize(\n slice: unknown,\n): slice is RoomSlice & Required<Pick<RoomSlice, 'initialize'>> {\n return (\n typeof slice === 'object' &&\n slice !== null &&\n 'initialize' in slice &&\n typeof slice.initialize === 'function'\n );\n}\n\n/**\n * Factory to create a room store creator with custom params.\n *\n * @template PC - Room config type\n * @template RS - Room state type\n * @param stateCreatorFactory - A function that takes params and returns a Zustand state creator\n * @returns An object with createRoomStore(params) and useRoomStore(selector)\n *\n */\nexport function createRoomStoreCreator<TState extends RoomState<any>>() {\n return function <TFactory extends (...args: any[]) => StateCreator<TState>>(\n stateCreatorFactory: TFactory,\n ): {\n createRoomStore: (...args: Parameters<TFactory>) => StoreApi<TState>;\n useRoomStore: <T>(selector: (state: TState) => T) => T;\n } {\n let store: StoreApi<TState> | undefined;\n\n function createRoomStore(...args: Parameters<TFactory>): StoreApi<TState> {\n store = createStore(stateCreatorFactory(...args));\n if (typeof window !== 'undefined') {\n (async () => {\n try {\n // Initialize the room\n await store.getState().room.initialize();\n // Set initialized to true after initialization\n store.setState((state) =>\n produce(state, ({room}) => {\n room.initialized = true;\n }),\n );\n // Subscribe to the project store changes after initialization\n store.subscribe(async (state) => {\n const {room} = state;\n if (room.onSaveConfig && room.hasUnsavedChanges()) {\n room.onSaveConfig(state.config);\n }\n });\n } catch (error) {\n store.getState().room.captureException(error);\n }\n })();\n } else {\n console.warn(\n 'Skipping room store initialization. Room store should be only used on client.',\n );\n }\n return store;\n }\n\n function useRoomStore<T>(selector: (state: TState) => T): T {\n if (!store)\n throw new Error(\n 'Room store not initialized. Call createRoomStore first.',\n );\n return useStore(store, selector);\n }\n\n return {createRoomStore, useRoomStore};\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"RoomStore.js","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,2BAA2B,EAAC,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAC,SAAS,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AACzC,OAAO,EAAyB,WAAW,EAAE,QAAQ,EAAC,MAAM,SAAS,CAAC;AAkDtE,MAAM,UAAU,eAAe,CAAK,KAGnC;IACC,MAAM,EACJ,MAAM,EAAE,aAAa,GAAG,2BAA2B,EAAQ,EAC3D,IAAI,EAAE,cAAc,EACpB,GAAG,SAAS,EACb,GAAG,KAAK,IAAI,EAAE,CAAC;IAChB,MAAM,gBAAgB,GAAuB;QAC3C,GAAG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IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;oBAChD,CAAC;gBACH,CAAC,CAAC,EAAE,CAAC;YACP,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;YACJ,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,SAAS,YAAY,CAAI,QAA8B;YACrD,IAAI,CAAC,KAAK;gBACR,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;YACJ,OAAO,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,EAAC,eAAe,EAAE,YAAY,EAAC,CAAC;IACzC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {createDefaultBaseRoomConfig} from '@sqlrooms/room-config';\nimport {castDraft, produce} from 'immer';\nimport {StateCreator, StoreApi, createStore, useStore} from 'zustand';\n\nexport type RoomStore<PC> = StoreApi<RoomState<PC>>;\n\nexport type RoomStateProps<PC> = {\n initialized: boolean;\n lastSavedConfig: PC | undefined;\n tasksProgress: Record<string, TaskProgress>;\n captureException: (exception: unknown, captureContext?: unknown) => void;\n};\n\nexport type TaskProgress = {\n progress?: number | undefined;\n message: string;\n};\n\nexport type RoomStateActions<PC> = {\n initialize: () => Promise<void>;\n\n /**\n * Set the room config.\n * @param config - The room config to set.\n */\n setRoomConfig: (config: PC) => void;\n /**\n * Set the last saved room config. This can be used to check if the room has unsaved changes.\n * @param config - The room config to set.\n */\n setLastSavedConfig: (config: PC) => void;\n /**\n * Check if the room has unsaved changes.\n * @returns True if the room has unsaved changes, false otherwise.\n */\n hasUnsavedChanges(): boolean; // since last save\n /**\n * Called when the project config gets changed. Can be used for saving.\n * To be overridden by the custom project state.\n * @param config - The project config to save.\n */\n onSaveConfig?: (config: PC) => Promise<void> | undefined;\n\n setTaskProgress: (id: string, taskProgress: TaskProgress | undefined) => void;\n getLoadingProgress: () => TaskProgress | undefined;\n};\n\nexport type RoomState<PC> = {\n config: PC;\n room: RoomStateProps<PC> & RoomStateActions<PC>;\n};\n\nexport function createRoomSlice<PC>(props?: {\n config?: PC;\n room?: Partial<Omit<RoomStateProps<PC>, 'config'>>;\n}): StateCreator<RoomState<PC>> {\n const {\n config: initialConfig = createDefaultBaseRoomConfig() as PC,\n room: roomStateProps,\n ...restState\n } = props ?? {};\n const initialRoomState: RoomStateProps<PC> = {\n ...roomStateProps,\n initialized: false,\n lastSavedConfig: undefined,\n tasksProgress: {},\n captureException: (exception: unknown) => {\n console.error(exception);\n },\n };\n const slice: StateCreator<RoomState<PC>> = (set, get) => {\n const roomState: RoomState<PC> = {\n config: initialConfig,\n room: {\n ...initialRoomState,\n initialize: async () => {\n // To be overridden by the room shell\n },\n\n setRoomConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.config = castDraft(config);\n }),\n ),\n setLastSavedConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.room.lastSavedConfig = castDraft(config);\n }),\n ),\n\n hasUnsavedChanges: () => {\n const {lastSavedConfig} = get().room;\n const {config} = get();\n return config !== lastSavedConfig;\n },\n\n /** Returns the progress of the last task */\n getLoadingProgress() {\n const {tasksProgress} = get().room;\n const keys = Object.keys(tasksProgress);\n const lastKey = keys[keys.length - 1];\n if (lastKey) {\n return tasksProgress[lastKey];\n }\n return undefined;\n },\n\n setTaskProgress(id, taskProgress) {\n set((state) =>\n produce(state, (draft) => {\n if (taskProgress) {\n draft.room.tasksProgress[id] = taskProgress;\n } else {\n delete draft.room.tasksProgress[id];\n }\n }),\n );\n },\n },\n ...restState,\n };\n\n return roomState;\n };\n\n return slice;\n}\n\nexport function createBaseSlice<PC, S>(\n sliceCreator: (...args: Parameters<StateCreator<S & RoomState<PC>>>) => S,\n): StateCreator<S> {\n return (set, get, store) =>\n sliceCreator(\n set,\n get as () => S & RoomState<PC>,\n store as StoreApi<S & RoomState<PC>>,\n );\n}\n\n/**\n * Create a room store with custom fields and methods\n * @param initialState - The initial state and config for the room\n * @param sliceCreators - The slices to add to the room store\n * @returns The room store and a hook for accessing the room store\n */\nexport function createRoomStore<PC, RS extends RoomState<PC>>(\n stateCreator: StateCreator<RS>,\n) {\n const factory = createRoomStoreCreator<RS>();\n const storeCreator = factory(() => stateCreator);\n const roomStore = storeCreator.createRoomStore();\n\n function useRoomStore<T>(selector: (state: RS) => T): T {\n return storeCreator.useRoomStore(selector);\n }\n\n return {roomStore, useRoomStore};\n}\n\nexport interface RoomSlice {\n initialize?: () => Promise<void>;\n}\n\nexport function isRoomSliceWithInitialize(\n slice: unknown,\n): slice is RoomSlice & Required<Pick<RoomSlice, 'initialize'>> {\n return (\n typeof slice === 'object' &&\n slice !== null &&\n 'initialize' in slice &&\n typeof slice.initialize === 'function'\n );\n}\n\n/**\n * Factory to create a room store creator with custom params.\n *\n * @template PC - Room config type\n * @template RS - Room state type\n * @param stateCreatorFactory - A function that takes params and returns a Zustand state creator\n * @returns An object with createRoomStore(params) and useRoomStore(selector)\n *\n */\nexport function createRoomStoreCreator<TState extends RoomState<any>>() {\n return function <TFactory extends (...args: any[]) => StateCreator<TState>>(\n stateCreatorFactory: TFactory,\n ): {\n createRoomStore: (...args: Parameters<TFactory>) => StoreApi<TState>;\n useRoomStore: <T>(selector: (state: TState) => T) => T;\n } {\n let store: StoreApi<TState> | undefined;\n\n function createRoomStore(...args: Parameters<TFactory>): StoreApi<TState> {\n store = createStore(stateCreatorFactory(...args));\n if (typeof window !== 'undefined') {\n (async () => {\n try {\n // Initialize the room\n await store.getState().room.initialize();\n // Set initialized to true after initialization\n store.setState((state) =>\n produce(state, ({room}) => {\n room.initialized = true;\n }),\n );\n // Subscribe to the project store changes after initialization\n store.subscribe(async (state) => {\n const {room} = state;\n if (room.onSaveConfig && room.hasUnsavedChanges()) {\n room.onSaveConfig(state.config);\n }\n });\n } catch (error) {\n store.getState().room.captureException(error);\n }\n })();\n } else {\n console.warn(\n 'Skipping room store initialization. Room store should be only used on client.',\n );\n }\n return store;\n }\n\n function useRoomStore<T>(selector: (state: TState) => T): T {\n if (!store)\n throw new Error(\n 'Room store not initialized. Call createRoomStore first.',\n );\n return useStore(store, selector);\n }\n\n return {createRoomStore, useRoomStore};\n };\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,5 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { RoomStateContext, RoomStateProvider, useBaseRoomStore, type RoomStateProviderProps, } from './RoomStateProvider';
|
|
6
6
|
export { createRoomStore, createRoomStoreCreator, createRoomSlice, type RoomStateActions, type RoomStateProps, type RoomState, type RoomStore, type TaskProgress, createBaseSlice, type RoomSlice, isRoomSliceWithInitialize, } from './RoomStore';
|
|
7
|
+
export type { StateCreator, StoreApi } from 'zustand';
|
|
7
8
|
export * from '@sqlrooms/room-config';
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,sBAAsB,GAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,eAAe,EACf,KAAK,SAAS,EACd,yBAAyB,GAC1B,MAAM,aAAa,CAAC;AAErB,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,sBAAsB,GAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,eAAe,EACf,KAAK,SAAS,EACd,yBAAyB,GAC1B,MAAM,aAAa,CAAC;AAErB,YAAY,EAAC,YAAY,EAAE,QAAQ,EAAC,MAAM,SAAS,CAAC;AAEpD,cAAc,uBAAuB,CAAC"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,GAEjB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,eAAe,EAMf,eAAe,EAEf,yBAAyB,GAC1B,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,GAEjB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,eAAe,EAMf,eAAe,EAEf,yBAAyB,GAC1B,MAAM,aAAa,CAAC;AAIrB,cAAc,uBAAuB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport {\n RoomStateContext,\n RoomStateProvider,\n useBaseRoomStore,\n type RoomStateProviderProps,\n} from './RoomStateProvider';\n\nexport {\n createRoomStore,\n createRoomStoreCreator,\n createRoomSlice,\n type RoomStateActions,\n type RoomStateProps,\n type RoomState,\n type RoomStore,\n type TaskProgress,\n createBaseSlice,\n type RoomSlice,\n isRoomSliceWithInitialize,\n} from './RoomStore';\n\nexport type {StateCreator, StoreApi} from 'zustand';\n\nexport * from '@sqlrooms/room-config';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/room-store",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0-rc.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@sqlrooms/room-config": "0.
|
|
21
|
+
"@sqlrooms/room-config": "0.26.0-rc.0",
|
|
22
22
|
"immer": "^10.1.1",
|
|
23
23
|
"zod": "^3.25.73",
|
|
24
24
|
"zustand": "^5.0.5"
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"typecheck": "tsc --noEmit",
|
|
34
34
|
"typedoc": "typedoc"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "05d355acee6ea65b33b09c6e49d7746ffacb866e"
|
|
37
37
|
}
|