@yiin/reactive-proxy-state 1.0.27 → 1.0.28

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/README.md CHANGED
@@ -80,6 +80,14 @@ onServerEvent((event) => {
80
80
  serverState.count = 5; // Automatically synced to all clients
81
81
  ```
82
82
 
83
+ If you need to relay state or events through `postMessage`, workers, or any structured-clone boundary, convert them first:
84
+
85
+ ```typescript
86
+ import { deepToRaw } from "@yiin/reactive-proxy-state";
87
+
88
+ worker.postMessage(deepToRaw(serverState));
89
+ ```
90
+
83
91
  See the [`updateState` documentation](./docs/api/update-state.md) and [`reactive` documentation](./docs/api/reactive.md) for more details on event emission and application.
84
92
 
85
93
  ## API
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Converts reactive proxies/refs into plain JavaScript data structures
3
+ * so they can be safely sent across structured-clone boundaries.
4
+ */
5
+ export declare function deepToRaw<T>(input: T, seen?: WeakMap<any, any>): T;
package/dist/index.cjs CHANGED
@@ -49,6 +49,7 @@ __export(exports_src, {
49
49
  isRef: () => isRef,
50
50
  isReactive: () => isReactive,
51
51
  isComputed: () => isComputed,
52
+ deepToRaw: () => deepToRaw,
52
53
  deepEqual: () => deepEqual,
53
54
  deepClone: () => deepClone,
54
55
  createRendererBridgeEmitter: () => createRendererBridgeEmitter,
@@ -1848,3 +1849,56 @@ function createMainBridgeEmitter(opts) {
1848
1849
  forward: (msg, ctx) => broadcast(msg, ctx?.senderId)
1849
1850
  });
1850
1851
  }
1852
+ // src/deep-to-raw.ts
1853
+ function deepToRaw(input, seen = new WeakMap) {
1854
+ if (input === null || typeof input !== "object") {
1855
+ return input;
1856
+ }
1857
+ if (isRef(input)) {
1858
+ return deepToRaw(input.value, seen);
1859
+ }
1860
+ if (seen.has(input)) {
1861
+ return seen.get(input);
1862
+ }
1863
+ if (Array.isArray(input)) {
1864
+ const result = [];
1865
+ seen.set(input, result);
1866
+ for (const item of input) {
1867
+ result.push(deepToRaw(item, seen));
1868
+ }
1869
+ return result;
1870
+ }
1871
+ if (input instanceof Date) {
1872
+ return new Date(input.getTime());
1873
+ }
1874
+ if (input instanceof Map) {
1875
+ const result = new Map;
1876
+ seen.set(input, result);
1877
+ for (const [key, value] of input) {
1878
+ result.set(deepToRaw(key, seen), deepToRaw(value, seen));
1879
+ }
1880
+ return result;
1881
+ }
1882
+ if (input instanceof Set) {
1883
+ const result = new Set;
1884
+ seen.set(input, result);
1885
+ for (const value of input) {
1886
+ result.add(deepToRaw(value, seen));
1887
+ }
1888
+ return result;
1889
+ }
1890
+ const source = isReactive(input) ? toRaw(input) : input;
1891
+ if (seen.has(source)) {
1892
+ return seen.get(source);
1893
+ }
1894
+ if (source && typeof source === "object" && (source.constructor === Object || source.constructor === null)) {
1895
+ const result = Object.create(Object.getPrototypeOf(source));
1896
+ seen.set(input, result);
1897
+ seen.set(source, result);
1898
+ for (const key of Reflect.ownKeys(source)) {
1899
+ result[key] = deepToRaw(source[key], seen);
1900
+ }
1901
+ return result;
1902
+ }
1903
+ return source;
1904
+ }
package/dist/index.d.ts CHANGED
@@ -9,3 +9,4 @@ export * from './computed';
9
9
  export * from './mark-raw';
10
10
  export * from './integrations/vue3';
11
11
  export * from './integrations/electron-bridge';
12
+ export { deepToRaw } from './deep-to-raw';
package/dist/index.js CHANGED
@@ -1786,6 +1786,59 @@ function createMainBridgeEmitter(opts) {
1786
1786
  forward: (msg, ctx) => broadcast(msg, ctx?.senderId)
1787
1787
  });
1788
1788
  }
1789
+ // src/deep-to-raw.ts
1790
+ function deepToRaw(input, seen = new WeakMap) {
1791
+ if (input === null || typeof input !== "object") {
1792
+ return input;
1793
+ }
1794
+ if (isRef(input)) {
1795
+ return deepToRaw(input.value, seen);
1796
+ }
1797
+ if (seen.has(input)) {
1798
+ return seen.get(input);
1799
+ }
1800
+ if (Array.isArray(input)) {
1801
+ const result = [];
1802
+ seen.set(input, result);
1803
+ for (const item of input) {
1804
+ result.push(deepToRaw(item, seen));
1805
+ }
1806
+ return result;
1807
+ }
1808
+ if (input instanceof Date) {
1809
+ return new Date(input.getTime());
1810
+ }
1811
+ if (input instanceof Map) {
1812
+ const result = new Map;
1813
+ seen.set(input, result);
1814
+ for (const [key, value] of input) {
1815
+ result.set(deepToRaw(key, seen), deepToRaw(value, seen));
1816
+ }
1817
+ return result;
1818
+ }
1819
+ if (input instanceof Set) {
1820
+ const result = new Set;
1821
+ seen.set(input, result);
1822
+ for (const value of input) {
1823
+ result.add(deepToRaw(value, seen));
1824
+ }
1825
+ return result;
1826
+ }
1827
+ const source = isReactive(input) ? toRaw(input) : input;
1828
+ if (seen.has(source)) {
1829
+ return seen.get(source);
1830
+ }
1831
+ if (source && typeof source === "object" && (source.constructor === Object || source.constructor === null)) {
1832
+ const result = Object.create(Object.getPrototypeOf(source));
1833
+ seen.set(input, result);
1834
+ seen.set(source, result);
1835
+ for (const key of Reflect.ownKeys(source)) {
1836
+ result[key] = deepToRaw(source[key], seen);
1837
+ }
1838
+ return result;
1839
+ }
1840
+ return source;
1841
+ }
1789
1842
  export {
1790
1843
  watchEffect,
1791
1844
  watch,
@@ -1807,6 +1860,7 @@ export {
1807
1860
  isRef,
1808
1861
  isReactive,
1809
1862
  isComputed,
1863
+ deepToRaw,
1810
1864
  deepEqual,
1811
1865
  deepClone,
1812
1866
  createRendererBridgeEmitter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yiin/reactive-proxy-state",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "author": "Yiin <stanislovas@yiin.lt>",
5
5
  "repository": {
6
6
  "type": "git",