valdres 0.2.0-alpha.44 → 0.2.0-alpha.46

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
@@ -566,11 +566,12 @@ var recursivlyResetTxnSelectorCache = (state, txnSubscribers, txnSelectorCache)
566
566
  }
567
567
  }
568
568
  };
569
- var transaction = (callback, data) => {
569
+ var transaction = (callback, data, autoCommit = true) => {
570
570
  let txnAtomMap = new Map;
571
571
  let txnSelectorCache = new Map;
572
572
  let txnSubscribers = new Map;
573
573
  let dirtySelectors = new Set;
574
+ let scopedTransactions;
574
575
  const txnGet = (state) => {
575
576
  if (isAtom(state)) {
576
577
  return txnAtomMap.has(state) ? txnAtomMap.get(state) : getState(state, data);
@@ -621,9 +622,40 @@ var transaction = (callback, data) => {
621
622
  const commit = () => {
622
623
  setAtoms(txnAtomMap, data);
623
624
  dirtySelectors.clear();
625
+ if (scopedTransactions) {
626
+ for (const scopedTxn of Object.values(scopedTransactions)) {
627
+ scopedTxn[3]();
628
+ }
629
+ }
624
630
  };
625
- const result = callback(txnSet, txnGet, txnReset, commit);
626
- commit();
631
+ const result = callback({
632
+ set: txnSet,
633
+ get: txnGet,
634
+ reset: txnReset,
635
+ commit,
636
+ scope: (scopeId, callback2) => {
637
+ if (scopeId in data.scopes) {
638
+ const scopedData = data.scopes[scopeId];
639
+ if (scopedTransactions?.[scopeId] === undefined) {
640
+ scopedTransactions ||= {};
641
+ transaction(({ set, get, reset, commit: commit2, scope }) => {
642
+ scopedTransactions[scopeId] = [
643
+ set,
644
+ get,
645
+ reset,
646
+ commit2,
647
+ scope
648
+ ];
649
+ }, scopedData, false);
650
+ }
651
+ callback2(...scopedTransactions[scopeId]);
652
+ } else {
653
+ throw new Error("Scope not found");
654
+ }
655
+ }
656
+ });
657
+ if (autoCommit)
658
+ commit();
627
659
  return result;
628
660
  };
629
661
 
@@ -695,7 +727,7 @@ var store = (id) => {
695
727
  return storeFromStoreData(data);
696
728
  };
697
729
  // package.json
698
- var version = "0.2.0-alpha.43";
730
+ var version = "0.2.0-alpha.45";
699
731
 
700
732
  // src/globalStore.ts
701
733
  if (globalThis.__valdres__) {
@@ -27,3 +27,5 @@ export type { SetAtomValue } from "./src/types/SetAtomValue";
27
27
  export type { State } from "./src/types/State";
28
28
  export type { Store } from "./src/types/Store";
29
29
  export type { StoreData } from "./src/types/StoreData";
30
+ export type { TransactionFn } from "./src/types/TransactionFn";
31
+ export type { TransactionInterface } from "./src/types/TransactionInterface";
@@ -1,9 +1,3 @@
1
- import type { State } from "../types/State";
2
1
  import type { StoreData } from "../types/StoreData";
3
- import type { Atom } from "../types/Atom";
4
- type GetValdresValue = <V>(state: State<V>) => V;
5
- type SetValdresValue = <V>(state: State<V>, value: V) => void;
6
- type ResetValdresValue = <V>(atom: Atom<V>) => V;
7
- type TransactionInterface = (set: SetValdresValue, get: GetValdresValue, reset: ResetValdresValue, commit: () => void) => void;
8
- export declare const transaction: (callback: TransactionInterface, data: StoreData) => void;
9
- export {};
2
+ import type { TransactionFn } from "../types/TransactionFn";
3
+ export declare const transaction: (callback: TransactionFn, data: StoreData, autoCommit?: boolean) => void;
@@ -1,4 +1,2 @@
1
- import type { GetValue } from "./GetValue";
2
- import type { ResetAtom } from "./ResetAtom";
3
- import type { SetAtom } from "./SetAtom";
4
- export type TransactionFn = (set: SetAtom, get: GetValue, reset: ResetAtom, commit: () => void) => void;
1
+ import type { TransactionInterface } from "./TransactionInterface";
2
+ export type TransactionFn = (args: TransactionInterface) => void;
@@ -0,0 +1,11 @@
1
+ import type { GetValue } from "./GetValue";
2
+ import type { ResetAtom } from "./ResetAtom";
3
+ import type { SetAtom } from "./SetAtom";
4
+ import type { TransactionFn } from "./TransactionFn";
5
+ export type TransactionInterface = {
6
+ set: SetAtom;
7
+ get: GetValue;
8
+ reset: ResetAtom;
9
+ commit: () => void;
10
+ scope: (scopeId: string, callback: TransactionFn) => void;
11
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.44",
3
+ "version": "0.2.0-alpha.46",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
@@ -36,5 +36,5 @@
36
36
  "access": "public",
37
37
  "registry": "https://registry.npmjs.org/"
38
38
  },
39
- "gitHead": "1b5b205b6b1d2783a2e8b41932199a71da540901"
39
+ "gitHead": "ffb8cb2b276f5f8ff00a7f922776218a6697ea26"
40
40
  }