@coaction/xstate 1.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/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/index.d.mts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +91 -0
- package/dist/index.mjs +88 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Michael Lin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @coaction/xstate
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://www.npmjs.com/package/@coaction/xstate)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A Coaction integration tool for XState.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
You can install it via npm, yarn or pnpm.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install coaction @coaction/xstate xstate
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { create } from 'coaction';
|
|
21
|
+
import {
|
|
22
|
+
adapt,
|
|
23
|
+
assign,
|
|
24
|
+
bindXState,
|
|
25
|
+
createActor,
|
|
26
|
+
createMachine
|
|
27
|
+
} from '@coaction/xstate';
|
|
28
|
+
|
|
29
|
+
const machine = createMachine({
|
|
30
|
+
context: {
|
|
31
|
+
count: 0
|
|
32
|
+
},
|
|
33
|
+
on: {
|
|
34
|
+
increment: {
|
|
35
|
+
actions: assign({
|
|
36
|
+
count: ({ context }) => context.count + 1
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const actor = createActor(machine);
|
|
43
|
+
actor.start();
|
|
44
|
+
|
|
45
|
+
const store = create(() => adapt(bindXState(actor)));
|
|
46
|
+
store.getState().send({ type: 'increment' });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Limitations
|
|
50
|
+
|
|
51
|
+
- `@coaction/xstate` only supports binding a whole XState actor.
|
|
52
|
+
- Coaction `Slices` mode is not supported in this adapter.
|
|
53
|
+
- `setState()` is blocked; update state through actor events.
|
|
54
|
+
|
|
55
|
+
## Documentation
|
|
56
|
+
|
|
57
|
+
You can find the documentation [here](https://github.com/unadlib/coaction).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from 'xstate';
|
|
2
|
+
|
|
3
|
+
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;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Bind an XState actor to Coaction.
|
|
16
|
+
*/
|
|
17
|
+
declare const bindXState: <TContext extends object, TEvent>(actor: XStateActor<TContext, TEvent>) => { [K in keyof TContext]: TContext[K]; } & {
|
|
18
|
+
send: (event: TEvent) => void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Adapt a state type for Coaction create function.
|
|
22
|
+
*/
|
|
23
|
+
declare const adapt: <T extends object>(store: T) => T;
|
|
24
|
+
|
|
25
|
+
export { adapt, bindXState };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from 'xstate';
|
|
2
|
+
|
|
3
|
+
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;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Bind an XState actor to Coaction.
|
|
16
|
+
*/
|
|
17
|
+
declare const bindXState: <TContext extends object, TEvent>(actor: XStateActor<TContext, TEvent>) => { [K in keyof TContext]: TContext[K]; } & {
|
|
18
|
+
send: (event: TEvent) => void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Adapt a state type for Coaction create function.
|
|
22
|
+
*/
|
|
23
|
+
declare const adapt: <T extends object>(store: T) => T;
|
|
24
|
+
|
|
25
|
+
export { adapt, bindXState };
|
package/dist/index.js
ADDED
|
@@ -0,0 +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 });
|
|
9
|
+
};
|
|
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
|
+
})
|
|
82
|
+
});
|
|
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
|
|
91
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
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 });
|
|
8
|
+
};
|
|
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
|
|
31
|
+
});
|
|
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
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coaction/xstate",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Coaction integration tool for XState",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"state",
|
|
7
|
+
"coaction",
|
|
8
|
+
"xstate",
|
|
9
|
+
"actor"
|
|
10
|
+
],
|
|
11
|
+
"authors": [
|
|
12
|
+
"Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-xstate#readme",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"module": "dist/index.mjs",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.js",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
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": "^0.2.0",
|
|
42
|
+
"xstate": "^5.0.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"coaction": {
|
|
46
|
+
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"xstate": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"coaction": "^0.2.0",
|
|
54
|
+
"xstate": "^5.23.0"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public",
|
|
58
|
+
"provenance": true
|
|
59
|
+
},
|
|
60
|
+
"author": "unadlib",
|
|
61
|
+
"scripts": {
|
|
62
|
+
"clean": "rm -rf dist",
|
|
63
|
+
"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"
|
|
66
|
+
}
|
|
67
|
+
}
|