@xstate/react 2.0.0 → 4.0.0-alpha.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/CHANGELOG.md +42 -0
- package/dist/declarations/src/fsm.d.ts +3 -0
- package/{es/index.js → dist/declarations/src/index.d.ts} +5 -5
- package/dist/declarations/src/types.d.ts +15 -0
- package/dist/declarations/src/useActor.d.ts +3 -0
- package/{es → dist/declarations/src}/useConstant.d.ts +1 -2
- package/dist/declarations/src/useInterpret.d.ts +13 -0
- package/dist/declarations/src/useMachine.d.ts +21 -0
- package/dist/declarations/src/useSelector.d.ts +2 -0
- package/{es → dist/declarations/src}/useSpawn.d.ts +9 -10
- package/dist/useConstant-644f0e11.cjs.dev.js +74 -0
- package/dist/useConstant-94bfdbb5.esm.js +71 -0
- package/dist/useConstant-eeb49d3f.cjs.prod.js +17 -0
- package/dist/xstate-react.cjs.d.ts +1 -0
- package/dist/xstate-react.cjs.dev.js +241 -0
- package/dist/xstate-react.cjs.js +7 -0
- package/dist/xstate-react.cjs.prod.js +231 -0
- package/dist/xstate-react.esm.js +229 -0
- package/fsm/dist/xstate-react-fsm.cjs.d.ts +1 -0
- package/fsm/dist/xstate-react-fsm.cjs.dev.js +98 -0
- package/fsm/dist/xstate-react-fsm.cjs.js +7 -0
- package/fsm/dist/xstate-react-fsm.cjs.prod.js +144 -0
- package/fsm/dist/xstate-react-fsm.esm.js +89 -0
- package/fsm/package.json +3 -5
- package/package.json +25 -48
- package/dist/xstate-react-fsm.umd.min.js +0 -28
- package/dist/xstate-react.umd.min.js +0 -28
- package/es/fsm.d.ts +0 -20
- package/es/fsm.js +0 -73
- package/es/index.d.ts +0 -6
- package/es/types.d.ts +0 -31
- package/es/types.js +0 -5
- package/es/useActor.d.ts +0 -9
- package/es/useActor.js +0 -79
- package/es/useConstant.js +0 -8
- package/es/useInterpret.d.ts +0 -13
- package/es/useInterpret.js +0 -116
- package/es/useMachine.d.ts +0 -24
- package/es/useMachine.js +0 -78
- package/es/useReactEffectActions.d.ts +0 -3
- package/es/useReactEffectActions.js +0 -76
- package/es/useSelector.d.ts +0 -3
- package/es/useSelector.js +0 -70
- package/es/useSpawn.js +0 -15
- package/es/utils.d.ts +0 -4
- package/es/utils.js +0 -53
- package/lib/fsm.d.ts +0 -20
- package/lib/fsm.js +0 -78
- package/lib/index.d.ts +0 -6
- package/lib/index.js +0 -15
- package/lib/types.d.ts +0 -31
- package/lib/types.js +0 -8
- package/lib/useActor.d.ts +0 -9
- package/lib/useActor.js +0 -84
- package/lib/useConstant.d.ts +0 -2
- package/lib/useConstant.js +0 -11
- package/lib/useInterpret.d.ts +0 -13
- package/lib/useInterpret.js +0 -120
- package/lib/useMachine.d.ts +0 -24
- package/lib/useMachine.js +0 -84
- package/lib/useReactEffectActions.d.ts +0 -3
- package/lib/useReactEffectActions.js +0 -80
- package/lib/useSelector.d.ts +0 -3
- package/lib/useSelector.js +0 -74
- package/lib/useSpawn.d.ts +0 -10
- package/lib/useSpawn.js +0 -19
- package/lib/utils.d.ts +0 -4
- package/lib/utils.js +0 -58
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,47 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 4.0.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#3148](https://github.com/statelyai/xstate/pull/3148) [`7a68cbb61`](https://github.com/statelyai/xstate/commit/7a68cbb615afb6556c83868535dae67af366a117) Thanks [@davidkpiano](https://github.com/davidkpiano)! - Removed `getSnapshot` parameter from hooks. It is expected that the received `actorRef` has to have a `getSnapshot` method on it that can be used internally.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`7f3b84816`](https://github.com/statelyai/xstate/commit/7f3b84816564d951b6b29afdd7075256f1f59501), [`969a2f4fc`](https://github.com/statelyai/xstate/commit/969a2f4fc0bc9147b9a52da25306e5c13b97f159), [`c0a6dcafa`](https://github.com/statelyai/xstate/commit/c0a6dcafa1a11a5ff1660b57e0728675f155c292), [`7a68cbb61`](https://github.com/statelyai/xstate/commit/7a68cbb615afb6556c83868535dae67af366a117), [`172d6a7e1`](https://github.com/statelyai/xstate/commit/172d6a7e1e4ab0fa73485f76c52675be8a1f3362), [`31bc73e05`](https://github.com/statelyai/xstate/commit/31bc73e05692f29301f5bb5cb4b87b90773e0ef2), [`e09efc720`](https://github.com/statelyai/xstate/commit/e09efc720f05246b692d0fdf17cf5d8ac0344ee6), [`145539c4c`](https://github.com/statelyai/xstate/commit/145539c4cfe1bde5aac247792622428e44342dd6), [`3de36bb24`](https://github.com/statelyai/xstate/commit/3de36bb24e8f59f54d571bf587407b1b6a9856e0), [`9e10660ec`](https://github.com/statelyai/xstate/commit/9e10660ec2f1e89cbb09a1094edb4f6b8a273a99), [`8fcbddd51`](https://github.com/statelyai/xstate/commit/8fcbddd51d66716ab1d326d934566a7664a4e175), [`515cdc9c1`](https://github.com/statelyai/xstate/commit/515cdc9c148a3a1b558120c309080e9a21e876bc), [`6043a1c28`](https://github.com/statelyai/xstate/commit/6043a1c28d21ff8cbabc420a6817a02a1a54fcc8), [`6a6b2b869`](https://github.com/statelyai/xstate/commit/6a6b2b8691626112d1d9dbf23d0a0e80ff7130a8), [`0b49437b1`](https://github.com/statelyai/xstate/commit/0b49437b1be3e6d9bc61304711b83300cba88dc4), [`0e24ea6d6`](https://github.com/statelyai/xstate/commit/0e24ea6d62a5c1a8b7e365f2252dc930d94997c4), [`04e89f90f`](https://github.com/statelyai/xstate/commit/04e89f90f97fe25a45b5908c45f25a513f0fd70f), [`0096d9f7a`](https://github.com/statelyai/xstate/commit/0096d9f7afda7546fc7b1d5fdd1546f55c32bfe4), [`8fcbddd51`](https://github.com/statelyai/xstate/commit/8fcbddd51d66716ab1d326d934566a7664a4e175), [`b200e0e0b`](https://github.com/statelyai/xstate/commit/b200e0e0b7123797086080b75abdfcf2fce45253), [`7a68cbb61`](https://github.com/statelyai/xstate/commit/7a68cbb615afb6556c83868535dae67af366a117), [`9437c3de9`](https://github.com/statelyai/xstate/commit/9437c3de912c2a38c04798cbb94f267a1e5db3f8), [`0038c7b1e`](https://github.com/statelyai/xstate/commit/0038c7b1e2050fe7262849aab8fdff4a7ce7cf92), [`7a68cbb61`](https://github.com/statelyai/xstate/commit/7a68cbb615afb6556c83868535dae67af366a117), [`b24e47b9e`](https://github.com/statelyai/xstate/commit/b24e47b9e7a59a5b0527d4386cea3af16c84ca7a), [`390eaaa52`](https://github.com/statelyai/xstate/commit/390eaaa523cb0dd243e39c6300e671606c1e45fc), [`7a68cbb61`](https://github.com/statelyai/xstate/commit/7a68cbb615afb6556c83868535dae67af366a117), [`0c6cfee9a`](https://github.com/statelyai/xstate/commit/0c6cfee9a6d603aa1756e3a6d0f76d4da1486caf), [`e09efc720`](https://github.com/statelyai/xstate/commit/e09efc720f05246b692d0fdf17cf5d8ac0344ee6), [`025a2d6a2`](https://github.com/statelyai/xstate/commit/025a2d6a295359a746bee6ffc2953ccc51a6aaad), [`e09efc720`](https://github.com/statelyai/xstate/commit/e09efc720f05246b692d0fdf17cf5d8ac0344ee6), [`c99bb43af`](https://github.com/statelyai/xstate/commit/c99bb43afec01ddee86fc746c346ea1aeeca687d), [`fc5ca7b7f`](https://github.com/statelyai/xstate/commit/fc5ca7b7fcd2d7821ce2409743c50505529104e7), [`c9cda27cb`](https://github.com/statelyai/xstate/commit/c9cda27cbe52b9c706ccb63b709d22d049be31e3), [`5d16a7365`](https://github.com/statelyai/xstate/commit/5d16a73651e97dd0228c5215cb2452a4d9951118), [`8fcbddd51`](https://github.com/statelyai/xstate/commit/8fcbddd51d66716ab1d326d934566a7664a4e175), [`53a594e9a`](https://github.com/statelyai/xstate/commit/53a594e9a1b49ccb1121048a5784676f83950024), [`31a0d890f`](https://github.com/statelyai/xstate/commit/31a0d890f55d8f0b06772c9fd510b18302b76ebb)]:
|
|
12
|
+
- xstate@5.0.0-alpha.0
|
|
13
|
+
|
|
14
|
+
## 3.0.0
|
|
15
|
+
|
|
16
|
+
### Major Changes
|
|
17
|
+
|
|
18
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - This package now accepts React 18 as a peer dep and the implementation has been rewritten to use [`use-sync-external-store`](https://www.npmjs.com/package/use-sync-external-store) package. This doesn't break compatibility with older versions of React since we are using the shim to keep compatibility with those older versions.
|
|
19
|
+
|
|
20
|
+
* [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - `asEffect` and `asLayoutEffect` action creators were removed. They were not fitting the React model that well and could lead to issues as their existence suggested that they are easy to use.
|
|
21
|
+
|
|
22
|
+
To execute actions at those exact times you can always either just call your stuff directly from those effects or send events to the machine from those effects and execute explicit actions in response to said events.
|
|
23
|
+
|
|
24
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - The signatures of `useMachine` and `useService` integrating with `@xstate/fsm` were changed. They now only accept a single generic each (`TMachine` and `TService` respectively). This has been done to match their signatures with the related hooks that integrate with `xstate` itself.
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - In v2 we have changed signatures of `useMachine` and `useInterpret`. Instead of accepting a list of generics they now only support a single generic: `TMachine`. This change, erroneously, was only introduced to types targeting TS@4.x but the types targeting previous TS releases were still using the older signatures. This has now been fixed and users of older TS versions should now be able to leverage typegen with `@xstate/react`.
|
|
29
|
+
|
|
30
|
+
* [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - `useMachine` for `xstate` now correctly rerenders with the initial state when the internal service is being restarted. This might happen during Fast Refresh and now you shouldn't be able to observe this stale state that didn't match the actual state of the service.
|
|
31
|
+
|
|
32
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - `useMachine` for `@xstate/fsm` now starts the service in an effect. This avoids side-effects in render and improves the compatibility with `StrictMode`.
|
|
33
|
+
|
|
34
|
+
* [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - Implementations given to `useMachine` targeting `@xstate/fsm` are now updated in a layout effect. This avoid some stale closure problems for actions that are executed in response to events sent from layout effects.
|
|
35
|
+
|
|
36
|
+
* Updated dependencies [[`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf), [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf)]:
|
|
37
|
+
- @xstate/fsm@2.0.0
|
|
38
|
+
|
|
39
|
+
## 2.0.1
|
|
40
|
+
|
|
41
|
+
### Patch Changes
|
|
42
|
+
|
|
43
|
+
- [#3089](https://github.com/statelyai/xstate/pull/3089) [`862697e29`](https://github.com/statelyai/xstate/commit/862697e2990934d46050580d7e09c749d09d8426) Thanks [@Andarist](https://github.com/Andarist)! - Fixed compatibility with Skypack by exporting some shared utilities from root entry of XState and consuming them directly in other packages (this avoids accessing those things using deep imports and thus it avoids creating those compatibility problems).
|
|
44
|
+
|
|
3
45
|
## 2.0.0
|
|
4
46
|
|
|
5
47
|
### Major Changes
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MachineImplementationsFrom, ServiceFrom, StateFrom, StateMachine } from '@xstate/fsm';
|
|
2
|
+
export declare function useMachine<TMachine extends StateMachine.AnyMachine>(stateMachine: TMachine, options?: MachineImplementationsFrom<TMachine>): [StateFrom<TMachine>, ServiceFrom<TMachine>['send'], ServiceFrom<TMachine>];
|
|
3
|
+
export declare function useService<TService extends StateMachine.AnyService>(service: TService): [StateFrom<TService>, TService['send'], TService];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { useMachine
|
|
2
|
-
export { useActor } from './useActor';
|
|
3
|
-
export { useInterpret } from './useInterpret';
|
|
4
|
-
export { useSelector } from './useSelector';
|
|
5
|
-
export { useSpawn } from './useSpawn';
|
|
1
|
+
export { useMachine } from './useMachine';
|
|
2
|
+
export { useActor } from './useActor';
|
|
3
|
+
export { useInterpret } from './useInterpret';
|
|
4
|
+
export { useSelector } from './useSelector';
|
|
5
|
+
export { useSpawn } from './useSpawn';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EventObject, MachineContext, StateConfig } from 'xstate';
|
|
2
|
+
export declare type MaybeLazy<T> = T | (() => T);
|
|
3
|
+
export declare type NoInfer<T> = [T][T extends any ? 0 : any];
|
|
4
|
+
export declare type Prop<T, K> = K extends keyof T ? T[K] : never;
|
|
5
|
+
export interface UseMachineOptions<TContext extends MachineContext, TEvent extends EventObject> {
|
|
6
|
+
/**
|
|
7
|
+
* If provided, will be merged with machine's `context`.
|
|
8
|
+
*/
|
|
9
|
+
context?: Partial<TContext>;
|
|
10
|
+
/**
|
|
11
|
+
* The state to rehydrate the machine to. The machine will
|
|
12
|
+
* start at this state instead of its `initialState`.
|
|
13
|
+
*/
|
|
14
|
+
state?: StateConfig<TContext, TEvent>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ActorRef, EventObject, SnapshotFrom } from 'xstate';
|
|
2
|
+
export declare function useActor<TActor extends ActorRef<any, any>>(actorRef: TActor): [SnapshotFrom<TActor>, TActor['send']];
|
|
3
|
+
export declare function useActor<TEvent extends EventObject, TSnapshot>(actorRef: ActorRef<TEvent, TSnapshot>): [TSnapshot, (event: TEvent) => void];
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export default function useConstant<T>(fn: () => T): T;
|
|
2
|
-
//# sourceMappingURL=useConstant.d.ts.map
|
|
1
|
+
export default function useConstant<T>(fn: () => T): T;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AnyInterpreter, AnyStateMachine, AreAllImplementationsAssumedToBeProvided, InternalMachineImplementations, InterpreterFrom, InterpreterOptions, MachineImplementations, Observer, StateFrom } from 'xstate';
|
|
2
|
+
import { MaybeLazy } from './types';
|
|
3
|
+
import { UseMachineOptions } from './useMachine';
|
|
4
|
+
export declare function useIdleInterpreter(getMachine: MaybeLazy<AnyStateMachine>, options: Partial<InterpreterOptions> & Partial<UseMachineOptions<any, never>> & Partial<MachineImplementations<any, never>>): AnyInterpreter;
|
|
5
|
+
declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
6
|
+
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineImplementations<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
|
|
7
|
+
observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
|
|
8
|
+
] : [
|
|
9
|
+
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineImplementations<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>,
|
|
10
|
+
observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
|
|
11
|
+
];
|
|
12
|
+
export declare function useInterpret<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AnyStateMachine, AreAllImplementationsAssumedToBeProvided, EventObject, InternalMachineImplementations, InterpreterFrom, InterpreterOptions, MachineContext, StateConfig, StateFrom } from 'xstate';
|
|
2
|
+
import { MaybeLazy, Prop } from './types';
|
|
3
|
+
export interface UseMachineOptions<TContext extends MachineContext, TEvent extends EventObject> {
|
|
4
|
+
/**
|
|
5
|
+
* If provided, will be merged with machine's `context`.
|
|
6
|
+
*/
|
|
7
|
+
context?: Partial<TContext>;
|
|
8
|
+
/**
|
|
9
|
+
* The state to rehydrate the machine to. The machine will
|
|
10
|
+
* start at this state instead of its `initialState`.
|
|
11
|
+
*/
|
|
12
|
+
state?: StateConfig<TContext, TEvent>;
|
|
13
|
+
}
|
|
14
|
+
declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
15
|
+
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineImplementations<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>
|
|
16
|
+
] : [
|
|
17
|
+
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineImplementations<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>
|
|
18
|
+
];
|
|
19
|
+
declare type UseMachineReturn<TMachine extends AnyStateMachine, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
20
|
+
export declare function useMachine<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
21
|
+
export {};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { ActorRef, Behavior, EventObject } from 'xstate';
|
|
2
|
-
/**
|
|
3
|
-
* React hook that spawns an `ActorRef` with the specified `behavior`.
|
|
4
|
-
* The returned `ActorRef` can be used with the `useActor(actorRef)` hook.
|
|
5
|
-
*
|
|
6
|
-
* @param behavior The actor behavior to spawn
|
|
7
|
-
* @returns An ActorRef with the specified `behavior`
|
|
8
|
-
*/
|
|
9
|
-
export declare function useSpawn<TState, TEvent extends EventObject>(behavior: Behavior<TEvent, TState>): ActorRef<TEvent, TState>;
|
|
10
|
-
//# sourceMappingURL=useSpawn.d.ts.map
|
|
1
|
+
import { ActorRef, Behavior, EventObject } from 'xstate';
|
|
2
|
+
/**
|
|
3
|
+
* React hook that spawns an `ActorRef` with the specified `behavior`.
|
|
4
|
+
* The returned `ActorRef` can be used with the `useActor(actorRef)` hook.
|
|
5
|
+
*
|
|
6
|
+
* @param behavior The actor behavior to spawn
|
|
7
|
+
* @returns An ActorRef with the specified `behavior`
|
|
8
|
+
*/
|
|
9
|
+
export declare function useSpawn<TState, TEvent extends EventObject>(behavior: Behavior<TEvent, TState>): ActorRef<TEvent, TState>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _arrayWithHoles(arr) {
|
|
6
|
+
if (Array.isArray(arr)) return arr;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function _iterableToArrayLimit(arr, i) {
|
|
10
|
+
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
|
|
11
|
+
var _arr = [];
|
|
12
|
+
var _n = true;
|
|
13
|
+
var _d = false;
|
|
14
|
+
var _e = undefined;
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
|
18
|
+
_arr.push(_s.value);
|
|
19
|
+
|
|
20
|
+
if (i && _arr.length === i) break;
|
|
21
|
+
}
|
|
22
|
+
} catch (err) {
|
|
23
|
+
_d = true;
|
|
24
|
+
_e = err;
|
|
25
|
+
} finally {
|
|
26
|
+
try {
|
|
27
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
28
|
+
} finally {
|
|
29
|
+
if (_d) throw _e;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return _arr;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function _arrayLikeToArray(arr, len) {
|
|
37
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
38
|
+
|
|
39
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
40
|
+
|
|
41
|
+
return arr2;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
45
|
+
if (!o) return;
|
|
46
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
47
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
48
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
49
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
50
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function _nonIterableRest() {
|
|
54
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function _slicedToArray(arr, i) {
|
|
58
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function useConstant(fn) {
|
|
62
|
+
var ref = React.useRef();
|
|
63
|
+
|
|
64
|
+
if (!ref.current) {
|
|
65
|
+
ref.current = {
|
|
66
|
+
v: fn()
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return ref.current.v;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
exports._slicedToArray = _slicedToArray;
|
|
74
|
+
exports.useConstant = useConstant;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
function _arrayWithHoles(arr) {
|
|
4
|
+
if (Array.isArray(arr)) return arr;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function _iterableToArrayLimit(arr, i) {
|
|
8
|
+
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
|
|
9
|
+
var _arr = [];
|
|
10
|
+
var _n = true;
|
|
11
|
+
var _d = false;
|
|
12
|
+
var _e = undefined;
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
|
16
|
+
_arr.push(_s.value);
|
|
17
|
+
|
|
18
|
+
if (i && _arr.length === i) break;
|
|
19
|
+
}
|
|
20
|
+
} catch (err) {
|
|
21
|
+
_d = true;
|
|
22
|
+
_e = err;
|
|
23
|
+
} finally {
|
|
24
|
+
try {
|
|
25
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
26
|
+
} finally {
|
|
27
|
+
if (_d) throw _e;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return _arr;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function _arrayLikeToArray(arr, len) {
|
|
35
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
36
|
+
|
|
37
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
38
|
+
|
|
39
|
+
return arr2;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
43
|
+
if (!o) return;
|
|
44
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
45
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
46
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
47
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
48
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function _nonIterableRest() {
|
|
52
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function _slicedToArray(arr, i) {
|
|
56
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function useConstant(fn) {
|
|
60
|
+
var ref = useRef();
|
|
61
|
+
|
|
62
|
+
if (!ref.current) {
|
|
63
|
+
ref.current = {
|
|
64
|
+
v: fn()
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return ref.current.v;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { _slicedToArray as _, useConstant as u };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./declarations/src/index";
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var withSelector = require('use-sync-external-store/shim/with-selector');
|
|
7
|
+
var xstate = require('xstate');
|
|
8
|
+
var useConstant = require('./useConstant-644f0e11.cjs.dev.js');
|
|
9
|
+
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
|
|
10
|
+
var shim = require('use-sync-external-store/shim');
|
|
11
|
+
|
|
12
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
|
|
15
|
+
|
|
16
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
17
|
+
if (source == null) return {};
|
|
18
|
+
var target = {};
|
|
19
|
+
var sourceKeys = Object.keys(source);
|
|
20
|
+
var key, i;
|
|
21
|
+
|
|
22
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
|
23
|
+
key = sourceKeys[i];
|
|
24
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
25
|
+
target[key] = source[key];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return target;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function _objectWithoutProperties(source, excluded) {
|
|
32
|
+
if (source == null) return {};
|
|
33
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
34
|
+
var key, i;
|
|
35
|
+
|
|
36
|
+
if (Object.getOwnPropertySymbols) {
|
|
37
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
38
|
+
|
|
39
|
+
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
|
40
|
+
key = sourceSymbolKeys[i];
|
|
41
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
42
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
43
|
+
target[key] = source[key];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return target;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
var _excluded = ["context", "actors", "guards", "actions", "delays", "state"];
|
|
51
|
+
function useIdleInterpreter(getMachine, options) {
|
|
52
|
+
var machine = useConstant.useConstant(function () {
|
|
53
|
+
return typeof getMachine === 'function' ? getMachine() : getMachine;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (process.env.NODE_ENV !== 'production' && typeof getMachine !== 'function') {
|
|
57
|
+
var _useState = React.useState(machine),
|
|
58
|
+
_useState2 = useConstant._slicedToArray(_useState, 1),
|
|
59
|
+
initialMachine = _useState2[0];
|
|
60
|
+
|
|
61
|
+
if (getMachine !== initialMachine) {
|
|
62
|
+
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' + 'Please make sure that you pass the same Machine as argument each time.');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
var context = options.context,
|
|
67
|
+
actors = options.actors,
|
|
68
|
+
guards = options.guards,
|
|
69
|
+
actions = options.actions,
|
|
70
|
+
delays = options.delays,
|
|
71
|
+
rehydratedState = options.state,
|
|
72
|
+
interpreterOptions = _objectWithoutProperties(options, _excluded);
|
|
73
|
+
|
|
74
|
+
var service = useConstant.useConstant(function () {
|
|
75
|
+
var machineConfig = {
|
|
76
|
+
context: context,
|
|
77
|
+
guards: guards,
|
|
78
|
+
actions: actions,
|
|
79
|
+
actors: actors,
|
|
80
|
+
delays: delays
|
|
81
|
+
};
|
|
82
|
+
var machineWithConfig = machine.provide(machineConfig);
|
|
83
|
+
return xstate.interpret(machineWithConfig, interpreterOptions);
|
|
84
|
+
}); // Make sure options are kept updated when they change.
|
|
85
|
+
// This mutation assignment is safe because the service instance is only used
|
|
86
|
+
// in one place -- this hook's caller.
|
|
87
|
+
|
|
88
|
+
useIsomorphicLayoutEffect__default['default'](function () {
|
|
89
|
+
Object.assign(service.machine.options.actions, actions);
|
|
90
|
+
Object.assign(service.machine.options.guards, guards);
|
|
91
|
+
Object.assign(service.machine.options.actors, actors);
|
|
92
|
+
Object.assign(service.machine.options.delays, delays);
|
|
93
|
+
}, [actions, guards, actors, delays]);
|
|
94
|
+
return service;
|
|
95
|
+
}
|
|
96
|
+
function useInterpret(getMachine) {
|
|
97
|
+
for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
98
|
+
_ref[_key - 1] = arguments[_key];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
var _ref$ = _ref[0],
|
|
102
|
+
options = _ref$ === void 0 ? {} : _ref$,
|
|
103
|
+
observerOrListener = _ref[1];
|
|
104
|
+
var service = useIdleInterpreter(getMachine, options);
|
|
105
|
+
React.useEffect(function () {
|
|
106
|
+
if (!observerOrListener) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var sub = service.subscribe(xstate.toObserver(observerOrListener));
|
|
111
|
+
return function () {
|
|
112
|
+
sub.unsubscribe();
|
|
113
|
+
};
|
|
114
|
+
}, [observerOrListener]);
|
|
115
|
+
React.useEffect(function () {
|
|
116
|
+
var rehydratedState = options.state;
|
|
117
|
+
service.start(rehydratedState ? xstate.State.create(rehydratedState) : undefined);
|
|
118
|
+
return function () {
|
|
119
|
+
service.stop();
|
|
120
|
+
service.status = xstate.InterpreterStatus.NotStarted;
|
|
121
|
+
};
|
|
122
|
+
}, []);
|
|
123
|
+
return service;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function identity(a) {
|
|
127
|
+
return a;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
var isEqual = function isEqual(prevState, nextState) {
|
|
131
|
+
return prevState === nextState || nextState.changed === false;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// TODO: rethink how we can do this better
|
|
135
|
+
var cachedRehydratedStates = new WeakMap();
|
|
136
|
+
function useMachine(getMachine) {
|
|
137
|
+
for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
138
|
+
_ref[_key - 1] = arguments[_key];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
var _ref$ = _ref[0],
|
|
142
|
+
options = _ref$ === void 0 ? {} : _ref$;
|
|
143
|
+
// using `useIdleInterpreter` allows us to subscribe to the service *before* we start it
|
|
144
|
+
// so we don't miss any notifications
|
|
145
|
+
var service = useIdleInterpreter(getMachine, options);
|
|
146
|
+
var getSnapshot = React.useCallback(function () {
|
|
147
|
+
if (service.status === xstate.InterpreterStatus.NotStarted && options.state) {
|
|
148
|
+
var cached = cachedRehydratedStates.get(options.state);
|
|
149
|
+
|
|
150
|
+
if (cached) {
|
|
151
|
+
return cached;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
var created = xstate.State.create(options.state);
|
|
155
|
+
cachedRehydratedStates.set(options.state, created);
|
|
156
|
+
return created;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return service.getSnapshot();
|
|
160
|
+
}, [service]);
|
|
161
|
+
var subscribe = React.useCallback(function (handleStoreChange) {
|
|
162
|
+
var _service$subscribe = service.subscribe(handleStoreChange),
|
|
163
|
+
unsubscribe = _service$subscribe.unsubscribe;
|
|
164
|
+
|
|
165
|
+
return unsubscribe;
|
|
166
|
+
}, [service]);
|
|
167
|
+
var storeSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
168
|
+
React.useEffect(function () {
|
|
169
|
+
var rehydratedState = options.state;
|
|
170
|
+
service.start(rehydratedState ? xstate.State.create(rehydratedState) : undefined);
|
|
171
|
+
return function () {
|
|
172
|
+
service.stop();
|
|
173
|
+
service.status = xstate.InterpreterStatus.NotStarted;
|
|
174
|
+
};
|
|
175
|
+
}, []);
|
|
176
|
+
return [storeSnapshot, service.send, service];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function useActor(actorRef) {
|
|
180
|
+
var subscribe = React.useCallback(function (handleStoreChange) {
|
|
181
|
+
var _actorRef$subscribe = actorRef.subscribe(handleStoreChange),
|
|
182
|
+
unsubscribe = _actorRef$subscribe.unsubscribe;
|
|
183
|
+
|
|
184
|
+
return unsubscribe;
|
|
185
|
+
}, [actorRef]);
|
|
186
|
+
var boundGetSnapshot = React.useCallback(function () {
|
|
187
|
+
return actorRef.getSnapshot();
|
|
188
|
+
}, [actorRef]);
|
|
189
|
+
var storeSnapshot = shim.useSyncExternalStore(subscribe, boundGetSnapshot, boundGetSnapshot);
|
|
190
|
+
var boundSend = React.useCallback(function (event) {
|
|
191
|
+
return actorRef.send(event);
|
|
192
|
+
}, [actorRef]);
|
|
193
|
+
return [storeSnapshot, boundSend];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function defaultCompare(a, b) {
|
|
197
|
+
return a === b;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function useSelector(actor, selector) {
|
|
201
|
+
var compare = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultCompare;
|
|
202
|
+
var subscribe = React.useCallback(function (handleStoreChange) {
|
|
203
|
+
var _actor$subscribe = actor.subscribe(handleStoreChange),
|
|
204
|
+
unsubscribe = _actor$subscribe.unsubscribe;
|
|
205
|
+
|
|
206
|
+
return unsubscribe;
|
|
207
|
+
}, [actor]);
|
|
208
|
+
var boundGetSnapshot = React.useCallback(function () {
|
|
209
|
+
return actor.getSnapshot();
|
|
210
|
+
}, [actor]);
|
|
211
|
+
var selectedSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
212
|
+
return selectedSnapshot;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* React hook that spawns an `ActorRef` with the specified `behavior`.
|
|
217
|
+
* The returned `ActorRef` can be used with the `useActor(actorRef)` hook.
|
|
218
|
+
*
|
|
219
|
+
* @param behavior The actor behavior to spawn
|
|
220
|
+
* @returns An ActorRef with the specified `behavior`
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
function useSpawn(behavior) {
|
|
224
|
+
var actorRef = useConstant.useConstant(function () {
|
|
225
|
+
// TODO: figure out what to do about the name argument
|
|
226
|
+
return new xstate.ObservableActorRef(behavior, '');
|
|
227
|
+
});
|
|
228
|
+
React.useEffect(function () {
|
|
229
|
+
actorRef.start();
|
|
230
|
+
return function () {
|
|
231
|
+
actorRef.stop();
|
|
232
|
+
};
|
|
233
|
+
}, []);
|
|
234
|
+
return actorRef;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
exports.useActor = useActor;
|
|
238
|
+
exports.useInterpret = useInterpret;
|
|
239
|
+
exports.useMachine = useMachine;
|
|
240
|
+
exports.useSelector = useSelector;
|
|
241
|
+
exports.useSpawn = useSpawn;
|