@minutemailer/kit 0.1.2 → 0.1.3
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/package.json +3 -3
- package/state/index.js +62 -52
- package/state/index.js.map +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@minutemailer/kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Minutemailer UI Kit",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Minutemailer",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
"./utils": "./utils/index.js",
|
|
10
10
|
"./icons": "./icons/index.js",
|
|
11
|
-
"./store": "./store/index.js"
|
|
11
|
+
"./store": "./store/index.js",
|
|
12
|
+
"./state": "./state/index.js"
|
|
12
13
|
},
|
|
13
14
|
"sideEffects": false,
|
|
14
15
|
"scripts": {
|
|
@@ -35,7 +36,6 @@
|
|
|
35
36
|
"@storybook/test": "^8.6.2",
|
|
36
37
|
"@storybook/theming": "^8.6.3",
|
|
37
38
|
"@svgr/cli": "^8.1.0",
|
|
38
|
-
"@testing-library/react": "^16.2.0",
|
|
39
39
|
"@vitest/coverage-v8": "^3.0.7",
|
|
40
40
|
"esbuild": "^0.25.0",
|
|
41
41
|
"react": "^18.0.0",
|
package/state/index.js
CHANGED
|
@@ -1,53 +1,63 @@
|
|
|
1
|
-
import { useEffect, useState } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
function createMachine(transitions, actions, initialContext, initialState = "IDLE") {
|
|
3
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
4
|
+
let state = initialState;
|
|
5
|
+
let context = initialContext;
|
|
6
|
+
return {
|
|
7
|
+
subscribe(listener) {
|
|
8
|
+
listeners.add(listener);
|
|
9
|
+
return () => listeners.delete(listener);
|
|
10
|
+
},
|
|
11
|
+
destroy() {
|
|
12
|
+
listeners.clear();
|
|
13
|
+
state = initialState;
|
|
14
|
+
context = initialContext;
|
|
15
|
+
},
|
|
16
|
+
action(action, input) {
|
|
17
|
+
const transition = transitions[state];
|
|
18
|
+
const nextState = transition.on[action];
|
|
19
|
+
if (!nextState) {
|
|
20
|
+
console.warn("Invalid transition", state, action);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
console.log(
|
|
24
|
+
`Machine transition: ${state} -> ${nextState} (${action}, ${input})`
|
|
25
|
+
);
|
|
26
|
+
const previousState = state;
|
|
27
|
+
if (actions[action]) {
|
|
28
|
+
context = actions[action].call(this, context, input) || context;
|
|
29
|
+
}
|
|
30
|
+
state = nextState;
|
|
31
|
+
listeners.forEach(
|
|
32
|
+
(listener) => listener(state, previousState, context)
|
|
33
|
+
);
|
|
34
|
+
},
|
|
35
|
+
can(action) {
|
|
36
|
+
const transition = transitions[state];
|
|
37
|
+
return !!transition.on[action];
|
|
38
|
+
},
|
|
39
|
+
useCurrentState() {
|
|
40
|
+
const [currentState, setCurrentState] = useState(state);
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
return this.subscribe(() => setCurrentState(state));
|
|
43
|
+
}, []);
|
|
44
|
+
return currentState;
|
|
45
|
+
},
|
|
46
|
+
useCurrentContext(selector) {
|
|
47
|
+
const [currentContext, setCurrentContext] = useState(
|
|
48
|
+
() => selector(context)
|
|
49
|
+
);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
return this.subscribe(() => setCurrentContext(selector(context)));
|
|
52
|
+
}, []);
|
|
53
|
+
return currentContext;
|
|
54
|
+
},
|
|
55
|
+
getContext() {
|
|
56
|
+
return context;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
53
59
|
}
|
|
60
|
+
export {
|
|
61
|
+
createMachine
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/state/index.ts"],
|
|
4
|
+
"sourcesContent": ["import { useEffect, useState } from 'react';\n\ninterface StateMachine<TContext> {\n subscribe: (listener: StateChange<TContext>) => () => void;\n destroy: () => void;\n action: (action: string, input?: any) => void;\n can: (action: string) => boolean;\n useCurrentState: () => string;\n useCurrentContext: <TSelected = any>(selector: (context: TContext) => TSelected) => TSelected;\n getContext: () => TContext;\n}\n\ninterface Transitions {\n [state: string]: {\n on: {\n [action: string]: string;\n };\n };\n}\n\ninterface Actions {\n [action: string]: (context: any, input?: any) => any;\n}\n\ntype StateChange<TContext> = (state: string, previousState: string, context: TContext) => void;\n\nexport function createMachine<TContext = any>(\n transitions: Transitions,\n actions: Actions,\n initialContext: TContext,\n initialState = 'IDLE',\n): StateMachine<TContext> {\n const listeners = new Set<StateChange<TContext>>();\n let state = initialState;\n let context = initialContext;\n\n return {\n subscribe(listener: StateChange<TContext>): (() => void) {\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n\n destroy(): void {\n listeners.clear();\n state = initialState;\n context = initialContext;\n },\n\n action(action: string, input?: any): void {\n const transition = transitions[state];\n const nextState = transition.on[action];\n\n if (!nextState) {\n console.warn('Invalid transition', state, action);\n return;\n }\n\n console.log(\n `Machine transition: ${state} -> ${nextState} (${action}, ${input})`,\n );\n\n const previousState = state;\n\n if (actions[action]) {\n context = actions[action].call(this, context, input) || context;\n }\n\n state = nextState;\n listeners.forEach((listener) =>\n listener(state, previousState, context),\n );\n },\n\n can(action: string): boolean {\n const transition = transitions[state];\n return !!transition.on[action];\n },\n\n useCurrentState(): string {\n const [currentState, setCurrentState] = useState<string>(state);\n\n useEffect(() => {\n return this.subscribe(() => setCurrentState(state));\n }, []);\n\n return currentState;\n },\n\n useCurrentContext<TSelected = any>(selector: (context: TContext) => TSelected): TSelected {\n const [currentContext, setCurrentContext] = useState<TSelected>(() =>\n selector(context),\n );\n\n useEffect(() => {\n return this.subscribe(() => setCurrentContext(selector(context)));\n }, []);\n\n return currentContext;\n },\n\n getContext(): TContext {\n return context;\n },\n };\n}"],
|
|
5
|
+
"mappings": "AAAA,SAAS,WAAW,gBAAgB;AA0B7B,SAAS,cACZ,aACA,SACA,gBACA,eAAe,QACO;AACtB,QAAM,YAAY,oBAAI,IAA2B;AACjD,MAAI,QAAQ;AACZ,MAAI,UAAU;AAEd,SAAO;AAAA,IACH,UAAU,UAA+C;AACrD,gBAAU,IAAI,QAAQ;AACtB,aAAO,MAAM,UAAU,OAAO,QAAQ;AAAA,IAC1C;AAAA,IAEA,UAAgB;AACZ,gBAAU,MAAM;AAChB,cAAQ;AACR,gBAAU;AAAA,IACd;AAAA,IAEA,OAAO,QAAgB,OAAmB;AACtC,YAAM,aAAa,YAAY,KAAK;AACpC,YAAM,YAAY,WAAW,GAAG,MAAM;AAEtC,UAAI,CAAC,WAAW;AACZ,gBAAQ,KAAK,sBAAsB,OAAO,MAAM;AAChD;AAAA,MACJ;AAEA,cAAQ;AAAA,QACJ,uBAAuB,KAAK,OAAO,SAAS,KA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