valdres 0.2.0-pre.21 → 0.2.0-pre.23

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/dist/index.js CHANGED
@@ -1125,26 +1125,41 @@ function storeFromStoreData(data, detach) {
1125
1125
  const del = (atom) => deleteFamilyAtom(atom, data);
1126
1126
  const sub = (state, callback, deepEqualCheckBeforeCallback = true) => subscribe(state, callback, deepEqualCheckBeforeCallback, data);
1127
1127
  const txn = (callback) => transaction(callback, data);
1128
- const scope = (scopeId) => {
1129
- let scopedStoreData;
1130
- if (scopeId in data.scopes) {
1131
- scopedStoreData = data.scopes[scopeId];
1128
+ const scope = (scopeId, callback) => {
1129
+ if (callback) {
1130
+ if (!(scopeId in data.scopes)) {
1131
+ throw new Error(`Scope ${scopeId} does not exist`);
1132
+ }
1133
+ const scopedStoreData = data.scopes[scopeId];
1134
+ const scopedStore = storeFromStoreData(scopedStoreData);
1135
+ const res = callback(scopedStore);
1136
+ return res;
1132
1137
  } else {
1133
- scopedStoreData = createStoreData(scopeId, data);
1134
- data.scopes[scopeId] = scopedStoreData;
1135
- }
1136
- const detach2 = () => {
1137
- scopedStoreData.scopeConsumers.delete(detach2);
1138
- if (scopedStoreData.scopeConsumers.size === 0) {
1139
- delete data.scopes[scopeId];
1140
- return true;
1138
+ let scopedStoreData;
1139
+ if (scopeId in data.scopes) {
1140
+ scopedStoreData = data.scopes[scopeId];
1141
+ } else {
1142
+ scopedStoreData = createStoreData(scopeId, data);
1143
+ data.scopes[scopeId] = scopedStoreData;
1141
1144
  }
1142
- console.warn(`Scope ${scopeId} still has ${scopedStoreData.scopeConsumers.size} consumers, will not detach`);
1143
- return false;
1144
- };
1145
- scopedStoreData.scopeConsumers.add(detach2);
1146
- const newStore = storeFromStoreData(data.scopes[scopeId], detach2);
1147
- return newStore;
1145
+ const detach2 = (expectedToDestory = false) => {
1146
+ scopedStoreData.scopeConsumers.delete(detach2);
1147
+ if (scopedStoreData.scopeConsumers.size === 0) {
1148
+ if (expectedToDestory) {
1149
+ console.log("Deleting scope", scopeId);
1150
+ }
1151
+ delete data.scopes[scopeId];
1152
+ return true;
1153
+ }
1154
+ if (expectedToDestory) {
1155
+ console.warn(`Scope ${scopeId} still has ${scopedStoreData.scopeConsumers.size} consumers, will not detach`);
1156
+ }
1157
+ return false;
1158
+ };
1159
+ scopedStoreData.scopeConsumers.add(detach2);
1160
+ const newStore = storeFromStoreData(data.scopes[scopeId], detach2);
1161
+ return newStore;
1162
+ }
1148
1163
  };
1149
1164
  if (detach) {
1150
1165
  return {
@@ -1503,9 +1518,9 @@ var isFamilySelector = (state) => isFamilyState(state) && isSelector(state);
1503
1518
 
1504
1519
  // src/index.ts
1505
1520
  if (globalThis.__valdres__) {
1506
- throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"0.2.0-pre.21"}`);
1521
+ throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"0.2.0-pre.23"}`);
1507
1522
  } else {
1508
- globalThis.__valdres__ = "0.2.0-pre.21";
1523
+ globalThis.__valdres__ = "0.2.0-pre.23";
1509
1524
  }
1510
1525
  export {
1511
1526
  store,
@@ -10,6 +10,10 @@ type SetAtom = {
10
10
  <Value extends any>(atom: Atom<Value>, value: Value): void;
11
11
  };
12
12
  type DeleteAtom = <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(atom: AtomFamilyAtom<Value, Args>) => void;
13
+ export type ScopeFn = {
14
+ <ReturnType extends any>(scopeId: string): ScopedStore;
15
+ <ReturnType extends any>(scopeId: string, callback: (store: ScopedStore) => ReturnType): ReturnType;
16
+ };
13
17
  export type Store<T = StoreData> = {
14
18
  data: T;
15
19
  get: GetValue;
@@ -18,9 +22,9 @@ export type Store<T = StoreData> = {
18
22
  reset: ResetAtom;
19
23
  del: DeleteAtom;
20
24
  txn: (callback: TransactionFn) => void;
21
- scope: (scopeId: string) => ScopedStore;
25
+ scope: ScopeFn;
22
26
  };
23
27
  export type ScopedStore = Store<ScopedStoreData> & {
24
- detach: () => void;
28
+ detach: (warnIfNotDestroyed?: boolean) => void;
25
29
  };
26
30
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-pre.21",
3
+ "version": "0.2.0-pre.23",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"