@doeixd/machine 1.2.0 → 1.3.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.
@@ -2218,9 +2218,10 @@ function freeze(obj) {
2218
2218
 
2219
2219
  // src/minimal.ts
2220
2220
  function machine(context, factory2) {
2221
- const next2 = (newContext) => machine(newContext, factory2);
2222
- const transitions = factory2(context, next2);
2223
- return Object.assign({}, context, transitions);
2221
+ let self;
2222
+ const next2 = (newContext) => newContext === context ? self : machine(newContext, factory2);
2223
+ self = Object.assign({}, context, factory2(context, next2));
2224
+ return self;
2224
2225
  }
2225
2226
  function match(state2, cases) {
2226
2227
  const handler = cases[state2.tag];
@@ -2307,13 +2308,20 @@ function withChildren(parent, children) {
2307
2308
  return result;
2308
2309
  }
2309
2310
  function factory() {
2310
- return (transitionFactory) => (context) => machine(context, transitionFactory);
2311
+ return (transitionFactory) => {
2312
+ const resultFactory = (context) => {
2313
+ const next2 = (c) => resultFactory(c);
2314
+ return machine(context, (ctx) => transitionFactory(ctx, next2));
2315
+ };
2316
+ return resultFactory;
2317
+ };
2311
2318
  }
2312
2319
  function union() {
2313
2320
  return (factories) => {
2314
2321
  const resultFactory = (context) => {
2315
2322
  const factoryFn = factories[context.tag];
2316
- return machine(context, (ctx, _next) => factoryFn(ctx, resultFactory));
2323
+ const next2 = (c) => resultFactory(c);
2324
+ return machine(context, (ctx) => factoryFn(ctx, next2));
2317
2325
  };
2318
2326
  return resultFactory;
2319
2327
  };
@@ -2391,12 +2399,14 @@ function renameMap() {
2391
2399
  // src/index.ts
2392
2400
  function createMachine(context, fnsOrFactory) {
2393
2401
  if (typeof fnsOrFactory === "function") {
2402
+ let self;
2394
2403
  let transitions2;
2395
2404
  const transition = (newContext) => {
2396
- return createMachine(newContext, transitions2);
2405
+ return newContext === context ? self : createMachine(newContext, transitions2);
2397
2406
  };
2398
2407
  transitions2 = fnsOrFactory(transition);
2399
- return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2408
+ self = attachTransitions(Object.assign({ context }, transitions2), transitions2);
2409
+ return self;
2400
2410
  }
2401
2411
  const stored = getStoredTransitions(fnsOrFactory);
2402
2412
  const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;