@coaction/xstate 1.4.1 → 2.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @coaction/xstate
2
2
 
3
- ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
3
+ ![Node CI](https://github.com/coactionjs/coaction/workflows/Node%20CI/badge.svg)
4
4
  [![npm](https://img.shields.io/npm/v/@coaction/xstate.svg)](https://www.npmjs.com/package/@coaction/xstate)
5
5
  ![license](https://img.shields.io/npm/l/@coaction/xstate)
6
6
 
@@ -54,4 +54,4 @@ store.getState().send({ type: 'increment' });
54
54
 
55
55
  ## Documentation
56
56
 
57
- You can find the documentation [here](https://github.com/unadlib/coaction).
57
+ You can find the documentation [here](https://github.com/coactionjs/coaction).
package/dist/index.d.mts CHANGED
@@ -1,25 +1,31 @@
1
- export * from 'xstate';
1
+ export * from "xstate";
2
2
 
3
+ //#region \0rolldown/runtime.js
4
+ //#endregion
5
+ //#region packages/coaction-xstate/src/index.d.ts
3
6
  type XStateActor<TContext extends object = object, TEvent = any> = {
4
- getSnapshot: () => {
5
- context: TContext;
6
- };
7
- subscribe: (observer: (snapshot: {
8
- context: TContext;
9
- }) => void) => {
10
- unsubscribe: () => void;
11
- };
12
- send: (event: TEvent) => void;
7
+ getSnapshot: () => {
8
+ context: TContext;
9
+ };
10
+ subscribe: (observer: (snapshot: {
11
+ context: TContext;
12
+ }) => void) => {
13
+ unsubscribe: () => void;
14
+ };
15
+ send: (event: TEvent) => void;
13
16
  };
14
17
  /**
15
18
  * Bind an XState actor to Coaction.
16
19
  */
17
- declare const bindXState: <TContext extends object, TEvent>(actor: XStateActor<TContext, TEvent>) => { [K in keyof TContext]: TContext[K]; } & {
18
- send: (event: TEvent) => void;
20
+ declare const bindXState: <TContext extends object, TEvent>(actor: XStateActor<TContext, TEvent>) => { [K in keyof TContext]: TContext[K] } & {
21
+ send: (event: TEvent) => void;
19
22
  };
20
23
  /**
21
24
  * Adapt a state type for Coaction create function.
22
25
  */
23
26
  declare const adapt: <T extends object>(store: T) => T;
24
-
25
- export { adapt, bindXState };
27
+ declare namespace index_d_exports {
28
+ export { adapt, bindXState };
29
+ }
30
+ //#endregion
31
+ export { adapt, bindXState };
package/dist/index.d.ts CHANGED
@@ -1,25 +1,31 @@
1
- export * from 'xstate';
1
+ export * from "xstate";
2
2
 
3
+ //#region \0rolldown/runtime.js
4
+ //#endregion
5
+ //#region packages/coaction-xstate/src/index.d.ts
3
6
  type XStateActor<TContext extends object = object, TEvent = any> = {
4
- getSnapshot: () => {
5
- context: TContext;
6
- };
7
- subscribe: (observer: (snapshot: {
8
- context: TContext;
9
- }) => void) => {
10
- unsubscribe: () => void;
11
- };
12
- send: (event: TEvent) => void;
7
+ getSnapshot: () => {
8
+ context: TContext;
9
+ };
10
+ subscribe: (observer: (snapshot: {
11
+ context: TContext;
12
+ }) => void) => {
13
+ unsubscribe: () => void;
14
+ };
15
+ send: (event: TEvent) => void;
13
16
  };
14
17
  /**
15
18
  * Bind an XState actor to Coaction.
16
19
  */
17
- declare const bindXState: <TContext extends object, TEvent>(actor: XStateActor<TContext, TEvent>) => { [K in keyof TContext]: TContext[K]; } & {
18
- send: (event: TEvent) => void;
20
+ declare const bindXState: <TContext extends object, TEvent>(actor: XStateActor<TContext, TEvent>) => { [K in keyof TContext]: TContext[K] } & {
21
+ send: (event: TEvent) => void;
19
22
  };
20
23
  /**
21
24
  * Adapt a state type for Coaction create function.
22
25
  */
23
26
  declare const adapt: <T extends object>(store: T) => T;
24
-
25
- export { adapt, bindXState };
27
+ declare namespace index_d_exports {
28
+ export { adapt, bindXState };
29
+ }
30
+ //#endregion
31
+ export { adapt, bindXState };
package/dist/index.js CHANGED
@@ -1,91 +1,91 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#endregion
3
+ let coaction = require("coaction");
4
+ //#region packages/coaction-xstate/src/index.ts
5
+ const actorMap = /* @__PURE__ */ new WeakMap();
6
+ const isUnsafeKey = (key) => typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
7
+ const getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
8
+ const assignContext = (target, source) => {
9
+ for (const key of getOwnEnumerableKeys(source)) {
10
+ if (isUnsafeKey(key)) continue;
11
+ target[key] = source[key];
12
+ }
9
13
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
-
21
- // index.ts
22
- var index_exports = {};
23
- __export(index_exports, {
24
- adapt: () => adapt,
25
- bindXState: () => bindXState
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
-
29
- // src/index.ts
30
- var src_exports = {};
31
- __export(src_exports, {
32
- adapt: () => adapt,
33
- bindXState: () => bindXState
34
- });
35
- var import_coaction = require("coaction");
36
- __reExport(src_exports, require("xstate"));
37
- var actorMap = /* @__PURE__ */ new WeakMap();
38
- var bindXState = (0, import_coaction.createBinder)({
39
- handleStore: (store, rawState) => {
40
- const actor = actorMap.get(rawState);
41
- if (!actor) {
42
- throw new Error("xstate actor is not found");
43
- }
44
- let isFromActor = false;
45
- const baseSetState = store.setState;
46
- store.setState = (next, updater) => {
47
- if (!isFromActor) {
48
- throw new Error(
49
- "setState is not supported with xstate binding. Please use actor events."
50
- );
51
- }
52
- return baseSetState(next, updater);
53
- };
54
- const subscription = actor.subscribe((snapshot) => {
55
- isFromActor = true;
56
- try {
57
- baseSetState(snapshot.context);
58
- } finally {
59
- isFromActor = false;
60
- }
61
- });
62
- const baseDestroy = store.destroy;
63
- store.destroy = () => {
64
- subscription.unsubscribe();
65
- baseDestroy();
66
- };
67
- },
68
- handleState: ((actor) => {
69
- const snapshot = actor.getSnapshot();
70
- const state = Object.assign({}, snapshot.context, {
71
- send: actor.send.bind(actor)
72
- });
73
- const descriptors = Object.getOwnPropertyDescriptors(state);
74
- const copyState = Object.defineProperties({}, descriptors);
75
- const rawState = Object.defineProperties({}, descriptors);
76
- actorMap.set(rawState, actor);
77
- return {
78
- copyState,
79
- bind: () => rawState
80
- };
81
- })
14
+ const unsupportedMutationMessage = "XState binding state cannot be mutated directly. Please use actor events.";
15
+ /**
16
+ * Bind an XState actor to Coaction.
17
+ */
18
+ const bindXState = (0, coaction.createBinder)({
19
+ handleStore: (store, rawState, _state, internal) => {
20
+ const actor = actorMap.get(rawState);
21
+ if (!actor) throw new Error("xstate actor is not found");
22
+ store.setState = () => {
23
+ throw new Error(unsupportedMutationMessage);
24
+ };
25
+ if (store.share === "client") return;
26
+ let isApplyingActorSnapshot = false;
27
+ internal.assertMutationAllowed = (operation) => {
28
+ if (operation === "apply" && isApplyingActorSnapshot) return;
29
+ throw new Error(unsupportedMutationMessage);
30
+ };
31
+ const baseApply = store.apply.bind(store);
32
+ store.apply = (state, patches) => {
33
+ if (!isApplyingActorSnapshot) throw new Error(unsupportedMutationMessage);
34
+ return baseApply(state, patches);
35
+ };
36
+ const applyActorSnapshot = (context) => {
37
+ isApplyingActorSnapshot = true;
38
+ try {
39
+ (0, coaction.replaceExternalStoreState)(store, internal, context);
40
+ } finally {
41
+ isApplyingActorSnapshot = false;
42
+ }
43
+ };
44
+ let subscription;
45
+ const cancelReadySubscription = (0, coaction.onStoreReady)(store, () => {
46
+ subscription = actor.subscribe((snapshot) => {
47
+ applyActorSnapshot(snapshot.context);
48
+ });
49
+ });
50
+ const baseDestroy = store.destroy;
51
+ let destroyed = false;
52
+ store.destroy = () => {
53
+ if (destroyed) return;
54
+ destroyed = true;
55
+ cancelReadySubscription();
56
+ subscription?.unsubscribe();
57
+ subscription = void 0;
58
+ baseDestroy();
59
+ };
60
+ },
61
+ handleState: ((actor) => {
62
+ const snapshot = actor.getSnapshot();
63
+ const state = {};
64
+ assignContext(state, snapshot.context);
65
+ state.send = actor.send.bind(actor);
66
+ const descriptors = Object.getOwnPropertyDescriptors(state);
67
+ const copyState = Object.defineProperties({}, descriptors);
68
+ const rawState = Object.defineProperties({}, descriptors);
69
+ actorMap.set(rawState, actor);
70
+ return {
71
+ copyState,
72
+ bind: () => rawState
73
+ };
74
+ })
82
75
  });
83
- var adapt = (store) => store;
84
-
85
- // index.ts
86
- __reExport(index_exports, src_exports, module.exports);
87
- // Annotate the CommonJS export names for ESM import in node:
88
- 0 && (module.exports = {
89
- adapt,
90
- bindXState
76
+ /**
77
+ * Adapt a state type for Coaction create function.
78
+ */
79
+ const adapt = (store) => store;
80
+ //#endregion
81
+ exports.adapt = adapt;
82
+ exports.bindXState = bindXState;
83
+ var xstate = require("xstate");
84
+ Object.keys(xstate).forEach(function(k) {
85
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
86
+ enumerable: true,
87
+ get: function() {
88
+ return xstate[k];
89
+ }
90
+ });
91
91
  });
package/dist/index.mjs CHANGED
@@ -1,88 +1,81 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ import { createBinder, onStoreReady, replaceExternalStoreState } from "coaction";
2
+ export * from "xstate";
3
+ //#endregion
4
+ //#region packages/coaction-xstate/src/index.ts
5
+ const actorMap = /* @__PURE__ */ new WeakMap();
6
+ const isUnsafeKey = (key) => typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
7
+ const getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
8
+ const assignContext = (target, source) => {
9
+ for (const key of getOwnEnumerableKeys(source)) {
10
+ if (isUnsafeKey(key)) continue;
11
+ target[key] = source[key];
12
+ }
8
13
  };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
-
19
- // index.ts
20
- var index_exports = {};
21
- __export(index_exports, {
22
- adapt: () => adapt,
23
- bindXState: () => bindXState
24
- });
25
-
26
- // src/index.ts
27
- var src_exports = {};
28
- __export(src_exports, {
29
- adapt: () => adapt,
30
- bindXState: () => bindXState
14
+ const unsupportedMutationMessage = "XState binding state cannot be mutated directly. Please use actor events.";
15
+ /**
16
+ * Bind an XState actor to Coaction.
17
+ */
18
+ const bindXState = createBinder({
19
+ handleStore: (store, rawState, _state, internal) => {
20
+ const actor = actorMap.get(rawState);
21
+ if (!actor) throw new Error("xstate actor is not found");
22
+ store.setState = () => {
23
+ throw new Error(unsupportedMutationMessage);
24
+ };
25
+ if (store.share === "client") return;
26
+ let isApplyingActorSnapshot = false;
27
+ internal.assertMutationAllowed = (operation) => {
28
+ if (operation === "apply" && isApplyingActorSnapshot) return;
29
+ throw new Error(unsupportedMutationMessage);
30
+ };
31
+ const baseApply = store.apply.bind(store);
32
+ store.apply = (state, patches) => {
33
+ if (!isApplyingActorSnapshot) throw new Error(unsupportedMutationMessage);
34
+ return baseApply(state, patches);
35
+ };
36
+ const applyActorSnapshot = (context) => {
37
+ isApplyingActorSnapshot = true;
38
+ try {
39
+ replaceExternalStoreState(store, internal, context);
40
+ } finally {
41
+ isApplyingActorSnapshot = false;
42
+ }
43
+ };
44
+ let subscription;
45
+ const cancelReadySubscription = onStoreReady(store, () => {
46
+ subscription = actor.subscribe((snapshot) => {
47
+ applyActorSnapshot(snapshot.context);
48
+ });
49
+ });
50
+ const baseDestroy = store.destroy;
51
+ let destroyed = false;
52
+ store.destroy = () => {
53
+ if (destroyed) return;
54
+ destroyed = true;
55
+ cancelReadySubscription();
56
+ subscription?.unsubscribe();
57
+ subscription = void 0;
58
+ baseDestroy();
59
+ };
60
+ },
61
+ handleState: ((actor) => {
62
+ const snapshot = actor.getSnapshot();
63
+ const state = {};
64
+ assignContext(state, snapshot.context);
65
+ state.send = actor.send.bind(actor);
66
+ const descriptors = Object.getOwnPropertyDescriptors(state);
67
+ const copyState = Object.defineProperties({}, descriptors);
68
+ const rawState = Object.defineProperties({}, descriptors);
69
+ actorMap.set(rawState, actor);
70
+ return {
71
+ copyState,
72
+ bind: () => rawState
73
+ };
74
+ })
31
75
  });
32
- __reExport(src_exports, xstate_star);
33
- import { createBinder } from "coaction";
34
- import * as xstate_star from "xstate";
35
- var actorMap = /* @__PURE__ */ new WeakMap();
36
- var bindXState = createBinder({
37
- handleStore: (store, rawState) => {
38
- const actor = actorMap.get(rawState);
39
- if (!actor) {
40
- throw new Error("xstate actor is not found");
41
- }
42
- let isFromActor = false;
43
- const baseSetState = store.setState;
44
- store.setState = (next, updater) => {
45
- if (!isFromActor) {
46
- throw new Error(
47
- "setState is not supported with xstate binding. Please use actor events."
48
- );
49
- }
50
- return baseSetState(next, updater);
51
- };
52
- const subscription = actor.subscribe((snapshot) => {
53
- isFromActor = true;
54
- try {
55
- baseSetState(snapshot.context);
56
- } finally {
57
- isFromActor = false;
58
- }
59
- });
60
- const baseDestroy = store.destroy;
61
- store.destroy = () => {
62
- subscription.unsubscribe();
63
- baseDestroy();
64
- };
65
- },
66
- handleState: ((actor) => {
67
- const snapshot = actor.getSnapshot();
68
- const state = Object.assign({}, snapshot.context, {
69
- send: actor.send.bind(actor)
70
- });
71
- const descriptors = Object.getOwnPropertyDescriptors(state);
72
- const copyState = Object.defineProperties({}, descriptors);
73
- const rawState = Object.defineProperties({}, descriptors);
74
- actorMap.set(rawState, actor);
75
- return {
76
- copyState,
77
- bind: () => rawState
78
- };
79
- })
80
- });
81
- var adapt = (store) => store;
82
-
83
- // index.ts
84
- __reExport(index_exports, src_exports);
85
- export {
86
- adapt,
87
- bindXState
88
- };
76
+ /**
77
+ * Adapt a state type for Coaction create function.
78
+ */
79
+ const adapt = (store) => store;
80
+ //#endregion
81
+ export { adapt, bindXState };
package/package.json CHANGED
@@ -1,20 +1,31 @@
1
1
  {
2
2
  "name": "@coaction/xstate",
3
- "version": "1.4.1",
3
+ "version": "2.0.0",
4
4
  "description": "A Coaction integration tool for XState",
5
5
  "keywords": [
6
- "state",
6
+ "actor",
7
7
  "coaction",
8
- "xstate",
9
- "actor"
10
- ],
11
- "authors": [
12
- "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
8
+ "state",
9
+ "xstate"
13
10
  ],
14
- "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-xstate#readme",
11
+ "homepage": "https://github.com/coactionjs/coaction/tree/main/packages/coaction-xstate#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/coactionjs/coaction/issues"
14
+ },
15
15
  "license": "MIT",
16
+ "author": "unadlib",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/coactionjs/coaction.git",
20
+ "directory": "packages/coaction-xstate"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "sideEffects": false,
16
26
  "main": "dist/index.js",
17
27
  "module": "dist/index.mjs",
28
+ "types": "dist/index.d.ts",
18
29
  "exports": {
19
30
  ".": {
20
31
  "types": "./dist/index.d.ts",
@@ -24,44 +35,25 @@
24
35
  },
25
36
  "./package.json": "./package.json"
26
37
  },
27
- "types": "dist/index.d.ts",
28
- "sideEffects": false,
29
- "files": [
30
- "dist"
31
- ],
32
- "repository": {
33
- "type": "git",
34
- "url": "https://github.com/unadlib/coaction.git",
35
- "directory": "packages/coaction-xstate"
36
- },
37
- "bugs": {
38
- "url": "https://github.com/unadlib/coaction/issues"
39
- },
40
- "peerDependencies": {
41
- "coaction": "^1.4.1",
42
- "xstate": "^5.0.0"
43
- },
44
- "peerDependenciesMeta": {
45
- "coaction": {
46
- "optional": true
47
- },
48
- "xstate": {
49
- "optional": true
50
- }
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true
51
41
  },
52
42
  "devDependencies": {
53
43
  "xstate": "^5.23.0",
54
- "coaction": "1.4.1"
44
+ "coaction": "2.0.0"
55
45
  },
56
- "publishConfig": {
57
- "access": "public",
58
- "provenance": true
46
+ "peerDependencies": {
47
+ "coaction": "^2.0.0",
48
+ "xstate": "^5.0.0"
59
49
  },
60
- "author": "unadlib",
50
+ "authors": [
51
+ "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
52
+ ],
61
53
  "scripts": {
62
54
  "clean": "rm -rf dist",
63
55
  "test": "vitest run test",
64
- "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
65
- "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
56
+ "build": "node ../../scripts/build-package.mjs",
57
+ "dev": "node ../../scripts/build-package.mjs --watch"
66
58
  }
67
59
  }