@contentstack/studio-registry 1.8.0 → 1.9.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/cjs/index.cjs +998 -201
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/common/global-store.js +2 -0
- package/dist/esm/common/global-store.js.map +1 -1
- package/dist/esm/component-registry/registry-options-processor.js +1 -0
- package/dist/esm/component-registry/registry-options-processor.js.map +1 -1
- package/dist/esm/component-registry/registry.constant.js +0 -3
- package/dist/esm/component-registry/registry.constant.js.map +1 -1
- package/dist/esm/component-registry/registry.js +1 -27
- package/dist/esm/component-registry/registry.js.map +1 -1
- package/dist/esm/data-binder/cslp.js +8 -1
- package/dist/esm/data-binder/cslp.js.map +1 -1
- package/dist/esm/data-binder/retrieve-data.js +11 -32
- package/dist/esm/data-binder/retrieve-data.js.map +1 -1
- package/dist/esm/data-binder/retrieve-data.util.js +7 -4
- package/dist/esm/data-binder/retrieve-data.util.js.map +1 -1
- package/dist/esm/data-binder/state-binding.util.js +158 -0
- package/dist/esm/data-binder/state-binding.util.js.map +1 -0
- package/dist/esm/design-registry/design-token.js +5 -2
- package/dist/esm/design-registry/design-token.js.map +1 -1
- package/dist/esm/index.d.ts +468 -26
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/state-store/action-props.util.js +42 -0
- package/dist/esm/state-store/action-props.util.js.map +1 -0
- package/dist/esm/state-store/function-registry.js +128 -0
- package/dist/esm/state-store/function-registry.js.map +1 -0
- package/dist/esm/state-store/public-api.js +109 -0
- package/dist/esm/state-store/public-api.js.map +1 -0
- package/dist/esm/state-store/singleton.js +27 -0
- package/dist/esm/state-store/singleton.js.map +1 -0
- package/dist/esm/state-store/state-store.js +384 -0
- package/dist/esm/state-store/state-store.js.map +1 -0
- package/package.json +6 -6
- package/dist/esm/utils/symbols.js +0 -12
- package/dist/esm/utils/symbols.js.map +0 -1
package/dist/esm/index.js
CHANGED
|
@@ -9,8 +9,13 @@ export { JSONRTERegistry } from './json-rte-registry/index.js';
|
|
|
9
9
|
export { clearReportedBindingErrorsForNode, retrievePropValue } from './data-binder/retrieve-data.js';
|
|
10
10
|
export { convertBindingPath, getBindingString, getDataType, getDefaultPropStaticValue, getFlattenedData, getPlaceHolderComponent, getRepeaterContextPath, getSlotData, isDataMatchingPropType, isKnownType, normalizeSingleSelectChoiceValue, resolveMetadataBindings } from './data-binder/retrieve-data.util.js';
|
|
11
11
|
export { getCslpPath, getCslpTag, getRepeaterBinding } from './data-binder/cslp.js';
|
|
12
|
+
export { collectStateBindingKeys, getNodeBoundFunctionName, getStateDefaultsFor, getStateValuesFor } from './data-binder/state-binding.util.js';
|
|
12
13
|
export { BreakpointRegistry } from './breakpoint-registry/index.js';
|
|
13
|
-
export { getSymbolUIDFromNode, getSymbolUIDFromUID } from './utils/symbols.js';
|
|
14
14
|
export { getSectionUIDFromNode } from './utils/sections.js';
|
|
15
|
+
export { StudioStateStore } from './state-store/state-store.js';
|
|
16
|
+
export { getStateStoreInstance, resetStateStoreInstance } from './state-store/singleton.js';
|
|
17
|
+
export { getStudioStateSnapshot, registerComponentStateVariable, registerGlobalStateVariables, studioState, subscribeStudioStateVariablesChanged } from './state-store/public-api.js';
|
|
18
|
+
export { callStudioFunction, getStudioFunctionNames, registerStudioFunctions, resetStudioFunctions, subscribeStudioFunctionsChanged } from './state-store/function-registry.js';
|
|
19
|
+
export { wrapActionProps } from './state-store/action-props.util.js';
|
|
15
20
|
export { getGlobalStudioStore, resetGlobalStudioStore } from './common/global-store.js';
|
|
16
21
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { callStudioFunction } from './function-registry.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Swap resolved `action` props (registered function NAMES saved from the
|
|
5
|
+
* Studio props panel) for real callables before the props reach the
|
|
6
|
+
* component. Runs in both renderers (live + canvas) right after
|
|
7
|
+
* `retrievePropValue`, so ANY component — SDK basic or app-registered —
|
|
8
|
+
* that declares a `{ type: "action" }` prop receives an invokable function:
|
|
9
|
+
*
|
|
10
|
+
* ```tsx
|
|
11
|
+
* function Card({ onBuy }: { onBuy?: (...args: unknown[]) => unknown }) {
|
|
12
|
+
* return <button onClick={() => onBuy?.({ sku: "X1" })}>Buy</button>;
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* An unset/empty selection resolves to `undefined` (component can feature-
|
|
17
|
+
* detect); a missing function name is contained inside `callStudioFunction`
|
|
18
|
+
* (warn, no crash). Non-action props pass through untouched.
|
|
19
|
+
*/
|
|
20
|
+
function wrapActionProps(componentProps, resolvedProps) {
|
|
21
|
+
if (!componentProps)
|
|
22
|
+
return resolvedProps;
|
|
23
|
+
let wrapped = null;
|
|
24
|
+
for (const [name, def] of Object.entries(componentProps)) {
|
|
25
|
+
const type = typeof def === "string" ? def : def?.type;
|
|
26
|
+
if (type !== "action")
|
|
27
|
+
continue;
|
|
28
|
+
const value = resolvedProps[name];
|
|
29
|
+
if (typeof value === "function")
|
|
30
|
+
continue; // already callable (direct usage)
|
|
31
|
+
if (!wrapped)
|
|
32
|
+
wrapped = { ...resolvedProps };
|
|
33
|
+
const functionName = typeof value === "string" ? value.trim() : "";
|
|
34
|
+
wrapped[name] = functionName
|
|
35
|
+
? (...args) => callStudioFunction(functionName, ...args)
|
|
36
|
+
: undefined;
|
|
37
|
+
}
|
|
38
|
+
return wrapped ?? resolvedProps;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { wrapActionProps };
|
|
42
|
+
//# sourceMappingURL=action-props.util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action-props.util.js","sources":["../../../src/state-store/action-props.util.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAGA;;;;;;;;;;;;;;;;AAgBG;AACG,SAAU,eAAe,CAC7B,cAAiC,EACjC,aAAgB,EAAA;AAEhB,IAAA,IAAI,CAAC,cAAc;AAAE,QAAA,OAAO,aAAa;IAEzC,IAAI,OAAO,GAAa,IAAI;AAC5B,IAAA,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;AACxD,QAAA,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,QAAQ,GAAG,GAAG,GAAG,GAAG,EAAE,IAAI;QACtD,IAAI,IAAI,KAAK,QAAQ;YAAE;AAEvB,QAAA,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC;QACjC,IAAI,OAAO,KAAK,KAAK,UAAU;AAAE,YAAA,SAAS;AAE1C,QAAA,IAAI,CAAC,OAAO;AAAE,YAAA,OAAO,GAAG,EAAE,GAAG,aAAa,EAAE;AAC5C,QAAA,MAAM,YAAY,GAAG,OAAO,KAAK,KAAK,QAAQ,GAAG,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE;AACjE,QAAA,OAAmC,CAAC,IAAI,CAAC,GAAG;AAC3C,cAAE,CAAC,GAAG,IAAe,KAAK,kBAAkB,CAAC,YAAY,EAAE,GAAG,IAAI;cAChE,SAAS;IACf;IACA,OAAO,OAAO,IAAI,aAAa;AACjC;;;;"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { getGlobalStudioStore } from '../common/global-store.js';
|
|
2
|
+
import { getStateStoreInstance } from './singleton.js';
|
|
3
|
+
|
|
4
|
+
function getRegistry() {
|
|
5
|
+
const store = getGlobalStudioStore();
|
|
6
|
+
if (!store.functionRegistry) {
|
|
7
|
+
store.functionRegistry = {
|
|
8
|
+
functions: new Map(),
|
|
9
|
+
changeListeners: new Set(),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
// Slot created by an older module instance (globalThis singleton outlives
|
|
13
|
+
// module graphs) — patch the field in rather than dropping registrations.
|
|
14
|
+
if (!store.functionRegistry.changeListeners) {
|
|
15
|
+
store.functionRegistry.changeListeners = new Set();
|
|
16
|
+
}
|
|
17
|
+
return store.functionRegistry;
|
|
18
|
+
}
|
|
19
|
+
function notifyFunctionsChanged() {
|
|
20
|
+
for (const listener of [...getRegistry().changeListeners]) {
|
|
21
|
+
try {
|
|
22
|
+
listener();
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
console.error("[Composable Studio SDK] studio-functions change listener threw:", err);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Subscribe to registry changes (functions added / reset). Used by the SDK's
|
|
31
|
+
* Studio bridge to push the fresh name list to the Studio props panel whenever
|
|
32
|
+
* the app registers functions — regardless of whether that happens before or
|
|
33
|
+
* after the component registry sync.
|
|
34
|
+
*/
|
|
35
|
+
function subscribeStudioFunctionsChanged(listener) {
|
|
36
|
+
const registry = getRegistry();
|
|
37
|
+
registry.changeListeners.add(listener);
|
|
38
|
+
return () => registry.changeListeners.delete(listener);
|
|
39
|
+
}
|
|
40
|
+
const isDev = () => typeof process !== "undefined" && process.env.NODE_ENV === "development";
|
|
41
|
+
/**
|
|
42
|
+
* Register named custom functions — the app's business logic that components
|
|
43
|
+
* invoke by NAME. Call as many times as you like (e.g. one call per feature
|
|
44
|
+
* module); registrations MERGE. Re-registering an existing name overwrites it
|
|
45
|
+
* with a dev warning (supports HMR).
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* registerStudioFunctions({
|
|
49
|
+
* "cart:add": (ctx, args) => {
|
|
50
|
+
* ctx.set("cart:count", (prev) => (prev ?? 0) + 1);
|
|
51
|
+
* ctx.set("cart:last_added", args?.sku ?? "");
|
|
52
|
+
* },
|
|
53
|
+
* "coupon:apply": (ctx, args) => {
|
|
54
|
+
* const discounts = { SAVE10: 10, SAVE20: 20 };
|
|
55
|
+
* ctx.set("checkout:discount", discounts[args?.code] ?? 0);
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
function registerStudioFunctions(functions) {
|
|
61
|
+
const registry = getRegistry();
|
|
62
|
+
for (const [name, fn] of Object.entries(functions ?? {})) {
|
|
63
|
+
if (typeof fn !== "function") {
|
|
64
|
+
console.warn(`[Composable Studio SDK] registerStudioFunctions: "${name}" is not a function — skipped`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (registry.functions.has(name) && isDev()) {
|
|
68
|
+
console.warn(`[Composable Studio SDK] studio function "${name}" re-registered (overwriting previous)`);
|
|
69
|
+
}
|
|
70
|
+
registry.functions.set(name, fn);
|
|
71
|
+
}
|
|
72
|
+
notifyFunctionsChanged();
|
|
73
|
+
}
|
|
74
|
+
/** Names of every registered function — for debugging / future picker UI. */
|
|
75
|
+
function getStudioFunctionNames() {
|
|
76
|
+
return [...getRegistry().functions.keys()];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Invoke a registered function by name from ANY component or code.
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* <button onClick={() => callStudioFunction("cart:add", { sku })}>Add</button>
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* Never throws into the UI: a missing name warns and returns undefined; sync
|
|
86
|
+
* throws and async rejections are caught and logged. The function receives
|
|
87
|
+
* the shared-state ctx as its first argument.
|
|
88
|
+
*/
|
|
89
|
+
function callStudioFunction(name, ...args) {
|
|
90
|
+
const fn = getRegistry().functions.get(name);
|
|
91
|
+
if (!fn) {
|
|
92
|
+
console.warn(`[Composable Studio SDK] studio function "${name}" is not registered. Did you call registerStudioFunctions()?`);
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const store = getStateStoreInstance();
|
|
96
|
+
const ctx = {
|
|
97
|
+
get: (key) => store.get(key),
|
|
98
|
+
set: (key, next) => store.set(key, next),
|
|
99
|
+
subscribe: (key, listener) => store.subscribe(key, listener),
|
|
100
|
+
};
|
|
101
|
+
try {
|
|
102
|
+
// Forward every argument the caller passed (action-prop callbacks can be
|
|
103
|
+
// invoked with multiple positional args); the cast bridges the variadic
|
|
104
|
+
// call to StudioFunction's (ctx, args?, ...rest) shape.
|
|
105
|
+
const result = fn(ctx, ...args);
|
|
106
|
+
if (result &&
|
|
107
|
+
typeof result.then === "function" &&
|
|
108
|
+
typeof result.catch === "function") {
|
|
109
|
+
return result.catch((err) => {
|
|
110
|
+
console.error(`[Composable Studio SDK] studio function "${name}" rejected:`, err);
|
|
111
|
+
return undefined;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
console.error(`[Composable Studio SDK] studio function "${name}" threw:`, err);
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Test-only: clear all registered functions. */
|
|
122
|
+
function resetStudioFunctions() {
|
|
123
|
+
getRegistry().functions.clear();
|
|
124
|
+
notifyFunctionsChanged();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { callStudioFunction, getStudioFunctionNames, registerStudioFunctions, resetStudioFunctions, subscribeStudioFunctionsChanged };
|
|
128
|
+
//# sourceMappingURL=function-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-registry.js","sources":["../../../src/state-store/function-registry.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAmCA,SAAS,WAAW,GAAA;AAClB,IAAA,MAAM,KAAK,GAAG,oBAAoB,EAEjC;AACD,IAAA,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE;QAC3B,KAAK,CAAC,gBAAgB,GAAG;YACvB,SAAS,EAAE,IAAI,GAAG,EAAE;YACpB,eAAe,E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AAC,GAAG,KAAI;gBAChD,OAAO,CAAC,KAAK,CACX,CAAA,yCAAA,EAA4C,IAAI,CAAA,WAAA,CAAa,EAC7D,GAAG,CACJ;AACD,gBAAA,OAAO,SAAS;AAClB,YAAA,CAAC,CAAC;QACJ;AACA,QAAA,OAAO,MAAM;IACf;IAAE,OAAO,GAAG,EAAE;QACZ,OAAO,CAAC,KAAK,CACX,CAAA,yCAAA,EAA4C,IAAI,CAAA,QAAA,CAAU,EAC1D,GAAG,CACJ;AACD,QAAA,OAAO,SAAS;IAClB;AACF;AAEA;SACgB,oBAAoB,GAAA;AAClC,IAAA,WAAW,EAAE,CAAC,SAAS,CAAC,KAAK,EAAE;AAC/B,IAAA,sBAAsB,EAAE;AAC1B;;;;"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { getStateStoreInstance } from './singleton.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Declare an author-bindable state variable at component/section module
|
|
5
|
+
* scope — the "component-declared" tier. Call at module load time (top of
|
|
6
|
+
* the file, next to the component definition):
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* registerComponentStateVariable("cart:filter", { type: "string", defaultValue: "" });
|
|
10
|
+
*
|
|
11
|
+
* export function CartFilter() {
|
|
12
|
+
* const [filter, setFilter] = useStudioState<string>("cart:filter");
|
|
13
|
+
* ...
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Declared variables appear for binding inside the existing External Data
|
|
18
|
+
* source; undeclared keys stay code-only (ad-hoc tier).
|
|
19
|
+
*
|
|
20
|
+
* Priority: component-level declarations WIN over the same key declared
|
|
21
|
+
* globally at init (`state.variables`) — deterministically, regardless of
|
|
22
|
+
* which module evaluated first. The global declaration is used only for keys
|
|
23
|
+
* no component declared. Runtime `set` values always beat declared defaults.
|
|
24
|
+
*/
|
|
25
|
+
function registerComponentStateVariable(key, options) {
|
|
26
|
+
if (!key || typeof key !== "string") {
|
|
27
|
+
throw new Error("[Composable Studio SDK] registerComponentStateVariable requires a non-empty string key");
|
|
28
|
+
}
|
|
29
|
+
getStateStoreInstance().declareVariable(key, options, "component");
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Imperative access to the shared state store — for custom user code outside
|
|
33
|
+
* React (event handlers, analytics glue, business logic):
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { studioState } from "@contentstack/studio-react";
|
|
37
|
+
*
|
|
38
|
+
* studioState.set("cart:count", (prev) => (prev ?? 0) + 1);
|
|
39
|
+
* const count = studioState.get<number>("cart:count");
|
|
40
|
+
* const stop = studioState.subscribe("cart:count", () => console.log("changed"));
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Same store instance the `useStudioState` hook and author bindings use —
|
|
44
|
+
* one source of truth across components, sections, templates, and code.
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* Snapshot of every DECLARED state variable with its CURRENT value —
|
|
48
|
+
* `[{ key, value }]`. This is what the Studio Data Picker lists under
|
|
49
|
+
* Component Props so authors can bind the values the app made available
|
|
50
|
+
* (init `state.variables`, `registerComponentStateVariable`) — e.g. `price → 2999`.
|
|
51
|
+
*
|
|
52
|
+
* Only declared variables are enumerable: BYOS user stores expose key-based
|
|
53
|
+
* `getState` with no key listing, so ad-hoc (code-only) keys can't be swept.
|
|
54
|
+
*/
|
|
55
|
+
function getStudioStateSnapshot() {
|
|
56
|
+
const store = getStateStoreInstance();
|
|
57
|
+
return Object.keys(store.getVariables()).map((key) => ({
|
|
58
|
+
key,
|
|
59
|
+
value: store.get(key),
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Subscribe to declared-variable-set changes on the page-global store —
|
|
64
|
+
* passthrough for the Studio bridge (see StudioStateStore.subscribeVariablesChanged).
|
|
65
|
+
*/
|
|
66
|
+
function subscribeStudioStateVariablesChanged(listener) {
|
|
67
|
+
return getStateStoreInstance().subscribeVariablesChanged(listener);
|
|
68
|
+
}
|
|
69
|
+
const studioState = {
|
|
70
|
+
get(key) {
|
|
71
|
+
return getStateStoreInstance().get(key);
|
|
72
|
+
},
|
|
73
|
+
set(key, next) {
|
|
74
|
+
getStateStoreInstance().set(key, next);
|
|
75
|
+
},
|
|
76
|
+
subscribe(key, listener) {
|
|
77
|
+
return getStateStoreInstance().subscribe(key, listener);
|
|
78
|
+
},
|
|
79
|
+
/** Clear all state — call on client-side route change if you want per-page state. */
|
|
80
|
+
reset() {
|
|
81
|
+
getStateStoreInstance().reset();
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Declare a SET of variables at the INIT tier — same priority as SDK init
|
|
86
|
+
* `state.variables`, so later component-level `registerComponentStateVariable` calls
|
|
87
|
+
* still override them.
|
|
88
|
+
*
|
|
89
|
+
* Purpose: SSR hydration alignment. The server declares variables at module
|
|
90
|
+
* scope (server init file), but apps typically run the CLIENT init from a
|
|
91
|
+
* lazily-imported module — AFTER hydration. State-bound nodes then hydrate
|
|
92
|
+
* against undeclared keys and mismatch the server HTML. Import a tiny module
|
|
93
|
+
* that calls this at module scope (e.g. from _app) so declarations exist on
|
|
94
|
+
* the client BEFORE hydration:
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* // lib/register-state-variables.ts — imported statically by _app
|
|
98
|
+
* registerGlobalStateVariables(studioStateConfig.variables ?? {});
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function registerGlobalStateVariables(variables) {
|
|
102
|
+
const store = getStateStoreInstance();
|
|
103
|
+
for (const [key, def] of Object.entries(variables ?? {})) {
|
|
104
|
+
store.declareVariable(key, def, "init");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { getStudioStateSnapshot, registerComponentStateVariable, registerGlobalStateVariables, studioState, subscribeStudioStateVariablesChanged };
|
|
109
|
+
//# sourceMappingURL=public-api.js.map
|
|
@@ -0,0 +1 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qB,EAAE,CAAC,KAAK,EAAE;IACjC,CAAC;;AAGH;;;;;;;;;;;;;;;;AAgBG;AACG,SAAU,4BAA4B,CAC1C,SAA2C,EAAA;AAE3C,IAAA,MAAM,KAAK,GAAG,qBAAqB,EAAE;AACrC,IAAA,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC,EAAE;QACxD,KAAK,CAAC,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC;IACzC;AACF;;;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getGlobalStudioStore } from '../common/global-store.js';
|
|
2
|
+
import { StudioStateStore } from './state-store.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Lazily create (or reuse) the page-global StudioStateStore in the SDK's
|
|
6
|
+
* versioned globalThis slot. Same pattern as the component/design registries
|
|
7
|
+
* — survives Next.js RSC/client module-graph splits so every surface talks
|
|
8
|
+
* to the same store instance.
|
|
9
|
+
*/
|
|
10
|
+
function getStateStoreInstance() {
|
|
11
|
+
const store = getGlobalStudioStore();
|
|
12
|
+
if (!store.stateStore) {
|
|
13
|
+
store.stateStore = new StudioStateStore();
|
|
14
|
+
}
|
|
15
|
+
return store.stateStore;
|
|
16
|
+
}
|
|
17
|
+
/** Test-only: drop the singleton so the next access builds a fresh store. */
|
|
18
|
+
function resetStateStoreInstance() {
|
|
19
|
+
const store = getGlobalStudioStore();
|
|
20
|
+
// Full teardown (unsub bridges, drop config/vars/listeners) — not the soft
|
|
21
|
+
// route-change reset() — since the slot itself is being dropped.
|
|
22
|
+
store.stateStore?.resetAll();
|
|
23
|
+
store.stateStore = null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { getStateStoreInstance, resetStateStoreInstance };
|
|
27
|
+
//# sourceMappingURL=singleton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"singleton.js","sources":["../../../src/state-store/singleton.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAGA;;;;;AAKG;SACa,qBAAqB,GAAA;AACnC,IAAA,MAAM,KAAK,GAAG,oBAAoB,EAAE;AACpC,IAAA,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AACrB,QAAA,KAAK,CAAC,UAAU,GAAG,IAAI,gBAAgB,EAAE;IAC3C;IACA,OAAO,KAAK,CAAC,UAA8B;AAC7C;AAEA;SACgB,uBAAuB,GAAA;AACrC,IAAA,MAAM,KAAK,GAAG,oBAAoB,EAAE;;;AAGnC,IAAA,KAAK,CAAC,UAAsC,EAAE,QAAQ,EAAE;AACzD,IAAA,KAAK,CAAC,UAAU,GAAG,IAAI;AACzB;;;;"}
|