@doeixd/machine 0.0.12 → 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 +7 -0
- package/dist/cjs/development/index.js.map +2 -2
- package/dist/cjs/production/index.js +3 -3
- package/dist/esm/development/index.js +7 -0
- package/dist/esm/development/index.js.map +2 -2
- package/dist/esm/production/index.js +3 -3
- package/dist/types/functional-combinators.d.ts +57 -0
- package/dist/types/functional-combinators.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/functional-combinators.ts +73 -0
- package/src/index.ts +2 -1
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)
|
|
@@ -90,6 +90,7 @@ __export(src_exports, {
|
|
|
90
90
|
runWithEnsemble: () => runWithEnsemble,
|
|
91
91
|
runWithRunner: () => runWithRunner,
|
|
92
92
|
setContext: () => setContext,
|
|
93
|
+
state: () => state,
|
|
93
94
|
step: () => step,
|
|
94
95
|
stepAsync: () => stepAsync,
|
|
95
96
|
toggle: () => toggle,
|
|
@@ -2185,6 +2186,12 @@ function createFunctionalMachine(initialContext) {
|
|
|
2185
2186
|
return createMachine(initialContext, transitions);
|
|
2186
2187
|
};
|
|
2187
2188
|
}
|
|
2189
|
+
function state(context, transitions) {
|
|
2190
|
+
if (transitions !== void 0) {
|
|
2191
|
+
return createMachine(context, transitions);
|
|
2192
|
+
}
|
|
2193
|
+
return createFunctionalMachine(context);
|
|
2194
|
+
}
|
|
2188
2195
|
|
|
2189
2196
|
// src/index.ts
|
|
2190
2197
|
function createMachine(context, fns) {
|