@doeixd/machine 0.0.11 → 0.0.13
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 +23 -0
- package/dist/cjs/development/index.js +62 -0
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/production/index.js +4 -4
- package/dist/esm/development/index.js +62 -0
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/production/index.js +3 -3
- package/dist/types/functional-combinators.d.ts +173 -0
- package/dist/types/functional-combinators.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/multi.d.ts +14 -0
- package/dist/types/multi.d.ts.map +1 -1
- package/dist/types/utils.d.ts +63 -0
- package/dist/types/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/functional-combinators.ts +340 -0
- package/src/index.ts +13 -1
- package/src/multi.ts +36 -0
- package/src/utils.ts +74 -0
package/README.md
CHANGED
|
@@ -112,6 +112,29 @@ counter.increment();
|
|
|
112
112
|
console.log(counter.context.count); // 1 (mutated in place)
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
+
### Smart State Creation with `state()`
|
|
116
|
+
|
|
117
|
+
The `state()` function automatically chooses between traditional and functional patterns:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { state } from "@doeixd/machine";
|
|
121
|
+
|
|
122
|
+
// Traditional pattern (like createMachine)
|
|
123
|
+
const counter1 = state({ count: 0 }, {
|
|
124
|
+
increment() { return state({ count: this.context.count + 1 }, this); }
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Functional pattern (like createFunctionalMachine)
|
|
128
|
+
const createCounter = state({ count: 0 });
|
|
129
|
+
const counter2 = createCounter({
|
|
130
|
+
increment: ctx => ({ count: ctx.count + 1 }),
|
|
131
|
+
add: (ctx, n: number) => ({ count: ctx.count + n })
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
console.log(counter1.increment().context.count); // 1
|
|
135
|
+
console.log(counter2.increment().add(5).context.count); // 6
|
|
136
|
+
```
|
|
137
|
+
|
|
115
138
|
This shows the **flexibility** of the library: immutability is the default pattern because it's safer, but you can choose mutability when it makes sense for your use case.
|
|
116
139
|
|
|
117
140
|
### Type-State Programming (Compile-Time State Safety)
|
|
@@ -38,9 +38,11 @@ __export(src_exports, {
|
|
|
38
38
|
createAsyncMachine: () => createAsyncMachine,
|
|
39
39
|
createCustomMiddleware: () => createCustomMiddleware,
|
|
40
40
|
createEnsemble: () => createEnsemble,
|
|
41
|
+
createEnsembleFactory: () => createEnsembleFactory,
|
|
41
42
|
createEvent: () => createEvent,
|
|
42
43
|
createFetchMachine: () => createFetchMachine,
|
|
43
44
|
createFlow: () => createFlow,
|
|
45
|
+
createFunctionalMachine: () => createFunctionalMachine,
|
|
44
46
|
createMachine: () => createMachine,
|
|
45
47
|
createMachineBuilder: () => createMachineBuilder,
|
|
46
48
|
createMachineFactory: () => createMachineFactory,
|
|
@@ -51,6 +53,9 @@ __export(src_exports, {
|
|
|
51
53
|
createParallelMachine: () => createParallelMachine,
|
|
52
54
|
createPipeline: () => createPipeline,
|
|
53
55
|
createRunner: () => createRunner,
|
|
56
|
+
createTransition: () => createTransition,
|
|
57
|
+
createTransitionExtender: () => createTransitionExtender,
|
|
58
|
+
createTransitionFactory: () => createTransitionFactory,
|
|
54
59
|
delegateToChild: () => delegateToChild,
|
|
55
60
|
describe: () => describe,
|
|
56
61
|
extendTransitions: () => extendTransitions,
|
|
@@ -85,6 +90,7 @@ __export(src_exports, {
|
|
|
85
90
|
runWithEnsemble: () => runWithEnsemble,
|
|
86
91
|
runWithRunner: () => runWithRunner,
|
|
87
92
|
setContext: () => setContext,
|
|
93
|
+
state: () => state,
|
|
88
94
|
step: () => step,
|
|
89
95
|
stepAsync: () => stepAsync,
|
|
90
96
|
toggle: () => toggle,
|
|
@@ -935,6 +941,11 @@ function createEnsemble(store, factories, getDiscriminant) {
|
|
|
935
941
|
actions
|
|
936
942
|
};
|
|
937
943
|
}
|
|
944
|
+
function createEnsembleFactory(store, getDiscriminant) {
|
|
945
|
+
return function withFactories(factories) {
|
|
946
|
+
return createEnsemble(store, factories, getDiscriminant);
|
|
947
|
+
};
|
|
948
|
+
}
|
|
938
949
|
function runWithRunner(flow, initialMachine) {
|
|
939
950
|
const runner = createRunner(initialMachine);
|
|
940
951
|
const generator = flow(runner);
|
|
@@ -2084,6 +2095,12 @@ function logState(machine, label) {
|
|
|
2084
2095
|
}
|
|
2085
2096
|
return machine;
|
|
2086
2097
|
}
|
|
2098
|
+
function createTransition(getTransitions, transformer) {
|
|
2099
|
+
return function(...args) {
|
|
2100
|
+
const nextContext = transformer(this.context, ...args);
|
|
2101
|
+
return createMachine(nextContext, getTransitions());
|
|
2102
|
+
};
|
|
2103
|
+
}
|
|
2087
2104
|
function call(fn, context, ...args) {
|
|
2088
2105
|
return fn.apply(context, args);
|
|
2089
2106
|
}
|
|
@@ -2131,6 +2148,51 @@ var BoundMachine = class _BoundMachine {
|
|
|
2131
2148
|
}
|
|
2132
2149
|
};
|
|
2133
2150
|
|
|
2151
|
+
// src/functional-combinators.ts
|
|
2152
|
+
function createTransitionFactory() {
|
|
2153
|
+
return function createTransition2(transformer) {
|
|
2154
|
+
return function(...args) {
|
|
2155
|
+
const nextContext = transformer(this.context, ...args);
|
|
2156
|
+
return createMachine(nextContext, this);
|
|
2157
|
+
};
|
|
2158
|
+
};
|
|
2159
|
+
}
|
|
2160
|
+
function createTransitionExtender(machine) {
|
|
2161
|
+
return {
|
|
2162
|
+
machine,
|
|
2163
|
+
addTransition: function(name, transformer) {
|
|
2164
|
+
const transitionFn = function(...args) {
|
|
2165
|
+
const nextContext = transformer(this.context, ...args);
|
|
2166
|
+
return createMachine(nextContext, this);
|
|
2167
|
+
};
|
|
2168
|
+
const newMachine = extendTransitions(machine, { [name]: transitionFn });
|
|
2169
|
+
return createTransitionExtender(newMachine);
|
|
2170
|
+
}
|
|
2171
|
+
};
|
|
2172
|
+
}
|
|
2173
|
+
function createFunctionalMachine(initialContext) {
|
|
2174
|
+
return function withTransitions(transformers) {
|
|
2175
|
+
const transitions = {};
|
|
2176
|
+
const machineTransitions = Object.fromEntries(
|
|
2177
|
+
Object.entries(transformers).map(([key, transformer]) => [
|
|
2178
|
+
key,
|
|
2179
|
+
function(...args) {
|
|
2180
|
+
const nextContext = transformer(this.context, ...args);
|
|
2181
|
+
return createMachine(nextContext, transitions);
|
|
2182
|
+
}
|
|
2183
|
+
])
|
|
2184
|
+
);
|
|
2185
|
+
Object.assign(transitions, machineTransitions);
|
|
2186
|
+
return createMachine(initialContext, transitions);
|
|
2187
|
+
};
|
|
2188
|
+
}
|
|
2189
|
+
function state(context, transitions) {
|
|
2190
|
+
if (transitions !== void 0) {
|
|
2191
|
+
return createMachine(context, transitions);
|
|
2192
|
+
}
|
|
2193
|
+
return createFunctionalMachine(context);
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2134
2196
|
// src/index.ts
|
|
2135
2197
|
function createMachine(context, fns) {
|
|
2136
2198
|
const transitions = "context" in fns ? Object.fromEntries(
|