@sqlrooms/room-store 0.19.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/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2025 Ilya Boyandin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # @sqlrooms/room-store
2
+
3
+ This package provides the core state management for SQLRooms using [Zustand](https://github.com/pmndrs/zustand). It is designed to be extensible, allowing you to build custom room experiences by creating and composing state "slices".
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @sqlrooms/room-store @sqlrooms/room-config zod zustand
9
+ ```
10
+
11
+ ## Core Concepts
12
+
13
+ ### RoomStore
14
+
15
+ The `RoomStore` is a Zustand store that holds the entire state of a room. It is created by calling `createRoomStore` with a function that composes one or more slice creators.
16
+
17
+ ### RoomState
18
+
19
+ The `RoomState` is the object that defines the shape of the store. It has two main properties:
20
+
21
+ - `config`: This holds the configuration of the room that is persisted. This is defined by a `zod` schema, often extending the `BaseRoomConfig` from `@sqlrooms/room-config`.
22
+ - `room`: This holds the client-side state of the room, such as task progress, and provides actions for interacting with the room.
23
+
24
+ ### Slices
25
+
26
+ A slice is a piece of the room's state and its associated actions. You can create your own slices to add custom functionality to your room. The framework provides `createRoomSlice` to create the base slice with core room functionality. You combine this with your own slices inside the `createRoomStore` composer function.
27
+
28
+ ## Basic Usage
29
+
30
+ Here's an example of how to create a room store with a custom feature slice.
31
+
32
+ ```typescript
33
+ import {
34
+ createRoomStore,
35
+ createRoomSlice,
36
+ RoomState,
37
+ } from '@sqlrooms/room-store';
38
+ import {BaseRoomConfig} from '@sqlrooms/room-config';
39
+ import {z} from 'zod';
40
+ import {StateCreator} from 'zustand';
41
+
42
+ // 1. Define your persisted room configuration schema using Zod.
43
+ // This extends the base config with a custom property.
44
+ export const MyRoomConfig = BaseRoomConfig.extend({
45
+ defaultChartType: z.enum(['bar', 'line', 'scatter']).default('bar'),
46
+ });
47
+ export type MyRoomConfig = z.infer<typeof MyRoomConfig>;
48
+
49
+ // 2. Define the state and actions for your custom feature slice.
50
+ export interface MyFeatureSlice {
51
+ activeChartId: string | null;
52
+ setActiveChartId: (id: string | null) => void;
53
+ }
54
+
55
+ // 3. Create your custom slice.
56
+ export const createMyFeatureSlice: StateCreator<MyFeatureSlice> = (set) => ({
57
+ activeChartId: null,
58
+ setActiveChartId: (id) => set({activeChartId: id}),
59
+ });
60
+
61
+ // 4. Define the full state of your room, combining the base RoomState
62
+ // with your custom slice's state.
63
+ export type MyRoomState = RoomState<MyRoomConfig> & MyFeatureSlice;
64
+
65
+ // 5. Create the room store by composing the base slice and your custom slice.
66
+ export const {roomStore, useRoomStore} = createRoomStore<
67
+ MyRoomConfig,
68
+ MyRoomState
69
+ >((set, get, store) => ({
70
+ ...createRoomSlice<MyRoomConfig>({
71
+ config: {
72
+ // You can provide initial values for your config here
73
+ title: 'My First Room',
74
+ defaultChartType: 'line',
75
+ // other properties from BaseRoomConfig will have defaults
76
+ },
77
+ room: {},
78
+ })(set, get, store),
79
+
80
+ // Add your custom slice to the store
81
+ ...createMyFeatureSlice(set, get, store),
82
+ }));
83
+
84
+ export {roomStore, useRoomStore};
85
+ ```
86
+
87
+ ## Providing the Store
88
+
89
+ To make the store available to your React components, you need to use the `RoomStateProvider` component at the root of your application.
90
+
91
+ ```tsx
92
+ import {RoomStateProvider} from '@sqlrooms/room-store';
93
+ import {roomStore} from './my-room-store';
94
+
95
+ function App() {
96
+ return (
97
+ <RoomStateProvider value={roomStore}>
98
+ {/* Your room components go here */}
99
+ </RoomStateProvider>
100
+ );
101
+ }
102
+ ```
103
+
104
+ ## Accessing the Store in Components
105
+
106
+ You can use the `useRoomStore` hook returned by `createRoomStore` to access the room's state and actions from any component.
107
+
108
+ ```tsx
109
+ import {useRoomStore} from './my-room-store';
110
+
111
+ function RoomTitle() {
112
+ const title = useRoomStore((state) => state.config.title);
113
+ const activeChartId = useRoomStore((state) => state.activeChartId);
114
+ const setActiveChartId = useRoomStore((state) => state.setActiveChartId);
115
+
116
+ return (
117
+ <div>
118
+ <h1>{title}</h1>
119
+ <p>Active Chart: {activeChartId || 'None'}</p>
120
+ <button onClick={() => setActiveChartId('chart-123')}>
121
+ Activate Chart
122
+ </button>
123
+ </div>
124
+ );
125
+ }
126
+ ```
@@ -0,0 +1,9 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { RoomState, RoomStore } from './RoomStore';
3
+ export declare const RoomStateContext: React.Context<RoomStore<unknown> | null>;
4
+ export type RoomStateProviderProps<PC> = React.PropsWithChildren<{
5
+ roomStore?: RoomStore<PC>;
6
+ }>;
7
+ export declare function RoomStateProvider<PC>({ children, roomStore, }: RoomStateProviderProps<PC>): ReactNode;
8
+ export declare function useBaseRoomStore<PC, PS extends RoomState<PC>, T>(selector: (state: RoomState<PC>) => T): T;
9
+ //# sourceMappingURL=RoomStateProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RoomStateProvider.d.ts","sourceRoot":"","sources":["../src/RoomStateProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAgB,SAAS,EAAa,MAAM,OAAO,CAAC;AAElE,OAAO,EAAC,SAAS,EAAE,SAAS,EAAC,MAAM,aAAa,CAAC;AAIjD,eAAO,MAAM,gBAAgB,0CAAiD,CAAC;AAE/E,MAAM,MAAM,sBAAsB,CAAC,EAAE,IAAI,KAAK,CAAC,iBAAiB,CAAC;IAC/D,SAAS,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC;CAC3B,CAAC,CAAC;AAEH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,EACpC,QAAQ,EACR,SAAS,GACV,EAAE,sBAAsB,CAAC,EAAE,CAAC,GAAG,SAAS,CAQxC;AAED,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,EAAE,SAAS,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC,EAC9D,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,CAAC,KAAK,CAAC,GACpC,CAAC,CAMH"}
@@ -0,0 +1,16 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext } from 'react';
3
+ import { useStore } from 'zustand';
4
+ // See https://docs.pmnd.rs/zustand/guides/initialize-state-with-props
5
+ export const RoomStateContext = createContext(null);
6
+ export function RoomStateProvider({ children, roomStore, }) {
7
+ return (_jsx(RoomStateContext.Provider, { value: roomStore, children: children }));
8
+ }
9
+ export function useBaseRoomStore(selector) {
10
+ const store = useContext(RoomStateContext);
11
+ if (!store) {
12
+ throw new Error('Missing RoomStateProvider in the tree');
13
+ }
14
+ return useStore(store, selector);
15
+ }
16
+ //# sourceMappingURL=RoomStateProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RoomStateProvider.js","sourceRoot":"","sources":["../src/RoomStateProvider.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAC,aAAa,EAAa,UAAU,EAAC,MAAM,OAAO,CAAC;AAClE,OAAO,EAAW,QAAQ,EAAC,MAAM,SAAS,CAAC;AAG3C,sEAAsE;AAEtE,MAAM,CAAC,MAAM,gBAAgB,GAAG,aAAa,CAA4B,IAAI,CAAC,CAAC;AAM/E,MAAM,UAAU,iBAAiB,CAAK,EACpC,QAAQ,EACR,SAAS,GACkB;IAC3B,OAAO,CACL,KAAC,gBAAgB,CAAC,QAAQ,IACxB,KAAK,EAAE,SAA0C,YAEhD,QAAQ,GACiB,CAC7B,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC9B,QAAqC;IAErC,MAAM,KAAK,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC3C,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,QAAQ,CAAC,KAAgC,EAAE,QAAQ,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import React, {createContext, ReactNode, useContext} from 'react';\nimport {StoreApi, useStore} from 'zustand';\nimport {RoomState, RoomStore} from './RoomStore';\n\n// See https://docs.pmnd.rs/zustand/guides/initialize-state-with-props\n\nexport const RoomStateContext = createContext<RoomStore<unknown> | null>(null);\n\nexport type RoomStateProviderProps<PC> = React.PropsWithChildren<{\n roomStore?: RoomStore<PC>;\n}>;\n\nexport function RoomStateProvider<PC>({\n children,\n roomStore,\n}: RoomStateProviderProps<PC>): ReactNode {\n return (\n <RoomStateContext.Provider\n value={roomStore as unknown as RoomStore<unknown>}\n >\n {children}\n </RoomStateContext.Provider>\n );\n}\n\nexport function useBaseRoomStore<PC, PS extends RoomState<PC>, T>(\n selector: (state: RoomState<PC>) => T,\n): T {\n const store = useContext(RoomStateContext);\n if (!store) {\n throw new Error('Missing RoomStateProvider in the tree');\n }\n return useStore(store as unknown as StoreApi<PS>, selector);\n}\n"]}
@@ -0,0 +1,51 @@
1
+ import { StateCreator, StoreApi } from 'zustand';
2
+ export type RoomStore<PC> = StoreApi<RoomState<PC>>;
3
+ export type RoomStateProps<PC> = {
4
+ lastSavedConfig: PC | undefined;
5
+ tasksProgress: Record<string, TaskProgress>;
6
+ captureException: (exception: unknown, captureContext?: unknown) => void;
7
+ };
8
+ export type TaskProgress = {
9
+ progress?: number | undefined;
10
+ message: string;
11
+ };
12
+ export type RoomStateActions<PC> = {
13
+ initialize: () => Promise<void>;
14
+ /**
15
+ * Set the room config.
16
+ * @param config - The room config to set.
17
+ */
18
+ setRoomConfig: (config: PC) => void;
19
+ /**
20
+ * Set the last saved room config. This can be used to check if the room has unsaved changes.
21
+ * @param config - The room config to set.
22
+ */
23
+ setLastSavedConfig: (config: PC) => void;
24
+ /**
25
+ * Check if the room has unsaved changes.
26
+ * @returns True if the room has unsaved changes, false otherwise.
27
+ */
28
+ hasUnsavedChanges(): boolean;
29
+ setTaskProgress: (id: string, taskProgress: TaskProgress | undefined) => void;
30
+ getLoadingProgress: () => TaskProgress | undefined;
31
+ };
32
+ export type RoomState<PC> = {
33
+ config: PC;
34
+ room: RoomStateProps<PC> & RoomStateActions<PC>;
35
+ };
36
+ export declare function createRoomSlice<PC>(props: {
37
+ config: PC;
38
+ room: Partial<Omit<RoomStateProps<PC>, 'config'>>;
39
+ }): StateCreator<RoomState<PC>>;
40
+ export declare function createBaseSlice<PC, S>(sliceCreator: (...args: Parameters<StateCreator<S & RoomState<PC>>>) => S): StateCreator<S>;
41
+ /**
42
+ * Create a room store with custom fields and methods
43
+ * @param initialState - The initial state and config for the room
44
+ * @param sliceCreators - The slices to add to the room store
45
+ * @returns The room store and a hook for accessing the room store
46
+ */
47
+ export declare function createRoomStore<PC, RS extends RoomState<PC>>(stateCreator: StateCreator<RS>): {
48
+ roomStore: StoreApi<RS>;
49
+ useRoomStore: <T>(selector: (state: RS) => T) => T;
50
+ };
51
+ //# sourceMappingURL=RoomStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RoomStore.d.ts","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings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;IACX,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;CACnD,GAAG,YAAY,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAoE9B;AAED,wBAAgB,eAAe,CAAC,EAAE,EAAE,CAAC,EACnC,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,YAAY,CAAC,CAAC,GAAG,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GACxE,YAAY,CAAC,CAAC,CAAC,CAOjB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,EAAE,SAAS,SAAS,CAAC,EAAE,CAAC,EAC1D,YAAY,EAAE,YAAY,CAAC,EAAE,CAAC;;mBAcR,CAAC,YAAY,CAAC,KAAK,EAAE,EAAE,KAAK,CAAC,KAAG,CAAC;EAKxD"}
@@ -0,0 +1,85 @@
1
+ import { castDraft, produce } from 'immer';
2
+ import { createStore } from 'zustand';
3
+ import { useBaseRoomStore } from './RoomStateProvider';
4
+ export function createRoomSlice(props) {
5
+ const { config: initialConfig, room: roomStateProps, ...restState } = props;
6
+ const initialRoomState = {
7
+ ...roomStateProps,
8
+ lastSavedConfig: undefined,
9
+ tasksProgress: {},
10
+ captureException: (exception) => {
11
+ console.error(exception);
12
+ },
13
+ };
14
+ const slice = (set, get) => {
15
+ const roomState = {
16
+ config: initialConfig,
17
+ room: {
18
+ ...initialRoomState,
19
+ initialize: async () => {
20
+ // To be overridden by the room shell
21
+ },
22
+ setRoomConfig: (config) => set((state) => produce(state, (draft) => {
23
+ draft.config = castDraft(config);
24
+ })),
25
+ setLastSavedConfig: (config) => set((state) => produce(state, (draft) => {
26
+ draft.room.lastSavedConfig = castDraft(config);
27
+ })),
28
+ hasUnsavedChanges: () => {
29
+ const { lastSavedConfig } = get().room;
30
+ const { config } = get();
31
+ return config !== lastSavedConfig;
32
+ },
33
+ /** Returns the progress of the last task */
34
+ getLoadingProgress() {
35
+ const { tasksProgress } = get().room;
36
+ const keys = Object.keys(tasksProgress);
37
+ const lastKey = keys[keys.length - 1];
38
+ if (lastKey) {
39
+ return tasksProgress[lastKey];
40
+ }
41
+ return undefined;
42
+ },
43
+ setTaskProgress(id, taskProgress) {
44
+ set((state) => produce(state, (draft) => {
45
+ if (taskProgress) {
46
+ draft.room.tasksProgress[id] = taskProgress;
47
+ }
48
+ else {
49
+ delete draft.room.tasksProgress[id];
50
+ }
51
+ }));
52
+ },
53
+ },
54
+ ...restState,
55
+ };
56
+ return roomState;
57
+ };
58
+ return slice;
59
+ }
60
+ export function createBaseSlice(sliceCreator) {
61
+ return (set, get, store) => sliceCreator(set, get, store);
62
+ }
63
+ /**
64
+ * Create a room store with custom fields and methods
65
+ * @param initialState - The initial state and config for the room
66
+ * @param sliceCreators - The slices to add to the room store
67
+ * @returns The room store and a hook for accessing the room store
68
+ */
69
+ export function createRoomStore(stateCreator) {
70
+ const roomStore = createStore((set, get, store) => ({
71
+ ...stateCreator(set, get, store),
72
+ }));
73
+ if (typeof window !== 'undefined') {
74
+ roomStore.getState().room.initialize();
75
+ }
76
+ else {
77
+ console.warn('Skipping room store initialization. Room store should be only used on client.');
78
+ }
79
+ function useRoomStore(selector) {
80
+ // @ts-ignore TODO fix typing
81
+ return useBaseRoomStore(selector);
82
+ }
83
+ return { roomStore, useRoomStore };
84
+ }
85
+ //# sourceMappingURL=RoomStore.js.map
@@ -0,0 +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,EAAC,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAC,gBAAgB,EAAC,MAAM,qBAAqB,CAAC;AA2CrD,MAAM,UAAU,eAAe,CAAK,KAGnC;IACC,MAAM,EAAC,MAAM,EAAE,aAAa,E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M,KAAK,WAAW,EAAE,CAAC;QAClC,SAAS,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;IACzC,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;IACJ,CAAC;IAED,SAAS,YAAY,CAAI,QAA0B;QACjD,6BAA6B;QAC7B,OAAO,gBAAgB,CAAC,QAA4B,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,EAAC,SAAS,EAAE,YAAY,EAAC,CAAC;AACnC,CAAC","sourcesContent":["import {castDraft, produce} from 'immer';\nimport {StateCreator, StoreApi, createStore} from 'zustand';\nimport {useBaseRoomStore} from './RoomStateProvider';\n\nexport type RoomStore<PC> = StoreApi<RoomState<PC>>;\n\nexport type RoomStateProps<PC> = {\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 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 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 roomStore = createStore<RS>((set, get, store) => ({\n ...stateCreator(set, get, store),\n }));\n\n if (typeof window !== 'undefined') {\n roomStore.getState().room.initialize();\n } else {\n console.warn(\n 'Skipping room store initialization. Room store should be only used on client.',\n );\n }\n\n function useRoomStore<T>(selector: (state: RS) => T): T {\n // @ts-ignore TODO fix typing\n return useBaseRoomStore(selector as (state: RS) => T);\n }\n return {roomStore, useRoomStore};\n}\n"]}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * {@include ../README.md}
3
+ * @packageDocumentation
4
+ */
5
+ export { RoomStateContext, RoomStateProvider, useBaseRoomStore, type RoomStateProviderProps, } from './RoomStateProvider';
6
+ export { createRoomStore, createRoomSlice, type RoomStateActions, type RoomStateProps, type RoomState, type RoomStore, type TaskProgress, createBaseSlice, } from './RoomStore';
7
+ export * from '@sqlrooms/room-config';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,cAAc,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * {@include ../README.md}
3
+ * @packageDocumentation
4
+ */
5
+ export { RoomStateContext, RoomStateProvider, useBaseRoomStore, } from './RoomStateProvider';
6
+ export { createRoomStore, createRoomSlice, createBaseSlice, } from './RoomStore';
7
+ export * from '@sqlrooms/room-config';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,eAAe,EAMf,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,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 createRoomSlice,\n type RoomStateActions,\n type RoomStateProps,\n type RoomState,\n type RoomStore,\n type TaskProgress,\n createBaseSlice,\n} from './RoomStore';\n\nexport * from '@sqlrooms/room-config';\n"]}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@sqlrooms/room-store",
3
+ "version": "0.19.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "module": "dist/index.js",
7
+ "type": "module",
8
+ "author": "Ilya Boyandin <ilya@boyandin.me>",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/sqlrooms/sqlrooms.git"
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "dependencies": {
21
+ "@sqlrooms/room-config": "0.19.0",
22
+ "immer": "^10.1.1",
23
+ "zod": "^3.25.57",
24
+ "zustand": "^5.0.5"
25
+ },
26
+ "peerDependencies": {
27
+ "react": ">=18"
28
+ },
29
+ "scripts": {
30
+ "dev": "tsc -w",
31
+ "build": "tsc",
32
+ "lint": "eslint .",
33
+ "typecheck": "tsc --noEmit",
34
+ "typedoc": "typedoc"
35
+ },
36
+ "gitHead": "ba6000f1e06d3ab01e309d805b5d187b32c9ff06"
37
+ }