@doeixd/machine 1.0.3 → 1.2.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.map +1 -1
- package/dist/cjs/development/delegate.js +89 -0
- package/dist/cjs/development/delegate.js.map +7 -0
- package/dist/cjs/development/index.js +383 -158
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/development/minimal.js +172 -0
- package/dist/cjs/development/minimal.js.map +7 -0
- package/dist/cjs/development/react.js.map +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/esm/development/core.js.map +1 -1
- package/dist/esm/development/delegate.js +68 -0
- package/dist/esm/development/delegate.js.map +7 -0
- package/dist/esm/development/index.js +389 -158
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/development/minimal.js +149 -0
- package/dist/esm/development/minimal.js.map +7 -0
- package/dist/esm/development/react.js.map +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/types/delegate.d.ts +101 -0
- package/dist/types/delegate.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- 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 +110 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +25 -1
- package/src/delegate.ts +267 -0
- package/src/index.ts +13 -0
- package/src/middleware.ts +1049 -1050
- package/src/minimal.ts +269 -0
- package/src/types.ts +129 -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") {
|
|
@@ -57,8 +63,8 @@ function yieldMachine(m) {
|
|
|
57
63
|
return m;
|
|
58
64
|
}
|
|
59
65
|
function runSequence(initial, flows) {
|
|
60
|
-
return flows.reduce((
|
|
61
|
-
return run(flow,
|
|
66
|
+
return flows.reduce((machine2, flow) => {
|
|
67
|
+
return run(flow, machine2);
|
|
62
68
|
}, initial);
|
|
63
69
|
}
|
|
64
70
|
function createFlow(flow) {
|
|
@@ -313,13 +319,13 @@ function createEnsemble(store, factories, getDiscriminant) {
|
|
|
313
319
|
const getCurrentMachine = () => {
|
|
314
320
|
const context = store.getContext();
|
|
315
321
|
const currentStateName = getDiscriminant(context);
|
|
316
|
-
const
|
|
317
|
-
if (!
|
|
322
|
+
const factory2 = factories[currentStateName];
|
|
323
|
+
if (!factory2) {
|
|
318
324
|
throw new Error(
|
|
319
325
|
`[Ensemble] Invalid state: No factory found for state "${String(currentStateName)}".`
|
|
320
326
|
);
|
|
321
327
|
}
|
|
322
|
-
return
|
|
328
|
+
return factory2(context);
|
|
323
329
|
};
|
|
324
330
|
const actions = new Proxy({}, {
|
|
325
331
|
get(_target, prop) {
|
|
@@ -465,13 +471,13 @@ function createMultiMachine(MachineClass, store) {
|
|
|
465
471
|
function createMutableMachine(sharedContext, factories, getDiscriminant) {
|
|
466
472
|
const getCurrentMachine = () => {
|
|
467
473
|
const currentStateName = getDiscriminant(sharedContext);
|
|
468
|
-
const
|
|
469
|
-
if (!
|
|
474
|
+
const factory2 = factories[currentStateName];
|
|
475
|
+
if (!factory2) {
|
|
470
476
|
throw new Error(
|
|
471
477
|
`[MutableMachine] Invalid state: No factory for state "${String(currentStateName)}".`
|
|
472
478
|
);
|
|
473
479
|
}
|
|
474
|
-
return
|
|
480
|
+
return factory2(sharedContext);
|
|
475
481
|
};
|
|
476
482
|
return new Proxy(sharedContext, {
|
|
477
483
|
get(target, prop, _receiver) {
|
|
@@ -623,18 +629,18 @@ function createParallelMachine(m1, m2) {
|
|
|
623
629
|
|
|
624
630
|
// src/middleware/core.ts
|
|
625
631
|
var CANCEL = Symbol("CANCEL");
|
|
626
|
-
function createMiddleware(
|
|
632
|
+
function createMiddleware(machine2, hooks, options = {}) {
|
|
627
633
|
const { continueOnError = false, logErrors = true, onError } = options;
|
|
628
|
-
const wrappedMachine = { ...
|
|
629
|
-
for (const prop in
|
|
630
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
631
|
-
if (prop !== "context" && typeof
|
|
632
|
-
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];
|
|
633
639
|
}
|
|
634
640
|
}
|
|
635
|
-
for (const prop in
|
|
636
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
637
|
-
const value =
|
|
641
|
+
for (const prop in machine2) {
|
|
642
|
+
if (!Object.prototype.hasOwnProperty.call(machine2, prop)) continue;
|
|
643
|
+
const value = machine2[prop];
|
|
638
644
|
if (typeof value === "function" && prop !== "context") {
|
|
639
645
|
wrappedMachine[prop] = function(...args) {
|
|
640
646
|
const transitionName = prop;
|
|
@@ -660,23 +666,23 @@ function createMiddleware(machine, hooks, options = {}) {
|
|
|
660
666
|
}
|
|
661
667
|
throw error;
|
|
662
668
|
}
|
|
663
|
-
const ensureMiddlewareProperties = (
|
|
664
|
-
if (
|
|
669
|
+
const ensureMiddlewareProperties = (machine3) => {
|
|
670
|
+
if (machine3 && typeof machine3 === "object" && machine3.context) {
|
|
665
671
|
for (const prop2 in wrappedMachine) {
|
|
666
672
|
if (!Object.prototype.hasOwnProperty.call(wrappedMachine, prop2)) continue;
|
|
667
|
-
if (prop2 !== "context" && !(prop2 in
|
|
668
|
-
|
|
673
|
+
if (prop2 !== "context" && !(prop2 in machine3)) {
|
|
674
|
+
machine3[prop2] = wrappedMachine[prop2];
|
|
669
675
|
}
|
|
670
676
|
}
|
|
671
|
-
for (const prop2 in
|
|
672
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
673
|
-
const value2 =
|
|
677
|
+
for (const prop2 in machine3) {
|
|
678
|
+
if (!Object.prototype.hasOwnProperty.call(machine3, prop2)) continue;
|
|
679
|
+
const value2 = machine3[prop2];
|
|
674
680
|
if (typeof value2 === "function" && prop2 !== "context" && wrappedMachine[prop2]) {
|
|
675
|
-
|
|
681
|
+
machine3[prop2] = wrappedMachine[prop2];
|
|
676
682
|
}
|
|
677
683
|
}
|
|
678
684
|
}
|
|
679
|
-
return
|
|
685
|
+
return machine3;
|
|
680
686
|
};
|
|
681
687
|
if (nextMachine && typeof nextMachine.then === "function") {
|
|
682
688
|
const asyncResult = nextMachine.then((resolvedMachine) => {
|
|
@@ -779,9 +785,9 @@ function createMiddleware(machine, hooks, options = {}) {
|
|
|
779
785
|
}
|
|
780
786
|
return wrappedMachine;
|
|
781
787
|
}
|
|
782
|
-
function withLogging(
|
|
788
|
+
function withLogging(machine2, options = {}) {
|
|
783
789
|
const { logger = console.log, includeArgs = false, includeContext = true } = options;
|
|
784
|
-
return createMiddleware(
|
|
790
|
+
return createMiddleware(machine2, {
|
|
785
791
|
before: ({ transitionName, args }) => {
|
|
786
792
|
const message = includeArgs ? `→ ${transitionName} [${args.join(", ")}]` : `→ ${transitionName}`;
|
|
787
793
|
logger(message);
|
|
@@ -795,9 +801,9 @@ function withLogging(machine, options = {}) {
|
|
|
795
801
|
}
|
|
796
802
|
});
|
|
797
803
|
}
|
|
798
|
-
function withAnalytics(
|
|
804
|
+
function withAnalytics(machine2, track, options = {}) {
|
|
799
805
|
const { eventPrefix = "state_transition", includePrevContext = false, includeArgs = false } = options;
|
|
800
|
-
return createMiddleware(
|
|
806
|
+
return createMiddleware(machine2, {
|
|
801
807
|
after: ({ transitionName, prevContext, nextContext, args }) => {
|
|
802
808
|
const event = `${eventPrefix}.${transitionName}`;
|
|
803
809
|
const data = { transition: transitionName };
|
|
@@ -808,8 +814,8 @@ function withAnalytics(machine, track, options = {}) {
|
|
|
808
814
|
}
|
|
809
815
|
});
|
|
810
816
|
}
|
|
811
|
-
function withValidation(
|
|
812
|
-
return createMiddleware(
|
|
817
|
+
function withValidation(machine2, validator) {
|
|
818
|
+
return createMiddleware(machine2, {
|
|
813
819
|
before: (ctx) => {
|
|
814
820
|
const result = validator(ctx);
|
|
815
821
|
if (result === false) {
|
|
@@ -818,8 +824,8 @@ function withValidation(machine, validator) {
|
|
|
818
824
|
}
|
|
819
825
|
});
|
|
820
826
|
}
|
|
821
|
-
function withPermissions(
|
|
822
|
-
return createMiddleware(
|
|
827
|
+
function withPermissions(machine2, checker) {
|
|
828
|
+
return createMiddleware(machine2, {
|
|
823
829
|
before: (ctx) => {
|
|
824
830
|
if (!checker(ctx)) {
|
|
825
831
|
throw new Error(`Unauthorized transition: ${ctx.transitionName}`);
|
|
@@ -827,9 +833,9 @@ function withPermissions(machine, checker) {
|
|
|
827
833
|
}
|
|
828
834
|
});
|
|
829
835
|
}
|
|
830
|
-
function withErrorReporting(
|
|
836
|
+
function withErrorReporting(machine2, reporter, options = {}) {
|
|
831
837
|
const { includeArgs = false } = options;
|
|
832
|
-
return createMiddleware(
|
|
838
|
+
return createMiddleware(machine2, {
|
|
833
839
|
error: (errorCtx) => {
|
|
834
840
|
const formattedCtx = {
|
|
835
841
|
transition: errorCtx.transitionName,
|
|
@@ -840,9 +846,9 @@ function withErrorReporting(machine, reporter, options = {}) {
|
|
|
840
846
|
}
|
|
841
847
|
});
|
|
842
848
|
}
|
|
843
|
-
function withPerformanceMonitoring(
|
|
849
|
+
function withPerformanceMonitoring(machine2, tracker) {
|
|
844
850
|
const startTimes = /* @__PURE__ */ new Map();
|
|
845
|
-
return createMiddleware(
|
|
851
|
+
return createMiddleware(machine2, {
|
|
846
852
|
before: (ctx) => {
|
|
847
853
|
startTimes.set(ctx.transitionName, Date.now());
|
|
848
854
|
},
|
|
@@ -861,7 +867,7 @@ function withPerformanceMonitoring(machine, tracker) {
|
|
|
861
867
|
}
|
|
862
868
|
});
|
|
863
869
|
}
|
|
864
|
-
function withRetry(
|
|
870
|
+
function withRetry(machine2, options = {}) {
|
|
865
871
|
var _a, _b;
|
|
866
872
|
const {
|
|
867
873
|
maxAttempts = (_a = options.maxRetries) != null ? _a : 3,
|
|
@@ -870,10 +876,10 @@ function withRetry(machine, options = {}) {
|
|
|
870
876
|
backoffMultiplier = 2,
|
|
871
877
|
onRetry
|
|
872
878
|
} = options;
|
|
873
|
-
const wrappedMachine = { ...
|
|
874
|
-
for (const prop in
|
|
875
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
876
|
-
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];
|
|
877
883
|
if (typeof value === "function" && prop !== "context") {
|
|
878
884
|
wrappedMachine[prop] = async function(...args) {
|
|
879
885
|
let lastError;
|
|
@@ -901,15 +907,15 @@ function withRetry(machine, options = {}) {
|
|
|
901
907
|
return wrappedMachine;
|
|
902
908
|
}
|
|
903
909
|
function createCustomMiddleware(hooks, options) {
|
|
904
|
-
return (
|
|
910
|
+
return (machine2) => createMiddleware(machine2, hooks, options);
|
|
905
911
|
}
|
|
906
912
|
|
|
907
913
|
// src/middleware/history.ts
|
|
908
|
-
function withHistory(
|
|
914
|
+
function withHistory(machine2, options = {}) {
|
|
909
915
|
const { maxSize, serializer, onEntry } = options;
|
|
910
916
|
const history = [];
|
|
911
917
|
let entryId = 0;
|
|
912
|
-
const instrumentedMachine = createMiddleware(
|
|
918
|
+
const instrumentedMachine = createMiddleware(machine2, {
|
|
913
919
|
before: ({ transitionName, args }) => {
|
|
914
920
|
const entry = {
|
|
915
921
|
id: `entry-${entryId++}`,
|
|
@@ -941,7 +947,7 @@ function withHistory(machine, options = {}) {
|
|
|
941
947
|
}
|
|
942
948
|
|
|
943
949
|
// src/middleware/snapshot.ts
|
|
944
|
-
function withSnapshot(
|
|
950
|
+
function withSnapshot(machine2, options = {}) {
|
|
945
951
|
const {
|
|
946
952
|
maxSize,
|
|
947
953
|
serializer,
|
|
@@ -950,7 +956,7 @@ function withSnapshot(machine, options = {}) {
|
|
|
950
956
|
} = options;
|
|
951
957
|
const snapshots = [];
|
|
952
958
|
let snapshotId = 0;
|
|
953
|
-
const instrumentedMachine = createMiddleware(
|
|
959
|
+
const instrumentedMachine = createMiddleware(machine2, {
|
|
954
960
|
after: ({ transitionName, prevContext, nextContext }) => {
|
|
955
961
|
if (onlyOnChange && JSON.stringify(prevContext) === JSON.stringify(nextContext)) {
|
|
956
962
|
return;
|
|
@@ -985,8 +991,8 @@ function withSnapshot(machine, options = {}) {
|
|
|
985
991
|
});
|
|
986
992
|
const restoreSnapshot = (context) => {
|
|
987
993
|
const transitions = Object.fromEntries(
|
|
988
|
-
Object.entries(
|
|
989
|
-
([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"
|
|
990
996
|
)
|
|
991
997
|
);
|
|
992
998
|
return Object.assign({ context }, transitions);
|
|
@@ -1002,13 +1008,13 @@ function withSnapshot(machine, options = {}) {
|
|
|
1002
1008
|
}
|
|
1003
1009
|
|
|
1004
1010
|
// src/middleware/time-travel.ts
|
|
1005
|
-
function withTimeTravel(
|
|
1011
|
+
function withTimeTravel(machine2, options = {}) {
|
|
1006
1012
|
const { maxSize, serializer, onRecord } = options;
|
|
1007
1013
|
const history = [];
|
|
1008
1014
|
const snapshots = [];
|
|
1009
1015
|
let historyId = 0;
|
|
1010
1016
|
let snapshotId = 0;
|
|
1011
|
-
const instrumentedMachine = createMiddleware(
|
|
1017
|
+
const instrumentedMachine = createMiddleware(machine2, {
|
|
1012
1018
|
before: ({ transitionName, args }) => {
|
|
1013
1019
|
const entry = {
|
|
1014
1020
|
id: `entry-${historyId++}`,
|
|
@@ -1054,8 +1060,8 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1054
1060
|
});
|
|
1055
1061
|
const restoreSnapshot = (context) => {
|
|
1056
1062
|
const transitions = Object.fromEntries(
|
|
1057
|
-
Object.entries(
|
|
1058
|
-
([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"
|
|
1059
1065
|
)
|
|
1060
1066
|
);
|
|
1061
1067
|
return Object.assign({ context }, transitions);
|
|
@@ -1073,8 +1079,8 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1073
1079
|
const freshMachine = Object.assign(
|
|
1074
1080
|
{ context: currentContext },
|
|
1075
1081
|
Object.fromEntries(
|
|
1076
|
-
Object.entries(
|
|
1077
|
-
([key]) => key !== "context" && typeof
|
|
1082
|
+
Object.entries(machine2).filter(
|
|
1083
|
+
([key]) => key !== "context" && typeof machine2[key] === "function"
|
|
1078
1084
|
)
|
|
1079
1085
|
)
|
|
1080
1086
|
);
|
|
@@ -1110,15 +1116,15 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1110
1116
|
}
|
|
1111
1117
|
|
|
1112
1118
|
// src/middleware/composition.ts
|
|
1113
|
-
function compose(
|
|
1114
|
-
return middlewares.reduce((acc, middleware) => middleware(acc),
|
|
1119
|
+
function compose(machine2, ...middlewares) {
|
|
1120
|
+
return middlewares.reduce((acc, middleware) => middleware(acc), machine2);
|
|
1115
1121
|
}
|
|
1116
|
-
function composeTyped(
|
|
1117
|
-
return middlewares.reduce((acc, middleware) => middleware(acc),
|
|
1122
|
+
function composeTyped(machine2, ...middlewares) {
|
|
1123
|
+
return middlewares.reduce((acc, middleware) => middleware(acc), machine2);
|
|
1118
1124
|
}
|
|
1119
1125
|
var MiddlewareChainBuilder = class _MiddlewareChainBuilder {
|
|
1120
|
-
constructor(
|
|
1121
|
-
this.machine =
|
|
1126
|
+
constructor(machine2) {
|
|
1127
|
+
this.machine = machine2;
|
|
1122
1128
|
}
|
|
1123
1129
|
/**
|
|
1124
1130
|
* Add a middleware to the composition chain.
|
|
@@ -1136,12 +1142,12 @@ var MiddlewareChainBuilder = class _MiddlewareChainBuilder {
|
|
|
1136
1142
|
return this.machine;
|
|
1137
1143
|
}
|
|
1138
1144
|
};
|
|
1139
|
-
function chain(
|
|
1140
|
-
return new MiddlewareChainBuilder(
|
|
1145
|
+
function chain(machine2) {
|
|
1146
|
+
return new MiddlewareChainBuilder(machine2);
|
|
1141
1147
|
}
|
|
1142
1148
|
function when(middleware, predicate) {
|
|
1143
|
-
const conditional = function(
|
|
1144
|
-
return predicate(
|
|
1149
|
+
const conditional = function(machine2) {
|
|
1150
|
+
return predicate(machine2) ? middleware(machine2) : machine2;
|
|
1145
1151
|
};
|
|
1146
1152
|
conditional.middleware = middleware;
|
|
1147
1153
|
conditional.when = predicate;
|
|
@@ -1153,7 +1159,7 @@ function inDevelopment(middleware) {
|
|
|
1153
1159
|
});
|
|
1154
1160
|
}
|
|
1155
1161
|
function whenContext(key, value, middleware) {
|
|
1156
|
-
return when(middleware, (
|
|
1162
|
+
return when(middleware, (machine2) => machine2.context[key] === value);
|
|
1157
1163
|
}
|
|
1158
1164
|
function createMiddlewareRegistry() {
|
|
1159
1165
|
const registry = /* @__PURE__ */ new Map();
|
|
@@ -1199,7 +1205,7 @@ function createMiddlewareRegistry() {
|
|
|
1199
1205
|
* Apply a selection of registered middlewares to a machine.
|
|
1200
1206
|
* Middlewares are applied in priority order (lowest to highest).
|
|
1201
1207
|
*/
|
|
1202
|
-
apply(
|
|
1208
|
+
apply(machine2, middlewareNames) {
|
|
1203
1209
|
const middlewares = middlewareNames.map((name) => {
|
|
1204
1210
|
const entry = registry.get(name);
|
|
1205
1211
|
if (!entry) {
|
|
@@ -1210,14 +1216,14 @@ function createMiddlewareRegistry() {
|
|
|
1210
1216
|
var _a, _b;
|
|
1211
1217
|
return ((_a = a.priority) != null ? _a : 0) - ((_b = b.priority) != null ? _b : 0);
|
|
1212
1218
|
});
|
|
1213
|
-
return composeTyped(
|
|
1219
|
+
return composeTyped(machine2, ...middlewares.map((m) => m.middleware));
|
|
1214
1220
|
},
|
|
1215
1221
|
/**
|
|
1216
1222
|
* Apply all registered middlewares to a machine in priority order.
|
|
1217
1223
|
*/
|
|
1218
|
-
applyAll(
|
|
1224
|
+
applyAll(machine2) {
|
|
1219
1225
|
const middlewares = this.list();
|
|
1220
|
-
return composeTyped(
|
|
1226
|
+
return composeTyped(machine2, ...middlewares.map((m) => m.middleware));
|
|
1221
1227
|
}
|
|
1222
1228
|
};
|
|
1223
1229
|
}
|
|
@@ -1227,8 +1233,8 @@ function createPipeline(config = {}) {
|
|
|
1227
1233
|
logErrors = true,
|
|
1228
1234
|
onError
|
|
1229
1235
|
} = config;
|
|
1230
|
-
return (
|
|
1231
|
-
let currentMachine =
|
|
1236
|
+
return (machine2, ...middlewares) => {
|
|
1237
|
+
let currentMachine = machine2;
|
|
1232
1238
|
const errors = [];
|
|
1233
1239
|
let success = true;
|
|
1234
1240
|
for (let i = 0; i < middlewares.length; i++) {
|
|
@@ -1262,16 +1268,16 @@ function createPipeline(config = {}) {
|
|
|
1262
1268
|
};
|
|
1263
1269
|
}
|
|
1264
1270
|
function combine(...middlewares) {
|
|
1265
|
-
return (
|
|
1271
|
+
return (machine2) => composeTyped(machine2, ...middlewares);
|
|
1266
1272
|
}
|
|
1267
1273
|
function branch(branches, fallback) {
|
|
1268
|
-
return (
|
|
1274
|
+
return (machine2) => {
|
|
1269
1275
|
for (const [predicate, middleware] of branches) {
|
|
1270
|
-
if (predicate(
|
|
1271
|
-
return middleware(
|
|
1276
|
+
if (predicate(machine2)) {
|
|
1277
|
+
return middleware(machine2);
|
|
1272
1278
|
}
|
|
1273
1279
|
}
|
|
1274
|
-
return fallback ? fallback(
|
|
1280
|
+
return fallback ? fallback(machine2) : machine2;
|
|
1275
1281
|
};
|
|
1276
1282
|
}
|
|
1277
1283
|
function isMiddlewareFn(value) {
|
|
@@ -1316,85 +1322,85 @@ function isPipelineConfig(value) {
|
|
|
1316
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);
|
|
1317
1323
|
}
|
|
1318
1324
|
var MiddlewareBuilder = class {
|
|
1319
|
-
constructor(
|
|
1320
|
-
this.machine =
|
|
1325
|
+
constructor(machine2) {
|
|
1326
|
+
this.machine = machine2;
|
|
1321
1327
|
this.middlewares = [];
|
|
1322
1328
|
}
|
|
1323
1329
|
/**
|
|
1324
1330
|
* Add logging middleware with type-safe configuration.
|
|
1325
1331
|
*/
|
|
1326
1332
|
withLogging(options) {
|
|
1327
|
-
this.middlewares.push((
|
|
1333
|
+
this.middlewares.push((machine2) => withLogging(machine2, options));
|
|
1328
1334
|
return this;
|
|
1329
1335
|
}
|
|
1330
1336
|
/**
|
|
1331
1337
|
* Add analytics middleware with type-safe configuration.
|
|
1332
1338
|
*/
|
|
1333
1339
|
withAnalytics(track, options) {
|
|
1334
|
-
this.middlewares.push((
|
|
1340
|
+
this.middlewares.push((machine2) => withAnalytics(machine2, track, options));
|
|
1335
1341
|
return this;
|
|
1336
1342
|
}
|
|
1337
1343
|
/**
|
|
1338
1344
|
* Add validation middleware with type-safe configuration.
|
|
1339
1345
|
*/
|
|
1340
1346
|
withValidation(validator, _options) {
|
|
1341
|
-
this.middlewares.push((
|
|
1347
|
+
this.middlewares.push((machine2) => withValidation(machine2, validator));
|
|
1342
1348
|
return this;
|
|
1343
1349
|
}
|
|
1344
1350
|
/**
|
|
1345
1351
|
* Add permission checking middleware with type-safe configuration.
|
|
1346
1352
|
*/
|
|
1347
1353
|
withPermissions(checker) {
|
|
1348
|
-
this.middlewares.push((
|
|
1354
|
+
this.middlewares.push((machine2) => withPermissions(machine2, checker));
|
|
1349
1355
|
return this;
|
|
1350
1356
|
}
|
|
1351
1357
|
/**
|
|
1352
1358
|
* Add error reporting middleware with type-safe configuration.
|
|
1353
1359
|
*/
|
|
1354
1360
|
withErrorReporting(reporter, options) {
|
|
1355
|
-
this.middlewares.push((
|
|
1361
|
+
this.middlewares.push((machine2) => withErrorReporting(machine2, reporter, options));
|
|
1356
1362
|
return this;
|
|
1357
1363
|
}
|
|
1358
1364
|
/**
|
|
1359
1365
|
* Add performance monitoring middleware with type-safe configuration.
|
|
1360
1366
|
*/
|
|
1361
1367
|
withPerformanceMonitoring(tracker, _options) {
|
|
1362
|
-
this.middlewares.push((
|
|
1368
|
+
this.middlewares.push((machine2) => withPerformanceMonitoring(machine2, tracker));
|
|
1363
1369
|
return this;
|
|
1364
1370
|
}
|
|
1365
1371
|
/**
|
|
1366
1372
|
* Add retry middleware with type-safe configuration.
|
|
1367
1373
|
*/
|
|
1368
1374
|
withRetry(options) {
|
|
1369
|
-
this.middlewares.push((
|
|
1375
|
+
this.middlewares.push((machine2) => withRetry(machine2, options));
|
|
1370
1376
|
return this;
|
|
1371
1377
|
}
|
|
1372
1378
|
/**
|
|
1373
1379
|
* Add history tracking middleware with type-safe configuration.
|
|
1374
1380
|
*/
|
|
1375
1381
|
withHistory(options) {
|
|
1376
|
-
this.middlewares.push((
|
|
1382
|
+
this.middlewares.push((machine2) => withHistory(machine2, options));
|
|
1377
1383
|
return this;
|
|
1378
1384
|
}
|
|
1379
1385
|
/**
|
|
1380
1386
|
* Add snapshot tracking middleware with type-safe configuration.
|
|
1381
1387
|
*/
|
|
1382
1388
|
withSnapshot(options) {
|
|
1383
|
-
this.middlewares.push((
|
|
1389
|
+
this.middlewares.push((machine2) => withSnapshot(machine2, options));
|
|
1384
1390
|
return this;
|
|
1385
1391
|
}
|
|
1386
1392
|
/**
|
|
1387
1393
|
* Add time travel middleware with type-safe configuration.
|
|
1388
1394
|
*/
|
|
1389
1395
|
withTimeTravel(options) {
|
|
1390
|
-
this.middlewares.push((
|
|
1396
|
+
this.middlewares.push((machine2) => withTimeTravel(machine2, options));
|
|
1391
1397
|
return this;
|
|
1392
1398
|
}
|
|
1393
1399
|
/**
|
|
1394
1400
|
* Add debugging middleware (combination of history, snapshot, and time travel).
|
|
1395
1401
|
*/
|
|
1396
1402
|
withDebugging() {
|
|
1397
|
-
this.middlewares.push((
|
|
1403
|
+
this.middlewares.push((machine2) => withDebugging(machine2));
|
|
1398
1404
|
return this;
|
|
1399
1405
|
}
|
|
1400
1406
|
/**
|
|
@@ -1435,13 +1441,13 @@ var MiddlewareBuilder = class {
|
|
|
1435
1441
|
return this;
|
|
1436
1442
|
}
|
|
1437
1443
|
};
|
|
1438
|
-
function middlewareBuilder(
|
|
1439
|
-
return new MiddlewareBuilder(
|
|
1444
|
+
function middlewareBuilder(machine2) {
|
|
1445
|
+
return new MiddlewareBuilder(machine2);
|
|
1440
1446
|
}
|
|
1441
1447
|
function createMiddlewareFactory(defaultOptions = {}) {
|
|
1442
1448
|
return {
|
|
1443
|
-
create: (
|
|
1444
|
-
const builder = middlewareBuilder(
|
|
1449
|
+
create: (machine2) => {
|
|
1450
|
+
const builder = middlewareBuilder(machine2);
|
|
1445
1451
|
if (defaultOptions.logging) {
|
|
1446
1452
|
builder.withLogging(defaultOptions.logging);
|
|
1447
1453
|
}
|
|
@@ -1467,8 +1473,8 @@ function createMiddlewareFactory(defaultOptions = {}) {
|
|
|
1467
1473
|
}
|
|
1468
1474
|
};
|
|
1469
1475
|
}
|
|
1470
|
-
function withDebugging(
|
|
1471
|
-
return withTimeTravel(withSnapshot(withHistory(
|
|
1476
|
+
function withDebugging(machine2) {
|
|
1477
|
+
return withTimeTravel(withSnapshot(withHistory(machine2)));
|
|
1472
1478
|
}
|
|
1473
1479
|
|
|
1474
1480
|
// src/mixins.ts
|
|
@@ -1504,8 +1510,8 @@ function MachineUnion(...machines) {
|
|
|
1504
1510
|
return result;
|
|
1505
1511
|
};
|
|
1506
1512
|
};
|
|
1507
|
-
for (const
|
|
1508
|
-
const descriptors = getAllPropertyDescriptors(
|
|
1513
|
+
for (const machine2 of machines) {
|
|
1514
|
+
const descriptors = getAllPropertyDescriptors(machine2.prototype);
|
|
1509
1515
|
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
1510
1516
|
if (key === "constructor") continue;
|
|
1511
1517
|
if (typeof descriptor.value === "function") {
|
|
@@ -1570,14 +1576,14 @@ function machineExclude(source, ...excluded) {
|
|
|
1570
1576
|
}
|
|
1571
1577
|
|
|
1572
1578
|
// src/utils.ts
|
|
1573
|
-
function isState(
|
|
1574
|
-
return
|
|
1579
|
+
function isState(machine2, machineClass) {
|
|
1580
|
+
return machine2 instanceof machineClass;
|
|
1575
1581
|
}
|
|
1576
1582
|
function createEvent(type, ...args) {
|
|
1577
1583
|
return { type, args };
|
|
1578
1584
|
}
|
|
1579
|
-
function mergeContext(
|
|
1580
|
-
return setContext(
|
|
1585
|
+
function mergeContext(machine2, partialContext) {
|
|
1586
|
+
return setContext(machine2, (ctx) => ({ ...ctx, ...partialContext }));
|
|
1581
1587
|
}
|
|
1582
1588
|
async function pipeTransitions(initialMachine, ...transitions) {
|
|
1583
1589
|
let current = initialMachine;
|
|
@@ -1586,13 +1592,13 @@ async function pipeTransitions(initialMachine, ...transitions) {
|
|
|
1586
1592
|
}
|
|
1587
1593
|
return current;
|
|
1588
1594
|
}
|
|
1589
|
-
function logState(
|
|
1595
|
+
function logState(machine2, label) {
|
|
1590
1596
|
if (label) {
|
|
1591
|
-
console.log(label,
|
|
1597
|
+
console.log(label, machine2.context);
|
|
1592
1598
|
} else {
|
|
1593
|
-
console.log(
|
|
1599
|
+
console.log(machine2.context);
|
|
1594
1600
|
}
|
|
1595
|
-
return
|
|
1601
|
+
return machine2;
|
|
1596
1602
|
}
|
|
1597
1603
|
function createTransition(getTransitions, transformer) {
|
|
1598
1604
|
return function(...args) {
|
|
@@ -1600,11 +1606,11 @@ function createTransition(getTransitions, transformer) {
|
|
|
1600
1606
|
return createMachine(nextContext, getTransitions());
|
|
1601
1607
|
};
|
|
1602
1608
|
}
|
|
1603
|
-
function call(fn,
|
|
1604
|
-
return fn.apply(
|
|
1609
|
+
function call(fn, machine2, ...args) {
|
|
1610
|
+
return fn.apply(machine2, args);
|
|
1605
1611
|
}
|
|
1606
|
-
function bindTransitions(
|
|
1607
|
-
return new Proxy(
|
|
1612
|
+
function bindTransitions(machine2) {
|
|
1613
|
+
return new Proxy(machine2, {
|
|
1608
1614
|
get(target, prop) {
|
|
1609
1615
|
const value = target[prop];
|
|
1610
1616
|
if (typeof value === "function") {
|
|
@@ -1621,8 +1627,8 @@ function bindTransitions(machine) {
|
|
|
1621
1627
|
});
|
|
1622
1628
|
}
|
|
1623
1629
|
var BoundMachine = class _BoundMachine {
|
|
1624
|
-
constructor(
|
|
1625
|
-
this.wrappedMachine =
|
|
1630
|
+
constructor(machine2) {
|
|
1631
|
+
this.wrappedMachine = machine2;
|
|
1626
1632
|
return new Proxy(this, {
|
|
1627
1633
|
get: (target, prop) => {
|
|
1628
1634
|
if (prop === "wrappedMachine") {
|
|
@@ -1656,15 +1662,15 @@ function createTransitionFactory() {
|
|
|
1656
1662
|
};
|
|
1657
1663
|
};
|
|
1658
1664
|
}
|
|
1659
|
-
function createTransitionExtender(
|
|
1665
|
+
function createTransitionExtender(machine2) {
|
|
1660
1666
|
return {
|
|
1661
|
-
machine,
|
|
1667
|
+
machine: machine2,
|
|
1662
1668
|
addTransition: function(name, transformer) {
|
|
1663
1669
|
const transitionFn = function(...args) {
|
|
1664
1670
|
const nextContext = transformer(this.context, ...args);
|
|
1665
1671
|
return createMachine(nextContext, this);
|
|
1666
1672
|
};
|
|
1667
|
-
const newMachine = extendTransitions(
|
|
1673
|
+
const newMachine = extendTransitions(machine2, { [name]: transitionFn });
|
|
1668
1674
|
return createTransitionExtender(newMachine);
|
|
1669
1675
|
}
|
|
1670
1676
|
};
|
|
@@ -1703,7 +1709,7 @@ function createMatcher(...cases) {
|
|
|
1703
1709
|
}
|
|
1704
1710
|
const isProxy = new Proxy({}, {
|
|
1705
1711
|
get(_target, prop) {
|
|
1706
|
-
return function isGuard(
|
|
1712
|
+
return function isGuard(machine2) {
|
|
1707
1713
|
const caseConfig = nameToCase.get(prop);
|
|
1708
1714
|
if (!caseConfig) {
|
|
1709
1715
|
const available = Array.from(nameToCase.keys()).join(", ");
|
|
@@ -1711,7 +1717,7 @@ function createMatcher(...cases) {
|
|
|
1711
1717
|
`Unknown matcher case: "${prop}". Available cases: ${available}`
|
|
1712
1718
|
);
|
|
1713
1719
|
}
|
|
1714
|
-
return caseConfig.predicate(
|
|
1720
|
+
return caseConfig.predicate(machine2);
|
|
1715
1721
|
};
|
|
1716
1722
|
}
|
|
1717
1723
|
});
|
|
@@ -1735,7 +1741,7 @@ function createMatcher(...cases) {
|
|
|
1735
1741
|
}
|
|
1736
1742
|
});
|
|
1737
1743
|
const exhaustive = { __exhaustive: true };
|
|
1738
|
-
function when2(
|
|
1744
|
+
function when2(machine2) {
|
|
1739
1745
|
return {
|
|
1740
1746
|
is(...handlers) {
|
|
1741
1747
|
if (handlers.length === 0) {
|
|
@@ -1754,8 +1760,8 @@ function createMatcher(...cases) {
|
|
|
1754
1760
|
if (!caseConfig) {
|
|
1755
1761
|
throw new Error(`Internal error: Unknown matcher case in handler: ${caseName}`);
|
|
1756
1762
|
}
|
|
1757
|
-
if (caseConfig.predicate(
|
|
1758
|
-
return caseHandler.handler(
|
|
1763
|
+
if (caseConfig.predicate(machine2)) {
|
|
1764
|
+
return caseHandler.handler(machine2);
|
|
1759
1765
|
}
|
|
1760
1766
|
}
|
|
1761
1767
|
const handledCases = actualHandlers.map((h) => h.__name).join(", ");
|
|
@@ -1769,9 +1775,9 @@ This may occur if predicates don't cover all runtime possibilities.`
|
|
|
1769
1775
|
}
|
|
1770
1776
|
};
|
|
1771
1777
|
}
|
|
1772
|
-
function simpleMatcher(
|
|
1778
|
+
function simpleMatcher(machine2) {
|
|
1773
1779
|
for (const [name, _, predicate] of cases) {
|
|
1774
|
-
if (predicate(
|
|
1780
|
+
if (predicate(machine2)) {
|
|
1775
1781
|
return name;
|
|
1776
1782
|
}
|
|
1777
1783
|
}
|
|
@@ -1940,15 +1946,15 @@ var _Actor = class _Actor {
|
|
|
1940
1946
|
// Global inspector
|
|
1941
1947
|
_Actor._inspector = null;
|
|
1942
1948
|
var Actor = _Actor;
|
|
1943
|
-
function createActor(
|
|
1944
|
-
return new Actor(
|
|
1949
|
+
function createActor(machine2) {
|
|
1950
|
+
return new Actor(machine2);
|
|
1945
1951
|
}
|
|
1946
|
-
function spawn(
|
|
1947
|
-
return createActor(
|
|
1952
|
+
function spawn(machine2) {
|
|
1953
|
+
return createActor(machine2);
|
|
1948
1954
|
}
|
|
1949
1955
|
function fromPromise(promiseFn) {
|
|
1950
1956
|
const initial = { status: "pending", data: void 0, error: void 0 };
|
|
1951
|
-
const
|
|
1957
|
+
const machine2 = createMachine(
|
|
1952
1958
|
initial,
|
|
1953
1959
|
(next2) => ({
|
|
1954
1960
|
resolve(data) {
|
|
@@ -1959,13 +1965,13 @@ function fromPromise(promiseFn) {
|
|
|
1959
1965
|
}
|
|
1960
1966
|
})
|
|
1961
1967
|
);
|
|
1962
|
-
const actor = createActor(
|
|
1968
|
+
const actor = createActor(machine2);
|
|
1963
1969
|
promiseFn().then((data) => actor.send.resolve(data)).catch((err) => actor.send.reject(err));
|
|
1964
1970
|
return actor;
|
|
1965
1971
|
}
|
|
1966
1972
|
function fromObservable(observable) {
|
|
1967
1973
|
const initial = { status: "active", value: void 0, error: void 0 };
|
|
1968
|
-
const
|
|
1974
|
+
const machine2 = createMachine(
|
|
1969
1975
|
initial,
|
|
1970
1976
|
(next2) => ({
|
|
1971
1977
|
next(value) {
|
|
@@ -1979,7 +1985,7 @@ function fromObservable(observable) {
|
|
|
1979
1985
|
}
|
|
1980
1986
|
})
|
|
1981
1987
|
);
|
|
1982
|
-
const actor = createActor(
|
|
1988
|
+
const actor = createActor(machine2);
|
|
1983
1989
|
observable.subscribe(
|
|
1984
1990
|
(val) => actor.send.next(val),
|
|
1985
1991
|
(err) => actor.send.error(err),
|
|
@@ -2014,19 +2020,240 @@ function createContextBoundMachine(initialContext, transformers) {
|
|
|
2014
2020
|
boundTransitions
|
|
2015
2021
|
);
|
|
2016
2022
|
}
|
|
2017
|
-
function callWithContext(
|
|
2018
|
-
const fn =
|
|
2019
|
-
const contextOnly = { context:
|
|
2023
|
+
function callWithContext(machine2, transitionName, ...args) {
|
|
2024
|
+
const fn = machine2[transitionName];
|
|
2025
|
+
const contextOnly = { context: machine2.context };
|
|
2020
2026
|
return fn.apply(contextOnly, args);
|
|
2021
2027
|
}
|
|
2022
|
-
function isContextBound(
|
|
2023
|
-
const firstTransition = Object.values(
|
|
2028
|
+
function isContextBound(machine2) {
|
|
2029
|
+
const firstTransition = Object.values(machine2).find(
|
|
2024
2030
|
(v) => typeof v === "function"
|
|
2025
2031
|
);
|
|
2026
2032
|
if (!firstTransition) return false;
|
|
2027
2033
|
return firstTransition.__contextBound === true;
|
|
2028
2034
|
}
|
|
2029
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
|
+
((tag2) => {
|
|
2062
|
+
function factory2(name) {
|
|
2063
|
+
if (name) {
|
|
2064
|
+
return (props) => tag2(name, props);
|
|
2065
|
+
}
|
|
2066
|
+
return (name2) => (props) => tag2(name2, props);
|
|
2067
|
+
}
|
|
2068
|
+
tag2.factory = factory2;
|
|
2069
|
+
})(tag || (tag = {}));
|
|
2070
|
+
function isState2(machine2, tagValue) {
|
|
2071
|
+
return machine2.tag === tagValue;
|
|
2072
|
+
}
|
|
2073
|
+
function freeze(obj) {
|
|
2074
|
+
Object.freeze(obj);
|
|
2075
|
+
if (typeof Object.values === "function") {
|
|
2076
|
+
for (const value of Object.values(obj)) {
|
|
2077
|
+
if (value && typeof value === "object") {
|
|
2078
|
+
freeze(value);
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
return obj;
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
// src/minimal.ts
|
|
2086
|
+
function machine(context, factory2) {
|
|
2087
|
+
const next2 = (newContext) => machine(newContext, factory2);
|
|
2088
|
+
const transitions = factory2(context, next2);
|
|
2089
|
+
return Object.assign({}, context, transitions);
|
|
2090
|
+
}
|
|
2091
|
+
function match(state2, cases) {
|
|
2092
|
+
const handler = cases[state2.tag];
|
|
2093
|
+
return handler(state2);
|
|
2094
|
+
}
|
|
2095
|
+
var LIFECYCLE = Symbol("lifecycle");
|
|
2096
|
+
function runnable(initialMachine, lifecycles) {
|
|
2097
|
+
const result = { ...initialMachine };
|
|
2098
|
+
result[LIFECYCLE] = lifecycles;
|
|
2099
|
+
return result;
|
|
2100
|
+
}
|
|
2101
|
+
function run2(initial) {
|
|
2102
|
+
let current = initial;
|
|
2103
|
+
let cleanup = null;
|
|
2104
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
2105
|
+
const notify = () => {
|
|
2106
|
+
listeners.forEach((fn) => fn(current));
|
|
2107
|
+
};
|
|
2108
|
+
const enter = () => {
|
|
2109
|
+
if (cleanup) {
|
|
2110
|
+
cleanup();
|
|
2111
|
+
cleanup = null;
|
|
2112
|
+
}
|
|
2113
|
+
const lifecycles = current[LIFECYCLE];
|
|
2114
|
+
const tagValue = current.tag;
|
|
2115
|
+
const lifecycle = lifecycles == null ? void 0 : lifecycles[tagValue];
|
|
2116
|
+
if (lifecycle == null ? void 0 : lifecycle.onEnter) {
|
|
2117
|
+
cleanup = lifecycle.onEnter(send);
|
|
2118
|
+
}
|
|
2119
|
+
};
|
|
2120
|
+
const send = (event, ...args) => {
|
|
2121
|
+
const transition = current[event];
|
|
2122
|
+
if (typeof transition === "function") {
|
|
2123
|
+
const nextValue = transition(...args);
|
|
2124
|
+
if (nextValue && typeof nextValue === "object" && "tag" in nextValue) {
|
|
2125
|
+
const nextMachine = nextValue;
|
|
2126
|
+
if (!nextMachine[LIFECYCLE] && current[LIFECYCLE]) {
|
|
2127
|
+
nextMachine[LIFECYCLE] = current[LIFECYCLE];
|
|
2128
|
+
}
|
|
2129
|
+
current = nextMachine;
|
|
2130
|
+
enter();
|
|
2131
|
+
notify();
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
};
|
|
2135
|
+
enter();
|
|
2136
|
+
return {
|
|
2137
|
+
get: () => current,
|
|
2138
|
+
send,
|
|
2139
|
+
stop: () => {
|
|
2140
|
+
if (cleanup) {
|
|
2141
|
+
cleanup();
|
|
2142
|
+
cleanup = null;
|
|
2143
|
+
}
|
|
2144
|
+
listeners.clear();
|
|
2145
|
+
},
|
|
2146
|
+
subscribe: (listener) => {
|
|
2147
|
+
listeners.add(listener);
|
|
2148
|
+
return () => listeners.delete(listener);
|
|
2149
|
+
}
|
|
2150
|
+
};
|
|
2151
|
+
}
|
|
2152
|
+
function withChildren(parent, children) {
|
|
2153
|
+
const result = { ...parent };
|
|
2154
|
+
for (const key of Object.keys(children)) {
|
|
2155
|
+
const child = children[key];
|
|
2156
|
+
const childProxy = new Proxy(child, {
|
|
2157
|
+
get(target, prop) {
|
|
2158
|
+
const value = target[prop];
|
|
2159
|
+
if (typeof value === "function") {
|
|
2160
|
+
return (...args) => {
|
|
2161
|
+
const nextChild = value(...args);
|
|
2162
|
+
return withChildren(
|
|
2163
|
+
{ ...parent },
|
|
2164
|
+
{ ...children, [key]: nextChild }
|
|
2165
|
+
);
|
|
2166
|
+
};
|
|
2167
|
+
}
|
|
2168
|
+
return value;
|
|
2169
|
+
}
|
|
2170
|
+
});
|
|
2171
|
+
result[key] = childProxy;
|
|
2172
|
+
}
|
|
2173
|
+
return result;
|
|
2174
|
+
}
|
|
2175
|
+
function factory() {
|
|
2176
|
+
return (transitionFactory) => (context) => machine(context, transitionFactory);
|
|
2177
|
+
}
|
|
2178
|
+
function union() {
|
|
2179
|
+
return (factories) => {
|
|
2180
|
+
const resultFactory = (context) => {
|
|
2181
|
+
const factoryFn = factories[context.tag];
|
|
2182
|
+
return machine(context, (ctx, _next) => factoryFn(ctx, resultFactory));
|
|
2183
|
+
};
|
|
2184
|
+
return resultFactory;
|
|
2185
|
+
};
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
// src/delegate.ts
|
|
2189
|
+
var delegate_exports = {};
|
|
2190
|
+
__export(delegate_exports, {
|
|
2191
|
+
createDelegate: () => createDelegate,
|
|
2192
|
+
delegate: () => delegate,
|
|
2193
|
+
delegateAll: () => delegateAll,
|
|
2194
|
+
renameMap: () => renameMap
|
|
2195
|
+
});
|
|
2196
|
+
function delegate(ctx, key, next2, options) {
|
|
2197
|
+
const child = ctx[key];
|
|
2198
|
+
const delegated = {};
|
|
2199
|
+
const allTransitions = Object.keys(child).filter(
|
|
2200
|
+
(k) => typeof child[k] === "function"
|
|
2201
|
+
);
|
|
2202
|
+
let transitionMap;
|
|
2203
|
+
if (!options) {
|
|
2204
|
+
transitionMap = Object.fromEntries(allTransitions.map((t) => [t, t]));
|
|
2205
|
+
} else if ("pick" in options) {
|
|
2206
|
+
transitionMap = Object.fromEntries(
|
|
2207
|
+
options.pick.filter((t) => allTransitions.includes(t)).map((t) => [t, t])
|
|
2208
|
+
);
|
|
2209
|
+
} else if ("omit" in options) {
|
|
2210
|
+
const omitSet = new Set(options.omit);
|
|
2211
|
+
transitionMap = Object.fromEntries(
|
|
2212
|
+
allTransitions.filter((t) => !omitSet.has(t)).map((t) => [t, t])
|
|
2213
|
+
);
|
|
2214
|
+
} else if ("rename" in options) {
|
|
2215
|
+
transitionMap = Object.fromEntries(
|
|
2216
|
+
Object.entries(options.rename).filter(
|
|
2217
|
+
([childName]) => allTransitions.includes(childName)
|
|
2218
|
+
)
|
|
2219
|
+
);
|
|
2220
|
+
} else {
|
|
2221
|
+
transitionMap = {};
|
|
2222
|
+
}
|
|
2223
|
+
for (const [childName, parentName] of Object.entries(transitionMap)) {
|
|
2224
|
+
const childTransition = child[childName];
|
|
2225
|
+
delegated[parentName] = (...args) => {
|
|
2226
|
+
const nextChild = childTransition.apply(child, args);
|
|
2227
|
+
return next2({ ...ctx, [key]: nextChild });
|
|
2228
|
+
};
|
|
2229
|
+
}
|
|
2230
|
+
return delegated;
|
|
2231
|
+
}
|
|
2232
|
+
function createDelegate(ctx, next2) {
|
|
2233
|
+
return (key, options) => delegate(ctx, key, next2, options);
|
|
2234
|
+
}
|
|
2235
|
+
function delegateAll(ctx, keys, next2, prefix = false) {
|
|
2236
|
+
const result = {};
|
|
2237
|
+
for (const key of keys) {
|
|
2238
|
+
const child = ctx[key];
|
|
2239
|
+
const transitions = Object.keys(child).filter(
|
|
2240
|
+
(k) => typeof child[k] === "function"
|
|
2241
|
+
);
|
|
2242
|
+
for (const transitionName of transitions) {
|
|
2243
|
+
const parentName = prefix ? `${String(key)}_${transitionName}` : transitionName;
|
|
2244
|
+
const childTransition = child[transitionName];
|
|
2245
|
+
result[parentName] = (...args) => {
|
|
2246
|
+
const nextChild = childTransition.apply(child, args);
|
|
2247
|
+
return next2({ ...ctx, [key]: nextChild });
|
|
2248
|
+
};
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
return result;
|
|
2252
|
+
}
|
|
2253
|
+
function renameMap() {
|
|
2254
|
+
return (mapping) => mapping;
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2030
2257
|
// src/index.ts
|
|
2031
2258
|
function createMachine(context, fnsOrFactory) {
|
|
2032
2259
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -2039,8 +2266,8 @@ function createMachine(context, fnsOrFactory) {
|
|
|
2039
2266
|
}
|
|
2040
2267
|
const stored = getStoredTransitions(fnsOrFactory);
|
|
2041
2268
|
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2042
|
-
const
|
|
2043
|
-
return attachTransitions(
|
|
2269
|
+
const machine2 = Object.assign({ context }, transitions);
|
|
2270
|
+
return attachTransitions(machine2, transitions);
|
|
2044
2271
|
}
|
|
2045
2272
|
function createAsyncMachine(context, fnsOrFactory) {
|
|
2046
2273
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -2053,8 +2280,8 @@ function createAsyncMachine(context, fnsOrFactory) {
|
|
|
2053
2280
|
}
|
|
2054
2281
|
const stored = getStoredTransitions(fnsOrFactory);
|
|
2055
2282
|
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2056
|
-
const
|
|
2057
|
-
return attachTransitions(
|
|
2283
|
+
const machine2 = Object.assign({ context }, transitions);
|
|
2284
|
+
return attachTransitions(machine2, transitions);
|
|
2058
2285
|
}
|
|
2059
2286
|
function createMachineFactory() {
|
|
2060
2287
|
return (transformers) => {
|
|
@@ -2072,23 +2299,23 @@ function createMachineFactory() {
|
|
|
2072
2299
|
};
|
|
2073
2300
|
};
|
|
2074
2301
|
}
|
|
2075
|
-
function setContext(
|
|
2302
|
+
function setContext(machine2, newContextOrFn) {
|
|
2076
2303
|
var _a;
|
|
2077
|
-
const currentContext =
|
|
2078
|
-
const transitions = (_a = getStoredTransitions(
|
|
2304
|
+
const currentContext = machine2.context;
|
|
2305
|
+
const transitions = (_a = getStoredTransitions(machine2)) != null ? _a : snapshotOwnTransitions(machine2);
|
|
2079
2306
|
const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
|
|
2080
2307
|
return createMachine(newContext, transitions);
|
|
2081
2308
|
}
|
|
2082
2309
|
function createContext(context) {
|
|
2083
2310
|
return { context };
|
|
2084
2311
|
}
|
|
2085
|
-
function overrideTransitions(
|
|
2086
|
-
const { context, ...originalTransitions } =
|
|
2312
|
+
function overrideTransitions(machine2, overrides) {
|
|
2313
|
+
const { context, ...originalTransitions } = machine2;
|
|
2087
2314
|
const newTransitions = { ...originalTransitions, ...overrides };
|
|
2088
2315
|
return createMachine(context, newTransitions);
|
|
2089
2316
|
}
|
|
2090
|
-
function extendTransitions(
|
|
2091
|
-
const { context, ...originalTransitions } =
|
|
2317
|
+
function extendTransitions(machine2, newTransitions) {
|
|
2318
|
+
const { context, ...originalTransitions } = machine2;
|
|
2092
2319
|
const combinedTransitions = { ...originalTransitions, ...newTransitions };
|
|
2093
2320
|
return createMachine(context, combinedTransitions);
|
|
2094
2321
|
}
|
|
@@ -2109,16 +2336,16 @@ function createMachineBuilder(templateMachine) {
|
|
|
2109
2336
|
return createMachine(newContext, transitions);
|
|
2110
2337
|
};
|
|
2111
2338
|
}
|
|
2112
|
-
function matchMachine(
|
|
2113
|
-
const discriminant =
|
|
2339
|
+
function matchMachine(machine2, discriminantKey, handlers) {
|
|
2340
|
+
const discriminant = machine2.context[discriminantKey];
|
|
2114
2341
|
const handler = handlers[discriminant];
|
|
2115
2342
|
if (!handler) {
|
|
2116
2343
|
throw new Error(`No handler found for state: ${String(discriminant)}`);
|
|
2117
2344
|
}
|
|
2118
|
-
return handler(
|
|
2345
|
+
return handler(machine2.context);
|
|
2119
2346
|
}
|
|
2120
|
-
function hasState(
|
|
2121
|
-
return
|
|
2347
|
+
function hasState(machine2, key, value) {
|
|
2348
|
+
return machine2.context[key] === value;
|
|
2122
2349
|
}
|
|
2123
2350
|
function runMachine(initial, onChange) {
|
|
2124
2351
|
let current = initial;
|
|
@@ -2216,11 +2443,13 @@ export {
|
|
|
2216
2443
|
createTransitionExtender,
|
|
2217
2444
|
createTransitionFactory,
|
|
2218
2445
|
customCase,
|
|
2446
|
+
delegate_exports as delegate,
|
|
2219
2447
|
delegateToChild,
|
|
2220
2448
|
describe,
|
|
2221
2449
|
discriminantCase,
|
|
2222
2450
|
extendTransitions,
|
|
2223
2451
|
forContext,
|
|
2452
|
+
freeze,
|
|
2224
2453
|
fromObservable,
|
|
2225
2454
|
fromPromise,
|
|
2226
2455
|
guard,
|
|
@@ -2247,6 +2476,7 @@ export {
|
|
|
2247
2476
|
mergeContext,
|
|
2248
2477
|
metadata,
|
|
2249
2478
|
middlewareBuilder,
|
|
2479
|
+
minimal_exports as minimal,
|
|
2250
2480
|
next,
|
|
2251
2481
|
overrideTransitions,
|
|
2252
2482
|
pipeTransitions,
|
|
@@ -2262,6 +2492,7 @@ export {
|
|
|
2262
2492
|
state,
|
|
2263
2493
|
step,
|
|
2264
2494
|
stepAsync,
|
|
2495
|
+
tag,
|
|
2265
2496
|
toggle,
|
|
2266
2497
|
transitionTo,
|
|
2267
2498
|
when,
|