@take-out/helpers 0.0.35 → 0.0.37

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.
@@ -0,0 +1,73 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { createStorage } from "./createStorage";
3
+ import { getStorageDriver, setStorageDriver } from "./driver";
4
+ describe("storage driver system", () => {
5
+ describe("driver initialization", () => {
6
+ it("getStorageDriver should return localStorage fallback on web", () => {
7
+ const driver = getStorageDriver();
8
+ typeof localStorage < "u" && expect(driver).toBeTruthy();
9
+ }), it("should use custom driver when set", () => {
10
+ const mockStorage = /* @__PURE__ */ new Map(), mockDriver = {
11
+ getItem: (key) => mockStorage.get(key) ?? null,
12
+ setItem: (key, value) => mockStorage.set(key, value),
13
+ removeItem: (key) => mockStorage.delete(key),
14
+ getAllKeys: () => Array.from(mockStorage.keys())
15
+ };
16
+ setStorageDriver(mockDriver);
17
+ const driver = getStorageDriver();
18
+ expect(driver).toBe(mockDriver);
19
+ });
20
+ }), describe("createStorage with driver", () => {
21
+ let mockStorage;
22
+ beforeEach(() => {
23
+ mockStorage = /* @__PURE__ */ new Map(), setStorageDriver({
24
+ getItem: (key) => mockStorage.get(key) ?? null,
25
+ setItem: (key, value) => mockStorage.set(key, value),
26
+ removeItem: (key) => mockStorage.delete(key),
27
+ getAllKeys: () => Array.from(mockStorage.keys())
28
+ });
29
+ }), it("should store and retrieve values", () => {
30
+ const storage = createStorage(`test-${Date.now()}-1`);
31
+ storage.set("token", "test-value"), expect(storage.get("token")).toBe("test-value");
32
+ }), it("should use namespace prefix", () => {
33
+ const namespace = `test-${Date.now()}-2`;
34
+ createStorage(namespace).set("key", "value"), expect(mockStorage.has(`${namespace}:key`)).toBe(!0);
35
+ }), it("should handle JSON serialization", () => {
36
+ const storage = createStorage(
37
+ `test-${Date.now()}-3`
38
+ ), obj = { name: "test", count: 42 };
39
+ storage.set("obj", obj), expect(storage.get("obj")).toEqual(obj);
40
+ }), it("should support raw string operations", () => {
41
+ const storage = createStorage(`test-${Date.now()}-4`);
42
+ storage.setItem("raw", "raw-value"), expect(storage.getItem("raw")).toBe("raw-value");
43
+ }), it("should return undefined for missing keys", () => {
44
+ const storage = createStorage(`test-${Date.now()}-5`);
45
+ expect(storage.get("missing")).toBeUndefined();
46
+ }), it("should support has() check", () => {
47
+ const storage = createStorage(`test-${Date.now()}-6`);
48
+ expect(storage.has("exists")).toBe(!1), storage.set("exists", "value"), expect(storage.has("exists")).toBe(!0);
49
+ }), it("should support remove()", () => {
50
+ const storage = createStorage(`test-${Date.now()}-7`);
51
+ storage.set("removable", "value"), expect(storage.has("removable")).toBe(!0), storage.remove("removable"), expect(storage.has("removable")).toBe(!1);
52
+ }), it("should list keys in namespace", () => {
53
+ const storage = createStorage(`test-${Date.now()}-8`);
54
+ storage.set("a", "1"), storage.set("b", "2"), storage.set("c", "3"), expect(storage.keys().sort()).toEqual(["a", "b", "c"]);
55
+ }), it("should clear only namespace keys", () => {
56
+ const ns1 = `test-${Date.now()}-9a`, ns2 = `test-${Date.now()}-9b`, storage1 = createStorage(ns1), storage2 = createStorage(ns2);
57
+ storage1.set("key", "value1"), storage2.set("key", "value2"), storage1.clear(), expect(storage1.has("key")).toBe(!1), expect(storage2.has("key")).toBe(!0);
58
+ });
59
+ }), describe("createStorage without driver (simulates native without setup)", () => {
60
+ it("should handle gracefully when operations fail", () => {
61
+ const storage = createStorage(`test-no-driver-${Date.now()}`);
62
+ expect(() => storage.get("key")).not.toThrow(), expect(() => storage.set("key", "value")).not.toThrow(), expect(() => storage.remove("key")).not.toThrow(), expect(() => storage.has("key")).not.toThrow(), expect(() => storage.keys()).not.toThrow(), expect(() => storage.clear()).not.toThrow(), expect(() => storage.getItem("key")).not.toThrow(), expect(() => storage.setItem("key", "value")).not.toThrow();
63
+ });
64
+ });
65
+ });
66
+ describe("auth storage requirements", () => {
67
+ it("storage must be initialized before auth client creates storage instances", () => {
68
+ expect(!0).toBe(!0);
69
+ }), it("native platforms require explicit storage driver setup", () => {
70
+ expect(!0).toBe(!0);
71
+ });
72
+ });
73
+ //# sourceMappingURL=storage.test.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/storage/storage.test.ts"],
4
+ "mappings": "AAAA,SAAS,YAAY,UAAU,QAAQ,UAAU;AAEjD,SAAS,qBAAqB;AAC9B,SAAS,kBAAkB,wBAAwB;AAMnD,SAAS,yBAAyB,MAAM;AACtC,WAAS,yBAAyB,MAAM;AACtC,OAAG,+DAA+D,MAAM;AAEtE,YAAM,SAAS,iBAAiB;AAGhC,MAAI,OAAO,eAAiB,OAC1B,OAAO,MAAM,EAAE,WAAW;AAAA,IAE9B,CAAC,GAED,GAAG,qCAAqC,MAAM;AAC5C,YAAM,cAAc,oBAAI,IAAoB,GACtC,aAAa;AAAA,QACjB,SAAS,CAAC,QAAgB,YAAY,IAAI,GAAG,KAAK;AAAA,QAClD,SAAS,CAAC,KAAa,UAAkB,YAAY,IAAI,KAAK,KAAK;AAAA,QACnE,YAAY,CAAC,QAAgB,YAAY,OAAO,GAAG;AAAA,QACnD,YAAY,MAAM,MAAM,KAAK,YAAY,KAAK,CAAC;AAAA,MACjD;AAEA,uBAAiB,UAAU;AAC3B,YAAM,SAAS,iBAAiB;AAChC,aAAO,MAAM,EAAE,KAAK,UAAU;AAAA,IAChC,CAAC;AAAA,EACH,CAAC,GAED,SAAS,6BAA6B,MAAM;AAC1C,QAAI;AAEJ,eAAW,MAAM;AACf,oBAAc,oBAAI,IAAoB,GAOtC,iBANmB;AAAA,QACjB,SAAS,CAAC,QAAgB,YAAY,IAAI,GAAG,KAAK;AAAA,QAClD,SAAS,CAAC,KAAa,UAAkB,YAAY,IAAI,KAAK,KAAK;AAAA,QACnE,YAAY,CAAC,QAAgB,YAAY,OAAO,GAAG;AAAA,QACnD,YAAY,MAAM,MAAM,KAAK,YAAY,KAAK,CAAC;AAAA,MACjD,CAC2B;AAAA,IAC7B,CAAC,GA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GAChD,OAAO,MAAM,QAAQ,IAAI,KAAK,CAAC,EAAE,IAAI,QAAQ,GAC7C,OAAO,MAAM,QAAQ,KAAK,CAAC,EAAE,IAAI,QAAQ,GACzC,OAAO,MAAM,QAAQ,MAAM,CAAC,EAAE,IAAI,QAAQ,GAC1C,OAAO,MAAM,QAAQ,QAAQ,KAAK,CAAC,EAAE,IAAI,QAAQ,GACjD,OAAO,MAAM,QAAQ,QAAQ,OAAO,OAAO,CAAC,EAAE,IAAI,QAAQ;AAAA,IAC5D,CAAC;AAAA,EACH,CAAC;AACH,CAAC;AAED,SAAS,6BAA6B,MAAM;AAG1C,KAAG,4EAA4E,MAAM;AAQnF,WAAO,EAAI,EAAE,KAAK,EAAI;AAAA,EACxB,CAAC,GAED,GAAG,0DAA0D,MAAM;AAQjE,WAAO,EAAI,EAAE,KAAK,EAAI;AAAA,EACxB,CAAC;AACH,CAAC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,79 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { createStorage } from "./createStorage.mjs";
3
+ import { getStorageDriver, setStorageDriver } from "./driver.mjs";
4
+ describe("storage driver system", () => {
5
+ describe("driver initialization", () => {
6
+ it("getStorageDriver should return localStorage fallback on web", () => {
7
+ const driver = getStorageDriver();
8
+ typeof localStorage < "u" && expect(driver).toBeTruthy();
9
+ }), it("should use custom driver when set", () => {
10
+ const mockStorage = /* @__PURE__ */new Map(),
11
+ mockDriver = {
12
+ getItem: key => mockStorage.get(key) ?? null,
13
+ setItem: (key, value) => mockStorage.set(key, value),
14
+ removeItem: key => mockStorage.delete(key),
15
+ getAllKeys: () => Array.from(mockStorage.keys())
16
+ };
17
+ setStorageDriver(mockDriver);
18
+ const driver = getStorageDriver();
19
+ expect(driver).toBe(mockDriver);
20
+ });
21
+ }), describe("createStorage with driver", () => {
22
+ let mockStorage;
23
+ beforeEach(() => {
24
+ mockStorage = /* @__PURE__ */new Map(), setStorageDriver({
25
+ getItem: key => mockStorage.get(key) ?? null,
26
+ setItem: (key, value) => mockStorage.set(key, value),
27
+ removeItem: key => mockStorage.delete(key),
28
+ getAllKeys: () => Array.from(mockStorage.keys())
29
+ });
30
+ }), it("should store and retrieve values", () => {
31
+ const storage = createStorage(`test-${Date.now()}-1`);
32
+ storage.set("token", "test-value"), expect(storage.get("token")).toBe("test-value");
33
+ }), it("should use namespace prefix", () => {
34
+ const namespace = `test-${Date.now()}-2`;
35
+ createStorage(namespace).set("key", "value"), expect(mockStorage.has(`${namespace}:key`)).toBe(!0);
36
+ }), it("should handle JSON serialization", () => {
37
+ const storage = createStorage(`test-${Date.now()}-3`),
38
+ obj = {
39
+ name: "test",
40
+ count: 42
41
+ };
42
+ storage.set("obj", obj), expect(storage.get("obj")).toEqual(obj);
43
+ }), it("should support raw string operations", () => {
44
+ const storage = createStorage(`test-${Date.now()}-4`);
45
+ storage.setItem("raw", "raw-value"), expect(storage.getItem("raw")).toBe("raw-value");
46
+ }), it("should return undefined for missing keys", () => {
47
+ const storage = createStorage(`test-${Date.now()}-5`);
48
+ expect(storage.get("missing")).toBeUndefined();
49
+ }), it("should support has() check", () => {
50
+ const storage = createStorage(`test-${Date.now()}-6`);
51
+ expect(storage.has("exists")).toBe(!1), storage.set("exists", "value"), expect(storage.has("exists")).toBe(!0);
52
+ }), it("should support remove()", () => {
53
+ const storage = createStorage(`test-${Date.now()}-7`);
54
+ storage.set("removable", "value"), expect(storage.has("removable")).toBe(!0), storage.remove("removable"), expect(storage.has("removable")).toBe(!1);
55
+ }), it("should list keys in namespace", () => {
56
+ const storage = createStorage(`test-${Date.now()}-8`);
57
+ storage.set("a", "1"), storage.set("b", "2"), storage.set("c", "3"), expect(storage.keys().sort()).toEqual(["a", "b", "c"]);
58
+ }), it("should clear only namespace keys", () => {
59
+ const ns1 = `test-${Date.now()}-9a`,
60
+ ns2 = `test-${Date.now()}-9b`,
61
+ storage1 = createStorage(ns1),
62
+ storage2 = createStorage(ns2);
63
+ storage1.set("key", "value1"), storage2.set("key", "value2"), storage1.clear(), expect(storage1.has("key")).toBe(!1), expect(storage2.has("key")).toBe(!0);
64
+ });
65
+ }), describe("createStorage without driver (simulates native without setup)", () => {
66
+ it("should handle gracefully when operations fail", () => {
67
+ const storage = createStorage(`test-no-driver-${Date.now()}`);
68
+ expect(() => storage.get("key")).not.toThrow(), expect(() => storage.set("key", "value")).not.toThrow(), expect(() => storage.remove("key")).not.toThrow(), expect(() => storage.has("key")).not.toThrow(), expect(() => storage.keys()).not.toThrow(), expect(() => storage.clear()).not.toThrow(), expect(() => storage.getItem("key")).not.toThrow(), expect(() => storage.setItem("key", "value")).not.toThrow();
69
+ });
70
+ });
71
+ });
72
+ describe("auth storage requirements", () => {
73
+ it("storage must be initialized before auth client creates storage instances", () => {
74
+ expect(!0).toBe(!0);
75
+ }), it("native platforms require explicit storage driver setup", () => {
76
+ expect(!0).toBe(!0);
77
+ });
78
+ });
79
+ //# sourceMappingURL=storage.test.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["beforeEach","describe","expect","it","createStorage","getStorageDriver","setStorageDriver","driver","localStorage","toBeTruthy","mockStorage","Map","mockDriver","getItem","key","get","setItem","value","set","removeItem","delete","getAllKeys","Array","from","keys","toBe","storage","Date","now","namespace","has","obj","name","count","toEqual","toBeUndefined","remove","sort","ns1","ns2","storage1","storage2","clear","not","toThrow"],"sources":["../../../src/storage/storage.test.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,EAAYC,QAAA,EAAUC,MAAA,EAAQC,EAAA,QAAU;AAEjD,SAASC,aAAA,QAAqB;AAC9B,SAASC,gBAAA,EAAkBC,gBAAA,QAAwB;AAMnDL,QAAA,CAAS,yBAAyB,MAAM;EACtCA,QAAA,CAAS,yBAAyB,MAAM;IACtCE,EAAA,CAAG,+DAA+D,MAAM;MAEtE,MAAMI,MAAA,GAASF,gBAAA,CAAiB;MAG5B,OAAOG,YAAA,GAAiB,OAC1BN,MAAA,CAAOK,MAAM,EAAEE,UAAA,CA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GAC1C1C,MAAA,CAAO,MAAMwB,OAAA,CAAQb,OAAA,CAAQ,KAAK,CAAC,EAAE8B,GAAA,CAAIC,OAAA,CAAQ,GACjD1C,MAAA,CAAO,MAAMwB,OAAA,CAAQV,OAAA,CAAQ,OAAO,OAAO,CAAC,EAAE2B,GAAA,CAAIC,OAAA,CAAQ;IAC5D,CAAC;EACH,CAAC;AACH,CAAC;AAED3C,QAAA,CAAS,6BAA6B,MAAM;EAG1CE,EAAA,CAAG,4EAA4E,MAAM;IAQnFD,MAAA,CAAO,EAAI,EAAEuB,IAAA,CAAK,EAAI;EACxB,CAAC,GAEDtB,EAAA,CAAG,0DAA0D,MAAM;IAQjED,MAAA,CAAO,EAAI,EAAEuB,IAAA,CAAK,EAAI;EACxB,CAAC;AACH,CAAC","ignoreList":[]}
@@ -0,0 +1,116 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { createStorage } from "./createStorage.native.js";
3
+ import { getStorageDriver, setStorageDriver } from "./driver.native.js";
4
+ describe("storage driver system", function () {
5
+ describe("driver initialization", function () {
6
+ it("getStorageDriver should return localStorage fallback on web", function () {
7
+ var driver = getStorageDriver();
8
+ typeof localStorage < "u" && expect(driver).toBeTruthy();
9
+ }), it("should use custom driver when set", function () {
10
+ var mockStorage = /* @__PURE__ */new Map(),
11
+ mockDriver = {
12
+ getItem: function (key) {
13
+ var _mockStorage_get;
14
+ return (_mockStorage_get = mockStorage.get(key)) !== null && _mockStorage_get !== void 0 ? _mockStorage_get : null;
15
+ },
16
+ setItem: function (key, value) {
17
+ return mockStorage.set(key, value);
18
+ },
19
+ removeItem: function (key) {
20
+ return mockStorage.delete(key);
21
+ },
22
+ getAllKeys: function () {
23
+ return Array.from(mockStorage.keys());
24
+ }
25
+ };
26
+ setStorageDriver(mockDriver);
27
+ var driver = getStorageDriver();
28
+ expect(driver).toBe(mockDriver);
29
+ });
30
+ }), describe("createStorage with driver", function () {
31
+ var mockStorage;
32
+ beforeEach(function () {
33
+ mockStorage = /* @__PURE__ */new Map();
34
+ var mockDriver = {
35
+ getItem: function (key) {
36
+ var _mockStorage_get;
37
+ return (_mockStorage_get = mockStorage.get(key)) !== null && _mockStorage_get !== void 0 ? _mockStorage_get : null;
38
+ },
39
+ setItem: function (key, value) {
40
+ return mockStorage.set(key, value);
41
+ },
42
+ removeItem: function (key) {
43
+ return mockStorage.delete(key);
44
+ },
45
+ getAllKeys: function () {
46
+ return Array.from(mockStorage.keys());
47
+ }
48
+ };
49
+ setStorageDriver(mockDriver);
50
+ }), it("should store and retrieve values", function () {
51
+ var storage = createStorage(`test-${Date.now()}-1`);
52
+ storage.set("token", "test-value"), expect(storage.get("token")).toBe("test-value");
53
+ }), it("should use namespace prefix", function () {
54
+ var namespace = `test-${Date.now()}-2`,
55
+ storage = createStorage(namespace);
56
+ storage.set("key", "value"), expect(mockStorage.has(`${namespace}:key`)).toBe(!0);
57
+ }), it("should handle JSON serialization", function () {
58
+ var storage = createStorage(`test-${Date.now()}-3`),
59
+ obj = {
60
+ name: "test",
61
+ count: 42
62
+ };
63
+ storage.set("obj", obj), expect(storage.get("obj")).toEqual(obj);
64
+ }), it("should support raw string operations", function () {
65
+ var storage = createStorage(`test-${Date.now()}-4`);
66
+ storage.setItem("raw", "raw-value"), expect(storage.getItem("raw")).toBe("raw-value");
67
+ }), it("should return undefined for missing keys", function () {
68
+ var storage = createStorage(`test-${Date.now()}-5`);
69
+ expect(storage.get("missing")).toBeUndefined();
70
+ }), it("should support has() check", function () {
71
+ var storage = createStorage(`test-${Date.now()}-6`);
72
+ expect(storage.has("exists")).toBe(!1), storage.set("exists", "value"), expect(storage.has("exists")).toBe(!0);
73
+ }), it("should support remove()", function () {
74
+ var storage = createStorage(`test-${Date.now()}-7`);
75
+ storage.set("removable", "value"), expect(storage.has("removable")).toBe(!0), storage.remove("removable"), expect(storage.has("removable")).toBe(!1);
76
+ }), it("should list keys in namespace", function () {
77
+ var storage = createStorage(`test-${Date.now()}-8`);
78
+ storage.set("a", "1"), storage.set("b", "2"), storage.set("c", "3"), expect(storage.keys().sort()).toEqual(["a", "b", "c"]);
79
+ }), it("should clear only namespace keys", function () {
80
+ var ns1 = `test-${Date.now()}-9a`,
81
+ ns2 = `test-${Date.now()}-9b`,
82
+ storage1 = createStorage(ns1),
83
+ storage2 = createStorage(ns2);
84
+ storage1.set("key", "value1"), storage2.set("key", "value2"), storage1.clear(), expect(storage1.has("key")).toBe(!1), expect(storage2.has("key")).toBe(!0);
85
+ });
86
+ }), describe("createStorage without driver (simulates native without setup)", function () {
87
+ it("should handle gracefully when operations fail", function () {
88
+ var storage = createStorage(`test-no-driver-${Date.now()}`);
89
+ expect(function () {
90
+ return storage.get("key");
91
+ }).not.toThrow(), expect(function () {
92
+ return storage.set("key", "value");
93
+ }).not.toThrow(), expect(function () {
94
+ return storage.remove("key");
95
+ }).not.toThrow(), expect(function () {
96
+ return storage.has("key");
97
+ }).not.toThrow(), expect(function () {
98
+ return storage.keys();
99
+ }).not.toThrow(), expect(function () {
100
+ return storage.clear();
101
+ }).not.toThrow(), expect(function () {
102
+ return storage.getItem("key");
103
+ }).not.toThrow(), expect(function () {
104
+ return storage.setItem("key", "value");
105
+ }).not.toThrow();
106
+ });
107
+ });
108
+ });
109
+ describe("auth storage requirements", function () {
110
+ it("storage must be initialized before auth client creates storage instances", function () {
111
+ expect(!0).toBe(!0);
112
+ }), it("native platforms require explicit storage driver setup", function () {
113
+ expect(!0).toBe(!0);
114
+ });
115
+ });
116
+ //# sourceMappingURL=storage.test.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["beforeEach","describe","expect","it","createStorage","getStorageDriver","setStorageDriver","driver","localStorage","toBeTruthy","mockStorage","Map","mockDriver","getItem","key","_mockStorage_get","get","setItem","value","set","removeItem","delete","getAllKeys","Array","from","keys","toBe","storage","Date","now","namespace","has","obj","name","count","toEqual","toBeUndefined","remove"],"sources":["../../../src/storage/storage.test.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,EAAY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,cAAuB;MAGpE,IAAAwB,OAAO,GAAMvB,aAAY,SAAQwB,IAAI,CAAAC,GAAA,CAAQ,KAC7C;MAODF,OAAA,CAAAV,OAAA,sBAAAf,MAAA,CAAAyB,OAAA,CAAAd,OAAA,SAAAa,IAAA;IACF,IAAAvB,EAAA;MACF,IAAAwB,OAAA,GAAAvB,aAAA,SAAAwB,IAAA,CAAAC,GAAA;MAED3B,MAAS,CAAAyB,OAAA,CAAAX,GAAA,aAAAoB,aAAmC;IAG1C,CAAG,GAAAjC,EAAA;MAQD,IAAAwB,OAAa,GAAKvB,aAAI,SAAAwB,IAAA,CAAAC,GAAA;MAGxB3B,MAAG,CAAAyB,OAAA,CAAAI,GAAA,YAAAL,IAAA,MAAAC,OAAA,CAAAR,GAAA,WAA0D,OAAM,GAAAjB,MAAA,CAAAyB,OAAA,CAAAI,GAAA,YAAAL,IAAA;IAQjE,IAAAvB,EAAA,CAAO,yBAAe;MACvB,IAAAwB,OAAA,GAAAvB,aAAA,SAAAwB,IAAA,CAAAC,GAAA;MACFF,OAAA,CAAAR,GAAA,wBAAAjB,MAAA,CAAAyB,OAAA,CAAAI,GAAA,eAAAL,IAAA,MAAAC,OAAA,CAAAU,MAAA,eAAAnC,MAAA,CAAAyB,OAAA,CAAAI,GAAA,eAAAL,IAAA","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@take-out/helpers",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "sideEffects": false,
5
5
  "type": "module",
6
6
  "source": "src/index.ts",
package/src/emitter.tsx CHANGED
@@ -144,20 +144,30 @@ export const useEmitter = <E extends Emitter<any>>(
144
144
  }, [emitter, getCallback])
145
145
  }
146
146
 
147
+ // i think this was useSyncExternalStore but removed for concurrent rendering improvements
148
+ // wondering if we could just always return a deferred value? or default to it?
149
+
147
150
  export const useEmitterValue = <E extends Emitter<any>>(
148
151
  emitter: E,
149
152
  options?: { disable?: boolean }
150
153
  ): EmitterType<E> => {
151
- const [state, setState] = useState<EmitterType<E>>(emitter.value)
152
154
  const disabled = options?.disable
153
155
 
156
+ // use a function initializer to get current emitter value
157
+ const [state, setState] = useState<EmitterType<E>>(() => emitter.value)
158
+
154
159
  useLayoutEffect(() => {
155
160
  if (disabled) return
161
+
162
+ // sync immediately in case emitter changed between render and effect
156
163
  if (emitter.value !== state) {
157
164
  setState(emitter.value)
158
165
  }
166
+
159
167
  return emitter.listen(setState)
160
- }, [disabled, emitter, state])
168
+ // intentionally omit state from deps - we only want to re-subscribe when emitter changes
169
+ // eslint-disable-next-line react-hooks/exhaustive-deps
170
+ }, [disabled, emitter])
161
171
 
162
172
  return state
163
173
  }
@@ -0,0 +1,172 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest'
2
+
3
+ import { createStorage } from './createStorage'
4
+ import { getStorageDriver, setStorageDriver } from './driver'
5
+
6
+ // test the storage driver system to ensure it works correctly
7
+ // this regression test validates that the storage system initializes properly
8
+ // which is critical for native auth to work
9
+
10
+ describe('storage driver system', () => {
11
+ describe('driver initialization', () => {
12
+ it('getStorageDriver should return localStorage fallback on web', () => {
13
+ // web environment has localStorage which should be used as fallback
14
+ const driver = getStorageDriver()
15
+ // in node/vitest environment, localStorage might not exist
16
+ // but if it does, the driver should work
17
+ if (typeof localStorage !== 'undefined') {
18
+ expect(driver).toBeTruthy()
19
+ }
20
+ })
21
+
22
+ it('should use custom driver when set', () => {
23
+ const mockStorage = new Map<string, string>()
24
+ const mockDriver = {
25
+ getItem: (key: string) => mockStorage.get(key) ?? null,
26
+ setItem: (key: string, value: string) => mockStorage.set(key, value),
27
+ removeItem: (key: string) => mockStorage.delete(key),
28
+ getAllKeys: () => Array.from(mockStorage.keys()),
29
+ }
30
+
31
+ setStorageDriver(mockDriver)
32
+ const driver = getStorageDriver()
33
+ expect(driver).toBe(mockDriver)
34
+ })
35
+ })
36
+
37
+ describe('createStorage with driver', () => {
38
+ let mockStorage: Map<string, string>
39
+
40
+ beforeEach(() => {
41
+ mockStorage = new Map<string, string>()
42
+ const mockDriver = {
43
+ getItem: (key: string) => mockStorage.get(key) ?? null,
44
+ setItem: (key: string, value: string) => mockStorage.set(key, value),
45
+ removeItem: (key: string) => mockStorage.delete(key),
46
+ getAllKeys: () => Array.from(mockStorage.keys()),
47
+ }
48
+ setStorageDriver(mockDriver)
49
+ })
50
+
51
+ it('should store and retrieve values', () => {
52
+ const storage = createStorage<'token', string>(`test-${Date.now()}-1`)
53
+ storage.set('token', 'test-value')
54
+ expect(storage.get('token')).toBe('test-value')
55
+ })
56
+
57
+ it('should use namespace prefix', () => {
58
+ const namespace = `test-${Date.now()}-2`
59
+ const storage = createStorage<'key', string>(namespace)
60
+ storage.set('key', 'value')
61
+
62
+ // verify the key in the underlying storage has the namespace prefix
63
+ expect(mockStorage.has(`${namespace}:key`)).toBe(true)
64
+ })
65
+
66
+ it('should handle JSON serialization', () => {
67
+ const storage = createStorage<'obj', { name: string; count: number }>(
68
+ `test-${Date.now()}-3`
69
+ )
70
+ const obj = { name: 'test', count: 42 }
71
+ storage.set('obj', obj)
72
+ expect(storage.get('obj')).toEqual(obj)
73
+ })
74
+
75
+ it('should support raw string operations', () => {
76
+ const storage = createStorage<'raw', string>(`test-${Date.now()}-4`)
77
+ storage.setItem('raw', 'raw-value')
78
+ expect(storage.getItem('raw')).toBe('raw-value')
79
+ })
80
+
81
+ it('should return undefined for missing keys', () => {
82
+ const storage = createStorage<'missing', string>(`test-${Date.now()}-5`)
83
+ expect(storage.get('missing')).toBeUndefined()
84
+ })
85
+
86
+ it('should support has() check', () => {
87
+ const storage = createStorage<'exists', string>(`test-${Date.now()}-6`)
88
+ expect(storage.has('exists')).toBe(false)
89
+ storage.set('exists', 'value')
90
+ expect(storage.has('exists')).toBe(true)
91
+ })
92
+
93
+ it('should support remove()', () => {
94
+ const storage = createStorage<'removable', string>(`test-${Date.now()}-7`)
95
+ storage.set('removable', 'value')
96
+ expect(storage.has('removable')).toBe(true)
97
+ storage.remove('removable')
98
+ expect(storage.has('removable')).toBe(false)
99
+ })
100
+
101
+ it('should list keys in namespace', () => {
102
+ const storage = createStorage<'a' | 'b' | 'c', string>(`test-${Date.now()}-8`)
103
+ storage.set('a', '1')
104
+ storage.set('b', '2')
105
+ storage.set('c', '3')
106
+ expect(storage.keys().sort()).toEqual(['a', 'b', 'c'])
107
+ })
108
+
109
+ it('should clear only namespace keys', () => {
110
+ const ns1 = `test-${Date.now()}-9a`
111
+ const ns2 = `test-${Date.now()}-9b`
112
+ const storage1 = createStorage<'key', string>(ns1)
113
+ const storage2 = createStorage<'key', string>(ns2)
114
+
115
+ storage1.set('key', 'value1')
116
+ storage2.set('key', 'value2')
117
+
118
+ storage1.clear()
119
+
120
+ expect(storage1.has('key')).toBe(false)
121
+ expect(storage2.has('key')).toBe(true)
122
+ })
123
+ })
124
+
125
+ describe('createStorage without driver (simulates native without setup)', () => {
126
+ // this test simulates what happens on native when setupStorage.native.ts
127
+ // is not imported before auth initialization
128
+
129
+ it('should handle gracefully when operations fail', () => {
130
+ // when driver returns null, operations should not throw
131
+ // they should just silently fail (return undefined, do nothing)
132
+ // this is the current behavior but we want to document it
133
+ const storage = createStorage<'key', string>(`test-no-driver-${Date.now()}`)
134
+
135
+ // these should not throw even without a driver
136
+ expect(() => storage.get('key')).not.toThrow()
137
+ expect(() => storage.set('key', 'value')).not.toThrow()
138
+ expect(() => storage.remove('key')).not.toThrow()
139
+ expect(() => storage.has('key')).not.toThrow()
140
+ expect(() => storage.keys()).not.toThrow()
141
+ expect(() => storage.clear()).not.toThrow()
142
+ expect(() => storage.getItem('key')).not.toThrow()
143
+ expect(() => storage.setItem('key', 'value')).not.toThrow()
144
+ })
145
+ })
146
+ })
147
+
148
+ describe('auth storage requirements', () => {
149
+ // these tests document the requirements for auth storage to work correctly
150
+
151
+ it('storage must be initialized before auth client creates storage instances', () => {
152
+ // the expo auth client creates storage at module load time:
153
+ // const expoStorage = createStorage('expo-auth-client')
154
+ //
155
+ // if setStorageDriver is not called before this, storage operations will fail
156
+ // this is why setupClient.ts must run before any auth code
157
+
158
+ // we verify this requirement is documented and understood
159
+ expect(true).toBe(true)
160
+ })
161
+
162
+ it('native platforms require explicit storage driver setup', () => {
163
+ // unlike web which has localStorage fallback, native platforms need
164
+ // MMKV or AsyncStorage to be configured via setStorageDriver()
165
+ //
166
+ // this happens in setupStorage.native.ts which must be imported
167
+ // before platformClient.native.ts creates its storage instance
168
+
169
+ // we verify this requirement is documented and understood
170
+ expect(true).toBe(true)
171
+ })
172
+ })
@@ -20,6 +20,8 @@ export declare function createGlobalEmitter<T>(name: string, defaultValue: T, op
20
20
  export declare function createEmitter<T>(name: string, defaultValue: T, options?: CreateEmitterOpts<T>): Emitter<T>;
21
21
  export type EmitterType<E extends Emitter<any>> = E extends Emitter<infer Val> ? Val : never;
22
22
  export declare const useEmitter: <E extends Emitter<any>>(emitter: E, cb: (cb: EmitterType<E>) => void, args?: any[]) => void;
23
+ // i think this was useSyncExternalStore but removed for concurrent rendering improvements
24
+ // wondering if we could just always return a deferred value? or default to it?
23
25
  export declare const useEmitterValue: <E extends Emitter<any>>(emitter: E, options?: {
24
26
  disable?: boolean;
25
27
  }) => EmitterType<E>;
@@ -1,5 +1,5 @@
1
1
  {
2
- "mappings": "AAUA,cAAc,KAAK,yBAAyB,OAAO;KAwB9C,eAAe,KAAK,kBAAkB,KAAK;CAC9C;AACD;KAEI,kBAAkB,KAAK;CAC1B;CACA,cAAcA,GAAG,GAAGC,GAAG;AACxB;AAED,OAAO,cAAM,cAAc,GAAG;CAC5B,QAAQ;CACR,OAAO;CACP,UAAU,eAAe;CAEzB,YAAYC,OAAO,GAAGC,UAAU,eAAe;CAK/C,SAAUC,aAAaC,IAAI;CAO3B,OAAQC,MAAM;CAqCd,iBAAgB,QAAQ;AAQzB;;AAGD,OAAO,iBAAS,oBAAoB,GAClCC,cACAC,cAAc,GACdC,UAAU,kBAAkB,KAC3B,QAAQ;AAIX,OAAO,iBAAS,cAAc,GAC5BF,cACA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src/emitter.tsx"
38
38
  ],
39
39
  "sourcesContent": [
40
- "import { dequal } from 'dequal'\nimport * as React from 'react'\nimport { use, useLayoutEffect, useState } from 'react'\n\nimport { handleAbortError } from './async/abortable'\nimport { DEBUG_LEVEL, EMPTY_ARRAY } from './constants'\nimport { AbortError } from './error/errors'\nimport { globalValue } from './global/globalValue'\nimport { createGlobalContext } from './react/createGlobalContext'\n\nimport type { JSX, PropsWithChildren } from 'react'\n\n// keeps a reference to the current value easily\n\n// TODO can replace with useEffectEvent\nfunction useGet<A>(\n currentValue: A,\n initialValue?: any,\n forwardToFunction?: boolean\n): () => A {\n const curRef = React.useRef<any>(initialValue ?? currentValue)\n\n useLayoutEffect(() => {\n curRef.current = currentValue\n })\n\n return React.useCallback(\n forwardToFunction\n ? (...args) => curRef.current?.apply(null, args)\n : () => curRef.current,\n []\n )\n}\n\ntype EmitterOptions<T> = CreateEmitterOpts<T> & {\n name: string\n}\n\ntype CreateEmitterOpts<T> = {\n silent?: boolean\n comparator?: (a: T, b: T) => boolean\n}\n\nexport class Emitter<const T> {\n private disposables = new Set<(cb: any) => void>()\n value: T\n options?: EmitterOptions<T>\n\n constructor(value: T, options?: EmitterOptions<T>) {\n this.value = value\n this.options = options\n }\n\n listen = (disposable: (cb: T) => void): (() => void) => {\n this.disposables.add(disposable)\n return (): void => {\n this.disposables.delete(disposable)\n }\n }\n\n emit = (next: T): void => {\n if (process.env.NODE_ENV === 'development') {\n setCache(this, next)\n }\n const compare = this.options?.comparator\n if (compare) {\n if (this.value && compare(this.value, next)) {\n return\n }\n } else {\n if (this.value === next) {\n if (process.env.NODE_ENV === 'development') {\n console.warn(\n `[emitter] ${this.options?.name} no comparator option but received same value!\n \nthis will emit the same value again, which can be desirable, but we warn to ensure it's not unintended:\n\n- if you want this behavior, add { comparator: isEqualNever }\n- if you want only non-equal values: { comparator: isEqualIdentity }\n- if you want only deeply non-equal values: { comparator: isEqualDeep }`\n )\n }\n }\n }\n this.value = next\n if (DEBUG_LEVEL > 1) {\n if (!this.options?.silent) {\n const name = this.options?.name\n console.groupCollapsed(`📣 ${name}`)\n console.info(next)\n console.trace(`trace >`)\n console.groupEnd()\n }\n }\n this.disposables.forEach((cb) => cb(next))\n }\n\n nextValue = (): Promise<T> => {\n return new Promise<T>((res) => {\n const dispose = this.listen((val) => {\n dispose()\n res(val)\n })\n })\n }\n}\n\n// just createEmitter but ensures it doesn't mess up on HMR\nexport function createGlobalEmitter<T>(\n name: string,\n defaultValue: T,\n options?: CreateEmitterOpts<T>\n): Emitter<T> {\n return globalValue(name, () => createEmitter(name, defaultValue, options))\n}\n\nexport function createEmitter<T>(\n name: string,\n defaultValue: T,\n options?: CreateEmitterOpts<T>\n): Emitter<T> {\n const existing = createOrUpdateCache(name, defaultValue) as T\n return new Emitter<T>(existing || defaultValue, { name, ...options })\n}\n\nexport type EmitterType<E extends Emitter<any>> =\n E extends Emitter<infer Val> ? Val : never\n\nexport const useEmitter = <E extends Emitter<any>>(\n emitter: E,\n cb: (cb: EmitterType<E>) => void,\n args?: any[]\n): void => {\n const getCallback = useGet(cb)\n\n useLayoutEffect(() => {\n return emitter.listen((val) => {\n try {\n getCallback()(val)\n } catch (err) {\n handleAbortError(err)\n }\n })\n }, [emitter, getCallback])\n}\n\nexport const useEmitterValue = <E extends Emitter<any>>(\n emitter: E,\n options?: { disable?: boolean }\n): EmitterType<E> => {\n const [state, setState] = useState<EmitterType<E>>(emitter.value)\n const disabled = options?.disable\n\n useLayoutEffect(() => {\n if (disabled) return\n if (emitter.value !== state) {\n setState(emitter.value)\n }\n return emitter.listen(setState)\n }, [disabled, emitter, state])\n\n return state\n}\n\n/**\n * By default selectors run every render, as well as when emitters update. This is a change\n * from the previous behavior where they only ran when emitters changed value.\n *\n * The reason for this is because emitters capture the variables in scope of the component\n * each render already by using \"useGet\" by default, which makes them easier to use - you\n * don't need to pass an args[] array except for edge cases.\n *\n * Before explaining why we switched to the default, understand the different uses:\n *\n * - Default behavior - selector is updated every render, and ran every render, as well as\n * when emitter value changes, so you basically are always up to date.\n *\n * - Set an args[] array as the fourth argument - this will stop the automatic capturing\n * and instead update selector only when you change args[] yourself. This is good for when you\n * want explicit control over re-selection and rendering.\n *\n * - With { lazy: true }, the selector only runs when the emitter value changes. If used with\n * args[], you capture the context of the selector based on args[], if not, it's based on the\n * current render.\n *\n * I made this change when we had 16 usages of useEmitterSelector and 100% of them are doing very\n * cheap calculations, so this feels like the right pattern. For the rare case of a heavy selector,\n * you have the option to control it.\n *\n */\nexport const useEmitterSelector = <E extends Emitter<any>, T extends EmitterType<E>, R>(\n emitter: E,\n selector: (value: T) => R,\n options?: {\n disable?: boolean\n lazy?: boolean\n },\n args: any[] = EMPTY_ARRAY\n): R => {\n const [state, setState] = useState<R>(() => selector(emitter.value))\n const disabled = options?.disable\n const getSelector = useGet(selector)\n\n if (options?.lazy !== true) {\n const next = selector(emitter.value)\n if (next !== state) {\n setState(next)\n }\n }\n\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useLayoutEffect(() => {\n if (disabled) return\n return emitter.listen((val) => {\n try {\n const selectorFn = args !== EMPTY_ARRAY ? selector : getSelector()\n const next = selectorFn(val)\n setState(next)\n } catch (error) {\n if (error instanceof AbortError) {\n return\n }\n throw error\n }\n })\n }, [disabled, emitter, getSelector, ...args])\n\n return state\n}\n\nexport const useEmittersSelector = <const E extends readonly Emitter<any>[], R>(\n emitters: E,\n selector: (values: { [K in keyof E]: EmitterType<E[K]> }) => R,\n options?: { disable?: boolean; isEqual?: (a: R, b: R) => boolean }\n): R => {\n const getSelector = useGet(selector)\n const disabled = options?.disable\n\n const [state, setState] = useState<R>(() => {\n const values = emitters.map((e) => e.value) as { [K in keyof E]: EmitterType<E[K]> }\n return getSelector()(values)\n })\n\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useLayoutEffect(() => {\n if (disabled) {\n return\n }\n\n const handler = () => {\n const values = emitters.map((e) => e.value) as {\n [K in keyof E]: EmitterType<E[K]>\n }\n try {\n const next = getSelector()(values)\n setState((prev) => {\n if (options?.isEqual?.(prev, next)) {\n return prev\n }\n if (dequal(prev, next)) {\n return prev\n }\n return next\n })\n } catch (error) {\n if (error instanceof AbortError) {\n return\n }\n throw error\n }\n }\n\n const disposals = emitters.map((emitter) => emitter.listen(handler))\n\n return () => {\n disposals.forEach((d) => d())\n }\n }, [disabled, getSelector, ...emitters])\n\n return state\n}\n\nexport const createUseEmitter = <E extends Emitter<any>>(\n emitter: E\n): ((cb: (val: EmitterType<E>) => void, args?: any[]) => void) => {\n return (cb: (val: EmitterType<E>) => void, args?: any[]) =>\n useEmitter(emitter, cb, args)\n}\n\nexport const createUseSelector = <E extends Emitter<any>>(\n emitter: E\n): (<R>(\n selector: (value: EmitterType<E>) => R,\n options?: { disable?: boolean; lazy?: boolean },\n args?: any[]\n) => R) => {\n return <R,>(\n selector: (value: EmitterType<E>) => R,\n options?: { disable?: boolean; lazy?: boolean },\n args?: any[]\n ): R => {\n return useEmitterSelector(emitter, selector, options, args)\n }\n}\n\nexport function createContextualEmitter<T>(\n name: string,\n defaultValue: T,\n defaultOptions?: Omit<EmitterOptions<T>, 'name'>\n): readonly [\n () => Emitter<T>,\n (props: PropsWithChildren<{ value?: T; silent?: boolean }>) => JSX.Element,\n] {\n const id = Math.random().toString(36)\n const EmitterContext = createGlobalContext<Emitter<T> | null>(\n `contextual-emitter/${id}`,\n null\n )\n\n const useContextEmitter = () => {\n const emitter = use(EmitterContext)\n if (!emitter) {\n throw new Error('useContextEmitter must be used within an EmitterProvider')\n }\n return emitter\n }\n\n type ProvideEmitterProps = PropsWithChildren<{\n value?: T\n silent?: boolean\n }>\n\n const ProvideEmitter = (props: ProvideEmitterProps) => {\n const { children, value, silent } = props\n const [emitter] = useState(\n () => new Emitter<T>(value ?? defaultValue, { name, silent, ...defaultOptions })\n )\n\n useLayoutEffect(() => {\n if (value !== undefined && value !== emitter.value) {\n emitter.emit(value)\n }\n }, [value, emitter])\n\n return <EmitterContext.Provider value={emitter}>{children}</EmitterContext.Provider>\n }\n\n return [useContextEmitter, ProvideEmitter] as const\n}\n\nconst HMRCache =\n process.env.NODE_ENV === 'development'\n ? new Map<string, { originalDefaultValue: unknown; currentValue: unknown }>()\n : null\n\nfunction setCache(emitter: Emitter<any>, value: unknown) {\n const name = emitter.options?.name\n if (!name) return\n const cache = HMRCache?.get(name)\n if (!cache) return\n cache.currentValue = value\n}\n\nfunction createOrUpdateCache(name: string, defaultValueProp: unknown) {\n const existing = HMRCache?.get(name)\n const defaultValue = dequal(existing?.originalDefaultValue, defaultValueProp)\n ? existing?.currentValue\n : defaultValueProp\n\n if (!existing) {\n HMRCache?.set(name, {\n originalDefaultValue: defaultValueProp,\n currentValue: defaultValue,\n })\n }\n\n return defaultValue\n}\n"
40
+ "import { dequal } from 'dequal'\nimport * as React from 'react'\nimport { use, useLayoutEffect, useState } from 'react'\n\nimport { handleAbortError } from './async/abortable'\nimport { DEBUG_LEVEL, EMPTY_ARRAY } from './constants'\nimport { AbortError } from './error/errors'\nimport { globalValue } from './global/globalValue'\nimport { createGlobalContext } from './react/createGlobalContext'\n\nimport type { JSX, PropsWithChildren } from 'react'\n\n// keeps a reference to the current value easily\n\n// TODO can replace with useEffectEvent\nfunction useGet<A>(\n currentValue: A,\n initialValue?: any,\n forwardToFunction?: boolean\n): () => A {\n const curRef = React.useRef<any>(initialValue ?? currentValue)\n\n useLayoutEffect(() => {\n curRef.current = currentValue\n })\n\n return React.useCallback(\n forwardToFunction\n ? (...args) => curRef.current?.apply(null, args)\n : () => curRef.current,\n []\n )\n}\n\ntype EmitterOptions<T> = CreateEmitterOpts<T> & {\n name: string\n}\n\ntype CreateEmitterOpts<T> = {\n silent?: boolean\n comparator?: (a: T, b: T) => boolean\n}\n\nexport class Emitter<const T> {\n private disposables = new Set<(cb: any) => void>()\n value: T\n options?: EmitterOptions<T>\n\n constructor(value: T, options?: EmitterOptions<T>) {\n this.value = value\n this.options = options\n }\n\n listen = (disposable: (cb: T) => void): (() => void) => {\n this.disposables.add(disposable)\n return (): void => {\n this.disposables.delete(disposable)\n }\n }\n\n emit = (next: T): void => {\n if (process.env.NODE_ENV === 'development') {\n setCache(this, next)\n }\n const compare = this.options?.comparator\n if (compare) {\n if (this.value && compare(this.value, next)) {\n return\n }\n } else {\n if (this.value === next) {\n if (process.env.NODE_ENV === 'development') {\n console.warn(\n `[emitter] ${this.options?.name} no comparator option but received same value!\n \nthis will emit the same value again, which can be desirable, but we warn to ensure it's not unintended:\n\n- if you want this behavior, add { comparator: isEqualNever }\n- if you want only non-equal values: { comparator: isEqualIdentity }\n- if you want only deeply non-equal values: { comparator: isEqualDeep }`\n )\n }\n }\n }\n this.value = next\n if (DEBUG_LEVEL > 1) {\n if (!this.options?.silent) {\n const name = this.options?.name\n console.groupCollapsed(`📣 ${name}`)\n console.info(next)\n console.trace(`trace >`)\n console.groupEnd()\n }\n }\n this.disposables.forEach((cb) => cb(next))\n }\n\n nextValue = (): Promise<T> => {\n return new Promise<T>((res) => {\n const dispose = this.listen((val) => {\n dispose()\n res(val)\n })\n })\n }\n}\n\n// just createEmitter but ensures it doesn't mess up on HMR\nexport function createGlobalEmitter<T>(\n name: string,\n defaultValue: T,\n options?: CreateEmitterOpts<T>\n): Emitter<T> {\n return globalValue(name, () => createEmitter(name, defaultValue, options))\n}\n\nexport function createEmitter<T>(\n name: string,\n defaultValue: T,\n options?: CreateEmitterOpts<T>\n): Emitter<T> {\n const existing = createOrUpdateCache(name, defaultValue) as T\n return new Emitter<T>(existing || defaultValue, { name, ...options })\n}\n\nexport type EmitterType<E extends Emitter<any>> =\n E extends Emitter<infer Val> ? Val : never\n\nexport const useEmitter = <E extends Emitter<any>>(\n emitter: E,\n cb: (cb: EmitterType<E>) => void,\n args?: any[]\n): void => {\n const getCallback = useGet(cb)\n\n useLayoutEffect(() => {\n return emitter.listen((val) => {\n try {\n getCallback()(val)\n } catch (err) {\n handleAbortError(err)\n }\n })\n }, [emitter, getCallback])\n}\n\n// i think this was useSyncExternalStore but removed for concurrent rendering improvements\n// wondering if we could just always return a deferred value? or default to it?\n\nexport const useEmitterValue = <E extends Emitter<any>>(\n emitter: E,\n options?: { disable?: boolean }\n): EmitterType<E> => {\n const disabled = options?.disable\n\n // use a function initializer to get current emitter value\n const [state, setState] = useState<EmitterType<E>>(() => emitter.value)\n\n useLayoutEffect(() => {\n if (disabled) return\n\n // sync immediately in case emitter changed between render and effect\n if (emitter.value !== state) {\n setState(emitter.value)\n }\n\n return emitter.listen(setState)\n // intentionally omit state from deps - we only want to re-subscribe when emitter changes\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [disabled, emitter])\n\n return state\n}\n\n/**\n * By default selectors run every render, as well as when emitters update. This is a change\n * from the previous behavior where they only ran when emitters changed value.\n *\n * The reason for this is because emitters capture the variables in scope of the component\n * each render already by using \"useGet\" by default, which makes them easier to use - you\n * don't need to pass an args[] array except for edge cases.\n *\n * Before explaining why we switched to the default, understand the different uses:\n *\n * - Default behavior - selector is updated every render, and ran every render, as well as\n * when emitter value changes, so you basically are always up to date.\n *\n * - Set an args[] array as the fourth argument - this will stop the automatic capturing\n * and instead update selector only when you change args[] yourself. This is good for when you\n * want explicit control over re-selection and rendering.\n *\n * - With { lazy: true }, the selector only runs when the emitter value changes. If used with\n * args[], you capture the context of the selector based on args[], if not, it's based on the\n * current render.\n *\n * I made this change when we had 16 usages of useEmitterSelector and 100% of them are doing very\n * cheap calculations, so this feels like the right pattern. For the rare case of a heavy selector,\n * you have the option to control it.\n *\n */\nexport const useEmitterSelector = <E extends Emitter<any>, T extends EmitterType<E>, R>(\n emitter: E,\n selector: (value: T) => R,\n options?: {\n disable?: boolean\n lazy?: boolean\n },\n args: any[] = EMPTY_ARRAY\n): R => {\n const [state, setState] = useState<R>(() => selector(emitter.value))\n const disabled = options?.disable\n const getSelector = useGet(selector)\n\n if (options?.lazy !== true) {\n const next = selector(emitter.value)\n if (next !== state) {\n setState(next)\n }\n }\n\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useLayoutEffect(() => {\n if (disabled) return\n return emitter.listen((val) => {\n try {\n const selectorFn = args !== EMPTY_ARRAY ? selector : getSelector()\n const next = selectorFn(val)\n setState(next)\n } catch (error) {\n if (error instanceof AbortError) {\n return\n }\n throw error\n }\n })\n }, [disabled, emitter, getSelector, ...args])\n\n return state\n}\n\nexport const useEmittersSelector = <const E extends readonly Emitter<any>[], R>(\n emitters: E,\n selector: (values: { [K in keyof E]: EmitterType<E[K]> }) => R,\n options?: { disable?: boolean; isEqual?: (a: R, b: R) => boolean }\n): R => {\n const getSelector = useGet(selector)\n const disabled = options?.disable\n\n const [state, setState] = useState<R>(() => {\n const values = emitters.map((e) => e.value) as { [K in keyof E]: EmitterType<E[K]> }\n return getSelector()(values)\n })\n\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useLayoutEffect(() => {\n if (disabled) {\n return\n }\n\n const handler = () => {\n const values = emitters.map((e) => e.value) as {\n [K in keyof E]: EmitterType<E[K]>\n }\n try {\n const next = getSelector()(values)\n setState((prev) => {\n if (options?.isEqual?.(prev, next)) {\n return prev\n }\n if (dequal(prev, next)) {\n return prev\n }\n return next\n })\n } catch (error) {\n if (error instanceof AbortError) {\n return\n }\n throw error\n }\n }\n\n const disposals = emitters.map((emitter) => emitter.listen(handler))\n\n return () => {\n disposals.forEach((d) => d())\n }\n }, [disabled, getSelector, ...emitters])\n\n return state\n}\n\nexport const createUseEmitter = <E extends Emitter<any>>(\n emitter: E\n): ((cb: (val: EmitterType<E>) => void, args?: any[]) => void) => {\n return (cb: (val: EmitterType<E>) => void, args?: any[]) =>\n useEmitter(emitter, cb, args)\n}\n\nexport const createUseSelector = <E extends Emitter<any>>(\n emitter: E\n): (<R>(\n selector: (value: EmitterType<E>) => R,\n options?: { disable?: boolean; lazy?: boolean },\n args?: any[]\n) => R) => {\n return <R,>(\n selector: (value: EmitterType<E>) => R,\n options?: { disable?: boolean; lazy?: boolean },\n args?: any[]\n ): R => {\n return useEmitterSelector(emitter, selector, options, args)\n }\n}\n\nexport function createContextualEmitter<T>(\n name: string,\n defaultValue: T,\n defaultOptions?: Omit<EmitterOptions<T>, 'name'>\n): readonly [\n () => Emitter<T>,\n (props: PropsWithChildren<{ value?: T; silent?: boolean }>) => JSX.Element,\n] {\n const id = Math.random().toString(36)\n const EmitterContext = createGlobalContext<Emitter<T> | null>(\n `contextual-emitter/${id}`,\n null\n )\n\n const useContextEmitter = () => {\n const emitter = use(EmitterContext)\n if (!emitter) {\n throw new Error('useContextEmitter must be used within an EmitterProvider')\n }\n return emitter\n }\n\n type ProvideEmitterProps = PropsWithChildren<{\n value?: T\n silent?: boolean\n }>\n\n const ProvideEmitter = (props: ProvideEmitterProps) => {\n const { children, value, silent } = props\n const [emitter] = useState(\n () => new Emitter<T>(value ?? defaultValue, { name, silent, ...defaultOptions })\n )\n\n useLayoutEffect(() => {\n if (value !== undefined && value !== emitter.value) {\n emitter.emit(value)\n }\n }, [value, emitter])\n\n return <EmitterContext.Provider value={emitter}>{children}</EmitterContext.Provider>\n }\n\n return [useContextEmitter, ProvideEmitter] as const\n}\n\nconst HMRCache =\n process.env.NODE_ENV === 'development'\n ? new Map<string, { originalDefaultValue: unknown; currentValue: unknown }>()\n : null\n\nfunction setCache(emitter: Emitter<any>, value: unknown) {\n const name = emitter.options?.name\n if (!name) return\n const cache = HMRCache?.get(name)\n if (!cache) return\n cache.currentValue = value\n}\n\nfunction createOrUpdateCache(name: string, defaultValueProp: unknown) {\n const existing = HMRCache?.get(name)\n const defaultValue = dequal(existing?.originalDefaultValue, defaultValueProp)\n ? existing?.currentValue\n : defaultValueProp\n\n if (!existing) {\n HMRCache?.set(name, {\n originalDefaultValue: defaultValueProp,\n currentValue: defaultValue,\n })\n }\n\n return defaultValue\n}\n"
41
41
  ],
42
42
  "version": 3
43
43
  }
@@ -0,0 +1,3 @@
1
+ export {};
2
+
3
+ //# sourceMappingURL=storage.test.d.ts.map