@doeixd/machine 1.0.2 → 1.1.0
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 +48 -0
- package/dist/cjs/development/core.js +11 -9
- package/dist/cjs/development/core.js.map +3 -3
- package/dist/cjs/development/delegate.js +89 -0
- package/dist/cjs/development/delegate.js.map +7 -0
- package/dist/cjs/development/index.js +385 -167
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/development/minimal.js +163 -0
- package/dist/cjs/development/minimal.js.map +7 -0
- package/dist/cjs/development/react.js +11 -9
- package/dist/cjs/development/react.js.map +3 -3
- package/dist/cjs/production/core.js +1 -1
- package/dist/cjs/production/delegate.js +1 -0
- package/dist/cjs/production/index.js +3 -3
- package/dist/cjs/production/minimal.js +1 -0
- package/dist/cjs/production/react.js +1 -1
- package/dist/esm/development/core.js +11 -9
- package/dist/esm/development/core.js.map +3 -3
- package/dist/esm/development/delegate.js +68 -0
- package/dist/esm/development/delegate.js.map +7 -0
- package/dist/esm/development/index.js +391 -167
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/development/minimal.js +140 -0
- package/dist/esm/development/minimal.js.map +7 -0
- package/dist/esm/development/react.js +11 -9
- package/dist/esm/development/react.js.map +3 -3
- package/dist/esm/production/core.js +1 -1
- package/dist/esm/production/delegate.js +1 -0
- package/dist/esm/production/index.js +3 -3
- package/dist/esm/production/minimal.js +1 -0
- package/dist/esm/production/react.js +1 -1
- package/dist/types/base.d.ts +56 -0
- package/dist/types/base.d.ts.map +1 -0
- package/dist/types/delegate.d.ts +101 -0
- package/dist/types/delegate.d.ts.map +1 -0
- package/dist/types/higher-order.d.ts +2 -1
- package/dist/types/higher-order.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -49
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/minimal.d.ts +95 -0
- package/dist/types/minimal.d.ts.map +1 -0
- package/dist/types/types.d.ts +63 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +25 -1
- package/src/base.ts +62 -0
- package/src/delegate.ts +267 -0
- package/src/higher-order.ts +2 -2
- package/src/index.ts +15 -55
- package/src/middleware.ts +1049 -1050
- package/src/minimal.ts +269 -0
- package/src/types.ts +85 -0
|
@@ -1,18 +1,24 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
1
7
|
// src/internal-transitions.ts
|
|
2
8
|
var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
|
|
3
|
-
function attachTransitions(
|
|
4
|
-
Object.defineProperty(
|
|
9
|
+
function attachTransitions(machine2, transitions) {
|
|
10
|
+
Object.defineProperty(machine2, TRANSITIONS_SYMBOL, {
|
|
5
11
|
value: transitions,
|
|
6
12
|
enumerable: false,
|
|
7
13
|
configurable: false
|
|
8
14
|
});
|
|
9
|
-
return
|
|
15
|
+
return machine2;
|
|
10
16
|
}
|
|
11
|
-
function getStoredTransitions(
|
|
12
|
-
if (!
|
|
17
|
+
function getStoredTransitions(machine2) {
|
|
18
|
+
if (!machine2 || typeof machine2 !== "object") {
|
|
13
19
|
return void 0;
|
|
14
20
|
}
|
|
15
|
-
return
|
|
21
|
+
return machine2[TRANSITIONS_SYMBOL];
|
|
16
22
|
}
|
|
17
23
|
function snapshotOwnTransitions(source) {
|
|
18
24
|
if (!source || typeof source !== "object") {
|
|
@@ -24,6 +30,17 @@ function snapshotOwnTransitions(source) {
|
|
|
24
30
|
return Object.fromEntries(entries);
|
|
25
31
|
}
|
|
26
32
|
|
|
33
|
+
// src/base.ts
|
|
34
|
+
var MachineBase = class {
|
|
35
|
+
/**
|
|
36
|
+
* Initializes a new machine instance with its starting context.
|
|
37
|
+
* @param context - The initial state of the machine.
|
|
38
|
+
*/
|
|
39
|
+
constructor(context) {
|
|
40
|
+
this.context = context;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
27
44
|
// src/generators.ts
|
|
28
45
|
function run(flow, initial) {
|
|
29
46
|
const generator = flow(initial);
|
|
@@ -46,8 +63,8 @@ function yieldMachine(m) {
|
|
|
46
63
|
return m;
|
|
47
64
|
}
|
|
48
65
|
function runSequence(initial, flows) {
|
|
49
|
-
return flows.reduce((
|
|
50
|
-
return run(flow,
|
|
66
|
+
return flows.reduce((machine2, flow) => {
|
|
67
|
+
return run(flow, machine2);
|
|
51
68
|
}, initial);
|
|
52
69
|
}
|
|
53
70
|
function createFlow(flow) {
|
|
@@ -302,13 +319,13 @@ function createEnsemble(store, factories, getDiscriminant) {
|
|
|
302
319
|
const getCurrentMachine = () => {
|
|
303
320
|
const context = store.getContext();
|
|
304
321
|
const currentStateName = getDiscriminant(context);
|
|
305
|
-
const
|
|
306
|
-
if (!
|
|
322
|
+
const factory2 = factories[currentStateName];
|
|
323
|
+
if (!factory2) {
|
|
307
324
|
throw new Error(
|
|
308
325
|
`[Ensemble] Invalid state: No factory found for state "${String(currentStateName)}".`
|
|
309
326
|
);
|
|
310
327
|
}
|
|
311
|
-
return
|
|
328
|
+
return factory2(context);
|
|
312
329
|
};
|
|
313
330
|
const actions = new Proxy({}, {
|
|
314
331
|
get(_target, prop) {
|
|
@@ -454,13 +471,13 @@ function createMultiMachine(MachineClass, store) {
|
|
|
454
471
|
function createMutableMachine(sharedContext, factories, getDiscriminant) {
|
|
455
472
|
const getCurrentMachine = () => {
|
|
456
473
|
const currentStateName = getDiscriminant(sharedContext);
|
|
457
|
-
const
|
|
458
|
-
if (!
|
|
474
|
+
const factory2 = factories[currentStateName];
|
|
475
|
+
if (!factory2) {
|
|
459
476
|
throw new Error(
|
|
460
477
|
`[MutableMachine] Invalid state: No factory for state "${String(currentStateName)}".`
|
|
461
478
|
);
|
|
462
479
|
}
|
|
463
|
-
return
|
|
480
|
+
return factory2(sharedContext);
|
|
464
481
|
};
|
|
465
482
|
return new Proxy(sharedContext, {
|
|
466
483
|
get(target, prop, _receiver) {
|
|
@@ -612,18 +629,18 @@ function createParallelMachine(m1, m2) {
|
|
|
612
629
|
|
|
613
630
|
// src/middleware/core.ts
|
|
614
631
|
var CANCEL = Symbol("CANCEL");
|
|
615
|
-
function createMiddleware(
|
|
632
|
+
function createMiddleware(machine2, hooks, options = {}) {
|
|
616
633
|
const { continueOnError = false, logErrors = true, onError } = options;
|
|
617
|
-
const wrappedMachine = { ...
|
|
618
|
-
for (const prop in
|
|
619
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
620
|
-
if (prop !== "context" && typeof
|
|
621
|
-
wrappedMachine[prop] =
|
|
634
|
+
const wrappedMachine = { ...machine2 };
|
|
635
|
+
for (const prop in machine2) {
|
|
636
|
+
if (!Object.prototype.hasOwnProperty.call(machine2, prop)) continue;
|
|
637
|
+
if (prop !== "context" && typeof machine2[prop] !== "function") {
|
|
638
|
+
wrappedMachine[prop] = machine2[prop];
|
|
622
639
|
}
|
|
623
640
|
}
|
|
624
|
-
for (const prop in
|
|
625
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
626
|
-
const value =
|
|
641
|
+
for (const prop in machine2) {
|
|
642
|
+
if (!Object.prototype.hasOwnProperty.call(machine2, prop)) continue;
|
|
643
|
+
const value = machine2[prop];
|
|
627
644
|
if (typeof value === "function" && prop !== "context") {
|
|
628
645
|
wrappedMachine[prop] = function(...args) {
|
|
629
646
|
const transitionName = prop;
|
|
@@ -649,23 +666,23 @@ function createMiddleware(machine, hooks, options = {}) {
|
|
|
649
666
|
}
|
|
650
667
|
throw error;
|
|
651
668
|
}
|
|
652
|
-
const ensureMiddlewareProperties = (
|
|
653
|
-
if (
|
|
669
|
+
const ensureMiddlewareProperties = (machine3) => {
|
|
670
|
+
if (machine3 && typeof machine3 === "object" && machine3.context) {
|
|
654
671
|
for (const prop2 in wrappedMachine) {
|
|
655
672
|
if (!Object.prototype.hasOwnProperty.call(wrappedMachine, prop2)) continue;
|
|
656
|
-
if (prop2 !== "context" && !(prop2 in
|
|
657
|
-
|
|
673
|
+
if (prop2 !== "context" && !(prop2 in machine3)) {
|
|
674
|
+
machine3[prop2] = wrappedMachine[prop2];
|
|
658
675
|
}
|
|
659
676
|
}
|
|
660
|
-
for (const prop2 in
|
|
661
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
662
|
-
const value2 =
|
|
677
|
+
for (const prop2 in machine3) {
|
|
678
|
+
if (!Object.prototype.hasOwnProperty.call(machine3, prop2)) continue;
|
|
679
|
+
const value2 = machine3[prop2];
|
|
663
680
|
if (typeof value2 === "function" && prop2 !== "context" && wrappedMachine[prop2]) {
|
|
664
|
-
|
|
681
|
+
machine3[prop2] = wrappedMachine[prop2];
|
|
665
682
|
}
|
|
666
683
|
}
|
|
667
684
|
}
|
|
668
|
-
return
|
|
685
|
+
return machine3;
|
|
669
686
|
};
|
|
670
687
|
if (nextMachine && typeof nextMachine.then === "function") {
|
|
671
688
|
const asyncResult = nextMachine.then((resolvedMachine) => {
|
|
@@ -768,9 +785,9 @@ function createMiddleware(machine, hooks, options = {}) {
|
|
|
768
785
|
}
|
|
769
786
|
return wrappedMachine;
|
|
770
787
|
}
|
|
771
|
-
function withLogging(
|
|
788
|
+
function withLogging(machine2, options = {}) {
|
|
772
789
|
const { logger = console.log, includeArgs = false, includeContext = true } = options;
|
|
773
|
-
return createMiddleware(
|
|
790
|
+
return createMiddleware(machine2, {
|
|
774
791
|
before: ({ transitionName, args }) => {
|
|
775
792
|
const message = includeArgs ? `→ ${transitionName} [${args.join(", ")}]` : `→ ${transitionName}`;
|
|
776
793
|
logger(message);
|
|
@@ -784,9 +801,9 @@ function withLogging(machine, options = {}) {
|
|
|
784
801
|
}
|
|
785
802
|
});
|
|
786
803
|
}
|
|
787
|
-
function withAnalytics(
|
|
804
|
+
function withAnalytics(machine2, track, options = {}) {
|
|
788
805
|
const { eventPrefix = "state_transition", includePrevContext = false, includeArgs = false } = options;
|
|
789
|
-
return createMiddleware(
|
|
806
|
+
return createMiddleware(machine2, {
|
|
790
807
|
after: ({ transitionName, prevContext, nextContext, args }) => {
|
|
791
808
|
const event = `${eventPrefix}.${transitionName}`;
|
|
792
809
|
const data = { transition: transitionName };
|
|
@@ -797,8 +814,8 @@ function withAnalytics(machine, track, options = {}) {
|
|
|
797
814
|
}
|
|
798
815
|
});
|
|
799
816
|
}
|
|
800
|
-
function withValidation(
|
|
801
|
-
return createMiddleware(
|
|
817
|
+
function withValidation(machine2, validator) {
|
|
818
|
+
return createMiddleware(machine2, {
|
|
802
819
|
before: (ctx) => {
|
|
803
820
|
const result = validator(ctx);
|
|
804
821
|
if (result === false) {
|
|
@@ -807,8 +824,8 @@ function withValidation(machine, validator) {
|
|
|
807
824
|
}
|
|
808
825
|
});
|
|
809
826
|
}
|
|
810
|
-
function withPermissions(
|
|
811
|
-
return createMiddleware(
|
|
827
|
+
function withPermissions(machine2, checker) {
|
|
828
|
+
return createMiddleware(machine2, {
|
|
812
829
|
before: (ctx) => {
|
|
813
830
|
if (!checker(ctx)) {
|
|
814
831
|
throw new Error(`Unauthorized transition: ${ctx.transitionName}`);
|
|
@@ -816,9 +833,9 @@ function withPermissions(machine, checker) {
|
|
|
816
833
|
}
|
|
817
834
|
});
|
|
818
835
|
}
|
|
819
|
-
function withErrorReporting(
|
|
836
|
+
function withErrorReporting(machine2, reporter, options = {}) {
|
|
820
837
|
const { includeArgs = false } = options;
|
|
821
|
-
return createMiddleware(
|
|
838
|
+
return createMiddleware(machine2, {
|
|
822
839
|
error: (errorCtx) => {
|
|
823
840
|
const formattedCtx = {
|
|
824
841
|
transition: errorCtx.transitionName,
|
|
@@ -829,9 +846,9 @@ function withErrorReporting(machine, reporter, options = {}) {
|
|
|
829
846
|
}
|
|
830
847
|
});
|
|
831
848
|
}
|
|
832
|
-
function withPerformanceMonitoring(
|
|
849
|
+
function withPerformanceMonitoring(machine2, tracker) {
|
|
833
850
|
const startTimes = /* @__PURE__ */ new Map();
|
|
834
|
-
return createMiddleware(
|
|
851
|
+
return createMiddleware(machine2, {
|
|
835
852
|
before: (ctx) => {
|
|
836
853
|
startTimes.set(ctx.transitionName, Date.now());
|
|
837
854
|
},
|
|
@@ -850,7 +867,7 @@ function withPerformanceMonitoring(machine, tracker) {
|
|
|
850
867
|
}
|
|
851
868
|
});
|
|
852
869
|
}
|
|
853
|
-
function withRetry(
|
|
870
|
+
function withRetry(machine2, options = {}) {
|
|
854
871
|
var _a, _b;
|
|
855
872
|
const {
|
|
856
873
|
maxAttempts = (_a = options.maxRetries) != null ? _a : 3,
|
|
@@ -859,10 +876,10 @@ function withRetry(machine, options = {}) {
|
|
|
859
876
|
backoffMultiplier = 2,
|
|
860
877
|
onRetry
|
|
861
878
|
} = options;
|
|
862
|
-
const wrappedMachine = { ...
|
|
863
|
-
for (const prop in
|
|
864
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
865
|
-
const value =
|
|
879
|
+
const wrappedMachine = { ...machine2 };
|
|
880
|
+
for (const prop in machine2) {
|
|
881
|
+
if (!Object.prototype.hasOwnProperty.call(machine2, prop)) continue;
|
|
882
|
+
const value = machine2[prop];
|
|
866
883
|
if (typeof value === "function" && prop !== "context") {
|
|
867
884
|
wrappedMachine[prop] = async function(...args) {
|
|
868
885
|
let lastError;
|
|
@@ -890,15 +907,15 @@ function withRetry(machine, options = {}) {
|
|
|
890
907
|
return wrappedMachine;
|
|
891
908
|
}
|
|
892
909
|
function createCustomMiddleware(hooks, options) {
|
|
893
|
-
return (
|
|
910
|
+
return (machine2) => createMiddleware(machine2, hooks, options);
|
|
894
911
|
}
|
|
895
912
|
|
|
896
913
|
// src/middleware/history.ts
|
|
897
|
-
function withHistory(
|
|
914
|
+
function withHistory(machine2, options = {}) {
|
|
898
915
|
const { maxSize, serializer, onEntry } = options;
|
|
899
916
|
const history = [];
|
|
900
917
|
let entryId = 0;
|
|
901
|
-
const instrumentedMachine = createMiddleware(
|
|
918
|
+
const instrumentedMachine = createMiddleware(machine2, {
|
|
902
919
|
before: ({ transitionName, args }) => {
|
|
903
920
|
const entry = {
|
|
904
921
|
id: `entry-${entryId++}`,
|
|
@@ -930,7 +947,7 @@ function withHistory(machine, options = {}) {
|
|
|
930
947
|
}
|
|
931
948
|
|
|
932
949
|
// src/middleware/snapshot.ts
|
|
933
|
-
function withSnapshot(
|
|
950
|
+
function withSnapshot(machine2, options = {}) {
|
|
934
951
|
const {
|
|
935
952
|
maxSize,
|
|
936
953
|
serializer,
|
|
@@ -939,7 +956,7 @@ function withSnapshot(machine, options = {}) {
|
|
|
939
956
|
} = options;
|
|
940
957
|
const snapshots = [];
|
|
941
958
|
let snapshotId = 0;
|
|
942
|
-
const instrumentedMachine = createMiddleware(
|
|
959
|
+
const instrumentedMachine = createMiddleware(machine2, {
|
|
943
960
|
after: ({ transitionName, prevContext, nextContext }) => {
|
|
944
961
|
if (onlyOnChange && JSON.stringify(prevContext) === JSON.stringify(nextContext)) {
|
|
945
962
|
return;
|
|
@@ -974,8 +991,8 @@ function withSnapshot(machine, options = {}) {
|
|
|
974
991
|
});
|
|
975
992
|
const restoreSnapshot = (context) => {
|
|
976
993
|
const transitions = Object.fromEntries(
|
|
977
|
-
Object.entries(
|
|
978
|
-
([key]) => key !== "context" && key !== "snapshots" && key !== "clearSnapshots" && key !== "restoreSnapshot" && typeof
|
|
994
|
+
Object.entries(machine2).filter(
|
|
995
|
+
([key]) => key !== "context" && key !== "snapshots" && key !== "clearSnapshots" && key !== "restoreSnapshot" && typeof machine2[key] === "function"
|
|
979
996
|
)
|
|
980
997
|
);
|
|
981
998
|
return Object.assign({ context }, transitions);
|
|
@@ -991,13 +1008,13 @@ function withSnapshot(machine, options = {}) {
|
|
|
991
1008
|
}
|
|
992
1009
|
|
|
993
1010
|
// src/middleware/time-travel.ts
|
|
994
|
-
function withTimeTravel(
|
|
1011
|
+
function withTimeTravel(machine2, options = {}) {
|
|
995
1012
|
const { maxSize, serializer, onRecord } = options;
|
|
996
1013
|
const history = [];
|
|
997
1014
|
const snapshots = [];
|
|
998
1015
|
let historyId = 0;
|
|
999
1016
|
let snapshotId = 0;
|
|
1000
|
-
const instrumentedMachine = createMiddleware(
|
|
1017
|
+
const instrumentedMachine = createMiddleware(machine2, {
|
|
1001
1018
|
before: ({ transitionName, args }) => {
|
|
1002
1019
|
const entry = {
|
|
1003
1020
|
id: `entry-${historyId++}`,
|
|
@@ -1043,8 +1060,8 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1043
1060
|
});
|
|
1044
1061
|
const restoreSnapshot = (context) => {
|
|
1045
1062
|
const transitions = Object.fromEntries(
|
|
1046
|
-
Object.entries(
|
|
1047
|
-
([key]) => key !== "context" && key !== "history" && key !== "snapshots" && key !== "clearHistory" && key !== "clearSnapshots" && key !== "restoreSnapshot" && key !== "clearTimeTravel" && key !== "replayFrom" && typeof
|
|
1063
|
+
Object.entries(machine2).filter(
|
|
1064
|
+
([key]) => key !== "context" && key !== "history" && key !== "snapshots" && key !== "clearHistory" && key !== "clearSnapshots" && key !== "restoreSnapshot" && key !== "clearTimeTravel" && key !== "replayFrom" && typeof machine2[key] === "function"
|
|
1048
1065
|
)
|
|
1049
1066
|
);
|
|
1050
1067
|
return Object.assign({ context }, transitions);
|
|
@@ -1062,8 +1079,8 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1062
1079
|
const freshMachine = Object.assign(
|
|
1063
1080
|
{ context: currentContext },
|
|
1064
1081
|
Object.fromEntries(
|
|
1065
|
-
Object.entries(
|
|
1066
|
-
([key]) => key !== "context" && typeof
|
|
1082
|
+
Object.entries(machine2).filter(
|
|
1083
|
+
([key]) => key !== "context" && typeof machine2[key] === "function"
|
|
1067
1084
|
)
|
|
1068
1085
|
)
|
|
1069
1086
|
);
|
|
@@ -1099,15 +1116,15 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1099
1116
|
}
|
|
1100
1117
|
|
|
1101
1118
|
// src/middleware/composition.ts
|
|
1102
|
-
function compose(
|
|
1103
|
-
return middlewares.reduce((acc, middleware) => middleware(acc),
|
|
1119
|
+
function compose(machine2, ...middlewares) {
|
|
1120
|
+
return middlewares.reduce((acc, middleware) => middleware(acc), machine2);
|
|
1104
1121
|
}
|
|
1105
|
-
function composeTyped(
|
|
1106
|
-
return middlewares.reduce((acc, middleware) => middleware(acc),
|
|
1122
|
+
function composeTyped(machine2, ...middlewares) {
|
|
1123
|
+
return middlewares.reduce((acc, middleware) => middleware(acc), machine2);
|
|
1107
1124
|
}
|
|
1108
1125
|
var MiddlewareChainBuilder = class _MiddlewareChainBuilder {
|
|
1109
|
-
constructor(
|
|
1110
|
-
this.machine =
|
|
1126
|
+
constructor(machine2) {
|
|
1127
|
+
this.machine = machine2;
|
|
1111
1128
|
}
|
|
1112
1129
|
/**
|
|
1113
1130
|
* Add a middleware to the composition chain.
|
|
@@ -1125,12 +1142,12 @@ var MiddlewareChainBuilder = class _MiddlewareChainBuilder {
|
|
|
1125
1142
|
return this.machine;
|
|
1126
1143
|
}
|
|
1127
1144
|
};
|
|
1128
|
-
function chain(
|
|
1129
|
-
return new MiddlewareChainBuilder(
|
|
1145
|
+
function chain(machine2) {
|
|
1146
|
+
return new MiddlewareChainBuilder(machine2);
|
|
1130
1147
|
}
|
|
1131
1148
|
function when(middleware, predicate) {
|
|
1132
|
-
const conditional = function(
|
|
1133
|
-
return predicate(
|
|
1149
|
+
const conditional = function(machine2) {
|
|
1150
|
+
return predicate(machine2) ? middleware(machine2) : machine2;
|
|
1134
1151
|
};
|
|
1135
1152
|
conditional.middleware = middleware;
|
|
1136
1153
|
conditional.when = predicate;
|
|
@@ -1142,7 +1159,7 @@ function inDevelopment(middleware) {
|
|
|
1142
1159
|
});
|
|
1143
1160
|
}
|
|
1144
1161
|
function whenContext(key, value, middleware) {
|
|
1145
|
-
return when(middleware, (
|
|
1162
|
+
return when(middleware, (machine2) => machine2.context[key] === value);
|
|
1146
1163
|
}
|
|
1147
1164
|
function createMiddlewareRegistry() {
|
|
1148
1165
|
const registry = /* @__PURE__ */ new Map();
|
|
@@ -1188,7 +1205,7 @@ function createMiddlewareRegistry() {
|
|
|
1188
1205
|
* Apply a selection of registered middlewares to a machine.
|
|
1189
1206
|
* Middlewares are applied in priority order (lowest to highest).
|
|
1190
1207
|
*/
|
|
1191
|
-
apply(
|
|
1208
|
+
apply(machine2, middlewareNames) {
|
|
1192
1209
|
const middlewares = middlewareNames.map((name) => {
|
|
1193
1210
|
const entry = registry.get(name);
|
|
1194
1211
|
if (!entry) {
|
|
@@ -1199,14 +1216,14 @@ function createMiddlewareRegistry() {
|
|
|
1199
1216
|
var _a, _b;
|
|
1200
1217
|
return ((_a = a.priority) != null ? _a : 0) - ((_b = b.priority) != null ? _b : 0);
|
|
1201
1218
|
});
|
|
1202
|
-
return composeTyped(
|
|
1219
|
+
return composeTyped(machine2, ...middlewares.map((m) => m.middleware));
|
|
1203
1220
|
},
|
|
1204
1221
|
/**
|
|
1205
1222
|
* Apply all registered middlewares to a machine in priority order.
|
|
1206
1223
|
*/
|
|
1207
|
-
applyAll(
|
|
1224
|
+
applyAll(machine2) {
|
|
1208
1225
|
const middlewares = this.list();
|
|
1209
|
-
return composeTyped(
|
|
1226
|
+
return composeTyped(machine2, ...middlewares.map((m) => m.middleware));
|
|
1210
1227
|
}
|
|
1211
1228
|
};
|
|
1212
1229
|
}
|
|
@@ -1216,8 +1233,8 @@ function createPipeline(config = {}) {
|
|
|
1216
1233
|
logErrors = true,
|
|
1217
1234
|
onError
|
|
1218
1235
|
} = config;
|
|
1219
|
-
return (
|
|
1220
|
-
let currentMachine =
|
|
1236
|
+
return (machine2, ...middlewares) => {
|
|
1237
|
+
let currentMachine = machine2;
|
|
1221
1238
|
const errors = [];
|
|
1222
1239
|
let success = true;
|
|
1223
1240
|
for (let i = 0; i < middlewares.length; i++) {
|
|
@@ -1251,16 +1268,16 @@ function createPipeline(config = {}) {
|
|
|
1251
1268
|
};
|
|
1252
1269
|
}
|
|
1253
1270
|
function combine(...middlewares) {
|
|
1254
|
-
return (
|
|
1271
|
+
return (machine2) => composeTyped(machine2, ...middlewares);
|
|
1255
1272
|
}
|
|
1256
1273
|
function branch(branches, fallback) {
|
|
1257
|
-
return (
|
|
1274
|
+
return (machine2) => {
|
|
1258
1275
|
for (const [predicate, middleware] of branches) {
|
|
1259
|
-
if (predicate(
|
|
1260
|
-
return middleware(
|
|
1276
|
+
if (predicate(machine2)) {
|
|
1277
|
+
return middleware(machine2);
|
|
1261
1278
|
}
|
|
1262
1279
|
}
|
|
1263
|
-
return fallback ? fallback(
|
|
1280
|
+
return fallback ? fallback(machine2) : machine2;
|
|
1264
1281
|
};
|
|
1265
1282
|
}
|
|
1266
1283
|
function isMiddlewareFn(value) {
|
|
@@ -1305,85 +1322,85 @@ function isPipelineConfig(value) {
|
|
|
1305
1322
|
return value === void 0 || value !== null && typeof value === "object" && ("continueOnError" in value ? typeof value.continueOnError === "boolean" : true) && ("logErrors" in value ? typeof value.logErrors === "boolean" : true) && ("onError" in value ? typeof value.onError === "function" || value.onError === void 0 : true);
|
|
1306
1323
|
}
|
|
1307
1324
|
var MiddlewareBuilder = class {
|
|
1308
|
-
constructor(
|
|
1309
|
-
this.machine =
|
|
1325
|
+
constructor(machine2) {
|
|
1326
|
+
this.machine = machine2;
|
|
1310
1327
|
this.middlewares = [];
|
|
1311
1328
|
}
|
|
1312
1329
|
/**
|
|
1313
1330
|
* Add logging middleware with type-safe configuration.
|
|
1314
1331
|
*/
|
|
1315
1332
|
withLogging(options) {
|
|
1316
|
-
this.middlewares.push((
|
|
1333
|
+
this.middlewares.push((machine2) => withLogging(machine2, options));
|
|
1317
1334
|
return this;
|
|
1318
1335
|
}
|
|
1319
1336
|
/**
|
|
1320
1337
|
* Add analytics middleware with type-safe configuration.
|
|
1321
1338
|
*/
|
|
1322
1339
|
withAnalytics(track, options) {
|
|
1323
|
-
this.middlewares.push((
|
|
1340
|
+
this.middlewares.push((machine2) => withAnalytics(machine2, track, options));
|
|
1324
1341
|
return this;
|
|
1325
1342
|
}
|
|
1326
1343
|
/**
|
|
1327
1344
|
* Add validation middleware with type-safe configuration.
|
|
1328
1345
|
*/
|
|
1329
1346
|
withValidation(validator, _options) {
|
|
1330
|
-
this.middlewares.push((
|
|
1347
|
+
this.middlewares.push((machine2) => withValidation(machine2, validator));
|
|
1331
1348
|
return this;
|
|
1332
1349
|
}
|
|
1333
1350
|
/**
|
|
1334
1351
|
* Add permission checking middleware with type-safe configuration.
|
|
1335
1352
|
*/
|
|
1336
1353
|
withPermissions(checker) {
|
|
1337
|
-
this.middlewares.push((
|
|
1354
|
+
this.middlewares.push((machine2) => withPermissions(machine2, checker));
|
|
1338
1355
|
return this;
|
|
1339
1356
|
}
|
|
1340
1357
|
/**
|
|
1341
1358
|
* Add error reporting middleware with type-safe configuration.
|
|
1342
1359
|
*/
|
|
1343
1360
|
withErrorReporting(reporter, options) {
|
|
1344
|
-
this.middlewares.push((
|
|
1361
|
+
this.middlewares.push((machine2) => withErrorReporting(machine2, reporter, options));
|
|
1345
1362
|
return this;
|
|
1346
1363
|
}
|
|
1347
1364
|
/**
|
|
1348
1365
|
* Add performance monitoring middleware with type-safe configuration.
|
|
1349
1366
|
*/
|
|
1350
1367
|
withPerformanceMonitoring(tracker, _options) {
|
|
1351
|
-
this.middlewares.push((
|
|
1368
|
+
this.middlewares.push((machine2) => withPerformanceMonitoring(machine2, tracker));
|
|
1352
1369
|
return this;
|
|
1353
1370
|
}
|
|
1354
1371
|
/**
|
|
1355
1372
|
* Add retry middleware with type-safe configuration.
|
|
1356
1373
|
*/
|
|
1357
1374
|
withRetry(options) {
|
|
1358
|
-
this.middlewares.push((
|
|
1375
|
+
this.middlewares.push((machine2) => withRetry(machine2, options));
|
|
1359
1376
|
return this;
|
|
1360
1377
|
}
|
|
1361
1378
|
/**
|
|
1362
1379
|
* Add history tracking middleware with type-safe configuration.
|
|
1363
1380
|
*/
|
|
1364
1381
|
withHistory(options) {
|
|
1365
|
-
this.middlewares.push((
|
|
1382
|
+
this.middlewares.push((machine2) => withHistory(machine2, options));
|
|
1366
1383
|
return this;
|
|
1367
1384
|
}
|
|
1368
1385
|
/**
|
|
1369
1386
|
* Add snapshot tracking middleware with type-safe configuration.
|
|
1370
1387
|
*/
|
|
1371
1388
|
withSnapshot(options) {
|
|
1372
|
-
this.middlewares.push((
|
|
1389
|
+
this.middlewares.push((machine2) => withSnapshot(machine2, options));
|
|
1373
1390
|
return this;
|
|
1374
1391
|
}
|
|
1375
1392
|
/**
|
|
1376
1393
|
* Add time travel middleware with type-safe configuration.
|
|
1377
1394
|
*/
|
|
1378
1395
|
withTimeTravel(options) {
|
|
1379
|
-
this.middlewares.push((
|
|
1396
|
+
this.middlewares.push((machine2) => withTimeTravel(machine2, options));
|
|
1380
1397
|
return this;
|
|
1381
1398
|
}
|
|
1382
1399
|
/**
|
|
1383
1400
|
* Add debugging middleware (combination of history, snapshot, and time travel).
|
|
1384
1401
|
*/
|
|
1385
1402
|
withDebugging() {
|
|
1386
|
-
this.middlewares.push((
|
|
1403
|
+
this.middlewares.push((machine2) => withDebugging(machine2));
|
|
1387
1404
|
return this;
|
|
1388
1405
|
}
|
|
1389
1406
|
/**
|
|
@@ -1424,13 +1441,13 @@ var MiddlewareBuilder = class {
|
|
|
1424
1441
|
return this;
|
|
1425
1442
|
}
|
|
1426
1443
|
};
|
|
1427
|
-
function middlewareBuilder(
|
|
1428
|
-
return new MiddlewareBuilder(
|
|
1444
|
+
function middlewareBuilder(machine2) {
|
|
1445
|
+
return new MiddlewareBuilder(machine2);
|
|
1429
1446
|
}
|
|
1430
1447
|
function createMiddlewareFactory(defaultOptions = {}) {
|
|
1431
1448
|
return {
|
|
1432
|
-
create: (
|
|
1433
|
-
const builder = middlewareBuilder(
|
|
1449
|
+
create: (machine2) => {
|
|
1450
|
+
const builder = middlewareBuilder(machine2);
|
|
1434
1451
|
if (defaultOptions.logging) {
|
|
1435
1452
|
builder.withLogging(defaultOptions.logging);
|
|
1436
1453
|
}
|
|
@@ -1456,8 +1473,8 @@ function createMiddlewareFactory(defaultOptions = {}) {
|
|
|
1456
1473
|
}
|
|
1457
1474
|
};
|
|
1458
1475
|
}
|
|
1459
|
-
function withDebugging(
|
|
1460
|
-
return withTimeTravel(withSnapshot(withHistory(
|
|
1476
|
+
function withDebugging(machine2) {
|
|
1477
|
+
return withTimeTravel(withSnapshot(withHistory(machine2)));
|
|
1461
1478
|
}
|
|
1462
1479
|
|
|
1463
1480
|
// src/mixins.ts
|
|
@@ -1493,8 +1510,8 @@ function MachineUnion(...machines) {
|
|
|
1493
1510
|
return result;
|
|
1494
1511
|
};
|
|
1495
1512
|
};
|
|
1496
|
-
for (const
|
|
1497
|
-
const descriptors = getAllPropertyDescriptors(
|
|
1513
|
+
for (const machine2 of machines) {
|
|
1514
|
+
const descriptors = getAllPropertyDescriptors(machine2.prototype);
|
|
1498
1515
|
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
1499
1516
|
if (key === "constructor") continue;
|
|
1500
1517
|
if (typeof descriptor.value === "function") {
|
|
@@ -1559,14 +1576,14 @@ function machineExclude(source, ...excluded) {
|
|
|
1559
1576
|
}
|
|
1560
1577
|
|
|
1561
1578
|
// src/utils.ts
|
|
1562
|
-
function isState(
|
|
1563
|
-
return
|
|
1579
|
+
function isState(machine2, machineClass) {
|
|
1580
|
+
return machine2 instanceof machineClass;
|
|
1564
1581
|
}
|
|
1565
1582
|
function createEvent(type, ...args) {
|
|
1566
1583
|
return { type, args };
|
|
1567
1584
|
}
|
|
1568
|
-
function mergeContext(
|
|
1569
|
-
return setContext(
|
|
1585
|
+
function mergeContext(machine2, partialContext) {
|
|
1586
|
+
return setContext(machine2, (ctx) => ({ ...ctx, ...partialContext }));
|
|
1570
1587
|
}
|
|
1571
1588
|
async function pipeTransitions(initialMachine, ...transitions) {
|
|
1572
1589
|
let current = initialMachine;
|
|
@@ -1575,13 +1592,13 @@ async function pipeTransitions(initialMachine, ...transitions) {
|
|
|
1575
1592
|
}
|
|
1576
1593
|
return current;
|
|
1577
1594
|
}
|
|
1578
|
-
function logState(
|
|
1595
|
+
function logState(machine2, label) {
|
|
1579
1596
|
if (label) {
|
|
1580
|
-
console.log(label,
|
|
1597
|
+
console.log(label, machine2.context);
|
|
1581
1598
|
} else {
|
|
1582
|
-
console.log(
|
|
1599
|
+
console.log(machine2.context);
|
|
1583
1600
|
}
|
|
1584
|
-
return
|
|
1601
|
+
return machine2;
|
|
1585
1602
|
}
|
|
1586
1603
|
function createTransition(getTransitions, transformer) {
|
|
1587
1604
|
return function(...args) {
|
|
@@ -1589,11 +1606,11 @@ function createTransition(getTransitions, transformer) {
|
|
|
1589
1606
|
return createMachine(nextContext, getTransitions());
|
|
1590
1607
|
};
|
|
1591
1608
|
}
|
|
1592
|
-
function call(fn,
|
|
1593
|
-
return fn.apply(
|
|
1609
|
+
function call(fn, machine2, ...args) {
|
|
1610
|
+
return fn.apply(machine2, args);
|
|
1594
1611
|
}
|
|
1595
|
-
function bindTransitions(
|
|
1596
|
-
return new Proxy(
|
|
1612
|
+
function bindTransitions(machine2) {
|
|
1613
|
+
return new Proxy(machine2, {
|
|
1597
1614
|
get(target, prop) {
|
|
1598
1615
|
const value = target[prop];
|
|
1599
1616
|
if (typeof value === "function") {
|
|
@@ -1610,8 +1627,8 @@ function bindTransitions(machine) {
|
|
|
1610
1627
|
});
|
|
1611
1628
|
}
|
|
1612
1629
|
var BoundMachine = class _BoundMachine {
|
|
1613
|
-
constructor(
|
|
1614
|
-
this.wrappedMachine =
|
|
1630
|
+
constructor(machine2) {
|
|
1631
|
+
this.wrappedMachine = machine2;
|
|
1615
1632
|
return new Proxy(this, {
|
|
1616
1633
|
get: (target, prop) => {
|
|
1617
1634
|
if (prop === "wrappedMachine") {
|
|
@@ -1645,15 +1662,15 @@ function createTransitionFactory() {
|
|
|
1645
1662
|
};
|
|
1646
1663
|
};
|
|
1647
1664
|
}
|
|
1648
|
-
function createTransitionExtender(
|
|
1665
|
+
function createTransitionExtender(machine2) {
|
|
1649
1666
|
return {
|
|
1650
|
-
machine,
|
|
1667
|
+
machine: machine2,
|
|
1651
1668
|
addTransition: function(name, transformer) {
|
|
1652
1669
|
const transitionFn = function(...args) {
|
|
1653
1670
|
const nextContext = transformer(this.context, ...args);
|
|
1654
1671
|
return createMachine(nextContext, this);
|
|
1655
1672
|
};
|
|
1656
|
-
const newMachine = extendTransitions(
|
|
1673
|
+
const newMachine = extendTransitions(machine2, { [name]: transitionFn });
|
|
1657
1674
|
return createTransitionExtender(newMachine);
|
|
1658
1675
|
}
|
|
1659
1676
|
};
|
|
@@ -1692,7 +1709,7 @@ function createMatcher(...cases) {
|
|
|
1692
1709
|
}
|
|
1693
1710
|
const isProxy = new Proxy({}, {
|
|
1694
1711
|
get(_target, prop) {
|
|
1695
|
-
return function isGuard(
|
|
1712
|
+
return function isGuard(machine2) {
|
|
1696
1713
|
const caseConfig = nameToCase.get(prop);
|
|
1697
1714
|
if (!caseConfig) {
|
|
1698
1715
|
const available = Array.from(nameToCase.keys()).join(", ");
|
|
@@ -1700,7 +1717,7 @@ function createMatcher(...cases) {
|
|
|
1700
1717
|
`Unknown matcher case: "${prop}". Available cases: ${available}`
|
|
1701
1718
|
);
|
|
1702
1719
|
}
|
|
1703
|
-
return caseConfig.predicate(
|
|
1720
|
+
return caseConfig.predicate(machine2);
|
|
1704
1721
|
};
|
|
1705
1722
|
}
|
|
1706
1723
|
});
|
|
@@ -1724,7 +1741,7 @@ function createMatcher(...cases) {
|
|
|
1724
1741
|
}
|
|
1725
1742
|
});
|
|
1726
1743
|
const exhaustive = { __exhaustive: true };
|
|
1727
|
-
function when2(
|
|
1744
|
+
function when2(machine2) {
|
|
1728
1745
|
return {
|
|
1729
1746
|
is(...handlers) {
|
|
1730
1747
|
if (handlers.length === 0) {
|
|
@@ -1743,8 +1760,8 @@ function createMatcher(...cases) {
|
|
|
1743
1760
|
if (!caseConfig) {
|
|
1744
1761
|
throw new Error(`Internal error: Unknown matcher case in handler: ${caseName}`);
|
|
1745
1762
|
}
|
|
1746
|
-
if (caseConfig.predicate(
|
|
1747
|
-
return caseHandler.handler(
|
|
1763
|
+
if (caseConfig.predicate(machine2)) {
|
|
1764
|
+
return caseHandler.handler(machine2);
|
|
1748
1765
|
}
|
|
1749
1766
|
}
|
|
1750
1767
|
const handledCases = actualHandlers.map((h) => h.__name).join(", ");
|
|
@@ -1758,9 +1775,9 @@ This may occur if predicates don't cover all runtime possibilities.`
|
|
|
1758
1775
|
}
|
|
1759
1776
|
};
|
|
1760
1777
|
}
|
|
1761
|
-
function simpleMatcher(
|
|
1778
|
+
function simpleMatcher(machine2) {
|
|
1762
1779
|
for (const [name, _, predicate] of cases) {
|
|
1763
|
-
if (predicate(
|
|
1780
|
+
if (predicate(machine2)) {
|
|
1764
1781
|
return name;
|
|
1765
1782
|
}
|
|
1766
1783
|
}
|
|
@@ -1929,15 +1946,15 @@ var _Actor = class _Actor {
|
|
|
1929
1946
|
// Global inspector
|
|
1930
1947
|
_Actor._inspector = null;
|
|
1931
1948
|
var Actor = _Actor;
|
|
1932
|
-
function createActor(
|
|
1933
|
-
return new Actor(
|
|
1949
|
+
function createActor(machine2) {
|
|
1950
|
+
return new Actor(machine2);
|
|
1934
1951
|
}
|
|
1935
|
-
function spawn(
|
|
1936
|
-
return createActor(
|
|
1952
|
+
function spawn(machine2) {
|
|
1953
|
+
return createActor(machine2);
|
|
1937
1954
|
}
|
|
1938
1955
|
function fromPromise(promiseFn) {
|
|
1939
1956
|
const initial = { status: "pending", data: void 0, error: void 0 };
|
|
1940
|
-
const
|
|
1957
|
+
const machine2 = createMachine(
|
|
1941
1958
|
initial,
|
|
1942
1959
|
(next2) => ({
|
|
1943
1960
|
resolve(data) {
|
|
@@ -1948,13 +1965,13 @@ function fromPromise(promiseFn) {
|
|
|
1948
1965
|
}
|
|
1949
1966
|
})
|
|
1950
1967
|
);
|
|
1951
|
-
const actor = createActor(
|
|
1968
|
+
const actor = createActor(machine2);
|
|
1952
1969
|
promiseFn().then((data) => actor.send.resolve(data)).catch((err) => actor.send.reject(err));
|
|
1953
1970
|
return actor;
|
|
1954
1971
|
}
|
|
1955
1972
|
function fromObservable(observable) {
|
|
1956
1973
|
const initial = { status: "active", value: void 0, error: void 0 };
|
|
1957
|
-
const
|
|
1974
|
+
const machine2 = createMachine(
|
|
1958
1975
|
initial,
|
|
1959
1976
|
(next2) => ({
|
|
1960
1977
|
next(value) {
|
|
@@ -1968,7 +1985,7 @@ function fromObservable(observable) {
|
|
|
1968
1985
|
}
|
|
1969
1986
|
})
|
|
1970
1987
|
);
|
|
1971
|
-
const actor = createActor(
|
|
1988
|
+
const actor = createActor(machine2);
|
|
1972
1989
|
observable.subscribe(
|
|
1973
1990
|
(val) => actor.send.next(val),
|
|
1974
1991
|
(err) => actor.send.error(err),
|
|
@@ -2003,19 +2020,231 @@ function createContextBoundMachine(initialContext, transformers) {
|
|
|
2003
2020
|
boundTransitions
|
|
2004
2021
|
);
|
|
2005
2022
|
}
|
|
2006
|
-
function callWithContext(
|
|
2007
|
-
const fn =
|
|
2008
|
-
const contextOnly = { context:
|
|
2023
|
+
function callWithContext(machine2, transitionName, ...args) {
|
|
2024
|
+
const fn = machine2[transitionName];
|
|
2025
|
+
const contextOnly = { context: machine2.context };
|
|
2009
2026
|
return fn.apply(contextOnly, args);
|
|
2010
2027
|
}
|
|
2011
|
-
function isContextBound(
|
|
2012
|
-
const firstTransition = Object.values(
|
|
2028
|
+
function isContextBound(machine2) {
|
|
2029
|
+
const firstTransition = Object.values(machine2).find(
|
|
2013
2030
|
(v) => typeof v === "function"
|
|
2014
2031
|
);
|
|
2015
2032
|
if (!firstTransition) return false;
|
|
2016
2033
|
return firstTransition.__contextBound === true;
|
|
2017
2034
|
}
|
|
2018
2035
|
|
|
2036
|
+
// src/minimal.ts
|
|
2037
|
+
var minimal_exports = {};
|
|
2038
|
+
__export(minimal_exports, {
|
|
2039
|
+
factory: () => factory,
|
|
2040
|
+
freeze: () => freeze,
|
|
2041
|
+
isState: () => isState2,
|
|
2042
|
+
machine: () => machine,
|
|
2043
|
+
match: () => match,
|
|
2044
|
+
run: () => run2,
|
|
2045
|
+
runnable: () => runnable,
|
|
2046
|
+
tag: () => tag,
|
|
2047
|
+
union: () => union,
|
|
2048
|
+
withChildren: () => withChildren
|
|
2049
|
+
});
|
|
2050
|
+
|
|
2051
|
+
// src/types.ts
|
|
2052
|
+
function tag(nameOrObj, props) {
|
|
2053
|
+
if (typeof nameOrObj === "object") {
|
|
2054
|
+
return nameOrObj;
|
|
2055
|
+
}
|
|
2056
|
+
if (props) {
|
|
2057
|
+
return { ...props, tag: nameOrObj };
|
|
2058
|
+
}
|
|
2059
|
+
return { tag: nameOrObj };
|
|
2060
|
+
}
|
|
2061
|
+
function isState2(machine2, tagValue) {
|
|
2062
|
+
return machine2.tag === tagValue;
|
|
2063
|
+
}
|
|
2064
|
+
function freeze(obj) {
|
|
2065
|
+
Object.freeze(obj);
|
|
2066
|
+
if (typeof Object.values === "function") {
|
|
2067
|
+
for (const value of Object.values(obj)) {
|
|
2068
|
+
if (value && typeof value === "object") {
|
|
2069
|
+
freeze(value);
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
return obj;
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
// src/minimal.ts
|
|
2077
|
+
function machine(context, factory2) {
|
|
2078
|
+
const next2 = (newContext) => machine(newContext, factory2);
|
|
2079
|
+
const transitions = factory2(context, next2);
|
|
2080
|
+
return Object.assign({}, context, transitions);
|
|
2081
|
+
}
|
|
2082
|
+
function match(state2, cases) {
|
|
2083
|
+
const handler = cases[state2.tag];
|
|
2084
|
+
return handler(state2);
|
|
2085
|
+
}
|
|
2086
|
+
var LIFECYCLE = Symbol("lifecycle");
|
|
2087
|
+
function runnable(initialMachine, lifecycles) {
|
|
2088
|
+
const result = { ...initialMachine };
|
|
2089
|
+
result[LIFECYCLE] = lifecycles;
|
|
2090
|
+
return result;
|
|
2091
|
+
}
|
|
2092
|
+
function run2(initial) {
|
|
2093
|
+
let current = initial;
|
|
2094
|
+
let cleanup = null;
|
|
2095
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
2096
|
+
const notify = () => {
|
|
2097
|
+
listeners.forEach((fn) => fn(current));
|
|
2098
|
+
};
|
|
2099
|
+
const enter = () => {
|
|
2100
|
+
if (cleanup) {
|
|
2101
|
+
cleanup();
|
|
2102
|
+
cleanup = null;
|
|
2103
|
+
}
|
|
2104
|
+
const lifecycles = current[LIFECYCLE];
|
|
2105
|
+
const tagValue = current.tag;
|
|
2106
|
+
const lifecycle = lifecycles == null ? void 0 : lifecycles[tagValue];
|
|
2107
|
+
if (lifecycle == null ? void 0 : lifecycle.onEnter) {
|
|
2108
|
+
cleanup = lifecycle.onEnter(send);
|
|
2109
|
+
}
|
|
2110
|
+
};
|
|
2111
|
+
const send = (event, ...args) => {
|
|
2112
|
+
const transition = current[event];
|
|
2113
|
+
if (typeof transition === "function") {
|
|
2114
|
+
const nextValue = transition(...args);
|
|
2115
|
+
if (nextValue && typeof nextValue === "object" && "tag" in nextValue) {
|
|
2116
|
+
const nextMachine = nextValue;
|
|
2117
|
+
if (!nextMachine[LIFECYCLE] && current[LIFECYCLE]) {
|
|
2118
|
+
nextMachine[LIFECYCLE] = current[LIFECYCLE];
|
|
2119
|
+
}
|
|
2120
|
+
current = nextMachine;
|
|
2121
|
+
enter();
|
|
2122
|
+
notify();
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
};
|
|
2126
|
+
enter();
|
|
2127
|
+
return {
|
|
2128
|
+
get: () => current,
|
|
2129
|
+
send,
|
|
2130
|
+
stop: () => {
|
|
2131
|
+
if (cleanup) {
|
|
2132
|
+
cleanup();
|
|
2133
|
+
cleanup = null;
|
|
2134
|
+
}
|
|
2135
|
+
listeners.clear();
|
|
2136
|
+
},
|
|
2137
|
+
subscribe: (listener) => {
|
|
2138
|
+
listeners.add(listener);
|
|
2139
|
+
return () => listeners.delete(listener);
|
|
2140
|
+
}
|
|
2141
|
+
};
|
|
2142
|
+
}
|
|
2143
|
+
function withChildren(parent, children) {
|
|
2144
|
+
const result = { ...parent };
|
|
2145
|
+
for (const key of Object.keys(children)) {
|
|
2146
|
+
const child = children[key];
|
|
2147
|
+
const childProxy = new Proxy(child, {
|
|
2148
|
+
get(target, prop) {
|
|
2149
|
+
const value = target[prop];
|
|
2150
|
+
if (typeof value === "function") {
|
|
2151
|
+
return (...args) => {
|
|
2152
|
+
const nextChild = value(...args);
|
|
2153
|
+
return withChildren(
|
|
2154
|
+
{ ...parent },
|
|
2155
|
+
{ ...children, [key]: nextChild }
|
|
2156
|
+
);
|
|
2157
|
+
};
|
|
2158
|
+
}
|
|
2159
|
+
return value;
|
|
2160
|
+
}
|
|
2161
|
+
});
|
|
2162
|
+
result[key] = childProxy;
|
|
2163
|
+
}
|
|
2164
|
+
return result;
|
|
2165
|
+
}
|
|
2166
|
+
function factory() {
|
|
2167
|
+
return (transitionFactory) => (context) => machine(context, transitionFactory);
|
|
2168
|
+
}
|
|
2169
|
+
function union() {
|
|
2170
|
+
return (factories) => {
|
|
2171
|
+
const resultFactory = (context) => {
|
|
2172
|
+
const factoryFn = factories[context.tag];
|
|
2173
|
+
return machine(context, (ctx, _next) => factoryFn(ctx, resultFactory));
|
|
2174
|
+
};
|
|
2175
|
+
return resultFactory;
|
|
2176
|
+
};
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
// src/delegate.ts
|
|
2180
|
+
var delegate_exports = {};
|
|
2181
|
+
__export(delegate_exports, {
|
|
2182
|
+
createDelegate: () => createDelegate,
|
|
2183
|
+
delegate: () => delegate,
|
|
2184
|
+
delegateAll: () => delegateAll,
|
|
2185
|
+
renameMap: () => renameMap
|
|
2186
|
+
});
|
|
2187
|
+
function delegate(ctx, key, next2, options) {
|
|
2188
|
+
const child = ctx[key];
|
|
2189
|
+
const delegated = {};
|
|
2190
|
+
const allTransitions = Object.keys(child).filter(
|
|
2191
|
+
(k) => typeof child[k] === "function"
|
|
2192
|
+
);
|
|
2193
|
+
let transitionMap;
|
|
2194
|
+
if (!options) {
|
|
2195
|
+
transitionMap = Object.fromEntries(allTransitions.map((t) => [t, t]));
|
|
2196
|
+
} else if ("pick" in options) {
|
|
2197
|
+
transitionMap = Object.fromEntries(
|
|
2198
|
+
options.pick.filter((t) => allTransitions.includes(t)).map((t) => [t, t])
|
|
2199
|
+
);
|
|
2200
|
+
} else if ("omit" in options) {
|
|
2201
|
+
const omitSet = new Set(options.omit);
|
|
2202
|
+
transitionMap = Object.fromEntries(
|
|
2203
|
+
allTransitions.filter((t) => !omitSet.has(t)).map((t) => [t, t])
|
|
2204
|
+
);
|
|
2205
|
+
} else if ("rename" in options) {
|
|
2206
|
+
transitionMap = Object.fromEntries(
|
|
2207
|
+
Object.entries(options.rename).filter(
|
|
2208
|
+
([childName]) => allTransitions.includes(childName)
|
|
2209
|
+
)
|
|
2210
|
+
);
|
|
2211
|
+
} else {
|
|
2212
|
+
transitionMap = {};
|
|
2213
|
+
}
|
|
2214
|
+
for (const [childName, parentName] of Object.entries(transitionMap)) {
|
|
2215
|
+
const childTransition = child[childName];
|
|
2216
|
+
delegated[parentName] = (...args) => {
|
|
2217
|
+
const nextChild = childTransition.apply(child, args);
|
|
2218
|
+
return next2({ ...ctx, [key]: nextChild });
|
|
2219
|
+
};
|
|
2220
|
+
}
|
|
2221
|
+
return delegated;
|
|
2222
|
+
}
|
|
2223
|
+
function createDelegate(ctx, next2) {
|
|
2224
|
+
return (key, options) => delegate(ctx, key, next2, options);
|
|
2225
|
+
}
|
|
2226
|
+
function delegateAll(ctx, keys, next2, prefix = false) {
|
|
2227
|
+
const result = {};
|
|
2228
|
+
for (const key of keys) {
|
|
2229
|
+
const child = ctx[key];
|
|
2230
|
+
const transitions = Object.keys(child).filter(
|
|
2231
|
+
(k) => typeof child[k] === "function"
|
|
2232
|
+
);
|
|
2233
|
+
for (const transitionName of transitions) {
|
|
2234
|
+
const parentName = prefix ? `${String(key)}_${transitionName}` : transitionName;
|
|
2235
|
+
const childTransition = child[transitionName];
|
|
2236
|
+
result[parentName] = (...args) => {
|
|
2237
|
+
const nextChild = childTransition.apply(child, args);
|
|
2238
|
+
return next2({ ...ctx, [key]: nextChild });
|
|
2239
|
+
};
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
return result;
|
|
2243
|
+
}
|
|
2244
|
+
function renameMap() {
|
|
2245
|
+
return (mapping) => mapping;
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2019
2248
|
// src/index.ts
|
|
2020
2249
|
function createMachine(context, fnsOrFactory) {
|
|
2021
2250
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -2028,8 +2257,8 @@ function createMachine(context, fnsOrFactory) {
|
|
|
2028
2257
|
}
|
|
2029
2258
|
const stored = getStoredTransitions(fnsOrFactory);
|
|
2030
2259
|
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2031
|
-
const
|
|
2032
|
-
return attachTransitions(
|
|
2260
|
+
const machine2 = Object.assign({ context }, transitions);
|
|
2261
|
+
return attachTransitions(machine2, transitions);
|
|
2033
2262
|
}
|
|
2034
2263
|
function createAsyncMachine(context, fnsOrFactory) {
|
|
2035
2264
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -2042,8 +2271,8 @@ function createAsyncMachine(context, fnsOrFactory) {
|
|
|
2042
2271
|
}
|
|
2043
2272
|
const stored = getStoredTransitions(fnsOrFactory);
|
|
2044
2273
|
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2045
|
-
const
|
|
2046
|
-
return attachTransitions(
|
|
2274
|
+
const machine2 = Object.assign({ context }, transitions);
|
|
2275
|
+
return attachTransitions(machine2, transitions);
|
|
2047
2276
|
}
|
|
2048
2277
|
function createMachineFactory() {
|
|
2049
2278
|
return (transformers) => {
|
|
@@ -2061,23 +2290,23 @@ function createMachineFactory() {
|
|
|
2061
2290
|
};
|
|
2062
2291
|
};
|
|
2063
2292
|
}
|
|
2064
|
-
function setContext(
|
|
2293
|
+
function setContext(machine2, newContextOrFn) {
|
|
2065
2294
|
var _a;
|
|
2066
|
-
const currentContext =
|
|
2067
|
-
const transitions = (_a = getStoredTransitions(
|
|
2295
|
+
const currentContext = machine2.context;
|
|
2296
|
+
const transitions = (_a = getStoredTransitions(machine2)) != null ? _a : snapshotOwnTransitions(machine2);
|
|
2068
2297
|
const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
|
|
2069
2298
|
return createMachine(newContext, transitions);
|
|
2070
2299
|
}
|
|
2071
2300
|
function createContext(context) {
|
|
2072
2301
|
return { context };
|
|
2073
2302
|
}
|
|
2074
|
-
function overrideTransitions(
|
|
2075
|
-
const { context, ...originalTransitions } =
|
|
2303
|
+
function overrideTransitions(machine2, overrides) {
|
|
2304
|
+
const { context, ...originalTransitions } = machine2;
|
|
2076
2305
|
const newTransitions = { ...originalTransitions, ...overrides };
|
|
2077
2306
|
return createMachine(context, newTransitions);
|
|
2078
2307
|
}
|
|
2079
|
-
function extendTransitions(
|
|
2080
|
-
const { context, ...originalTransitions } =
|
|
2308
|
+
function extendTransitions(machine2, newTransitions) {
|
|
2309
|
+
const { context, ...originalTransitions } = machine2;
|
|
2081
2310
|
const combinedTransitions = { ...originalTransitions, ...newTransitions };
|
|
2082
2311
|
return createMachine(context, combinedTransitions);
|
|
2083
2312
|
}
|
|
@@ -2098,16 +2327,16 @@ function createMachineBuilder(templateMachine) {
|
|
|
2098
2327
|
return createMachine(newContext, transitions);
|
|
2099
2328
|
};
|
|
2100
2329
|
}
|
|
2101
|
-
function matchMachine(
|
|
2102
|
-
const discriminant =
|
|
2330
|
+
function matchMachine(machine2, discriminantKey, handlers) {
|
|
2331
|
+
const discriminant = machine2.context[discriminantKey];
|
|
2103
2332
|
const handler = handlers[discriminant];
|
|
2104
2333
|
if (!handler) {
|
|
2105
2334
|
throw new Error(`No handler found for state: ${String(discriminant)}`);
|
|
2106
2335
|
}
|
|
2107
|
-
return handler(
|
|
2336
|
+
return handler(machine2.context);
|
|
2108
2337
|
}
|
|
2109
|
-
function hasState(
|
|
2110
|
-
return
|
|
2338
|
+
function hasState(machine2, key, value) {
|
|
2339
|
+
return machine2.context[key] === value;
|
|
2111
2340
|
}
|
|
2112
2341
|
function runMachine(initial, onChange) {
|
|
2113
2342
|
let current = initial;
|
|
@@ -2154,15 +2383,6 @@ function runMachine(initial, onChange) {
|
|
|
2154
2383
|
}
|
|
2155
2384
|
};
|
|
2156
2385
|
}
|
|
2157
|
-
var MachineBase = class {
|
|
2158
|
-
/**
|
|
2159
|
-
* Initializes a new machine instance with its starting context.
|
|
2160
|
-
* @param context - The initial state of the machine.
|
|
2161
|
-
*/
|
|
2162
|
-
constructor(context) {
|
|
2163
|
-
this.context = context;
|
|
2164
|
-
}
|
|
2165
|
-
};
|
|
2166
2386
|
function next(m, update) {
|
|
2167
2387
|
return setContext(m, (ctx) => update(ctx));
|
|
2168
2388
|
}
|
|
@@ -2214,11 +2434,13 @@ export {
|
|
|
2214
2434
|
createTransitionExtender,
|
|
2215
2435
|
createTransitionFactory,
|
|
2216
2436
|
customCase,
|
|
2437
|
+
delegate_exports as delegate,
|
|
2217
2438
|
delegateToChild,
|
|
2218
2439
|
describe,
|
|
2219
2440
|
discriminantCase,
|
|
2220
2441
|
extendTransitions,
|
|
2221
2442
|
forContext,
|
|
2443
|
+
freeze,
|
|
2222
2444
|
fromObservable,
|
|
2223
2445
|
fromPromise,
|
|
2224
2446
|
guard,
|
|
@@ -2245,6 +2467,7 @@ export {
|
|
|
2245
2467
|
mergeContext,
|
|
2246
2468
|
metadata,
|
|
2247
2469
|
middlewareBuilder,
|
|
2470
|
+
minimal_exports as minimal,
|
|
2248
2471
|
next,
|
|
2249
2472
|
overrideTransitions,
|
|
2250
2473
|
pipeTransitions,
|
|
@@ -2260,6 +2483,7 @@ export {
|
|
|
2260
2483
|
state,
|
|
2261
2484
|
step,
|
|
2262
2485
|
stepAsync,
|
|
2486
|
+
tag,
|
|
2263
2487
|
toggle,
|
|
2264
2488
|
transitionTo,
|
|
2265
2489
|
when,
|