@matheuspuel/state-machine 0.2.0 → 0.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.
- package/dist/StateMachine.js +4 -0
- package/dist/StateMachine.js.map +1 -0
- package/dist/definition.js +12 -0
- package/dist/definition.js.map +1 -0
- package/dist/dts/StateMachine.d.ts +6 -0
- package/dist/dts/StateMachine.d.ts.map +1 -0
- package/dist/dts/definition.d.ts +28 -0
- package/dist/dts/definition.d.ts.map +1 -0
- package/dist/dts/form/definition.d.ts +20 -0
- package/dist/dts/form/definition.d.ts.map +1 -0
- package/dist/dts/form/index.d.ts +2 -0
- package/dist/dts/form/index.d.ts.map +1 -0
- package/dist/dts/index.d.ts +2 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/dts/machines/Form.d.ts +28 -0
- package/dist/dts/machines/Form.d.ts.map +1 -0
- package/dist/dts/machines/FormValue.d.ts +12 -0
- package/dist/dts/machines/FormValue.d.ts.map +1 -0
- package/dist/dts/machines/Struct.d.ts +3 -0
- package/dist/dts/machines/Struct.d.ts.map +1 -0
- package/dist/dts/machines/index.d.ts +5 -0
- package/dist/dts/machines/index.d.ts.map +1 -0
- package/dist/dts/machines/of.d.ts +6 -0
- package/dist/dts/machines/of.d.ts.map +1 -0
- package/dist/dts/react/context.d.ts +10 -0
- package/dist/dts/react/context.d.ts.map +1 -0
- package/dist/dts/react/index.d.ts +4 -0
- package/dist/dts/react/index.d.ts.map +1 -0
- package/dist/dts/react/useSelector.d.ts +5 -0
- package/dist/dts/react/useSelector.d.ts.map +1 -0
- package/dist/dts/react/useStateMachine.d.ts +6 -0
- package/dist/dts/react/useStateMachine.d.ts.map +1 -0
- package/dist/dts/runtime/index.d.ts +14 -0
- package/dist/dts/runtime/index.d.ts.map +1 -0
- package/dist/form/definition.js +14 -0
- package/dist/form/definition.js.map +1 -0
- package/dist/form/index.js +2 -0
- package/dist/form/index.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/machines/Form.js +47 -0
- package/dist/machines/Form.js.map +1 -0
- package/dist/machines/FormValue.js +12 -0
- package/dist/machines/FormValue.js.map +1 -0
- package/dist/machines/Struct.js +15 -0
- package/dist/machines/Struct.js.map +1 -0
- package/dist/machines/index.js +5 -0
- package/dist/machines/index.js.map +1 -0
- package/dist/machines/of.js +10 -0
- package/dist/machines/of.js.map +1 -0
- package/dist/react/context.js +19 -0
- package/dist/react/context.js.map +1 -0
- package/dist/react/index.js +4 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/useSelector.js +9 -0
- package/dist/react/useSelector.js.map +1 -0
- package/dist/react/useStateMachine.js +25 -0
- package/dist/react/useStateMachine.js.map +1 -0
- package/dist/runtime/index.js +26 -0
- package/dist/runtime/index.js.map +1 -0
- package/package.json +33 -8
- package/src/StateMachine.ts +5 -3
- package/src/form/definition.ts +33 -0
- package/src/form/index.ts +1 -0
- package/src/index.ts +1 -1
- package/src/machines/Form.test.ts +67 -0
- package/src/machines/Form.ts +132 -0
- package/src/machines/FormValue.test.ts +3 -4
- package/src/machines/FormValue.ts +1 -1
- package/src/machines/Struct.test.ts +15 -14
- package/src/machines/Struct.ts +6 -20
- package/src/machines/index.ts +4 -3
- package/src/machines/of.test.ts +3 -4
- package/src/machines/of.ts +1 -1
- package/src/react/context.tsx +2 -2
- package/src/react/index.ts +3 -3
- package/src/react/useSelector.ts +1 -1
- package/src/react/useStateMachine.ts +1 -1
- package/src/runtime/index.ts +1 -1
- package/.eslintrc.js +0 -34
- package/.eslintrc.production.js +0 -11
- package/.prettierrc +0 -9
- package/pnpm-workspace.yaml +0 -2
- package/tsconfig.json +0 -14
- package/vitest.config.ts +0 -5
package/package.json
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matheuspuel/state-machine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"
|
|
5
|
+
"files": [
|
|
6
|
+
"/dist",
|
|
7
|
+
"/src"
|
|
8
|
+
],
|
|
6
9
|
"exports": {
|
|
7
10
|
"./package.json": "./package.json",
|
|
8
|
-
".":
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"./
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/dts/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./definition": {
|
|
16
|
+
"types": "./dist/dts/definition.d.ts",
|
|
17
|
+
"default": "./dist/definition.js"
|
|
18
|
+
},
|
|
19
|
+
"./form": {
|
|
20
|
+
"types": "./dist/dts/form/index.d.ts",
|
|
21
|
+
"default": "./dist/form/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./machines": {
|
|
24
|
+
"types": "./dist/dts/machines/index.d.ts",
|
|
25
|
+
"default": "./dist/machines/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./react": {
|
|
28
|
+
"types": "./dist/dts/react/index.d.ts",
|
|
29
|
+
"default": "./dist/react/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./runtime": {
|
|
32
|
+
"types": "./dist/dts/runtime/index.d.ts",
|
|
33
|
+
"default": "./dist/runtime/index.js"
|
|
34
|
+
}
|
|
13
35
|
},
|
|
14
36
|
"scripts": {
|
|
37
|
+
"build": "tsc -b tsconfig.build.json",
|
|
15
38
|
"lint": "eslint . --max-warnings 0 --config .eslintrc.production.js",
|
|
16
39
|
"lint:fix": "eslint . --fix --config .eslintrc.production.js",
|
|
17
40
|
"typecheck": "tsc",
|
|
18
41
|
"test": "vitest"
|
|
19
42
|
},
|
|
20
43
|
"dependencies": {
|
|
21
|
-
"@matheuspuel/optic": "^0.
|
|
44
|
+
"@matheuspuel/optic": "^0.2.0",
|
|
22
45
|
"effect": "^3.18.1",
|
|
23
46
|
"react": "19.1.0",
|
|
24
47
|
"use-sync-external-store": "^1.5.0"
|
|
@@ -26,6 +49,8 @@
|
|
|
26
49
|
"devDependencies": {
|
|
27
50
|
"@effect/language-service": "^0.41.1",
|
|
28
51
|
"@effect/vitest": "^0.26.0",
|
|
52
|
+
"@matheuspuel/state-machine": "file:.",
|
|
53
|
+
"@types/node": "^25.0.8",
|
|
29
54
|
"@types/react": "~19.1.17",
|
|
30
55
|
"@types/use-sync-external-store": "^1.5.0",
|
|
31
56
|
"@typescript-eslint/parser": "^8.29.1",
|
package/src/StateMachine.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export { make
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export { make } from './definition.js'
|
|
2
|
+
export type { StateMachine } from './definition.js'
|
|
3
|
+
export * from './machines/index.js'
|
|
4
|
+
export { run } from './runtime/index.js'
|
|
5
|
+
export type { Instance } from './runtime/index.js'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Effect, Schema } from 'effect'
|
|
2
|
+
import { ParseError } from 'effect/ParseResult'
|
|
3
|
+
|
|
4
|
+
export type AnyForm = {
|
|
5
|
+
[key: string]: AnyForm | FormField<any, any, any>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type FormField<A, I, E> = {
|
|
9
|
+
initial: I
|
|
10
|
+
validate: (value: I) => Effect.Effect<A, E>
|
|
11
|
+
fromData: (data: A) => I
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const field = <A, I, E>(args: FormField<A, I, E>) => args
|
|
15
|
+
|
|
16
|
+
export const fieldSchema = <A, I>(args: {
|
|
17
|
+
initial: I
|
|
18
|
+
schema: Schema.Schema<A, I>
|
|
19
|
+
}) =>
|
|
20
|
+
field<A, I, ParseError>({
|
|
21
|
+
initial: args.initial,
|
|
22
|
+
validate: Schema.decode(args.schema),
|
|
23
|
+
fromData: Schema.encodeSync(args.schema),
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
export const fieldSuccess = <A>(args: { initial: A }) =>
|
|
27
|
+
field<A, A, never>({
|
|
28
|
+
initial: args.initial,
|
|
29
|
+
validate: Effect.succeed,
|
|
30
|
+
fromData: _ => _,
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export const Struct = <Fields extends AnyForm>(fields: Fields) => fields
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Form from './definition.js'
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * as StateMachine from './StateMachine'
|
|
1
|
+
export * as StateMachine from './StateMachine.js'
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, expect, it } from '@effect/vitest'
|
|
2
|
+
import { StateMachine } from '@matheuspuel/state-machine'
|
|
3
|
+
import { Form } from '@matheuspuel/state-machine/form'
|
|
4
|
+
import { Effect } from 'effect'
|
|
5
|
+
|
|
6
|
+
describe('Form', () => {
|
|
7
|
+
it('should work', () => {
|
|
8
|
+
const formField = Form.field({
|
|
9
|
+
initial: 0,
|
|
10
|
+
validate: _ =>
|
|
11
|
+
_ > 1 ? Effect.succeed({ n: _ }) : Effect.fail('low' as const),
|
|
12
|
+
fromData: (data: { n: number }) => data.n,
|
|
13
|
+
})
|
|
14
|
+
const form = Form.Struct({
|
|
15
|
+
a: formField,
|
|
16
|
+
b: Form.Struct({ c: formField }),
|
|
17
|
+
})
|
|
18
|
+
const machine = StateMachine.Form(form)
|
|
19
|
+
const instance = StateMachine.run(machine)
|
|
20
|
+
const getState = () => instance.ref.get.pipe(Effect.runSync)
|
|
21
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
22
|
+
a: { value: 0, error: null },
|
|
23
|
+
b: { c: { value: 0, error: null } },
|
|
24
|
+
})
|
|
25
|
+
instance.actions.a.set(1)
|
|
26
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
27
|
+
a: { value: 1, error: null },
|
|
28
|
+
b: { c: { value: 0, error: null } },
|
|
29
|
+
})
|
|
30
|
+
instance.actions.validate().pipe(Effect.runSyncExit)
|
|
31
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
32
|
+
a: { value: 1, error: 'low' },
|
|
33
|
+
b: { c: { value: 0, error: 'low' } },
|
|
34
|
+
})
|
|
35
|
+
instance.actions.a.update(_ => _ + 1)
|
|
36
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
37
|
+
a: { value: 2, error: null },
|
|
38
|
+
b: { c: { value: 0, error: 'low' } },
|
|
39
|
+
})
|
|
40
|
+
instance.actions.b.c.set(1)
|
|
41
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
42
|
+
a: { value: 2, error: null },
|
|
43
|
+
b: { c: { value: 1, error: null } },
|
|
44
|
+
})
|
|
45
|
+
instance.actions.validate().pipe(Effect.runSyncExit)
|
|
46
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
47
|
+
a: { value: 2, error: null },
|
|
48
|
+
b: { c: { value: 1, error: 'low' } },
|
|
49
|
+
})
|
|
50
|
+
instance.actions.b.c.update(_ => _ + 1)
|
|
51
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
52
|
+
a: { value: 2, error: null },
|
|
53
|
+
b: { c: { value: 2, error: null } },
|
|
54
|
+
})
|
|
55
|
+
const data = instance.actions.validate().pipe(Effect.runSync)
|
|
56
|
+
expect(data).toStrictEqual<typeof data>({ a: { n: 2 }, b: { c: { n: 2 } } })
|
|
57
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
58
|
+
a: { value: 2, error: null },
|
|
59
|
+
b: { c: { value: 2, error: null } },
|
|
60
|
+
})
|
|
61
|
+
instance.actions.setStateFromData({ a: { n: 3 }, b: { c: { n: 3 } } })
|
|
62
|
+
expect(getState()).toStrictEqual<ReturnType<typeof getState>>({
|
|
63
|
+
a: { value: 3, error: null },
|
|
64
|
+
b: { c: { value: 3, error: null } },
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Effect, Either, Option, Record } from 'effect'
|
|
2
|
+
import { make, makeStore, Store } from '../definition.js'
|
|
3
|
+
import { AnyForm, FormField } from '../form/definition.js'
|
|
4
|
+
|
|
5
|
+
export type FormState<Form extends AnyForm> = {
|
|
6
|
+
[K in keyof Form]: Form[K] extends FormField<infer A, infer I, infer E>
|
|
7
|
+
? { value: I; error: E | null }
|
|
8
|
+
: Form[K] extends AnyForm
|
|
9
|
+
? FormState<Form[K]>
|
|
10
|
+
: never
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type FormData<Form extends AnyForm> = {
|
|
14
|
+
[K in keyof Form]: Form[K] extends FormField<infer A, infer I, infer E>
|
|
15
|
+
? A
|
|
16
|
+
: Form[K] extends AnyForm
|
|
17
|
+
? FormData<Form[K]>
|
|
18
|
+
: never
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type FormError<Form extends AnyForm> = {
|
|
22
|
+
[K in keyof Form]: Form[K] extends FormField<infer A, infer I, infer E>
|
|
23
|
+
? Option.Option<E>
|
|
24
|
+
: Form[K] extends AnyForm
|
|
25
|
+
? FormError<Form[K]>
|
|
26
|
+
: never
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type FormActions<Form extends AnyForm> = {
|
|
30
|
+
[K in keyof Form]: Form[K] extends FormField<infer A, infer I, infer E>
|
|
31
|
+
? {
|
|
32
|
+
set: (value: I) => void
|
|
33
|
+
update: (f: (previous: I) => I) => void
|
|
34
|
+
error: { set: (error: E | null) => void }
|
|
35
|
+
}
|
|
36
|
+
: Form[K] extends AnyForm
|
|
37
|
+
? FormActions<Form[K]>
|
|
38
|
+
: never
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isField = (
|
|
42
|
+
value: AnyForm | FormField<any, any, any>,
|
|
43
|
+
): value is FormField<any, any, any> => typeof value.validate === 'function'
|
|
44
|
+
|
|
45
|
+
export const Form = <F extends AnyForm>(form: F) => {
|
|
46
|
+
const getInitialState = <F extends AnyForm>(form: F): FormState<F> =>
|
|
47
|
+
Record.map(form, _ =>
|
|
48
|
+
isField(_) ? { value: _.initial, error: null } : getInitialState(_),
|
|
49
|
+
) as any
|
|
50
|
+
const getActions = <F extends AnyForm>(
|
|
51
|
+
form: F,
|
|
52
|
+
Store: Store<FormState<F>>,
|
|
53
|
+
): FormActions<F> =>
|
|
54
|
+
Record.map(form, (_, key) =>
|
|
55
|
+
isField(_)
|
|
56
|
+
? {
|
|
57
|
+
update: (f: (previous: any) => any) =>
|
|
58
|
+
Store.update(_ => ({
|
|
59
|
+
..._,
|
|
60
|
+
[key]: { value: f(_[key]!.value), error: null },
|
|
61
|
+
})),
|
|
62
|
+
set: (value: any) =>
|
|
63
|
+
Store.update(_ => ({ ..._, [key]: { value, error: null } })),
|
|
64
|
+
error: {
|
|
65
|
+
set: (error: any) =>
|
|
66
|
+
Store.update(_ => ({ ..._, [key]: { value: _.value, error } })),
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
: getActions(
|
|
70
|
+
_,
|
|
71
|
+
makeStore({
|
|
72
|
+
get: () => Store.get()[key],
|
|
73
|
+
update: f => Store.update(_ => ({ ..._, [key]: f(_[key]) })),
|
|
74
|
+
}) as any,
|
|
75
|
+
),
|
|
76
|
+
) as any
|
|
77
|
+
const validate = <F extends AnyForm>(
|
|
78
|
+
form: F,
|
|
79
|
+
Store: Store<FormState<F>>,
|
|
80
|
+
): any =>
|
|
81
|
+
Effect.all(
|
|
82
|
+
Record.map(form, (_, key) =>
|
|
83
|
+
isField(_)
|
|
84
|
+
? _.validate(Store.get()[key]!.value).pipe(
|
|
85
|
+
Effect.either,
|
|
86
|
+
Effect.tap(e =>
|
|
87
|
+
Store.update(_ => ({
|
|
88
|
+
..._,
|
|
89
|
+
[key]: {
|
|
90
|
+
value: _[key]!.value,
|
|
91
|
+
error: Option.getOrNull(Either.getLeft(e)),
|
|
92
|
+
},
|
|
93
|
+
})),
|
|
94
|
+
),
|
|
95
|
+
Effect.flatten,
|
|
96
|
+
)
|
|
97
|
+
: validate(
|
|
98
|
+
_,
|
|
99
|
+
makeStore({
|
|
100
|
+
get: () => Store.get()[key],
|
|
101
|
+
update: f => Store.update(_ => ({ ..._, [key]: f(_[key]) })),
|
|
102
|
+
}) as any,
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
{ mode: 'validate' },
|
|
106
|
+
) as any
|
|
107
|
+
return make<FormState<F>>()<
|
|
108
|
+
FormActions<F> & {
|
|
109
|
+
validate: () => Effect.Effect<FormData<F>, FormError<F>>
|
|
110
|
+
setStateFromData: (data: FormData<F>) => void
|
|
111
|
+
}
|
|
112
|
+
>({
|
|
113
|
+
initialState: getInitialState(form),
|
|
114
|
+
actions: ({ Store }) => ({
|
|
115
|
+
...getActions(form, Store),
|
|
116
|
+
validate: () => validate(form, Store),
|
|
117
|
+
setStateFromData: (data: FormData<F>) =>
|
|
118
|
+
Store.update(() => {
|
|
119
|
+
const updateState = <F extends AnyForm>(
|
|
120
|
+
form: F,
|
|
121
|
+
data: FormData<F>,
|
|
122
|
+
): any =>
|
|
123
|
+
Record.map(form, (_, key) =>
|
|
124
|
+
isField(_)
|
|
125
|
+
? { value: _.fromData(data[key]), error: null }
|
|
126
|
+
: updateState(_, data[key]!),
|
|
127
|
+
)
|
|
128
|
+
return updateState(form, data)
|
|
129
|
+
}),
|
|
130
|
+
}),
|
|
131
|
+
})
|
|
132
|
+
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { describe, expect, it } from '@effect/vitest'
|
|
2
|
+
import { StateMachine } from '@matheuspuel/state-machine'
|
|
2
3
|
import { Effect } from 'effect'
|
|
3
|
-
import { run } from '../runtime'
|
|
4
|
-
import { FormValue } from './FormValue'
|
|
5
4
|
|
|
6
5
|
describe('FormValue', () => {
|
|
7
6
|
it('should work', () => {
|
|
8
|
-
const machine = FormValue<number, string>(0)
|
|
9
|
-
const instance = run(machine)
|
|
7
|
+
const machine = StateMachine.FormValue<number, string>(0)
|
|
8
|
+
const instance = StateMachine.run(machine)
|
|
10
9
|
const getState = () => instance.ref.get.pipe(Effect.runSync)
|
|
11
10
|
expect(getState()).toStrictEqual({ value: 0, error: null })
|
|
12
11
|
instance.actions.set(1)
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import { describe, expect, it } from '@effect/vitest'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { Struct } from './Struct'
|
|
5
|
-
import { of } from './of'
|
|
2
|
+
import { StateMachine } from '@matheuspuel/state-machine'
|
|
3
|
+
import { Effect, pipe } from 'effect'
|
|
6
4
|
|
|
7
5
|
describe('Struct', () => {
|
|
8
6
|
it('should work', () => {
|
|
9
|
-
const machine =
|
|
10
|
-
{
|
|
11
|
-
a: of(0),
|
|
12
|
-
b: of(''),
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
const machine = pipe(
|
|
8
|
+
StateMachine.Struct({
|
|
9
|
+
a: StateMachine.of(0),
|
|
10
|
+
b: StateMachine.of(''),
|
|
11
|
+
}),
|
|
12
|
+
base =>
|
|
13
|
+
StateMachine.make<(typeof base)['initialState']>()({
|
|
14
|
+
...base,
|
|
15
|
+
actions: ({ Store }) => ({
|
|
16
|
+
...base.actions({ Store }),
|
|
17
|
+
get: () => Store.get(),
|
|
18
|
+
}),
|
|
17
19
|
}),
|
|
18
|
-
},
|
|
19
20
|
)
|
|
20
|
-
const instance = run(machine)
|
|
21
|
+
const instance = StateMachine.run(machine)
|
|
21
22
|
const getState = () => instance.ref.get.pipe(Effect.runSync)
|
|
22
23
|
expect(getState()).toStrictEqual({ a: 0, b: '' })
|
|
23
24
|
instance.actions.a.set(1)
|
package/src/machines/Struct.ts
CHANGED
|
@@ -4,33 +4,20 @@ import {
|
|
|
4
4
|
make,
|
|
5
5
|
makeStore,
|
|
6
6
|
StateMachine,
|
|
7
|
-
|
|
8
|
-
} from '../definition'
|
|
7
|
+
} from '../definition.js'
|
|
9
8
|
|
|
10
9
|
export const Struct = <
|
|
11
10
|
A extends Record<string, StateMachine<any, AnyStateActions>>,
|
|
12
|
-
ExtraActions extends AnyStateActions = {},
|
|
13
11
|
>(
|
|
14
12
|
fields: A,
|
|
15
|
-
options?: {
|
|
16
|
-
extraActions?: (machine: {
|
|
17
|
-
Store: Store<{
|
|
18
|
-
[K in keyof A]: A[K] extends StateMachine<infer State, any>
|
|
19
|
-
? State
|
|
20
|
-
: never
|
|
21
|
-
}>
|
|
22
|
-
}) => ExtraActions
|
|
23
|
-
},
|
|
24
13
|
) =>
|
|
25
14
|
make<{
|
|
26
15
|
[K in keyof A]: A[K] extends StateMachine<infer State, any> ? State : never
|
|
27
|
-
}>()<
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
} & ExtraActions
|
|
33
|
-
>({
|
|
16
|
+
}>()<{
|
|
17
|
+
[K in keyof A]: A[K] extends StateMachine<any, infer Actions>
|
|
18
|
+
? Actions
|
|
19
|
+
: never
|
|
20
|
+
}>({
|
|
34
21
|
initialState: Record.map(fields, _ => _.initialState) as any,
|
|
35
22
|
actions: ({ Store }) => ({
|
|
36
23
|
...(Record.map(fields, (_, key) =>
|
|
@@ -41,7 +28,6 @@ export const Struct = <
|
|
|
41
28
|
}) as any,
|
|
42
29
|
}),
|
|
43
30
|
) as any),
|
|
44
|
-
...options?.extraActions?.({ Store }),
|
|
45
31
|
}),
|
|
46
32
|
// TODO implement other options
|
|
47
33
|
})
|
package/src/machines/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { Form } from './Form.js'
|
|
2
|
+
export { FormValue } from './FormValue.js'
|
|
3
|
+
export { of } from './of.js'
|
|
4
|
+
export { Struct } from './Struct.js'
|
package/src/machines/of.test.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { describe, expect, it } from '@effect/vitest'
|
|
2
|
+
import { StateMachine } from '@matheuspuel/state-machine'
|
|
2
3
|
import { Effect } from 'effect'
|
|
3
|
-
import { run } from '../runtime'
|
|
4
|
-
import { of } from './of'
|
|
5
4
|
|
|
6
5
|
describe('of', () => {
|
|
7
6
|
it('should work', () => {
|
|
8
|
-
const machine = of('')
|
|
9
|
-
const instance = run(machine)
|
|
7
|
+
const machine = StateMachine.of('')
|
|
8
|
+
const instance = StateMachine.run(machine)
|
|
10
9
|
instance.actions.set('a')
|
|
11
10
|
const state = instance.ref.get.pipe(Effect.runSync)
|
|
12
11
|
expect(state).toStrictEqual('a')
|
package/src/machines/of.ts
CHANGED
package/src/react/context.tsx
CHANGED
|
@@ -3,8 +3,8 @@ import {
|
|
|
3
3
|
AnyStateActions,
|
|
4
4
|
PreparedStateActions,
|
|
5
5
|
StateMachine,
|
|
6
|
-
} from '../definition'
|
|
7
|
-
import { useStateMachine } from './useStateMachine'
|
|
6
|
+
} from '../definition.js'
|
|
7
|
+
import { useStateMachine } from './useStateMachine.js'
|
|
8
8
|
|
|
9
9
|
export const makeStateMachineContext = <State, Actions extends AnyStateActions>(
|
|
10
10
|
machine: StateMachine<State, Actions>,
|
package/src/react/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './context'
|
|
2
|
-
export * from './useSelector'
|
|
3
|
-
export * from './useStateMachine'
|
|
1
|
+
export * from './context.js'
|
|
2
|
+
export * from './useSelector.js'
|
|
3
|
+
export * from './useStateMachine.js'
|
package/src/react/useSelector.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Equal, Equivalence } from 'effect'
|
|
2
2
|
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector'
|
|
3
3
|
import { AnyStateActions } from '../definition.js'
|
|
4
|
-
import { Instance } from '../runtime'
|
|
4
|
+
import { Instance } from '../runtime/index.js'
|
|
5
5
|
|
|
6
6
|
export const makeUseSelector =
|
|
7
7
|
<State, Actions extends AnyStateActions>(
|
package/src/runtime/index.ts
CHANGED
package/.eslintrc.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
extends: [
|
|
3
|
-
'eslint:recommended',
|
|
4
|
-
'plugin:@typescript-eslint/recommended',
|
|
5
|
-
'plugin:@typescript-eslint/recommended-requiring-type-checking',
|
|
6
|
-
'plugin:@typescript-eslint/strict',
|
|
7
|
-
],
|
|
8
|
-
parserOptions: {
|
|
9
|
-
tsconfigRootDir: __dirname,
|
|
10
|
-
project: ['./tsconfig.json'],
|
|
11
|
-
ecmaVersion: 'latest',
|
|
12
|
-
sourceType: 'module',
|
|
13
|
-
},
|
|
14
|
-
ignorePatterns: [
|
|
15
|
-
'/.vscode/*',
|
|
16
|
-
'/coverage/*',
|
|
17
|
-
'/dist/*',
|
|
18
|
-
'/experiments/*',
|
|
19
|
-
'/node_modules/*',
|
|
20
|
-
'.eslintrc.js',
|
|
21
|
-
'.eslintrc.production.js',
|
|
22
|
-
],
|
|
23
|
-
rules: {
|
|
24
|
-
// already checked by typescript
|
|
25
|
-
'@typescript-eslint/no-redeclare': 'off',
|
|
26
|
-
// warn instead of error
|
|
27
|
-
'@typescript-eslint/no-unused-vars': 'warn',
|
|
28
|
-
// allow namespace declaration for types
|
|
29
|
-
'@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],
|
|
30
|
-
// warn instead of error to prevent covering more specific errors
|
|
31
|
-
'@typescript-eslint/no-unsafe-return': 'warn',
|
|
32
|
-
'@typescript-eslint/no-unsafe-call': 'warn',
|
|
33
|
-
},
|
|
34
|
-
}
|
package/.eslintrc.production.js
DELETED
package/.prettierrc
DELETED
package/pnpm-workspace.yaml
DELETED
package/tsconfig.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"noEmit": true,
|
|
4
|
-
"skipLibCheck": true,
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"target": "ESNext",
|
|
7
|
-
"jsx": "preserve",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"noUncheckedIndexedAccess": true,
|
|
10
|
-
"noFallthroughCasesInSwitch": true,
|
|
11
|
-
"plugins": [{ "name": "@effect/language-service" }]
|
|
12
|
-
},
|
|
13
|
-
"include": ["**/*.ts", "**/*.tsx"]
|
|
14
|
-
}
|