@coherent.js/state 1.0.0-rc.6 → 1.0.1

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/state-manager.js"],
4
- "sourcesContent": ["/**\n * Simple state management for server-side rendering\n * This is mainly for component state during rendering\n */\n\nconst globalState = new Map();\n\n/**\n * Creates a state container for a request/render cycle\n * @param {Object} initialState - Initial state object\n * @returns {Object} State container\n */\nexport function createState(initialState = {}) {\n const state = new Map(Object.entries(initialState));\n\n return {\n get(key) {\n return state.get(key);\n },\n\n set(key, value) {\n state.set(key, value);\n return this;\n },\n\n has(key) {\n return state.has(key);\n },\n\n delete(key) {\n return state.delete(key);\n },\n\n clear() {\n state.clear();\n return this;\n },\n\n toObject() {\n return Object.fromEntries(state);\n },\n\n // For debugging\n _internal: state\n };\n}\n\n/**\n * Global state for sharing data across components during SSR\n */\nexport const globalStateManager = {\n set(key, value) {\n globalState.set(key, value);\n },\n\n get(key) {\n return globalState.get(key);\n },\n\n has(key) {\n return globalState.has(key);\n },\n\n clear() {\n globalState.clear();\n },\n\n // Create isolated state for each request\n createRequestState() {\n return createState();\n }\n};\n\n/**\n * Context stack for managing nested context providers\n */\nconst contextStacks = new Map();\n\n/**\n * Context provider for passing data down the component tree\n * @param {string} key - Context key\n * @param {*} value - Context value\n * @param {Object} children - Children to render with context\n * @returns {Object} Children with context available\n */\nexport function provideContext(key, value) {\n // Initialize context stack if it doesn't exist\n if (!contextStacks.has(key)) {\n contextStacks.set(key, []);\n }\n \n const stack = contextStacks.get(key);\n \n // Store previous value\n const previousValue = globalState.get(key);\n \n // Push previous value to stack and set new value\n stack.push(previousValue);\n globalState.set(key, value);\n}\n\n/**\n * Create a context provider component that works with the rendering system\n * @param {string} key - Context key\n * @param {*} value - Context value\n * @param {Object} children - Children to render with context\n * @returns {Function} Component function that provides context\n */\nexport function createContextProvider(key, value, children) {\n // Return a function that will render the children within the context\n return (renderFunction) => {\n try {\n // Provide context\n provideContext(key, value);\n \n // If a render function is provided, use it to render children\n // Otherwise return children to be rendered by the caller\n if (renderFunction && typeof renderFunction === 'function') {\n return renderFunction(children);\n } else {\n return children;\n }\n } finally {\n // Always restore context when done\n restoreContext(key);\n }\n };\n}\n\n/**\n * Restore context to previous value\n * @param {string} key - Context key\n */\nexport function restoreContext(key) {\n if (!contextStacks.has(key)) return;\n \n const stack = contextStacks.get(key);\n \n // Restore previous value from stack\n const previousValue = stack.pop();\n \n if (stack.length === 0) {\n // No more providers, delete the key if it was undefined before\n if (previousValue === undefined) {\n globalState.delete(key);\n } else {\n globalState.set(key, previousValue);\n }\n \n // Clean up empty stack\n contextStacks.delete(key);\n } else {\n // Restore previous value\n globalState.set(key, previousValue);\n }\n}\n\n/**\n * Clear all context stacks (useful for cleanup after rendering)\n */\nexport function clearAllContexts() {\n contextStacks.clear();\n // Note: This doesn't clear globalState as it might contain other data\n}\n\n/**\n * Context consumer to access provided context\n * @param {string} key - Context key\n * @returns {*} Context value\n */\nexport function useContext(key) {\n return globalState.get(key);\n}\n"],
5
- "mappings": ";AAKA,IAAM,cAAc,oBAAI,IAAI;AAOrB,SAAS,YAAY,eAAe,CAAC,GAAG;AAC3C,QAAM,QAAQ,IAAI,IAAI,OAAO,QAAQ,YAAY,CAAC;AAElD,SAAO;AAAA,IACH,IAAI,KAAK;AACL,aAAO,MAAM,IAAI,GAAG;AAAA,IACxB;AAAA,IAEA,IAAI,KAAK,OAAO;AACZ,YAAM,IAAI,KAAK,KAAK;AACpB,aAAO;AAAA,IACX;AAAA,IAEA,IAAI,KAAK;AACL,aAAO,MAAM,IAAI,GAAG;AAAA,IACxB;AAAA,IAEA,OAAO,KAAK;AACR,aAAO,MAAM,OAAO,GAAG;AAAA,IAC3B;AAAA,IAEA,QAAQ;AACJ,YAAM,MAAM;AACZ,aAAO;AAAA,IACX;AAAA,IAEA,WAAW;AACP,aAAO,OAAO,YAAY,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I,KAAK,aAAa;AAAA,IACtC;AAGA,kBAAc,OAAO,GAAG;AAAA,EAC5B,OAAO;AAEH,gBAAY,IAAI,KAAK,aAAa;AAAA,EACtC;AACJ;AAKO,SAAS,mBAAmB;AAC/B,gBAAc,MAAM;AAExB;AAOO,SAAS,WAAW,KAAK;AAC5B,SAAO,YAAY,IAAI,GAAG;AAC9B;",
4
+ "sourcesContent": ["/**\n * Simple state management for server-side rendering\n * This is mainly for component state during rendering\n */\n\nconst globalState = new Map();\n\n/**\n * Creates a state container for a request/render cycle\n * @param {Object} initialState - Initial state object\n * @returns {Object} State container\n */\nexport function createState(initialState = {}) {\n const state = new Map(Object.entries(initialState));\n\n return {\n get(key) {\n return state.get(key);\n },\n\n set(key, value) {\n state.set(key, value);\n return this;\n },\n\n has(key) {\n return state.has(key);\n },\n\n delete(key) {\n return state.delete(key);\n },\n\n clear() {\n state.clear();\n return this;\n },\n\n toObject() {\n return Object.fromEntries(state);\n },\n\n // For debugging\n _internal: state\n };\n}\n\n/**\n * Global state for sharing data across components during SSR\n */\nexport const globalStateManager = {\n set(key, value) {\n globalState.set(key, value);\n },\n\n get(key) {\n return globalState.get(key);\n },\n\n has(key) {\n return globalState.has(key);\n },\n\n clear() {\n globalState.clear();\n },\n\n // Create isolated state for each request\n createRequestState() {\n return createState();\n }\n};\n\n/**\n * Context stack for managing nested context providers\n */\nconst contextStacks = new Map();\n\n/**\n * Context provider for passing data down the component tree\n * @param {string} key - Context key\n * @param {*} value - Context value\n * @param {Object} children - Children to render with context\n * @returns {Object} Children with context available\n */\nexport function provideContext(key, value) {\n // Initialize context stack if it doesn't exist\n if (!contextStacks.has(key)) {\n contextStacks.set(key, []);\n }\n \n const stack = contextStacks.get(key);\n \n // Store previous value\n const previousValue = globalState.get(key);\n \n // Push previous value to stack and set new value\n stack.push(previousValue);\n globalState.set(key, value);\n}\n\n/**\n * Create a context provider component that works with the rendering system\n * @param {string} key - Context key\n * @param {*} value - Context value\n * @param {Object} children - Children to render with context\n * @returns {Function} Component function that provides context\n */\nexport function createContextProvider(key, value, children) {\n // Return a function that will render the children within the context\n return (renderFunction) => {\n try {\n // Provide context\n provideContext(key, value);\n \n // If a render function is provided, use it to render children\n // Otherwise return children to be rendered by the caller\n if (renderFunction && typeof renderFunction === 'function') {\n return renderFunction(children);\n } else {\n return children;\n }\n } finally {\n // Always restore context when done\n restoreContext(key);\n }\n };\n}\n\n/**\n * Restore context to previous value\n * @param {string} key - Context key\n */\nexport function restoreContext(key) {\n if (!contextStacks.has(key)) return;\n \n const stack = contextStacks.get(key);\n \n // Restore previous value from stack\n const previousValue = stack.pop();\n \n if (stack.length === 0) {\n // No more providers, delete the key if it was undefined before\n if (previousValue === undefined) {\n globalState.delete(key);\n } else {\n globalState.set(key, previousValue);\n }\n \n // Clean up empty stack\n contextStacks.delete(key);\n } else {\n // Restore previous value\n globalState.set(key, previousValue);\n }\n}\n\n/**\n * Clear all context stacks (useful for cleanup after rendering)\n *\n * useContext() reads from globalState, which is module-level and therefore\n * shared by every render in the process. Clearing only contextStacks left the\n * values themselves in place, so a context provided while rendering one\n * request stayed readable while rendering the next \u2014 and became unrecoverable,\n * since restoreContext() bails out once a key's stack is gone.\n *\n * Unwind each tracked key to the value it held before its first\n * provideContext() \u2014 the bottom of that key's stack. Only keys that were\n * actually provided as contexts are touched, so unrelated global state\n * survives, which is what the previous implementation was trying to protect.\n */\nexport function clearAllContexts() {\n for (const [key, stack] of contextStacks) {\n const beforeFirstProvide = stack[0];\n\n if (beforeFirstProvide === undefined) {\n globalState.delete(key);\n } else {\n globalState.set(key, beforeFirstProvide);\n }\n }\n\n contextStacks.clear();\n}\n\n/**\n * Context consumer to access provided context\n * @param {string} key - Context key\n * @returns {*} Context value\n */\nexport function useContext(key) {\n return globalState.get(key);\n}\n"],
5
+ "mappings": ";AAKA,IAAM,cAAc,oBAAI,IAAI;AAOrB,SAAS,YAAY,eAAe,CAAC,GAAG;AAC3C,QAAM,QAAQ,IAAI,IAAI,OAAO,QAA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,mBAAmB,YAAY;AACxD,eAAO,eAAe,QAAQ;AAAA,MAClC,OAAO;AACH,eAAO;AAAA,MACX;AAAA,IACJ,UAAE;AAEE,qBAAe,GAAG;AAAA,IACtB;AAAA,EACJ;AACJ;AAMO,SAAS,eAAe,KAAK;AAChC,MAAI,CAAC,cAAc,IAAI,GAAG,EAAG;AAE7B,QAAM,QAAQ,cAAc,IAAI,GAAG;AAGnC,QAAM,gBAAgB,MAAM,IAAI;AAEhC,MAAI,MAAM,WAAW,GAAG;AAEpB,QAAI,kBAAkB,QAAW;AAC7B,kBAAY,OAAO,GAAG;AAAA,IAC1B,OAAO;AACH,kBAAY,IAAI,KAAK,aAAa;AAAA,IACtC;AAGA,kBAAc,OAAO,GAAG;AAAA,EAC5B,OAAO;AAEH,gBAAY,IAAI,KAAK,aAAa;AAAA,EACtC;AACJ;AAgBO,SAAS,mBAAmB;AAC/B,aAAW,CAAC,KAAK,KAAK,KAAK,eAAe;AACtC,UAAM,qBAAqB,MAAM,CAAC;AAElC,QAAI,uBAAuB,QAAW;AAClC,kBAAY,OAAO,GAAG;AAAA,IAC1B,OAAO;AACH,kBAAY,IAAI,KAAK,kBAAkB;AAAA,IAC3C;AAAA,EACJ;AAEA,gBAAc,MAAM;AACxB;AAOO,SAAS,WAAW,KAAK;AAC5B,SAAO,YAAY,IAAI,GAAG;AAC9B;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coherent.js/state",
3
- "version": "1.0.0-rc.6",
3
+ "version": "1.0.1",
4
4
  "description": "Reactive state management for Coherent.js applications",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -47,7 +47,7 @@
47
47
  "node": ">=22.12.0"
48
48
  },
49
49
  "peerDependencies": {
50
- "@coherent.js/core": "1.0.0-rc.6"
50
+ "@coherent.js/core": "1.0.1"
51
51
  },
52
52
  "types": "./types/index.d.ts",
53
53
  "files": [
@@ -61,6 +61,7 @@
61
61
  "build": "node build.mjs",
62
62
  "clean": "rm -rf dist",
63
63
  "test": "vitest run",
64
- "test:watch": "vitest"
64
+ "test:watch": "vitest",
65
+ "typecheck": "tsc -p tsconfig.typecheck.json --noEmit"
65
66
  }
66
67
  }