@kdeveloper/kvark 1.3.0 → 1.3.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.
- package/README.md +2 -2
- package/dist/preact/index.js +15 -4
- package/dist/preact/index.js.map +1 -1
- package/dist/react/index.js +15 -4
- package/dist/react/index.js.map +1 -1
- package/dist/{use-atom-value-Bqy1TMFt.js → use-atom-value-KMOshe0x.js} +75 -12
- package/dist/use-atom-value-KMOshe0x.js.map +1 -0
- package/package.json +1 -1
- package/dist/use-atom-value-Bqy1TMFt.js.map +0 -1
package/README.md
CHANGED
|
@@ -353,7 +353,7 @@ The `ctx` passed to `write` provides:
|
|
|
353
353
|
- **`ctx.writeOptimistic(atom, value)`** — synchronously write into another atom's cache with automatic rollback on error (see [Dependent mutations](#dependent-mutations)). Also accepts a mutator.
|
|
354
354
|
- **`ctx.invalidate(atom)`** / **`ctx.invalidateMany(atoms)`** — mark unrelated atoms as stale so they refetch (see [Dependent mutations](#dependent-mutations)).
|
|
355
355
|
|
|
356
|
-
In-flight reads abort when the last subscriber for that atom unmounts, when a new read replaces the current one, when the atom is invalidated, or when `store.cancel(atom)` / `client.cancel(atom)` is called. Use `invalidate` when you want stale-while-revalidate; use `cancel` when you only want to stop work that is no longer needed without marking the atom stale or notifying subscribers.
|
|
356
|
+
In-flight reads abort when the last subscriber for that atom unmounts, when a new read replaces the current one, when the atom is invalidated, or when `store.cancel(atom)` / `client.cancel(atom)` is called. Any remaining subscriber, including `store.observe`, keeps the read alive. Use `invalidate` when you want stale-while-revalidate; use `cancel` when you only want to stop work that is no longer needed without marking the atom stale or notifying subscribers. For screen transitions that drop several conditional async atoms at once, collect those atom instances and call `store.cancelMany(atoms)` / `client.cancelMany(atoms)` if other subscribers may still exist.
|
|
357
357
|
|
|
358
358
|
#### Optimistic updates
|
|
359
359
|
|
|
@@ -878,7 +878,7 @@ Returns the `Store` instance from context — for advanced cases (e.g. calling `
|
|
|
878
878
|
### `useAtomValue`
|
|
879
879
|
|
|
880
880
|
Reads an atom value and subscribes to updates. Suspends on first load.
|
|
881
|
-
If the consumer unmounts while a read is still in flight, the hook aborts the read through `ctx.signal`, including React / Preact Suspense branches that are removed before the suspended component commits.
|
|
881
|
+
If the consumer unmounts while a read is still in flight, the hook aborts the read through `ctx.signal`, including React / Preact Suspense branches that are removed before the suspended component commits. The abort still follows the store-level subscriber rule: if another mounted consumer or `store.observe` subscription is keeping the same atom alive, use `store.cancel(atom)` / `store.cancelMany(atoms)` for an explicit screen-transition cancel.
|
|
882
882
|
|
|
883
883
|
```tsx
|
|
884
884
|
// Basic — throws if error, suspends if pending
|
package/dist/preact/index.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import { n as useAtomSubscription, r as useAtomSubscriptionScope, t as getAtomValueResult } from "../use-atom-value-
|
|
1
|
+
import { n as useAtomSubscription, r as useAtomSubscriptionScope, t as getAtomValueResult } from "../use-atom-value-KMOshe0x.js";
|
|
2
2
|
import { createContext } from "preact";
|
|
3
|
-
import { useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
|
|
3
|
+
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
|
|
4
4
|
import { jsx } from "preact/jsx-runtime";
|
|
5
5
|
//#region src/preact/provider.tsx
|
|
6
6
|
/** @jsxImportSource preact */
|
|
7
7
|
const StoreContext = createContext(null);
|
|
8
|
+
const SubscriptionScopeContext = createContext(0);
|
|
8
9
|
function Provider({ store, children }) {
|
|
9
|
-
useAtomSubscriptionScope(store, {
|
|
10
|
+
const subscriptionTick = useAtomSubscriptionScope(store, {
|
|
10
11
|
useLayoutEffect,
|
|
11
12
|
useState
|
|
12
13
|
});
|
|
13
14
|
return /* @__PURE__ */ jsx(StoreContext.Provider, {
|
|
14
15
|
value: store,
|
|
15
|
-
children
|
|
16
|
+
children: /* @__PURE__ */ jsx(SubscriptionScopeContext.Provider, {
|
|
17
|
+
value: subscriptionTick,
|
|
18
|
+
children
|
|
19
|
+
})
|
|
16
20
|
});
|
|
17
21
|
}
|
|
18
22
|
function useStore() {
|
|
@@ -20,6 +24,9 @@ function useStore() {
|
|
|
20
24
|
if (store == null) throw new Error("useStore must be used within a <Provider>");
|
|
21
25
|
return store;
|
|
22
26
|
}
|
|
27
|
+
function useAtomSubscriptionTick() {
|
|
28
|
+
useContext(SubscriptionScopeContext);
|
|
29
|
+
}
|
|
23
30
|
//#endregion
|
|
24
31
|
//#region src/preact/use-sync-external-store.ts
|
|
25
32
|
function didSnapshotChange(inst) {
|
|
@@ -63,12 +70,16 @@ function useStableFn(fn) {
|
|
|
63
70
|
//#region src/preact/use-atom-value.ts
|
|
64
71
|
function useAtomValue(atom, opts) {
|
|
65
72
|
const store = useStore();
|
|
73
|
+
useAtomSubscriptionTick();
|
|
66
74
|
const ownerRef = useRef(null);
|
|
67
75
|
if (ownerRef.current == null) ownerRef.current = {};
|
|
68
76
|
const subscription = useAtomSubscription(store, atom, ownerRef.current);
|
|
69
77
|
const unsubscribeRef = useRef(null);
|
|
70
78
|
const releaseRef = useRef(subscription.release);
|
|
71
79
|
releaseRef.current = subscription.release;
|
|
80
|
+
useMemo(() => {
|
|
81
|
+
subscription.retain();
|
|
82
|
+
}, [subscription]);
|
|
72
83
|
const snapshot = useSyncExternalStore(useStableFn((notify) => {
|
|
73
84
|
const unsubscribe = subscription.subscribe(notify);
|
|
74
85
|
let active = true;
|
package/dist/preact/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/preact/provider.tsx","../../src/preact/use-sync-external-store.ts","../../src/preact/use-stable-fn.ts","../../src/preact/use-atom-value.ts","../../src/preact/use-apply-atom.ts","../../src/preact/use-mutation.ts","../../src/preact/use-atom.ts","../../src/preact/use-atom-context.ts"],"mappings":";;;;;;AAMA,MAAM,eAAe,cAA4B,KAAK;
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/preact/provider.tsx","../../src/preact/use-sync-external-store.ts","../../src/preact/use-stable-fn.ts","../../src/preact/use-atom-value.ts","../../src/preact/use-apply-atom.ts","../../src/preact/use-mutation.ts","../../src/preact/use-atom.ts","../../src/preact/use-atom-context.ts"],"mappings":";;;;;;AAMA,MAAM,eAAe,cAA4B,KAAK;AACtD,MAAM,2BAA2B,cAAc,EAAE;AAOjD,SAAgB,SAAS,EAAE,OAAO,YAAkC;CAClE,MAAM,mBAAmB,yBAAyB,OAAO;EAAE;EAAiB;EAAU,CAAC;CACvF,OACE,oBAAC,aAAa,UAAd;EAAuB,OAAO;YAC5B,oBAAC,yBAAyB,UAA1B;GAAmC,OAAO;GACvC;GACiC,CAAA;EACd,CAAA;;AAI5B,SAAgB,WAAkB;CAChC,MAAM,QAAQ,WAAW,aAAa;CACtC,IAAI,SAAS,MACX,MAAM,IAAI,MAAM,4CAA4C;CAE9D,OAAO;;AAGT,SAAgB,0BAAgC;CAC9C,WAAW,yBAAyB;;;;AC3BtC,SAAS,kBAAqB,MAAiC;CAC7D,IAAI;EACF,OAAO,CAAC,OAAO,GAAG,KAAK,QAAQ,KAAK,cAAc,CAAC;SAC7C;EACN,OAAO;;;AAIX,SAAgB,qBACd,WACA,aACG;CACH,MAAM,QAAQ,aAAa;CAE3B,MAAM,CAAC,EAAE,aAAa,eAAe,gBAAgB,EACnD,WAAW;EAAE,QAAQ;EAAO,cAAc;EAAa,EACxD,EAAE;CAEH,sBAAsB;EACpB,UAAU,SAAS;EACnB,UAAU,eAAe;EAEzB,IAAI,kBAAkB,UAAU,EAC9B,YAAY,EAAE,WAAW,CAAC;IAE3B;EAAC;EAAW;EAAO;EAAY,CAAC;CAEnC,gBAAgB;EACd,IAAI,kBAAkB,UAAU,EAC9B,YAAY,EAAE,WAAW,CAAC;EAG5B,OAAO,gBAAgB;GACrB,IAAI,kBAAkB,UAAU,EAC9B,YAAY,EAAE,WAAW,CAAC;IAE5B;IACD,CAAC,UAAU,CAAC;CAEf,OAAO;;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C;CAClC,MAAM,QAAQ,UAAU;CACxB,OAAO,aACJ,GAAG,SAA8B,MAAM,OAAO,UAAU,GAAG,KAAK,EACjE,CAAC,OAAO,SAAS,CAClB;;;;ACPH,SAAgB,QACd,MAC6C;CAG7C,OAAO,CAFO,aAAa,KAEd,EADE,aAAa,KACP,CAAC;;;;ACLxB,SAAgB,eAAkB,UAA8D;CAC9F,MAAM,QAAQ,UAAU;CACxB,OAAO,kBAA8B,SAAS,MAAM,WAAW,CAAC,EAAE,CAAC,OAAO,SAAS,CAAC"}
|
package/dist/react/index.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import { n as useAtomSubscription, r as useAtomSubscriptionScope, t as getAtomValueResult } from "../use-atom-value-
|
|
2
|
-
import { createContext, useCallback, useContext, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
1
|
+
import { n as useAtomSubscription, r as useAtomSubscriptionScope, t as getAtomValueResult } from "../use-atom-value-KMOshe0x.js";
|
|
2
|
+
import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
//#region src/react/provider.tsx
|
|
5
5
|
const StoreContext = createContext(null);
|
|
6
|
+
const SubscriptionScopeContext = createContext(0);
|
|
6
7
|
function Provider({ store, children }) {
|
|
7
|
-
useAtomSubscriptionScope(store, {
|
|
8
|
+
const subscriptionTick = useAtomSubscriptionScope(store, {
|
|
8
9
|
useLayoutEffect,
|
|
9
10
|
useState
|
|
10
11
|
});
|
|
11
12
|
return /* @__PURE__ */ jsx(StoreContext.Provider, {
|
|
12
13
|
value: store,
|
|
13
|
-
children
|
|
14
|
+
children: /* @__PURE__ */ jsx(SubscriptionScopeContext.Provider, {
|
|
15
|
+
value: subscriptionTick,
|
|
16
|
+
children
|
|
17
|
+
})
|
|
14
18
|
});
|
|
15
19
|
}
|
|
16
20
|
function useStore() {
|
|
@@ -18,6 +22,9 @@ function useStore() {
|
|
|
18
22
|
if (store == null) throw new Error("useStore must be used within a <Provider>");
|
|
19
23
|
return store;
|
|
20
24
|
}
|
|
25
|
+
function useAtomSubscriptionTick() {
|
|
26
|
+
useContext(SubscriptionScopeContext);
|
|
27
|
+
}
|
|
21
28
|
//#endregion
|
|
22
29
|
//#region src/react/use-stable-fn.ts
|
|
23
30
|
function useStableFn(fn) {
|
|
@@ -29,12 +36,16 @@ function useStableFn(fn) {
|
|
|
29
36
|
//#region src/react/use-atom-value.ts
|
|
30
37
|
function useAtomValue(atom, opts) {
|
|
31
38
|
const store = useStore();
|
|
39
|
+
useAtomSubscriptionTick();
|
|
32
40
|
const ownerRef = useRef(null);
|
|
33
41
|
if (ownerRef.current == null) ownerRef.current = {};
|
|
34
42
|
const subscription = useAtomSubscription(store, atom, ownerRef.current);
|
|
35
43
|
const unsubscribeRef = useRef(null);
|
|
36
44
|
const releaseRef = useRef(subscription.release);
|
|
37
45
|
releaseRef.current = subscription.release;
|
|
46
|
+
useMemo(() => {
|
|
47
|
+
subscription.retain();
|
|
48
|
+
}, [subscription]);
|
|
38
49
|
const snapshot = useSyncExternalStore(useStableFn((notify) => {
|
|
39
50
|
const unsubscribe = subscription.subscribe(notify);
|
|
40
51
|
let active = true;
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/react/provider.tsx","../../src/react/use-stable-fn.ts","../../src/react/use-atom-value.ts","../../src/react/use-apply-atom.ts","../../src/react/use-mutation.ts","../../src/react/use-atom.ts","../../src/react/use-atom-context.ts"],"mappings":";;;;AAIA,MAAM,eAAe,cAA4B,KAAK;
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/react/provider.tsx","../../src/react/use-stable-fn.ts","../../src/react/use-atom-value.ts","../../src/react/use-apply-atom.ts","../../src/react/use-mutation.ts","../../src/react/use-atom.ts","../../src/react/use-atom-context.ts"],"mappings":";;;;AAIA,MAAM,eAAe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D,kBAAkB,MAAM,kBAAkB,KAAK,CAEoB,CAAC;CAE9F,sBAAsB;EACpB,aAAa;GACX,eAAe,WAAW;GAC1B,eAAe,UAAU;GACzB,WAAW,SAAS;;IAErB,EAAE,CAAC;CAKN,MAAM,mBAAmB,OAAO,KAAK;CACrC,MAAM,kBAAkB,iBAAiB;CACzC,iBAAiB,UAAU;CAE3B,OAAO,mBAAmB,OAAO,MAAM,UAAU,MAAM,YAAY,MAAM,gBAAgB;;;;AChE3F,SAAgB,aACd,MAC+B;CAC/B,MAAM,QAAQ,UAAU;CACxB,OAAO,aAAa,GAAG,SAA2B,MAAM,MAAM,MAAM,GAAG,KAAK,EAAE,CAAC,OAAO,KAAK,CAAC;;;;ACJ9F,SAAgB,YACd,UACkC;CAClC,MAAM,QAAQ,UAAU;CACxB,OAAO,aACJ,GAAG,SAA8B,MAAM,OAAO,UAAU,GAAG,KAAK,EACjE,CAAC,OAAO,SAAS,CAClB;;;;ACPH,SAAgB,QACd,MAC6C;CAG7C,OAAO,CAFO,aAAa,KAEd,EADE,aAAa,KACP,CAAC;;;;ACLxB,SAAgB,eAAkB,UAA8D;CAC9F,MAAM,QAAQ,UAAU;CACxB,OAAO,kBAA8B,SAAS,MAAM,WAAW,CAAC,EAAE,CAAC,OAAO,SAAS,CAAC"}
|
|
@@ -2,12 +2,14 @@ import { t as CONFIG } from "./types-B2mzc3A5.js";
|
|
|
2
2
|
//#region src/internal/use-atom-subscription.ts
|
|
3
3
|
const scopes = /* @__PURE__ */ new WeakMap();
|
|
4
4
|
const ownerRecords = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
const retainedRecords = /* @__PURE__ */ new WeakMap();
|
|
5
6
|
function getScope(store) {
|
|
6
7
|
let scope = scopes.get(store);
|
|
7
8
|
if (scope == null) {
|
|
8
9
|
scope = {
|
|
9
10
|
generation: 0,
|
|
10
11
|
provisionals: /* @__PURE__ */ new Set(),
|
|
12
|
+
retained: /* @__PURE__ */ new Set(),
|
|
11
13
|
byAtom: /* @__PURE__ */ new WeakMap(),
|
|
12
14
|
sweepTimer: null,
|
|
13
15
|
disposeTimer: null
|
|
@@ -19,11 +21,21 @@ function getScope(store) {
|
|
|
19
21
|
function releaseProvisional(scope, provisional) {
|
|
20
22
|
scope.provisionals.delete(provisional);
|
|
21
23
|
if (scope.byAtom.get(provisional.atom) === provisional) scope.byAtom.delete(provisional.atom);
|
|
22
|
-
for (const owner of provisional.owners)
|
|
24
|
+
for (const owner of provisional.owners) {
|
|
25
|
+
ownerRecords.delete(owner);
|
|
26
|
+
const retained = retainedRecords.get(owner);
|
|
27
|
+
if (retained != null && !retained.committed) releaseRetained(retained.scope, retained);
|
|
28
|
+
}
|
|
23
29
|
provisional.owners.clear();
|
|
24
30
|
provisional.unsubscribe();
|
|
25
31
|
}
|
|
26
|
-
function
|
|
32
|
+
function releaseRetained(scope, retained) {
|
|
33
|
+
scope.retained.delete(retained);
|
|
34
|
+
if (retainedRecords.get(retained.owner) === retained) retainedRecords.delete(retained.owner);
|
|
35
|
+
retained.listeners.clear();
|
|
36
|
+
retained.unsubscribe();
|
|
37
|
+
}
|
|
38
|
+
function releaseProvisionalOwner(owner) {
|
|
27
39
|
const record = ownerRecords.get(owner);
|
|
28
40
|
if (record == null) return;
|
|
29
41
|
ownerRecords.delete(owner);
|
|
@@ -32,14 +44,24 @@ function releaseOwner(owner) {
|
|
|
32
44
|
provisional.owners.delete(owner);
|
|
33
45
|
if (provisional.owners.size === 0) releaseProvisional(record.scope, provisional);
|
|
34
46
|
}
|
|
47
|
+
function releaseOwner(owner) {
|
|
48
|
+
releaseProvisionalOwner(owner);
|
|
49
|
+
const retained = retainedRecords.get(owner);
|
|
50
|
+
if (retained != null) releaseRetained(retained.scope, retained);
|
|
51
|
+
}
|
|
35
52
|
function sweepStaleProvisionals(scope) {
|
|
36
53
|
for (const provisional of scope.provisionals) if (provisional.generation < scope.generation) releaseProvisional(scope, provisional);
|
|
54
|
+
for (const retained of scope.retained) if (retained.generation < scope.generation) releaseRetained(scope, retained);
|
|
37
55
|
}
|
|
38
56
|
function releaseAllProvisionals(scope) {
|
|
39
57
|
for (const provisional of scope.provisionals) releaseProvisional(scope, provisional);
|
|
58
|
+
for (const retained of scope.retained) releaseRetained(scope, retained);
|
|
59
|
+
}
|
|
60
|
+
function hasUncommittedSubscriptions(scope) {
|
|
61
|
+
return scope.provisionals.size > 0 || scope.retained.size > 0;
|
|
40
62
|
}
|
|
41
63
|
function useAtomSubscriptionScope(store, hooks) {
|
|
42
|
-
const [, setTick] = hooks.useState(0);
|
|
64
|
+
const [tick, setTick] = hooks.useState(0);
|
|
43
65
|
const scope = getScope(store);
|
|
44
66
|
if (scope.disposeTimer != null) {
|
|
45
67
|
clearTimeout(scope.disposeTimer);
|
|
@@ -48,7 +70,7 @@ function useAtomSubscriptionScope(store, hooks) {
|
|
|
48
70
|
scope.generation++;
|
|
49
71
|
hooks.useLayoutEffect(() => {
|
|
50
72
|
sweepStaleProvisionals(scope);
|
|
51
|
-
if (scope
|
|
73
|
+
if (hasUncommittedSubscriptions(scope) && scope.sweepTimer == null) scope.sweepTimer = setTimeout(() => {
|
|
52
74
|
scope.sweepTimer = null;
|
|
53
75
|
setTick((tick) => tick + 1);
|
|
54
76
|
}, 50);
|
|
@@ -59,19 +81,23 @@ function useAtomSubscriptionScope(store, hooks) {
|
|
|
59
81
|
clearTimeout(scope.sweepTimer);
|
|
60
82
|
scope.sweepTimer = null;
|
|
61
83
|
}
|
|
62
|
-
if (scope
|
|
84
|
+
if (hasUncommittedSubscriptions(scope) && scope.disposeTimer == null) scope.disposeTimer = setTimeout(() => {
|
|
63
85
|
scope.disposeTimer = null;
|
|
64
86
|
releaseAllProvisionals(scope);
|
|
65
87
|
}, 50);
|
|
66
88
|
};
|
|
67
89
|
}, [scope]);
|
|
90
|
+
return tick;
|
|
68
91
|
}
|
|
69
92
|
function useAtomSubscription(store, atom, owner) {
|
|
70
93
|
const scope = getScope(store);
|
|
94
|
+
const retained = retainedRecords.get(owner);
|
|
95
|
+
if (retained != null) if (retained.scope !== scope || retained.atom !== atom) releaseRetained(retained.scope, retained);
|
|
96
|
+
else retained.generation = scope.generation;
|
|
71
97
|
const previous = ownerRecords.get(owner);
|
|
72
|
-
if (previous != null && (previous.scope !== scope || previous.atom !== atom))
|
|
98
|
+
if (previous != null && (previous.scope !== scope || previous.atom !== atom)) releaseProvisionalOwner(owner);
|
|
73
99
|
let provisional = scope.byAtom.get(atom);
|
|
74
|
-
if (provisional == null) {
|
|
100
|
+
if (retainedRecords.get(owner) == null && provisional == null) {
|
|
75
101
|
provisional = {
|
|
76
102
|
atom,
|
|
77
103
|
generation: scope.generation,
|
|
@@ -80,7 +106,7 @@ function useAtomSubscription(store, atom, owner) {
|
|
|
80
106
|
};
|
|
81
107
|
scope.byAtom.set(atom, provisional);
|
|
82
108
|
scope.provisionals.add(provisional);
|
|
83
|
-
} else {
|
|
109
|
+
} else if (retainedRecords.get(owner) == null && provisional != null) {
|
|
84
110
|
provisional.generation = scope.generation;
|
|
85
111
|
provisional.owners.add(owner);
|
|
86
112
|
}
|
|
@@ -88,11 +114,48 @@ function useAtomSubscription(store, atom, owner) {
|
|
|
88
114
|
scope,
|
|
89
115
|
atom
|
|
90
116
|
});
|
|
117
|
+
const retain = (committed, notify) => {
|
|
118
|
+
let current = retainedRecords.get(owner);
|
|
119
|
+
if (current != null && (current.scope !== scope || current.atom !== atom)) {
|
|
120
|
+
releaseRetained(current.scope, current);
|
|
121
|
+
current = void 0;
|
|
122
|
+
}
|
|
123
|
+
if (current == null) {
|
|
124
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
125
|
+
current = {
|
|
126
|
+
owner,
|
|
127
|
+
scope,
|
|
128
|
+
atom,
|
|
129
|
+
generation: scope.generation,
|
|
130
|
+
committed,
|
|
131
|
+
listeners,
|
|
132
|
+
unsubscribe: store.subscribe(atom, () => {
|
|
133
|
+
for (const listener of listeners) listener();
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
retainedRecords.set(owner, current);
|
|
137
|
+
if (committed) releaseProvisionalOwner(owner);
|
|
138
|
+
else if (ownerRecords.get(owner) == null) scope.retained.add(current);
|
|
139
|
+
} else {
|
|
140
|
+
current.generation = scope.generation;
|
|
141
|
+
current.committed ||= committed;
|
|
142
|
+
if (committed) {
|
|
143
|
+
scope.retained.delete(current);
|
|
144
|
+
releaseProvisionalOwner(owner);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (notify == null) return () => void 0;
|
|
148
|
+
current.listeners.add(notify);
|
|
149
|
+
return () => {
|
|
150
|
+
current?.listeners.delete(notify);
|
|
151
|
+
};
|
|
152
|
+
};
|
|
91
153
|
return {
|
|
154
|
+
retain: () => {
|
|
155
|
+
retain(false);
|
|
156
|
+
},
|
|
92
157
|
subscribe: (notify) => {
|
|
93
|
-
|
|
94
|
-
releaseOwner(owner);
|
|
95
|
-
return unsubscribe;
|
|
158
|
+
return retain(true, notify);
|
|
96
159
|
},
|
|
97
160
|
release: () => releaseOwner(owner)
|
|
98
161
|
};
|
|
@@ -131,4 +194,4 @@ function getAtomValueResult(store, atom, snapshot, observe, isInitialRender) {
|
|
|
131
194
|
//#endregion
|
|
132
195
|
export { useAtomSubscription as n, useAtomSubscriptionScope as r, getAtomValueResult as t };
|
|
133
196
|
|
|
134
|
-
//# sourceMappingURL=use-atom-value-
|
|
197
|
+
//# sourceMappingURL=use-atom-value-KMOshe0x.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-atom-value-KMOshe0x.js","names":[],"sources":["../src/internal/use-atom-subscription.ts","../src/internal/use-atom-value.ts"],"mappings":";;AA+CA,MAAM,yBAAS,IAAI,SAAuB;AAC1C,MAAM,+BAAe,IAAI,SAA8B;AACvD,MAAM,kCAAkB,IAAI,SAAuC;AAEnE,SAAS,SAAS,OAAqB;CACrC,IAAI,QAAQ,OAAO,IAAI,MAAM;CAC7B,IAAI,SAAS,MAAM;EACjB,QAAQ;GACN,YAAY;GACZ,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,QAAQ,UAAU,IAAI,OAAO;EAC7B,aAAa;GACX,SAAS,UAAU,OAAO,OAAO;;;CAIrC,OAAO;EACL,cAAc;GACZ,OAAO,MAAM;;EAEf,YAAY,WAAuB;GACjC,OAAO,OAAO,MAAM,OAAO;;EAE7B,eAAe,aAAa,MAAM;EACnC;;;;ACzQH,SAAgB,mBACd,OACA,MACA,UACA,SACA,iBACsB;CACtB,MAAM,cAA2B,KAAK,QAAQ,eAAe;CAE7D,IAAI,SAAS,WAAW,WACtB,MAAM,MAAM,KAAK,KAAK;CAGxB,IAAI,SAAS,WAAW,SAAS;EAC/B,IAAI,gBAAgB,WAClB,MAAM,MAAM,KAAK,KAAK;EAExB,MAAW,KAAK,KAAK;EAErB,IAAI,SACF,OAAO;GAAE,OAAO,SAAS;GAAO,SAAS;GAAM,OAAO,KAAA;GAAW;EAEnE,OAAO,SAAS;;CAGlB,IAAI,SAAS,WAAW,SAAS;EAC/B,IAAI,WAAW,SAAS,UAAU,KAAA,GAChC,OAAO;GACL,OAAO,SAAS;GAChB,SAAS;GACT,OAAO,SAAS;GACjB;EAEH,IAAI,iBACF,MAAW,KAAK,KAAK;EAEvB,MAAM,SAAS;;CAGjB,IAAI,SACF,OAAO;EAAE,OAAO,SAAS;EAAO,SAAS;EAAO,OAAO,KAAA;EAAW;CAEpE,OAAO,SAAS"}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-atom-value-Bqy1TMFt.js","names":[],"sources":["../src/internal/use-atom-subscription.ts","../src/internal/use-atom-value.ts"],"mappings":";;AAmCA,MAAM,yBAAS,IAAI,SAAuB;AAC1C,MAAM,+BAAe,IAAI,SAA8B;AAEvD,SAAS,SAAS,OAAqB;CACrC,IAAI,QAAQ,OAAO,IAAI,MAAM;CAC7B,IAAI,SAAS,MAAM;EACjB,QAAQ;GACN,YAAY;GACZ,8BAAc,IAAI,KAAK;GACvB,wBAAQ,IAAI,SAAS;GACrB,YAAY;GACZ,c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