@deephaven/plugin 1.10.2 → 1.11.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/index.d.ts CHANGED
@@ -6,7 +6,10 @@ export * from './useCustomThemes';
6
6
  export * from './useDashboardPlugins';
7
7
  export * from './usePlugins';
8
8
  export * from './WidgetView';
9
- export * from './PersistentStateContext';
10
- export * from './usePersistentState';
9
+ export {
10
+ /** @deprecated Import from '@deephaven/dashboard' */
11
+ PersistentStateProvider,
12
+ /** @deprecated Import from '@deephaven/dashboard' */
13
+ usePersistentState, } from '@deephaven/dashboard';
11
14
  export * from './usePluginsElementMap';
12
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,OAAO;AACL,qDAAqD;AACrD,uBAAuB;AACvB,qDAAqD;AACrD,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAC9B,cAAc,wBAAwB,CAAC"}
package/dist/index.js CHANGED
@@ -6,7 +6,8 @@ export * from "./useCustomThemes.js";
6
6
  export * from "./useDashboardPlugins.js";
7
7
  export * from "./usePlugins.js";
8
8
  export * from "./WidgetView.js";
9
- export * from "./PersistentStateContext.js";
10
- export * from "./usePersistentState.js";
9
+ export { /** @deprecated Import from '@deephaven/dashboard' */
10
+ PersistentStateProvider, /** @deprecated Import from '@deephaven/dashboard' */
11
+ usePersistentState } from '@deephaven/dashboard';
11
12
  export * from "./usePluginsElementMap.js";
12
13
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["export * from './PluginsContext';\nexport * from './PluginTypes';\nexport * from './PluginUtils';\nexport * from './TablePlugin';\nexport * from './useCustomThemes';\nexport * from './useDashboardPlugins';\nexport * from './usePlugins';\nexport * from './WidgetView';\nexport * from './PersistentStateContext';\nexport * from './usePersistentState';\nexport * from './usePluginsElementMap';\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["PersistentStateProvider","usePersistentState"],"sources":["../src/index.ts"],"sourcesContent":["export * from './PluginsContext';\nexport * from './PluginTypes';\nexport * from './PluginUtils';\nexport * from './TablePlugin';\nexport * from './useCustomThemes';\nexport * from './useDashboardPlugins';\nexport * from './usePlugins';\nexport * from './WidgetView';\nexport {\n /** @deprecated Import from '@deephaven/dashboard' */\n PersistentStateProvider,\n /** @deprecated Import from '@deephaven/dashboard' */\n usePersistentState,\n} from '@deephaven/dashboard';\nexport * from './usePluginsElementMap';\n"],"mappings":";;;;;;;;AAQA,SACE;AACAA,uBAAuB,EACvB;AACAC,kBAAkB,QACb,sBAAsB;AAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deephaven/plugin",
3
- "version": "1.10.2",
3
+ "version": "1.11.0",
4
4
  "description": "Deephaven JS Plugin Core",
5
5
  "author": "Deephaven Data Labs LLC",
6
6
  "license": "Apache-2.0",
@@ -22,11 +22,12 @@
22
22
  "build:babel": "babel ./src --out-dir ./dist --extensions \".ts,.tsx,.js,.jsx\" --source-maps --root-mode upward"
23
23
  },
24
24
  "dependencies": {
25
- "@deephaven/components": "^1.10.0",
26
- "@deephaven/golden-layout": "^1.10.2",
27
- "@deephaven/grid": "^1.10.0",
25
+ "@deephaven/components": "^1.11.0",
26
+ "@deephaven/dashboard": "^1.11.0",
27
+ "@deephaven/golden-layout": "^1.11.0",
28
+ "@deephaven/grid": "^1.11.0",
28
29
  "@deephaven/icons": "^1.2.0",
29
- "@deephaven/iris-grid": "^1.10.1",
30
+ "@deephaven/iris-grid": "^1.11.0",
30
31
  "@deephaven/jsapi-types": "^1.0.0-dev0.40.4",
31
32
  "@deephaven/log": "^1.8.0",
32
33
  "@deephaven/react-hooks": "^1.10.0",
@@ -43,5 +44,5 @@
43
44
  "publishConfig": {
44
45
  "access": "public"
45
46
  },
46
- "gitHead": "39e389c481a4ec75bfed7a45d1ffaf5a98d8f98e"
47
+ "gitHead": "7b2a23ea3b33c11f136e7b9d44a204880c674866"
47
48
  }
@@ -1,59 +0,0 @@
1
- import React from 'react';
2
- export type PersistentStateContextType = {
3
- /**
4
- *
5
- * @param id A unique ID generated by the caller.
6
- * This identifies the specific state internally and is used to prevent duplicate state
7
- * @param state The state to persist. Needs to be JSON serializable.
8
- */
9
- addState: (id: string, state: {
10
- type: string;
11
- version: number;
12
- state: unknown;
13
- }) => void;
14
- /**
15
- * Schedules a persistent state update.
16
- * Will re-render the components that have persistent state to capture their current state.
17
- */
18
- scheduleStateUpdate: () => void;
19
- /**
20
- * Gets the next item from the initial state iterator.
21
- * It is expected that each hook will call this once to get its initial state.
22
- * @param id A unique ID generated by the caller.
23
- * This is used to prevent one caller from consuming multiple values from the initial state.
24
- * @returns The iterator result containing the initial state value (state, version, type) and a done flag.
25
- */
26
- getInitialState: <S>(id: string) => {
27
- value: {
28
- state: S;
29
- version: number;
30
- type: string;
31
- };
32
- done: boolean;
33
- };
34
- };
35
- /**
36
- * Context that holds methods to track the state of all calls to usePersistentState below this provider.
37
- */
38
- export declare const PersistentStateContext: React.Context<PersistentStateContextType | null>;
39
- export type PersistentStateProviderProps = React.PropsWithChildren<{
40
- /**
41
- * The initial state of all calls to usePersistentState.
42
- * If there are more calls to usePersistentState than there are elements in this array,
43
- * the state initializer of the usePersistentState call will be used for the rest.
44
- */
45
- initialState: unknown[];
46
- /**
47
- * Called when the state changes.
48
- * The state is passed as an array of the values of all calls to usePersistentState.
49
- * The order of the values is the same as the order of the calls to usePersistentState.
50
- * @param state The state of all calls to usePersistentState.
51
- */
52
- onChange: (state: unknown[]) => void;
53
- }>;
54
- /**
55
- * Tracks all calls to the usePersistentState hook below this provider.
56
- * Keeps track of the state in call order so and calls onChange when the state changes.
57
- */
58
- export declare function PersistentStateProvider(props: PersistentStateProviderProps): JSX.Element;
59
- //# sourceMappingURL=PersistentStateContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PersistentStateContext.d.ts","sourceRoot":"","sources":["../src/PersistentStateContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAMN,MAAM,OAAO,CAAC;AAEf,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;;OAKG;IACH,QAAQ,EAAE,CACR,EAAE,EAAE,MAAM,EACV,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,KACrD,IAAI,CAAC;IAEV;;;OAGG;IACH,mBAAmB,EAAE,MAAM,IAAI,CAAC;IAEhC;;;;;;OAMG;IACH,eAAe,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,KAAK;QAClC,KAAK,EAAE;YAAE,KAAK,EAAE,CAAC,CAAC;YAAC,OAAO,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,CAAC;QACnD,IAAI,EAAE,OAAO,CAAC;KACf,CAAC;CACH,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB,kDACqB,CAAC;AAGzD,MAAM,MAAM,4BAA4B,GAAG,KAAK,CAAC,iBAAiB,CAAC;IACjE;;;;OAIG;IACH,YAAY,EAAE,OAAO,EAAE,CAAC;IAExB;;;;;OAKG;IACH,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;CACtC,CAAC,CAAC;AAEH;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,4BAA4B,GAClC,GAAG,CAAC,OAAO,CA2Eb"}
@@ -1,79 +0,0 @@
1
- import React, { createContext, useCallback, useEffect, useMemo, useRef } from 'react';
2
- import { jsx as _jsx } from "react/jsx-runtime";
3
- /**
4
- * Context that holds methods to track the state of all calls to usePersistentState below this provider.
5
- */
6
- export var PersistentStateContext = /*#__PURE__*/createContext(null);
7
- PersistentStateContext.displayName = 'PersistentStateContext';
8
- /**
9
- * Tracks all calls to the usePersistentState hook below this provider.
10
- * Keeps track of the state in call order so and calls onChange when the state changes.
11
- */
12
- export function PersistentStateProvider(props) {
13
- var {
14
- initialState,
15
- onChange,
16
- children
17
- } = props;
18
- var [updateId, setUpdateId] = React.useState(0);
19
-
20
- // We store the previous and next state in a map in case a component calls setState in its render function.
21
- // This would cause an immediate re-render and usePersistentState may be called multiple times from 1 component
22
- // before the render finishes and we run the effect to persist the state.
23
- // JS Maps iterate based on insertion order, so if a component renders multiple times its original position will be maintained
24
- // while updating its state if it changed in that re-render.
25
- var persistentData = useRef({
26
- initial: initialState,
27
- initialStateMap: new Map(),
28
- state: new Map(),
29
- isTracking: true // We want to start tracking on the first render
30
- });
31
- var addState = useCallback((id, state) => {
32
- if (persistentData.current.isTracking) {
33
- persistentData.current.state.set(id, state);
34
- }
35
- }, []);
36
- var scheduleStateUpdate = useCallback(() => {
37
- // Don't trigger again if we are already tracking a render
38
- if (!persistentData.current.isTracking) {
39
- persistentData.current.isTracking = true;
40
- persistentData.current.state = new Map();
41
- setUpdateId(prev => prev + 1);
42
- }
43
- }, []);
44
- var initialStateIterator = useRef(persistentData.current.initial[Symbol.iterator]());
45
- var getInitialState = useCallback(function getState(id) {
46
- // Prevents a component re-rendering multiple times in the same render cycle from taking multiple values from the iterator
47
- var initialStateForId = persistentData.current.initialStateMap.get(id);
48
- if (initialStateForId) {
49
- return initialStateForId;
50
- }
51
- var {
52
- value,
53
- done
54
- } = initialStateIterator.current.next();
55
- var stateVal = {
56
- value: value,
57
- done: done !== null && done !== void 0 ? done : false
58
- };
59
- persistentData.current.initialStateMap.set(id, stateVal);
60
- return stateVal;
61
- }, []);
62
- useEffect(function persistState() {
63
- if (persistentData.current.isTracking) {
64
- onChange([...persistentData.current.state.values()]);
65
- persistentData.current.isTracking = false;
66
- }
67
- }, [updateId, onChange]);
68
- var contextValue = useMemo(() => ({
69
- addState,
70
- scheduleStateUpdate,
71
- getInitialState,
72
- updateId
73
- }), [addState, getInitialState, scheduleStateUpdate, updateId]);
74
- return /*#__PURE__*/_jsx(PersistentStateContext.Provider, {
75
- value: contextValue,
76
- children: children
77
- });
78
- }
79
- //# sourceMappingURL=PersistentStateContext.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PersistentStateContext.js","names":["React","createContext","useCallback","useEffect","useMemo","useRef","jsx","_jsx","PersistentStateContext","displayName","PersistentStateProvider","props","initialState","onChange","children","updateId","setUpdateId","useState","persistentData","initial","initialStateMap","Map","state","isTracking","addState","id","current","set","scheduleStateUpdate","prev","initialStateIterator","Symbol","iterator","getInitialState","getState","initialStateForId","get","value","done","next","stateVal","persistState","values","contextValue","Provider"],"sources":["../src/PersistentStateContext.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n} from 'react';\n\nexport type PersistentStateContextType = {\n /**\n *\n * @param id A unique ID generated by the caller.\n * This identifies the specific state internally and is used to prevent duplicate state\n * @param state The state to persist. Needs to be JSON serializable.\n */\n addState: (\n id: string,\n state: { type: string; version: number; state: unknown }\n ) => void;\n\n /**\n * Schedules a persistent state update.\n * Will re-render the components that have persistent state to capture their current state.\n */\n scheduleStateUpdate: () => void;\n\n /**\n * Gets the next item from the initial state iterator.\n * It is expected that each hook will call this once to get its initial state.\n * @param id A unique ID generated by the caller.\n * This is used to prevent one caller from consuming multiple values from the initial state.\n * @returns The iterator result containing the initial state value (state, version, type) and a done flag.\n */\n getInitialState: <S>(id: string) => {\n value: { state: S; version: number; type: string };\n done: boolean;\n };\n};\n\n/**\n * Context that holds methods to track the state of all calls to usePersistentState below this provider.\n */\nexport const PersistentStateContext =\n createContext<PersistentStateContextType | null>(null);\nPersistentStateContext.displayName = 'PersistentStateContext';\n\nexport type PersistentStateProviderProps = React.PropsWithChildren<{\n /**\n * The initial state of all calls to usePersistentState.\n * If there are more calls to usePersistentState than there are elements in this array,\n * the state initializer of the usePersistentState call will be used for the rest.\n */\n initialState: unknown[];\n\n /**\n * Called when the state changes.\n * The state is passed as an array of the values of all calls to usePersistentState.\n * The order of the values is the same as the order of the calls to usePersistentState.\n * @param state The state of all calls to usePersistentState.\n */\n onChange: (state: unknown[]) => void;\n}>;\n\n/**\n * Tracks all calls to the usePersistentState hook below this provider.\n * Keeps track of the state in call order so and calls onChange when the state changes.\n */\nexport function PersistentStateProvider(\n props: PersistentStateProviderProps\n): JSX.Element {\n const { initialState, onChange, children } = props;\n const [updateId, setUpdateId] = React.useState(0);\n\n // We store the previous and next state in a map in case a component calls setState in its render function.\n // This would cause an immediate re-render and usePersistentState may be called multiple times from 1 component\n // before the render finishes and we run the effect to persist the state.\n // JS Maps iterate based on insertion order, so if a component renders multiple times its original position will be maintained\n // while updating its state if it changed in that re-render.\n const persistentData = useRef({\n initial: initialState,\n initialStateMap: new Map<string, { value: unknown; done: boolean }>(),\n state: new Map<string, unknown>(),\n isTracking: true, // We want to start tracking on the first render\n });\n\n const addState = useCallback((id: string, state: unknown) => {\n if (persistentData.current.isTracking) {\n persistentData.current.state.set(id, state);\n }\n }, []);\n\n const scheduleStateUpdate = useCallback(() => {\n // Don't trigger again if we are already tracking a render\n if (!persistentData.current.isTracking) {\n persistentData.current.isTracking = true;\n persistentData.current.state = new Map<string, unknown>();\n setUpdateId(prev => prev + 1);\n }\n }, []);\n\n const initialStateIterator = useRef(\n persistentData.current.initial[Symbol.iterator]()\n );\n\n const getInitialState = useCallback(function getState<S>(id: string): {\n value: S;\n done: boolean;\n } {\n // Prevents a component re-rendering multiple times in the same render cycle from taking multiple values from the iterator\n const initialStateForId = persistentData.current.initialStateMap.get(id);\n if (initialStateForId) {\n return initialStateForId as { value: S; done: boolean };\n }\n const { value, done } = initialStateIterator.current.next();\n const stateVal = { value: value as S, done: done ?? false };\n persistentData.current.initialStateMap.set(id, stateVal);\n return stateVal;\n }, []);\n\n useEffect(\n function persistState() {\n if (persistentData.current.isTracking) {\n onChange([...persistentData.current.state.values()]);\n persistentData.current.isTracking = false;\n }\n },\n [updateId, onChange]\n );\n\n const contextValue = useMemo(\n () => ({\n addState,\n scheduleStateUpdate,\n getInitialState,\n updateId,\n }),\n [addState, getInitialState, scheduleStateUpdate, updateId]\n );\n\n return (\n <PersistentStateContext.Provider value={contextValue}>\n {children}\n </PersistentStateContext.Provider>\n );\n}\n"],"mappings":"AAAA,OAAOA,KAAK,IACVC,aAAa,EACbC,WAAW,EACXC,SAAS,EACTC,OAAO,EACPC,MAAM,QACD,OAAO;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAiCf;AACA;AACA;AACA,OAAO,IAAMC,sBAAsB,gBACjCP,aAAa,CAAoC,IAAI,CAAC;AACxDO,sBAAsB,CAACC,WAAW,GAAG,wBAAwB;AAmB7D;AACA;AACA;AACA;AACA,OAAO,SAASC,uBAAuBA,CACrCC,KAAmC,EACtB;EACb,IAAM;IAAEC,YAAY;IAAEC,QAAQ;IAAEC;EAAS,CAAC,GAAGH,KAAK;EAClD,IAAM,CAACI,QAAQ,EAAEC,WAAW,CAAC,GAAGhB,KAAK,CAACiB,QAAQ,CAAC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,OAAO,CAACa,IAAI,CAAC,CAAC;IAC3D,IAAMC,QAAQ,GAAG;MAAEH,KAAK,EAAEA,KAAU;MAAEC,IAAI,EAAEA,IAAI,aAAJA,IAAI,cAAJA,IAAI,GAAI;IAAM,CAAC;IAC3DpB,cAAc,CAACQ,OAAO,CAACN,eAAe,CAACO,GAAG,CAACF,EAAE,EAAEe,QAAQ,CAAC;IACxD,OAAOA,QAAQ;EACjB,CAAC,EAAE,EAAE,CAAC;EAENrC,SAAS,CACP,SAASsC,YAAYA,CAAA,EAAG;IACtB,IAAIvB,cAAc,CAACQ,OAAO,CAACH,UAAU,EAAE;MACrCV,QAAQ,CAAC,CAAC,GAAGK,cAAc,CAACQ,OAAO,CAACJ,KAAK,CAACoB,MAAM,CAAC,CAAC,CAAC,CAAC;MACpDxB,cAAc,CAACQ,OAAO,CAACH,UAAU,GAAG,KAAK;IAC3C;EACF,CAAC,EACD,CAACR,QAAQ,EAAEF,QAAQ,CACrB,CAAC;EAED,IAAM8B,YAAY,GAAGvC,OAAO,CAC1B,OAAO;IACLoB,QAAQ;IACRI,mBAAmB;IACnBK,eAAe;IACflB;EACF,CAAC,CAAC,EACF,CAACS,QAAQ,EAAES,eAAe,EAAEL,mBAAmB,EAAEb,QAAQ,CAC3D,CAAC;EAED,oBACER,IAAA,CAACC,sBAAsB,CAACoC,QAAQ;IAACP,KAAK,EAAEM,YAAa;IAAA7B,QAAA,EAClDA;EAAQ,CACsB,CAAC;AAEtC","ignoreList":[]}
@@ -1,25 +0,0 @@
1
- import { type Dispatch, type SetStateAction } from 'react';
2
- export type PersistentStateMigration = {
3
- from: number;
4
- migrate: (state: unknown) => unknown;
5
- };
6
- /**
7
- * Functions identically to useState except that a PersistentStateProvider can be used to
8
- * track all calls to this hook and persist the value for future page loads.
9
- * Primarily used in Deephaven UI so we can persist state of multiple components within a panel.
10
- *
11
- * @param initialState The initial state if there is no previously persisted state.
12
- * @param config.type The type of the state. This identifier is used to validate the state being rehydrated. Should be unique to your component.
13
- * @param config.version The version of the state. This should be an integer, and is used to determine if the state needs to be migrated. Value should be incremented when data structure changes for this type.
14
- * @param config.migrations An array of migrations to apply to the state if the version of the persisted state is below the current version. Each migration increments the version by 1.
15
- * @param config.migrations.from The starting version of the migration. The migration will increment the version by 1.
16
- * @param config.migrations.migrate The function to call to migrate the state.
17
- * @returns [state, setState] tuple just like useState.
18
- */
19
- export declare function usePersistentState<S>(initialState: S | (() => S), config: {
20
- type: string;
21
- version: number;
22
- migrations?: PersistentStateMigration[];
23
- }): [state: S, setState: Dispatch<SetStateAction<S>>];
24
- export default usePersistentState;
25
- //# sourceMappingURL=usePersistentState.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"usePersistentState.d.ts","sourceRoot":"","sources":["../src/usePersistentState.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,OAAO,CAAC;AAIf,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;CACtC,CAAC;AA+CF;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAC3B,MAAM,EAAE;IACN,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,wBAAwB,EAAE,CAAC;CACzC,GACA,CAAC,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAiEnD;AAED,eAAe,kBAAkB,CAAC"}
@@ -1,97 +0,0 @@
1
- import { useContext, useDebugValue, useEffect, useState } from 'react';
2
- import { nanoid } from 'nanoid';
3
- import { PersistentStateContext } from "./PersistentStateContext.js";
4
- function migrateState(state, from, to, migrations, type) {
5
- if (from > to) {
6
- throw new Error("Persisted state ".concat(type, " is a newer version than the current version. Persisted version: ").concat(from, ". Current version: ").concat(to));
7
- }
8
- var migratedState = state;
9
- var currentVersion = from;
10
- while (currentVersion < to) {
11
- // eslint-disable-next-line no-loop-func
12
- var possibleMigration = migrations.filter(m => m.from === currentVersion);
13
- if (possibleMigration.length === 0) {
14
- throw new Error("No migration found for persisted state ".concat(type, " from version ").concat(currentVersion));
15
- }
16
- if (possibleMigration.length > 1) {
17
- throw new Error("Multiple migrations found for persisted state ".concat(type, " from version ").concat(currentVersion));
18
- }
19
- var migration = possibleMigration[0];
20
- try {
21
- migratedState = migration.migrate(migratedState);
22
- currentVersion += 1;
23
- } catch (e) {
24
- throw new Error("Error migrating persisted state ".concat(type, " from version ").concat(migration.from, ": ").concat(e), {
25
- cause: e
26
- });
27
- }
28
- }
29
- return migratedState;
30
- }
31
-
32
- /**
33
- * Functions identically to useState except that a PersistentStateProvider can be used to
34
- * track all calls to this hook and persist the value for future page loads.
35
- * Primarily used in Deephaven UI so we can persist state of multiple components within a panel.
36
- *
37
- * @param initialState The initial state if there is no previously persisted state.
38
- * @param config.type The type of the state. This identifier is used to validate the state being rehydrated. Should be unique to your component.
39
- * @param config.version The version of the state. This should be an integer, and is used to determine if the state needs to be migrated. Value should be incremented when data structure changes for this type.
40
- * @param config.migrations An array of migrations to apply to the state if the version of the persisted state is below the current version. Each migration increments the version by 1.
41
- * @param config.migrations.from The starting version of the migration. The migration will increment the version by 1.
42
- * @param config.migrations.migrate The function to call to migrate the state.
43
- * @returns [state, setState] tuple just like useState.
44
- */
45
- export function usePersistentState(initialState, config) {
46
- var _context$getInitialSt;
47
- // We use this id to track if the component re-renders due to calling setState in its render function.
48
- // Otherwise, usePersistentState might be called twice by the same component in the same render cycle before flushing in the provider.
49
- var [id] = useState(() => nanoid());
50
- var context = useContext(PersistentStateContext);
51
- var {
52
- value: persistedData,
53
- done
54
- } = (_context$getInitialSt = context === null || context === void 0 ? void 0 : context.getInitialState(id)) !== null && _context$getInitialSt !== void 0 ? _context$getInitialSt : {
55
- value: undefined,
56
- done: true
57
- };
58
-
59
- // If not done, then we can use the persisted state
60
- // Otherwise, we have exhausted the persisted state
61
- // By checking done, we are able to explicitly save undefined as a state value
62
- var [state, setState] = useState(() => {
63
- if (persistedData == null || done) {
64
- return typeof initialState === 'function' ? initialState() : initialState;
65
- }
66
- if (persistedData.type !== config.type) {
67
- throw new Error("usePersistentState type mismatch. Expected ".concat(config.type, " but got ").concat(persistedData.type, "."));
68
- }
69
- if (persistedData.version !== config.version) {
70
- var _config$migrations;
71
- return migrateState(persistedData.state, persistedData.version, config.version, (_config$migrations = config.migrations) !== null && _config$migrations !== void 0 ? _config$migrations : [], config.type);
72
- }
73
- return persistedData.state;
74
- });
75
- var stateWithConfig = {
76
- type: config.type,
77
- version: config.version,
78
- state
79
- };
80
- useDebugValue(stateWithConfig);
81
- context === null || context === void 0 || context.addState(id, stateWithConfig);
82
-
83
- // This won't cause unnecessary renders on initial mount because the state is already tracking,
84
- // so calls to scheduleStateUpdate will be no-ops since tracking finishes in an effect at the provider after this effect.
85
- // When a component mounts after the parents have already rendered, this will trigger a re-render to track the new state immediately.
86
- useEffect(function scheduleUpdateOnMountAndChange() {
87
- context === null || context === void 0 || context.scheduleStateUpdate();
88
- }, [context, state]);
89
- useEffect(function scheduleUpdateOnUnmount() {
90
- return () => {
91
- context === null || context === void 0 || context.scheduleStateUpdate();
92
- };
93
- }, [context]);
94
- return [state, setState];
95
- }
96
- export default usePersistentState;
97
- //# sourceMappingURL=usePersistentState.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"usePersistentState.js","names":["useContext","useDebugValue","useEffect","useState","nanoid","PersistentStateContext","migrateState","state","from","to","migrations","type","Error","concat","migratedState","currentVersion","possibleMigration","filter","m","length","migration","migrate","e","cause","usePersistentState","initialState","config","_context$getInitialSt","id","context","value","persistedData","done","getInitialState","undefined","setState","version","_config$migrations","stateWithConfig","addState","scheduleUpdateOnMountAndChange","scheduleStateUpdate","scheduleUpdateOnUnmount"],"sources":["../src/usePersistentState.ts"],"sourcesContent":["import {\n useContext,\n useDebugValue,\n useEffect,\n useState,\n type Dispatch,\n type SetStateAction,\n} from 'react';\nimport { nanoid } from 'nanoid';\nimport { PersistentStateContext } from './PersistentStateContext';\n\nexport type PersistentStateMigration = {\n from: number;\n migrate: (state: unknown) => unknown;\n};\n\nfunction migrateState(\n state: unknown,\n from: number,\n to: number,\n migrations: PersistentStateMigration[],\n type: string\n): unknown {\n if (from > to) {\n throw new Error(\n `Persisted state ${type} is a newer version than the current version. Persisted version: ${from}. Current version: ${to}`\n );\n }\n\n let migratedState = state;\n let currentVersion = from;\n while (currentVersion < to) {\n // eslint-disable-next-line no-loop-func\n const possibleMigration = migrations.filter(m => m.from === currentVersion);\n if (possibleMigration.length === 0) {\n throw new Error(\n `No migration found for persisted state ${type} from version ${currentVersion}`\n );\n }\n\n if (possibleMigration.length > 1) {\n throw new Error(\n `Multiple migrations found for persisted state ${type} from version ${currentVersion}`\n );\n }\n\n const migration = possibleMigration[0];\n\n try {\n migratedState = migration.migrate(migratedState);\n currentVersion += 1;\n } catch (e) {\n throw new Error(\n `Error migrating persisted state ${type} from version ${migration.from}: ${e}`,\n { cause: e }\n );\n }\n }\n return migratedState;\n}\n\n/**\n * Functions identically to useState except that a PersistentStateProvider can be used to\n * track all calls to this hook and persist the value for future page loads.\n * Primarily used in Deephaven UI so we can persist state of multiple components within a panel.\n *\n * @param initialState The initial state if there is no previously persisted state.\n * @param config.type The type of the state. This identifier is used to validate the state being rehydrated. Should be unique to your component.\n * @param config.version The version of the state. This should be an integer, and is used to determine if the state needs to be migrated. Value should be incremented when data structure changes for this type.\n * @param config.migrations An array of migrations to apply to the state if the version of the persisted state is below the current version. Each migration increments the version by 1.\n * @param config.migrations.from The starting version of the migration. The migration will increment the version by 1.\n * @param config.migrations.migrate The function to call to migrate the state.\n * @returns [state, setState] tuple just like useState.\n */\nexport function usePersistentState<S>(\n initialState: S | (() => S),\n config: {\n type: string;\n version: number;\n migrations?: PersistentStateMigration[];\n }\n): [state: S, setState: Dispatch<SetStateAction<S>>] {\n // We use this id to track if the component re-renders due to calling setState in its render function.\n // Otherwise, usePersistentState might be called twice by the same component in the same render cycle before flushing in the provider.\n const [id] = useState(() => nanoid());\n const context = useContext(PersistentStateContext);\n const { value: persistedData, done } = context?.getInitialState<S>(id) ?? {\n value: undefined,\n done: true,\n };\n\n // If not done, then we can use the persisted state\n // Otherwise, we have exhausted the persisted state\n // By checking done, we are able to explicitly save undefined as a state value\n const [state, setState] = useState<S>(() => {\n if (persistedData == null || done) {\n return typeof initialState === 'function'\n ? (initialState as () => S)()\n : initialState;\n }\n\n if (persistedData.type !== config.type) {\n throw new Error(\n `usePersistentState type mismatch. Expected ${config.type} but got ${persistedData.type}.`\n );\n }\n\n if (persistedData.version !== config.version) {\n return migrateState(\n persistedData.state,\n persistedData.version,\n config.version,\n config.migrations ?? [],\n config.type\n ) as S;\n }\n\n return persistedData.state;\n });\n\n const stateWithConfig = { type: config.type, version: config.version, state };\n\n useDebugValue(stateWithConfig);\n\n context?.addState(id, stateWithConfig);\n\n // This won't cause unnecessary renders on initial mount because the state is already tracking,\n // so calls to scheduleStateUpdate will be no-ops since tracking finishes in an effect at the provider after this effect.\n // When a component mounts after the parents have already rendered, this will trigger a re-render to track the new state immediately.\n useEffect(\n function scheduleUpdateOnMountAndChange() {\n context?.scheduleStateUpdate();\n },\n [context, state]\n );\n\n useEffect(\n function scheduleUpdateOnUnmount() {\n return () => {\n context?.scheduleStateUpdate();\n };\n },\n [context]\n );\n\n return [state, setState];\n}\n\nexport default usePersistentState;\n"],"mappings":"AAAA,SACEA,UAAU,EACVC,aAAa,EACbC,SAAS,EACTC,QAAQ,QAGH,OAAO;AACd,SAASC,MAAM,QAAQ,QAAQ;AAAC,SACvBC,sBAAsB;AAO/B,SAASC,YAAYA,CACnBC,KAAc,EACdC,IAAY,EACZC,EAAU,EACVC,UAAsC,EACtCC,IAAY,E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