@livestore/react 0.2.0 → 0.3.0-dev.11

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.
Files changed (87) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/LiveStoreContext.d.ts +5 -3
  3. package/dist/LiveStoreContext.d.ts.map +1 -1
  4. package/dist/LiveStoreContext.js +7 -3
  5. package/dist/LiveStoreContext.js.map +1 -1
  6. package/dist/LiveStoreProvider.d.ts +6 -4
  7. package/dist/LiveStoreProvider.d.ts.map +1 -1
  8. package/dist/LiveStoreProvider.js +47 -45
  9. package/dist/LiveStoreProvider.js.map +1 -1
  10. package/dist/LiveStoreProvider.test.js +8 -2
  11. package/dist/LiveStoreProvider.test.js.map +1 -1
  12. package/dist/__tests__/fixture.d.ts +7 -10
  13. package/dist/__tests__/fixture.d.ts.map +1 -1
  14. package/dist/__tests__/fixture.js +10 -15
  15. package/dist/__tests__/fixture.js.map +1 -1
  16. package/dist/experimental/components/LiveList.d.ts +2 -2
  17. package/dist/experimental/components/LiveList.d.ts.map +1 -1
  18. package/dist/experimental/components/LiveList.js +5 -4
  19. package/dist/experimental/components/LiveList.js.map +1 -1
  20. package/dist/mod.d.ts +0 -1
  21. package/dist/mod.d.ts.map +1 -1
  22. package/dist/mod.js +0 -1
  23. package/dist/mod.js.map +1 -1
  24. package/dist/useAtom.d.ts +4 -2
  25. package/dist/useAtom.d.ts.map +1 -1
  26. package/dist/useAtom.js +32 -28
  27. package/dist/useAtom.js.map +1 -1
  28. package/dist/useQuery.d.ts +26 -3
  29. package/dist/useQuery.d.ts.map +1 -1
  30. package/dist/useQuery.js +60 -45
  31. package/dist/useQuery.js.map +1 -1
  32. package/dist/useQuery.test.js +70 -16
  33. package/dist/useQuery.test.js.map +1 -1
  34. package/dist/useRcRef.d.ts +72 -0
  35. package/dist/useRcRef.d.ts.map +1 -0
  36. package/dist/useRcRef.js +146 -0
  37. package/dist/useRcRef.js.map +1 -0
  38. package/dist/useRcRef.test.d.ts +2 -0
  39. package/dist/useRcRef.test.d.ts.map +1 -0
  40. package/dist/useRcRef.test.js +128 -0
  41. package/dist/useRcRef.test.js.map +1 -0
  42. package/dist/useRcResource.d.ts +76 -0
  43. package/dist/useRcResource.d.ts.map +1 -0
  44. package/dist/useRcResource.js +150 -0
  45. package/dist/useRcResource.js.map +1 -0
  46. package/dist/useRcResource.test.d.ts +2 -0
  47. package/dist/useRcResource.test.d.ts.map +1 -0
  48. package/dist/useRcResource.test.js +122 -0
  49. package/dist/useRcResource.test.js.map +1 -0
  50. package/dist/useRow.d.ts +10 -7
  51. package/dist/useRow.d.ts.map +1 -1
  52. package/dist/useRow.js +16 -19
  53. package/dist/useRow.js.map +1 -1
  54. package/dist/useRow.test.js +74 -97
  55. package/dist/useRow.test.js.map +1 -1
  56. package/dist/useScopedQuery.d.ts +10 -4
  57. package/dist/useScopedQuery.d.ts.map +1 -1
  58. package/dist/useScopedQuery.js +97 -52
  59. package/dist/useScopedQuery.js.map +1 -1
  60. package/dist/useScopedQuery.test.js +13 -12
  61. package/dist/useScopedQuery.test.js.map +1 -1
  62. package/dist/utils/useStateRefWithReactiveInput.d.ts +1 -1
  63. package/dist/utils/useStateRefWithReactiveInput.d.ts.map +1 -1
  64. package/dist/utils/useStateRefWithReactiveInput.js.map +1 -1
  65. package/package.json +18 -17
  66. package/src/LiveStoreContext.ts +10 -6
  67. package/src/LiveStoreProvider.test.tsx +13 -2
  68. package/src/LiveStoreProvider.tsx +69 -53
  69. package/src/__snapshots__/useQuery.test.tsx.snap +2011 -0
  70. package/src/__snapshots__/useRow.test.tsx.snap +347 -151
  71. package/src/__tests__/fixture.tsx +11 -19
  72. package/src/experimental/components/LiveList.tsx +8 -7
  73. package/src/mod.ts +0 -1
  74. package/src/useAtom.ts +22 -11
  75. package/src/useQuery.test.tsx +165 -67
  76. package/src/useQuery.ts +84 -54
  77. package/src/useRcResource.test.tsx +167 -0
  78. package/src/useRcResource.ts +180 -0
  79. package/src/useRow.test.tsx +130 -164
  80. package/src/useRow.ts +32 -35
  81. package/src/utils/useStateRefWithReactiveInput.ts +1 -1
  82. package/dist/useTemporaryQuery.d.ts +0 -22
  83. package/dist/useTemporaryQuery.d.ts.map +0 -1
  84. package/dist/useTemporaryQuery.js +0 -75
  85. package/dist/useTemporaryQuery.js.map +0 -1
  86. package/src/useScopedQuery.test.tsx +0 -96
  87. package/src/useScopedQuery.ts +0 -142
@@ -0,0 +1,146 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Creates a reference-counted ref that is "stable" across React lifecycles.
4
+ *
5
+ * The hook is primarily intended for creating stateful objects or entities where
6
+ * reference identity is crucial (e.g. stateful objects, references, ...)
7
+ * needed to persist across multiple component instances. Instead of focusing on the cost of execution,
8
+ * its main goal is to ensure that the same instance is reused while there is at least one consumer,
9
+ * and properly disposed of only when no longer referenced.
10
+ *
11
+ * **Goals:**
12
+ * - Guarantee stable reference identity for stateful entities that are created via the `make` function.
13
+ * - Ensure that such state persists across component re-mounts, even in development environments like
14
+ * React Strict Mode or during Fast Refresh.
15
+ * - Automatically handle resource disposal by decrementing a reference count and disposing of the resource
16
+ * when no components are using it.
17
+ *
18
+ * **Behavior:**
19
+ * - On the first render with a specific key, the `make` function is invoked to create the stateful entity,
20
+ * and the resource is stored in a cache with a reference count set to 1.
21
+ * - If another component renders with the same key, the cached entity is reused and its reference count is incremented.
22
+ * - When a component renders with a new key, the previous key's reference count is decremented and, if it reaches zero,
23
+ * the `dispose` function is called for that resource.
24
+ * - Upon component unmount, the reference count is decremented, leading to disposal (via the `dispose` function)
25
+ * if the reference count drops to zero. An unmount is either detected via React's `useEffect` callback or
26
+ * in the useMemo hook when the key changes.
27
+ *
28
+ * Why this is needed in LiveStore:
29
+ * - Let's first take a look at the "trivial implementation":
30
+ * ```ts
31
+ * const useSimpleResource = <T>(make: () => T, dispose: (resource: T) => void) => {
32
+ * const val = React.useMemo(() => make(), [make])
33
+ *
34
+ * React.useEffect(() => {
35
+ * return () => {
36
+ * dispose(val)
37
+ * }
38
+ * }, [dispose, val])
39
+
40
+ * return val
41
+ * }
42
+ * ```
43
+ *
44
+ * **Usage:**
45
+ * ```tsx
46
+ * // Create a stateful object instance for a unique key and share it between components.
47
+ * const statefulObject = useRcResource(
48
+ * 'stable-object-key',
49
+ * () => createObjectInstance(),
50
+ * (object) => object.dispose()
51
+ * );
52
+ * ```
53
+ *
54
+ * **Caveats:**
55
+ * - The `make` function is intentionally omitted from the dependency array in `useMemo` to prevent
56
+ * unintended re-creations of the stateful entity. Avoid closing over changing values within `make`
57
+ * or include them in the `key`.
58
+ * - Ensure that the `dispose` function is stable or properly memoized as its reference is used in a `useEffect`.
59
+ * - Although the caching mechanism prevents duplicate instance creation for the same key, the strategy
60
+ * can interact in unexpected ways with React’s development patterns. Please report any issues if encountered.
61
+ *
62
+ * @template T The type of the stateful entity managed by the hook.
63
+ * @param key A unique identifier for the stateful entity. A change in this key triggers a disposal of the previous resource.
64
+ * @param make Function to create the stateful entity when it does not exist in the cache.
65
+ * @param dispose Function to dispose of the stateful entity when it’s no longer needed. Needs to be stable.
66
+ * @param _options Optional. Additional options such as a debug print callback for logging purposes.
67
+ * @returns The stateful entity corresponding to the provided key.
68
+ */
69
+ export const useRcResource = (key, make, dispose, _options) => {
70
+ const keyRef = React.useRef(undefined);
71
+ const didDisposeInMemo = React.useRef(false);
72
+ const resource = React.useMemo(() => {
73
+ // console.debug('useMemo', key)
74
+ if (didDisposeInMemo.current) {
75
+ // console.debug('useMemo', key, 'skip')
76
+ const cachedItem = cache.get(key);
77
+ if (cachedItem !== undefined && cachedItem._tag === 'active') {
78
+ return cachedItem.resource;
79
+ }
80
+ }
81
+ // Check if the key has changed (or is undefined)
82
+ if (keyRef.current !== undefined && keyRef.current !== key) {
83
+ // If the key has changed, decrement the reference on the previous key
84
+ const previousKey = keyRef.current;
85
+ const cachedItemForPreviousKey = cache.get(previousKey);
86
+ if (cachedItemForPreviousKey !== undefined && cachedItemForPreviousKey._tag === 'active') {
87
+ // previousKeyRef.current = previousKey
88
+ cachedItemForPreviousKey.rc--;
89
+ // console.debug('useMemo', key, 'rc--', previousKey, cachedItemForPreviousKey.rc)
90
+ if (cachedItemForPreviousKey.rc === 0) {
91
+ // Clean up the stateful resource if no longer referenced
92
+ dispose(cachedItemForPreviousKey.resource);
93
+ cache.set(previousKey, { _tag: 'destroyed' });
94
+ didDisposeInMemo.current = true;
95
+ }
96
+ }
97
+ }
98
+ const cachedItem = cache.get(key);
99
+ if (cachedItem !== undefined && cachedItem._tag === 'active') {
100
+ // In React Strict Mode, the `useMemo` hook is called multiple times,
101
+ // so we only increment the reference from the first call for this component.
102
+ cachedItem.rc++;
103
+ // console.debug('rc++', cachedItem.rc, ...(_options?.debugPrint?.(cachedItem.resource) ?? []))
104
+ return cachedItem.resource;
105
+ }
106
+ // Create a new stateful resource if not cached
107
+ const resource = make();
108
+ cache.set(key, { _tag: 'active', rc: 1, resource });
109
+ return resource;
110
+ // Dependency is deliberately limited to `key` to avoid unintended re-creations.
111
+ // eslint-disable-next-line react-hooks/exhaustive-deps
112
+ }, [key]);
113
+ React.useEffect(() => {
114
+ return () => {
115
+ if (didDisposeInMemo.current) {
116
+ // console.debug('unmount', keyRef.current, 'skip')
117
+ didDisposeInMemo.current = false;
118
+ return;
119
+ }
120
+ // console.debug('unmount', keyRef.current)
121
+ const cachedItem = cache.get(key);
122
+ // If the stateful resource is already cleaned up, do nothing.
123
+ if (cachedItem === undefined || cachedItem._tag === 'destroyed')
124
+ return;
125
+ cachedItem.rc--;
126
+ // console.debug('rc--', cachedItem.rc, ...(_options?.debugPrint?.(cachedItem.resource) ?? []))
127
+ if (cachedItem.rc === 0) {
128
+ dispose(cachedItem.resource);
129
+ cache.delete(key);
130
+ }
131
+ };
132
+ // We assume the `dispose` function is stable and won't change across renders
133
+ // eslint-disable-next-line react-hooks/exhaustive-deps
134
+ }, [key]);
135
+ keyRef.current = key;
136
+ return resource;
137
+ };
138
+ // NOTE Given `useMemo` will be called multiple times (e.g. when using React Strict mode or Fast Refresh),
139
+ // we are using this cache to avoid starting multiple queries/spans for the same component.
140
+ // This is somewhat against some recommended React best practices, but it should be fine in our case below.
141
+ // Please definitely open an issue if you see or run into any problems with this approach!
142
+ const cache = new Map();
143
+ export const __resetUseRcRefCache = () => {
144
+ cache.clear();
145
+ };
146
+ //# sourceMappingURL=useRcRef.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRcRef.js","sourceRoot":"","sources":["../src/useRcRef.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,GAAW,EACX,IAAa,EACb,OAAuC,EACvC,QAAwE,EACxE,EAAE;IACF,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAqB,SAAS,CAAC,CAAA;IAC1D,MAAM,gBAAgB,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAE5C,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;QAClC,gCAAgC;QAChC,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;YAC7B,wCAAwC;YACxC,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACjC,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC7D,OAAO,UAAU,CAAC,QAAQ,CAAA;YAC5B,CAAC;QACH,CAAC;QAED,iDAAiD;QACjD,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,MAAM,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;YAC3D,sEAAsE;YACtE,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAA;YAClC,MAAM,wBAAwB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AChC,OAAM;YACR,CAAC;YAED,2CAA2C;YAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACjC,8DAA8D;YAC9D,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,KAAK,WAAW;gBAAE,OAAM;YAEvE,UAAU,CAAC,EAAE,EAAE,CAAA;YAEf,+FAA+F;YAE/F,IAAI,UAAU,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC;gBACxB,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;gBAC5B,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACnB,CAAC;QACH,CAAC,CAAA;QACD,6EAA6E;QAC7E,uDAAuD;IACzD,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;IAET,MAAM,CAAC,OAAO,GAAG,GAAG,CAAA;IAEpB,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA;AAED,0GAA0G;AAC1G,2FAA2F;AAC3F,2GAA2G;AAC3G,0FAA0F;AAC1F,MAAM,KAAK,GAAG,IAAI,GAAG,EAUlB,CAAA;AAEH,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,EAAE;IACvC,KAAK,CAAC,KAAK,EAAE,CAAA;AACf,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=useRcRef.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRcRef.test.d.ts","sourceRoot":"","sources":["../src/useRcRef.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,128 @@
1
+ import * as ReactTesting from '@testing-library/react';
2
+ import * as React from 'react';
3
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
4
+ import { __resetUseRcRefCache, useRcRef } from './useRcResource.js';
5
+ describe.each([{ strictMode: true }, { strictMode: false }])('useRcRef (strictMode=%s)', ({ strictMode }) => {
6
+ beforeEach(() => {
7
+ __resetUseRcRefCache();
8
+ });
9
+ const wrapper = strictMode ? React.StrictMode : React.Fragment;
10
+ it('should create a stateful entity using make and call cleanup on unmount', () => {
11
+ const makeSpy = vi.fn(() => Symbol('statefulResource'));
12
+ const cleanupSpy = vi.fn();
13
+ const { result, unmount } = ReactTesting.renderHook(() => useRcRef('key-1', makeSpy, cleanupSpy), { wrapper });
14
+ expect(makeSpy).toHaveBeenCalledTimes(1);
15
+ expect(result.current).toBeDefined();
16
+ expect(cleanupSpy).toHaveBeenCalledTimes(0);
17
+ unmount();
18
+ expect(cleanupSpy).toHaveBeenCalledTimes(1);
19
+ });
20
+ it('should reuse the same entity when the key remains unchanged', () => {
21
+ const makeSpy = vi.fn(() => Symbol('statefulResource'));
22
+ const cleanupSpy = vi.fn();
23
+ const { result, rerender, unmount } = ReactTesting.renderHook(({ key }) => useRcRef(key, makeSpy, cleanupSpy), {
24
+ initialProps: { key: 'consistent-key' },
25
+ wrapper,
26
+ });
27
+ const instance1 = result.current;
28
+ // Re-render with the same key
29
+ rerender({ key: 'consistent-key' });
30
+ const instance2 = result.current;
31
+ expect(instance1).toBe(instance2);
32
+ expect(makeSpy).toHaveBeenCalledTimes(1);
33
+ unmount();
34
+ expect(cleanupSpy).toHaveBeenCalledTimes(1);
35
+ });
36
+ it('should dispose the previous instance when the key changes', () => {
37
+ const makeSpy = vi.fn(() => Symbol('statefulResource'));
38
+ const cleanupSpy = vi.fn();
39
+ const { result, rerender, unmount } = ReactTesting.renderHook(({ key }) => useRcRef(key, makeSpy, cleanupSpy), {
40
+ initialProps: { key: 'a' },
41
+ wrapper,
42
+ });
43
+ const instanceA = result.current;
44
+ // Change the key; this should trigger the disposal of the 'a' instance
45
+ rerender({ key: 'b' });
46
+ const instanceB = result.current;
47
+ expect(instanceA).not.toBe(instanceB);
48
+ expect(makeSpy).toHaveBeenCalledTimes(2);
49
+ expect(cleanupSpy).toHaveBeenCalledTimes(1);
50
+ unmount();
51
+ expect(cleanupSpy).toHaveBeenCalledTimes(2);
52
+ });
53
+ it('should not dispose the entity until all consumers unmount', () => {
54
+ const makeSpy = vi.fn(() => Symbol('statefulResource'));
55
+ const cleanupSpy = vi.fn();
56
+ // Simulate two consumers using the same key independently.
57
+ const { unmount: unmount1 } = ReactTesting.renderHook(() => useRcRef('shared-key', makeSpy, cleanupSpy), {
58
+ wrapper,
59
+ });
60
+ const { unmount: unmount2, result } = ReactTesting.renderHook(() => useRcRef('shared-key', makeSpy, cleanupSpy), {
61
+ wrapper,
62
+ });
63
+ expect(result.current).toBeDefined();
64
+ expect(makeSpy).toHaveBeenCalledTimes(1);
65
+ // Unmount first consumer; the entity should remain active.
66
+ unmount1();
67
+ expect(cleanupSpy).not.toHaveBeenCalled();
68
+ // Unmount second consumer; now the entity is disposed.
69
+ unmount2();
70
+ expect(cleanupSpy).toHaveBeenCalledTimes(1);
71
+ });
72
+ it('should handle rapid key changes correctly', () => {
73
+ const makeSpy = vi.fn(() => Symbol('statefulResource'));
74
+ const cleanupSpy = vi.fn();
75
+ const { rerender, unmount } = ReactTesting.renderHook(({ key }) => useRcRef(key, makeSpy, cleanupSpy), {
76
+ initialProps: { key: '1' },
77
+ wrapper,
78
+ });
79
+ // Rapid sequence of key changes.
80
+ rerender({ key: '2' });
81
+ rerender({ key: '3' });
82
+ // Expect three creations: one each for keys '1', '2', '3'
83
+ expect(makeSpy).toHaveBeenCalledTimes(3);
84
+ // Cleanup should have been triggered for key '1' and key '2'
85
+ expect(cleanupSpy).toHaveBeenCalledTimes(2);
86
+ unmount();
87
+ // Unmounting the final consumer disposes the key '3' instance.
88
+ expect(cleanupSpy).toHaveBeenCalledTimes(3);
89
+ });
90
+ });
91
+ // This code was useful to better understand the hook behaviour with and without strict mode
92
+ // describe('debug', () => {
93
+ // const useStrictTest = (key: string) => {
94
+ // const id = React.useId()
95
+ // console.log(key, 'id', id)
96
+ // const x = React.useMemo(() => {
97
+ // console.log('useMemo', key)
98
+ // return 'hi' + key
99
+ // }, [key])
100
+ // React.useEffect(() => {
101
+ // console.log('useEffect', key)
102
+ // return () => {
103
+ // console.log('unmount', key)
104
+ // }
105
+ // }, [])
106
+ // return x
107
+ // }
108
+ // it('strict mode component', () => {
109
+ // console.log('strict mode component')
110
+ // const Root = () => {
111
+ // useStrictTest('a')
112
+ // return null
113
+ // }
114
+ // const { unmount } = ReactTesting.render(
115
+ // <React.StrictMode>
116
+ // <Root />
117
+ // </React.StrictMode>,
118
+ // )
119
+ // unmount()
120
+ // })
121
+ // it('strict mode hook', () => {
122
+ // console.log('strict mode hook')
123
+ // const wrapper: React.FC<{ children: React.ReactNode }> = React.StrictMode
124
+ // const { unmount } = ReactTesting.renderHook(() => useStrictTest('b'), { wrapper })
125
+ // unmount()
126
+ // })
127
+ // })
128
+ //# sourceMappingURL=useRcRef.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRcRef.test.js","sourceRoot":"","sources":["../src/useRcRef.test.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,wBAAwB,CAAA;AACtD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7D,OAAO,EAAE,oBAAoB,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAEnE,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,0BAA0B,EAAE,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1G,UAAU,CAAC,GAAG,EAAE;QACd,oBAAoB,EAAE,CAAA;IACxB,CAAC,CAAC,CAAA;IAEF,MAAM,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAA;IAE9D,EAAE,CAAC,wEAAwE,EAAE,GAAG,EAAE;Q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AAK"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Creates a reference-counted resource object that is "stable" across React lifecycles.
3
+ *
4
+ * The hook is primarily intended for creating stateful objects or entities where:
5
+ * 1) reference identity is crucial (e.g. stateful objects, references, ...)
6
+ * and needed to persist across multiple component instances
7
+ * 2) the `create`/`dispose` functions might be effectful and can be called exactly once per key
8
+ *
9
+ * **Goals:**
10
+ * - Guarantee stable reference identity for stateful entities that are created via the `create` function.
11
+ * - Strong guarantees about the `create`/`dispose` calls (i.e. exactly one `create`/`dispose` call per key)
12
+ * - Ensure that such state is retained across component re-mounts, even in development environments like
13
+ * React Strict Mode or during Fast Refresh.
14
+ * - Automatically handle resource disposal by decrementing a reference count and disposing of the resource
15
+ * when no components are using it.
16
+ *
17
+ * **Behavior:**
18
+ * - On the first render with a specific key, the `create` function is invoked to create the stateful entity,
19
+ * and the resource is stored in a cache with a reference count set to 1.
20
+ * - If another component renders with the same key, the cached entity is reused and its reference count is incremented.
21
+ * - When a component renders with a new key, the previous key's reference count is decremented and, if it reaches zero,
22
+ * the `dispose` function is called for that resource.
23
+ * - Upon component unmount, the reference count is decremented, leading to disposal (via the `dispose` function)
24
+ * if the reference count drops to zero. An unmount is either detected via React's `useEffect` callback or
25
+ * in the useMemo hook when the key changes.
26
+ *
27
+ * Why this is needed in LiveStore:
28
+ * Let's first take a look at the "trivial implementation":
29
+ *
30
+ * ```ts
31
+ * const useSimpleResource = <T>(create: () => T, dispose: (resource: T) => void) => {
32
+ * const val = React.useMemo(() => create(), [create])
33
+ *
34
+ * React.useEffect(() => {
35
+ * return () => {
36
+ * dispose(val)
37
+ * }
38
+ * }, [dispose, val])
39
+
40
+ * return val
41
+ * }
42
+ * ```
43
+ *
44
+ * LiveStore uses this hook to create LiveQuery instances which are stateful and must not be leaked.
45
+ * The simple implementation above would leak the LiveQuery instance if the component is unmounted or props change.
46
+ *
47
+ * **Usage:**
48
+ * ```tsx
49
+ * // Create a stateful object instance for a unique key and share it between components.
50
+ * const statefulObject = useRcResource(
51
+ * 'stable-object-key',
52
+ * () => createObjectInstance(),
53
+ * (object) => object.dispose()
54
+ * );
55
+ * ```
56
+ *
57
+ * **Caveats:**
58
+ * - The `create` function is intentionally omitted from the dependency array in `useMemo` to prevent
59
+ * unintended re-creations of the stateful entity. Avoid closing over changing values within `create`
60
+ * or include them in the `key`.
61
+ * - Ensure that the `dispose` function is stable or properly memoized as its reference is used in a `useEffect`.
62
+ * - Although the caching mechanism prevents duplicate instance creation for the same key, the strategy
63
+ * can interact in unexpected ways with React’s development patterns. Please report any issues if encountered.
64
+ *
65
+ * @template T The type of the stateful entity managed by the hook.
66
+ * @param key A unique identifier for the stateful entity. A change in this key triggers a disposal of the previous resource.
67
+ * @param create Function to create the stateful entity when it does not exist in the cache.
68
+ * @param dispose Function to dispose of the stateful entity when it’s no longer needed. Needs to be stable.
69
+ * @param _options Optional. Additional options such as a debug print callback for logging purposes.
70
+ * @returns The stateful entity corresponding to the provided key.
71
+ */
72
+ export declare const useRcResource: <T>(key: string, create: () => T, dispose: (resource: NoInfer<T>) => void, _options?: {
73
+ debugPrint?: (resource: NoInfer<T>) => ReadonlyArray<any>;
74
+ }) => any;
75
+ export declare const __resetUseRcResourceCache: () => void;
76
+ //# sourceMappingURL=useRcResource.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRcResource.d.ts","sourceRoot":"","sources":["../src/useRcResource.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,OACxB,MAAM,UACH,MAAM,CAAC,WACN,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,aAC5B;IAAE,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,aAAa,CAAC,GAAG,CAAC,CAAA;CAAE,QAkFzE,CAAA;AAkBD,eAAO,MAAM,yBAAyB,YAErC,CAAA"}
@@ -0,0 +1,150 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Creates a reference-counted resource object that is "stable" across React lifecycles.
4
+ *
5
+ * The hook is primarily intended for creating stateful objects or entities where:
6
+ * 1) reference identity is crucial (e.g. stateful objects, references, ...)
7
+ * and needed to persist across multiple component instances
8
+ * 2) the `create`/`dispose` functions might be effectful and can be called exactly once per key
9
+ *
10
+ * **Goals:**
11
+ * - Guarantee stable reference identity for stateful entities that are created via the `create` function.
12
+ * - Strong guarantees about the `create`/`dispose` calls (i.e. exactly one `create`/`dispose` call per key)
13
+ * - Ensure that such state is retained across component re-mounts, even in development environments like
14
+ * React Strict Mode or during Fast Refresh.
15
+ * - Automatically handle resource disposal by decrementing a reference count and disposing of the resource
16
+ * when no components are using it.
17
+ *
18
+ * **Behavior:**
19
+ * - On the first render with a specific key, the `create` function is invoked to create the stateful entity,
20
+ * and the resource is stored in a cache with a reference count set to 1.
21
+ * - If another component renders with the same key, the cached entity is reused and its reference count is incremented.
22
+ * - When a component renders with a new key, the previous key's reference count is decremented and, if it reaches zero,
23
+ * the `dispose` function is called for that resource.
24
+ * - Upon component unmount, the reference count is decremented, leading to disposal (via the `dispose` function)
25
+ * if the reference count drops to zero. An unmount is either detected via React's `useEffect` callback or
26
+ * in the useMemo hook when the key changes.
27
+ *
28
+ * Why this is needed in LiveStore:
29
+ * Let's first take a look at the "trivial implementation":
30
+ *
31
+ * ```ts
32
+ * const useSimpleResource = <T>(create: () => T, dispose: (resource: T) => void) => {
33
+ * const val = React.useMemo(() => create(), [create])
34
+ *
35
+ * React.useEffect(() => {
36
+ * return () => {
37
+ * dispose(val)
38
+ * }
39
+ * }, [dispose, val])
40
+
41
+ * return val
42
+ * }
43
+ * ```
44
+ *
45
+ * LiveStore uses this hook to create LiveQuery instances which are stateful and must not be leaked.
46
+ * The simple implementation above would leak the LiveQuery instance if the component is unmounted or props change.
47
+ *
48
+ * **Usage:**
49
+ * ```tsx
50
+ * // Create a stateful object instance for a unique key and share it between components.
51
+ * const statefulObject = useRcResource(
52
+ * 'stable-object-key',
53
+ * () => createObjectInstance(),
54
+ * (object) => object.dispose()
55
+ * );
56
+ * ```
57
+ *
58
+ * **Caveats:**
59
+ * - The `create` function is intentionally omitted from the dependency array in `useMemo` to prevent
60
+ * unintended re-creations of the stateful entity. Avoid closing over changing values within `create`
61
+ * or include them in the `key`.
62
+ * - Ensure that the `dispose` function is stable or properly memoized as its reference is used in a `useEffect`.
63
+ * - Although the caching mechanism prevents duplicate instance creation for the same key, the strategy
64
+ * can interact in unexpected ways with React’s development patterns. Please report any issues if encountered.
65
+ *
66
+ * @template T The type of the stateful entity managed by the hook.
67
+ * @param key A unique identifier for the stateful entity. A change in this key triggers a disposal of the previous resource.
68
+ * @param create Function to create the stateful entity when it does not exist in the cache.
69
+ * @param dispose Function to dispose of the stateful entity when it’s no longer needed. Needs to be stable.
70
+ * @param _options Optional. Additional options such as a debug print callback for logging purposes.
71
+ * @returns The stateful entity corresponding to the provided key.
72
+ */
73
+ export const useRcResource = (key, create, dispose, _options) => {
74
+ const keyRef = React.useRef(undefined);
75
+ const didDisposeInMemo = React.useRef(false);
76
+ const resource = React.useMemo(() => {
77
+ // console.debug('useMemo', key)
78
+ if (didDisposeInMemo.current) {
79
+ // console.debug('useMemo', key, 'skip')
80
+ const cachedItem = cache.get(key);
81
+ if (cachedItem !== undefined && cachedItem._tag === 'active') {
82
+ return cachedItem.resource;
83
+ }
84
+ }
85
+ // Check if the key has changed (or is undefined)
86
+ if (keyRef.current !== undefined && keyRef.current !== key) {
87
+ // If the key has changed, decrement the reference on the previous key
88
+ const previousKey = keyRef.current;
89
+ const cachedItemForPreviousKey = cache.get(previousKey);
90
+ if (cachedItemForPreviousKey !== undefined && cachedItemForPreviousKey._tag === 'active') {
91
+ // previousKeyRef.current = previousKey
92
+ cachedItemForPreviousKey.rc--;
93
+ // console.debug('useMemo', key, 'rc--', previousKey, cachedItemForPreviousKey.rc)
94
+ if (cachedItemForPreviousKey.rc === 0) {
95
+ // Clean up the stateful resource if no longer referenced
96
+ dispose(cachedItemForPreviousKey.resource);
97
+ cache.set(previousKey, { _tag: 'destroyed' });
98
+ didDisposeInMemo.current = true;
99
+ }
100
+ }
101
+ }
102
+ const cachedItem = cache.get(key);
103
+ if (cachedItem !== undefined && cachedItem._tag === 'active') {
104
+ // In React Strict Mode, the `useMemo` hook is called multiple times,
105
+ // so we only increment the reference from the first call for this component.
106
+ cachedItem.rc++;
107
+ // console.debug('rc++', cachedItem.rc, ...(_options?.debugPrint?.(cachedItem.resource) ?? []))
108
+ return cachedItem.resource;
109
+ }
110
+ // Create a new stateful resource if not cached
111
+ const resource = create();
112
+ cache.set(key, { _tag: 'active', rc: 1, resource });
113
+ return resource;
114
+ // Dependency is deliberately limited to `key` to avoid unintended re-creations.
115
+ // eslint-disable-next-line react-hooks/exhaustive-deps
116
+ }, [key]);
117
+ React.useEffect(() => {
118
+ return () => {
119
+ if (didDisposeInMemo.current) {
120
+ // console.debug('unmount', keyRef.current, 'skip')
121
+ didDisposeInMemo.current = false;
122
+ return;
123
+ }
124
+ // console.debug('unmount', keyRef.current)
125
+ const cachedItem = cache.get(key);
126
+ // If the stateful resource is already cleaned up, do nothing.
127
+ if (cachedItem === undefined || cachedItem._tag === 'destroyed')
128
+ return;
129
+ cachedItem.rc--;
130
+ // console.debug('rc--', cachedItem.rc, ...(_options?.debugPrint?.(cachedItem.resource) ?? []))
131
+ if (cachedItem.rc === 0) {
132
+ dispose(cachedItem.resource);
133
+ cache.delete(key);
134
+ }
135
+ };
136
+ // We assume the `dispose` function is stable and won't change across renders
137
+ // eslint-disable-next-line react-hooks/exhaustive-deps
138
+ }, [key]);
139
+ keyRef.current = key;
140
+ return resource;
141
+ };
142
+ // NOTE Given `useMemo` will be called multiple times (e.g. when using React Strict mode or Fast Refresh),
143
+ // we are using this cache to avoid starting multiple queries/spans for the same component.
144
+ // This is somewhat against some recommended React best practices, but it should be fine in our case below.
145
+ // Please definitely open an issue if you see or run into any problems with this approach!
146
+ const cache = new Map();
147
+ export const __resetUseRcResourceCache = () => {
148
+ cache.clear();
149
+ };
150
+ //# sourceMappingURL=useRcResource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRcResource.js","sourceRoot":"","sources":["../src/useRcResource.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,GAAW,EACX,MAAe,EACf,OAAuC,EACvC,QAAwE,EACxE,EAAE;IACF,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAqB,SAAS,CAAC,CAAA;IAC1D,MAAM,gBAAgB,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAE5C,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;QAClC,gCAAgC;QAChC,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;YAC7B,wCAAwC;YACxC,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACjC,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC7D,OAAO,UAAU,CAAC,QAAQ,CAAA;YAC5B,CAAC;QACH,CAAC;QAED,iDAAiD;QACjD,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,MAAM,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;YAC3D,sEAAsE;YACtE,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAA;YAClC,MAAM,wBAAwB,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;YACvD,IAAI,wBAAwB,KAAK,SAAS,IAAI,wBAAw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C,CAAA;YACjC,8DAA8D;YAC9D,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,KAAK,WAAW;gBAAE,OAAM;YAEvE,UAAU,CAAC,EAAE,EAAE,CAAA;YAEf,+FAA+F;YAE/F,IAAI,UAAU,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC;gBACxB,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;gBAC5B,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACnB,CAAC;QACH,CAAC,CAAA;QACD,6EAA6E;QAC7E,uDAAuD;IACzD,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;IAET,MAAM,CAAC,OAAO,GAAG,GAAG,CAAA;IAEpB,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA;AAED,0GAA0G;AAC1G,2FAA2F;AAC3F,2GAA2G;AAC3G,0FAA0F;AAC1F,MAAM,KAAK,GAAG,IAAI,GAAG,EAUlB,CAAA;AAEH,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAG,EAAE;IAC5C,KAAK,CAAC,KAAK,EAAE,CAAA;AACf,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=useRcResource.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRcResource.test.d.ts","sourceRoot":"","sources":["../src/useRcResource.test.tsx"],"names":[],"mappings":""}