@doeixd/machine 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/development/core.js +39 -14
- package/dist/cjs/development/core.js.map +3 -3
- package/dist/cjs/development/index.js +51 -15
- package/dist/cjs/development/index.js.map +3 -3
- package/dist/cjs/development/react.js +39 -14
- package/dist/cjs/development/react.js.map +3 -3
- package/dist/cjs/production/core.js +1 -1
- package/dist/cjs/production/index.js +2 -2
- package/dist/cjs/production/react.js +1 -1
- package/dist/esm/development/core.js +39 -14
- package/dist/esm/development/core.js.map +3 -3
- package/dist/esm/development/index.js +51 -15
- package/dist/esm/development/index.js.map +3 -3
- package/dist/esm/development/react.js +39 -14
- package/dist/esm/development/react.js.map +3 -3
- package/dist/esm/production/core.js +1 -1
- package/dist/esm/production/index.js +2 -2
- package/dist/esm/production/react.js +1 -1
- package/dist/types/context-bound.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/internal-transitions.d.ts +5 -0
- package/dist/types/internal-transitions.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/context-bound.ts +14 -1
- package/src/index.ts +19 -15
- package/src/internal-transitions.ts +32 -0
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* @author doeixd
|
|
4
4
|
* @version 1.0.0
|
|
5
5
|
*/
|
|
6
|
+
import { attachTransitions, getStoredTransitions, snapshotOwnTransitions } from './internal-transitions';
|
|
6
7
|
|
|
7
8
|
// =============================================================================
|
|
8
9
|
// SECTION: CORE TYPES & INTERFACES
|
|
@@ -389,16 +390,17 @@ export function createMachine(context: any, fnsOrFactory: any): any {
|
|
|
389
390
|
};
|
|
390
391
|
transitions = fnsOrFactory(transition);
|
|
391
392
|
|
|
392
|
-
return Object.assign({ context }, transitions);
|
|
393
|
+
return attachTransitions(Object.assign({ context }, transitions), transitions);
|
|
393
394
|
}
|
|
394
395
|
|
|
395
396
|
// If fns is a machine (has context property), extract just the transition functions
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
|
|
397
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
398
|
+
const transitions = stored ?? ('context' in fnsOrFactory
|
|
399
|
+
? snapshotOwnTransitions(fnsOrFactory)
|
|
400
|
+
: fnsOrFactory);
|
|
399
401
|
|
|
400
402
|
const machine = Object.assign({ context }, transitions);
|
|
401
|
-
return machine;
|
|
403
|
+
return attachTransitions(machine, transitions);
|
|
402
404
|
}
|
|
403
405
|
|
|
404
406
|
/**
|
|
@@ -453,16 +455,17 @@ export function createAsyncMachine(context: any, fnsOrFactory: any): any {
|
|
|
453
455
|
};
|
|
454
456
|
transitions = fnsOrFactory(transition);
|
|
455
457
|
|
|
456
|
-
return Object.assign({ context }, transitions);
|
|
458
|
+
return attachTransitions(Object.assign({ context }, transitions), transitions);
|
|
457
459
|
}
|
|
458
460
|
|
|
459
461
|
// If fns is a machine (has context property), extract just the transition functions
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
|
|
462
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
463
|
+
const transitions = stored ?? ('context' in fnsOrFactory
|
|
464
|
+
? snapshotOwnTransitions(fnsOrFactory)
|
|
465
|
+
: fnsOrFactory);
|
|
463
466
|
|
|
464
467
|
const machine = Object.assign({ context }, transitions);
|
|
465
|
-
return machine;
|
|
468
|
+
return attachTransitions(machine, transitions);
|
|
466
469
|
}
|
|
467
470
|
|
|
468
471
|
/**
|
|
@@ -527,10 +530,12 @@ export function setContext<M extends Machine<any>>(
|
|
|
527
530
|
machine: M,
|
|
528
531
|
newContextOrFn: Context<M> | ((ctx: Readonly<Context<M>>) => Context<M>)
|
|
529
532
|
): M {
|
|
530
|
-
const
|
|
533
|
+
const currentContext = machine.context;
|
|
534
|
+
const transitions =
|
|
535
|
+
getStoredTransitions(machine) ?? snapshotOwnTransitions(machine);
|
|
531
536
|
const newContext =
|
|
532
537
|
typeof newContextOrFn === "function"
|
|
533
|
-
? (newContextOrFn as (ctx: Readonly<Context<M>>) => Context<M>)(
|
|
538
|
+
? (newContextOrFn as (ctx: Readonly<Context<M>>) => Context<M>)(currentContext)
|
|
534
539
|
: newContextOrFn;
|
|
535
540
|
|
|
536
541
|
return createMachine(newContext, transitions as any) as M;
|
|
@@ -938,8 +943,7 @@ export function next<C extends object>(
|
|
|
938
943
|
m: Machine<C>,
|
|
939
944
|
update: (ctx: Readonly<C>) => C
|
|
940
945
|
): Machine<C> {
|
|
941
|
-
|
|
942
|
-
return createMachine(update(context), transitions as any) as Machine<C>;
|
|
946
|
+
return setContext(m, (ctx) => update(ctx)) as Machine<C>;
|
|
943
947
|
}
|
|
944
948
|
|
|
945
949
|
/**
|
|
@@ -1127,4 +1131,4 @@ export {
|
|
|
1127
1131
|
callWithContext,
|
|
1128
1132
|
isContextBound,
|
|
1129
1133
|
type ContextBoundMachine
|
|
1130
|
-
} from './context-bound';
|
|
1134
|
+
} from './context-bound';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
|
|
2
|
+
|
|
3
|
+
export type TransitionMap = Record<string, (...args: any[]) => any>;
|
|
4
|
+
|
|
5
|
+
export function attachTransitions<T extends object>(
|
|
6
|
+
machine: T,
|
|
7
|
+
transitions: TransitionMap
|
|
8
|
+
): T {
|
|
9
|
+
Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
|
|
10
|
+
value: transitions,
|
|
11
|
+
enumerable: false,
|
|
12
|
+
configurable: false,
|
|
13
|
+
});
|
|
14
|
+
return machine;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getStoredTransitions(machine: any): TransitionMap | undefined {
|
|
18
|
+
if (!machine || typeof machine !== "object") {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
return machine[TRANSITIONS_SYMBOL];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function snapshotOwnTransitions(source: any): TransitionMap {
|
|
25
|
+
if (!source || typeof source !== "object") {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
const entries = Object.entries(source).filter(
|
|
29
|
+
([key, value]) => key !== "context" && typeof value === "function"
|
|
30
|
+
);
|
|
31
|
+
return Object.fromEntries(entries) as TransitionMap;
|
|
32
|
+
}
|