@xstate/react
Advanced tools
Comparing version 1.6.1 to 2.0.0-pr2674-202191175724
# Changelog | ||
## 2.0.0-pr2674-202191175724 | ||
### Major Changes | ||
- e5a8b8dff: author: @Andarist | ||
author: @mattpocock | ||
To avoid breaking any consumers and to leverage the newly introduced typegen support the major version of this package had to be bumped. While you can still use it with older versions of TS the typegen support in this package required at least TS 4.0. | ||
### Patch Changes | ||
- Updated dependencies [1ff4f7976] | ||
- Updated dependencies [1cd26811c] | ||
- xstate@4.26.0-pr2674-202191175724 | ||
## 1.6.1 | ||
@@ -4,0 +19,0 @@ |
@@ -15,2 +15,2 @@ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("xstate"),require("xstate/lib/behaviors")):"function"==typeof define&&define.amd?define(["exports","react","xstate","xstate/lib/behaviors"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateReact={},t.React,t.XState,t.behaviors)}(this,(function(t,e,n,r){"use strict"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var u,i=function(){return(i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var u in e=arguments[n])Object.prototype.hasOwnProperty.call(e,u)&&(t[u]=e[u]);return t}).apply(this,arguments)};function o(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,u,i=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)o.push(r.value)}catch(t){u={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(u)throw u.error}}return o}function c(t,e){for(var n=0,r=e.length,u=t.length;n<r;n++,u++)t[u]=e[n];return t}!function(t){t[t.Effect=1]="Effect",t[t.LayoutEffect=2]="LayoutEffect"}(u||(u={}));var a=e.useLayoutEffect;function f(t){var n=e.useRef();return n.current||(n.current={v:t()}),n.current.v}function s(t,e){var n,r,u=o([[],[]],2),i=u[0],c=u[1];try{for(var a=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),f=a.next();!f.done;f=a.next()){var s=f.value;e(s)?i.push(s):c.push(s)}}catch(t){n={error:t}}finally{try{f&&!f.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return[i,c]}function l(t,e){(0,t.exec)(e.context,e._event.data,{action:t,state:e,_event:e._event})()}function v(t,r,v){void 0===r&&(r={});var p=f((function(){return"function"==typeof t?t():t})),d=r.context,h=r.guards,b=r.actions,y=r.activities,g=r.services,m=r.delays,x=r.state,O=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(t);u<r.length;u++)e.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(t,r[u])&&(n[r[u]]=t[r[u]])}return n}(r,["context","guards","actions","activities","services","delays","state"]),S=f((function(){var t={context:d,guards:h,actions:b,activities:y,services:g,delays:m},e=p.withConfig(t,(function(){return i(i({},p.context),d)}));return n.interpret(e,i({deferEvents:!0},O))}));return a((function(){var t;return v&&(t=S.subscribe(function(t,e,n){if("object"==typeof t)return t;var r=function(){};return{next:t,error:e||r,complete:n||r}}(v))),function(){null==t||t.unsubscribe()}}),[v]),a((function(){return S.start(x?n.State.create(x):void 0),function(){S.stop()}}),[]),a((function(){Object.assign(S.machine.options.actions,b),Object.assign(S.machine.options.guards,h),Object.assign(S.machine.options.activities,y),Object.assign(S.machine.options.services,g),Object.assign(S.machine.options.delays,m)}),[b,h,y,g,m]),function(t){var n=e.useRef([]),r=e.useRef([]);a((function(){var e=t.subscribe((function(t){var e,i;if(t.actions.length){var a=o(s(t.actions.filter((function(t){return"function"==typeof t.exec&&"__effect"in t.exec})),(function(t){return t.exec.__effect===u.Effect})),2),f=a[0],l=a[1];(e=n.current).push.apply(e,c([],o(f.map((function(e){return[e,t]}))))),(i=r.current).push.apply(i,c([],o(l.map((function(e){return[e,t]})))))}}));return function(){e.unsubscribe()}}),[]),a((function(){for(;r.current.length;){var t=o(r.current.shift(),2);l(t[0],t[1])}})),e.useEffect((function(){for(;n.current.length;){var t=o(n.current.shift(),2);l(t[0],t[1])}}))}(S),S}function p(t,e){var n=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return function(){return t.apply(void 0,c([],o(e)))}};return Object.defineProperties(n,{name:{value:"effect:"+t.name},__effect:{value:e}}),n}function d(t){return"state"in t}function h(t){return"deferred"in t}var b=function(){};function y(t){return"getSnapshot"in t?t.getSnapshot():d(t)?t.state:void 0}function g(t,n){void 0===n&&(n=y);var r=e.useRef(t),u=e.useRef([]),i=o(e.useState((function(){return n(t)})),2),c=i[0],s=i[1],l=f((function(){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=t[0],i=r.current;h(i)&&i.deferred?u.current.push(n):i.send(n)}}));return a((function(){r.current=t,s(n(t));for(var e=t.subscribe({next:function(t){return s(t)},error:b,complete:b});u.current.length>0;){var i=u.current.shift();t.send(i)}return function(){e.unsubscribe()}}),[t]),[c,l]}var m=function(t,e){return t===e},x=function(t){return"state"in(n=t)&&"machine"in n?0!==("status"in(e=t)?e.status:e._status)?e.state:e.machine.initialState:d(t)?t.state:void 0;var e,n};t.asEffect=function(t){return p(t,u.Effect)},t.asLayoutEffect=function(t){return p(t,u.LayoutEffect)},t.useActor=g,t.useInterpret=v,t.useMachine=function(t,r){void 0===r&&(r={});var u=e.useCallback((function(t){var e=void 0===t.changed&&Object.keys(t.children).length;(t.changed||e)&&f(t)}),[]),i=v(t,r,u),c=o(e.useState((function(){var t=i.machine.initialState;return r.state?n.State.create(r.state):t})),2),a=c[0],f=c[1];return[a,i.send,i]},t.useSelector=function(t,n,r,u){void 0===r&&(r=m),void 0===u&&(u=x);var i=o(e.useState((function(){return n(u(t))})),2),c=i[0],a=i[1],f=e.useRef(c);return e.useEffect((function(){var e=function(t){r(f.current,t)||(a(t),f.current=t)},i=n(u(t));e(i);var o=t.subscribe((function(t){var r=n(t);e(r)}));return function(){return o.unsubscribe()}}),[n,r]),c},t.useService=function(t){return[o(g(t),1)[0],t.send]},t.useSpawn=function(t){return f((function(){return r.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
***************************************************************************** */var u,i=function(){return(i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var u in e=arguments[n])Object.prototype.hasOwnProperty.call(e,u)&&(t[u]=e[u]);return t}).apply(this,arguments)};function o(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(t);u<r.length;u++)e.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(t,r[u])&&(n[r[u]]=t[r[u]])}return n}function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,u,i=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)o.push(r.value)}catch(t){u={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(u)throw u.error}}return o}function a(t,e){for(var n=0,r=e.length,u=t.length;n<r;n++,u++)t[u]=e[n];return t}!function(t){t[t.Effect=1]="Effect",t[t.LayoutEffect=2]="LayoutEffect"}(u||(u={}));var f=e.useLayoutEffect;function s(t){var n=e.useRef();return n.current||(n.current={v:t()}),n.current.v}function l(t,e){var n,r,u=c([[],[]],2),i=u[0],o=u[1];try{for(var a=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),f=a.next();!f.done;f=a.next()){var s=f.value;e(s)?i.push(s):o.push(s)}}catch(t){n={error:t}}finally{try{f&&!f.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return[i,o]}function v(t,e){(0,t.exec)(e.context,e._event.data,{action:t,state:e,_event:e._event})()}function p(t){var n=e.useRef([]),r=e.useRef([]);f((function(){var e=t.subscribe((function(t){var e,i;if(t.actions.length){var o=c(l(t.actions.filter((function(t){return"function"==typeof t.exec&&"__effect"in t.exec})),(function(t){return t.exec.__effect===u.Effect})),2),f=o[0],s=o[1];(e=n.current).push.apply(e,a([],c(f.map((function(e){return[e,t]}))))),(i=r.current).push.apply(i,a([],c(s.map((function(e){return[e,t]})))))}}));return function(){e.unsubscribe()}}),[]),f((function(){for(;r.current.length;){var t=c(r.current.shift(),2);v(t[0],t[1])}})),e.useEffect((function(){for(;n.current.length;){var t=c(n.current.shift(),2);v(t[0],t[1])}}))}function d(t,e,n){if("object"==typeof t)return t;var r=function(){};return{next:t,error:e||r,complete:n||r}}function h(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];var u=c(e,2),a=u[0],l=void 0===a?{}:a,v=u[1],h=s((function(){return"function"==typeof t?t():t})),b=l.context,y=l.guards,g=l.actions,m=l.services,x=l.delays,O=l.state,S=o(l,["context","guards","actions","services","delays","state"]),j=l.activities,E=s((function(){var t={context:b,guards:y,actions:g,activities:j,services:m,delays:x},e=h.withConfig(t,(function(){return i(i({},h.context),b)}));return n.interpret(e,i({deferEvents:!0},S))}));return f((function(){var t;return v&&(t=E.subscribe(d(v))),function(){null==t||t.unsubscribe()}}),[v]),f((function(){return E.start(O?n.State.create(O):void 0),function(){E.stop()}}),[]),f((function(){Object.assign(E.machine.options.actions,g),Object.assign(E.machine.options.guards,y),Object.assign(E.machine.options.activities,j),Object.assign(E.machine.options.services,m),Object.assign(E.machine.options.delays,x)}),[g,y,j,m,x]),p(E),E}function b(t,e){var n=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return function(){return t.apply(void 0,a([],c(e)))}};return Object.defineProperties(n,{name:{value:"effect:"+t.name},__effect:{value:e}}),n}function y(t){return"state"in t}function g(t){return"deferred"in t}var m=function(){};function x(t){return"getSnapshot"in t?t.getSnapshot():y(t)?t.state:void 0}function O(t,n){void 0===n&&(n=x);var r=e.useRef(t),u=e.useRef([]),i=c(e.useState((function(){return n(t)})),2),o=i[0],a=i[1],l=s((function(){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=t[0],i=r.current;g(i)&&i.deferred?u.current.push(n):i.send(n)}}));return f((function(){r.current=t,a(n(t));for(var e=t.subscribe({next:function(t){return a(t)},error:m,complete:m});u.current.length>0;){var i=u.current.shift();t.send(i)}return function(){e.unsubscribe()}}),[t]),[o,l]}var S=function(t,e){return t===e},j=function(t){return"state"in(n=t)&&"machine"in n?0!==("status"in(e=t)?e.status:e._status)?e.state:e.machine.initialState:y(t)?t.state:void 0;var e,n};t.asEffect=function(t){return b(t,u.Effect)},t.asLayoutEffect=function(t){return b(t,u.LayoutEffect)},t.useActor=O,t.useInterpret=h,t.useMachine=function(t){for(var r=[],u=1;u<arguments.length;u++)r[u-1]=arguments[u];var i=c(r,1),o=i[0],a=void 0===o?{}:o,f=e.useCallback((function(t){var e=void 0===t.changed&&Object.keys(t.children).length;(t.changed||e)&&p(t)}),[]),s=h(t,a,f),l=c(e.useState((function(){var t=s.machine.initialState;return a.state?n.State.create(a.state):t})),2),v=l[0],p=l[1];return[v,s.send,s]},t.useSelector=function(t,n,r,u){void 0===r&&(r=S),void 0===u&&(u=j);var i=c(e.useState((function(){return n(u(t))})),2),o=i[0],a=i[1],f=e.useRef(o);return e.useEffect((function(){var e=function(t){r(f.current,t)||(a(t),f.current=t)},i=n(u(t));e(i);var o=t.subscribe((function(t){var r=n(t);e(r)}));return function(){return o.unsubscribe()}}),[n,r]),o},t.useService=function(t){return[c(O(t),1)[0],t.send]},t.useSpawn=function(t){return s((function(){return r.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})})); |
import { ActionMeta, ActionObject, EventObject, State, StateConfig } from 'xstate'; | ||
export declare type Sender<TEvent extends EventObject> = (event: TEvent) => void; | ||
export interface Subscription { | ||
unsubscribe(): void; | ||
} | ||
export interface Observer<T> { | ||
next?: (value: T) => void; | ||
error?: (errorValue: any) => void; | ||
complete: () => void; | ||
} | ||
export interface Subscribable<T> { | ||
subscribe(observer: Observer<T>): Subscription; | ||
subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription; | ||
} | ||
export declare type MaybeLazy<T> = T | (() => T); | ||
declare type ExcludeType<A> = { | ||
[K in Exclude<keyof A, 'type'>]: A[K]; | ||
}; | ||
declare type ExtractExtraParameters<A, T> = A extends { | ||
type: T; | ||
} ? ExcludeType<A> : never; | ||
declare type ExtractSimple<A> = A extends any ? {} extends ExcludeType<A> ? A : never : never; | ||
declare type NeverIfEmpty<T> = {} extends T ? never : T; | ||
export interface PayloadSender<TEvent extends EventObject> { | ||
/** | ||
* Send an event object or just the event type, if the event has no other payload | ||
*/ | ||
(event: TEvent | ExtractSimple<TEvent>['type']): void; | ||
/** | ||
* Send an event type and its payload | ||
*/ | ||
<K extends TEvent['type']>(eventType: K, payload: NeverIfEmpty<ExtractExtraParameters<TEvent, K>>): void; | ||
} | ||
export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Subscribable<TEmitted> { | ||
send: Sender<TEvent>; | ||
} | ||
export declare type NoInfer<T> = [T][T extends any ? 0 : any]; | ||
export declare type Prop<T, K> = K extends keyof T ? T[K] : never; | ||
export declare enum ReactEffectType { | ||
@@ -63,3 +31,2 @@ Effect = 1, | ||
]; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,3 +0,2 @@ | ||
import { Sender } from './types'; | ||
import { ActorRef, EventObject } from 'xstate'; | ||
import { ActorRef, EventObject, Sender } from 'xstate'; | ||
export declare function isActorWithState<T extends ActorRef<any>>(actorRef: T): actorRef is T & { | ||
@@ -4,0 +3,0 @@ state: any; |
@@ -1,8 +0,13 @@ | ||
import { EventObject, StateMachine, State, Interpreter, InterpreterOptions, MachineOptions, Typestate, Observer } from 'xstate'; | ||
import { StateMachine, State, InterpreterFrom, InterpreterOptions, Observer, AreAllImplementationsAssumedToBeProvided, MaybeTypegenMachineOptions, BaseActionObject } from 'xstate'; | ||
import { MaybeLazy } from './types'; | ||
import { UseMachineOptions } from './useMachine'; | ||
export declare function useInterpret<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = { | ||
value: any; | ||
context: TContext; | ||
}>(getMachine: MaybeLazy<StateMachine<TContext, any, TEvent, TTypestate>>, options?: Partial<InterpreterOptions> & Partial<UseMachineOptions<TContext, TEvent>> & Partial<MachineOptions<TContext, TEvent>>, observerOrListener?: Observer<State<TContext, TEvent, any, TTypestate>> | ((value: State<TContext, TEvent, any, TTypestate>) => void)): Interpreter<TContext, any, TEvent, TTypestate>; | ||
declare type RestParams<TMachine> = TMachine extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate, any, infer TResolvedTypesMeta> ? AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? [ | ||
options: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta, true>, | ||
observerOrListener?: Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>> | ((value: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>) => void) | ||
] : [ | ||
options?: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta>, | ||
observerOrListener?: Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>> | ((value: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>) => void) | ||
] : never; | ||
export declare function useInterpret<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>; | ||
export {}; | ||
//# sourceMappingURL=useInterpret.d.ts.map |
@@ -57,4 +57,8 @@ var __assign = (this && this.__assign) || function () { | ||
} | ||
export function useInterpret(getMachine, options, observerOrListener) { | ||
if (options === void 0) { options = {}; } | ||
export function useInterpret(getMachine) { | ||
var _a = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
_a[_i - 1] = arguments[_i]; | ||
} | ||
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1]; | ||
var machine = useConstant(function () { | ||
@@ -65,3 +69,3 @@ return typeof getMachine === 'function' ? getMachine() : getMachine; | ||
typeof getMachine !== 'function') { | ||
var _a = __read(useState(machine), 1), initialMachine = _a[0]; | ||
var _d = __read(useState(machine), 1), initialMachine = _d[0]; | ||
if (machine !== initialMachine) { | ||
@@ -72,3 +76,5 @@ console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' + | ||
} | ||
var context = options.context, guards = options.guards, actions = options.actions, activities = options.activities, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "activities", "services", "delays", "state"]); | ||
var context = options.context, guards = options.guards, actions = options.actions, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "services", "delays", "state"]); | ||
// it's not defined in `TypegenMachineOptions` so we can't just unpack this property here freely | ||
var activities = options.activities; | ||
var service = useConstant(function () { | ||
@@ -75,0 +81,0 @@ var machineConfig = { |
@@ -1,3 +0,3 @@ | ||
import { EventObject, StateMachine, State, Interpreter, InterpreterOptions, MachineOptions, StateConfig, Typestate, ActionFunction } from 'xstate'; | ||
import { MaybeLazy, ReactActionFunction } from './types'; | ||
import { ActionFunction, AreAllImplementationsAssumedToBeProvided, BaseActionObject, EventObject, InterpreterFrom, InterpreterOptions, MaybeTypegenMachineOptions, StateConfig, StateFrom, StateMachine } from 'xstate'; | ||
import { MaybeLazy, Prop, ReactActionFunction } from './types'; | ||
export declare function asEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>; | ||
@@ -16,10 +16,10 @@ export declare function asLayoutEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>; | ||
} | ||
export declare function useMachine<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = { | ||
value: any; | ||
context: TContext; | ||
}>(getMachine: MaybeLazy<StateMachine<TContext, any, TEvent, TTypestate>>, options?: Partial<InterpreterOptions> & Partial<UseMachineOptions<TContext, TEvent>> & Partial<MachineOptions<TContext, TEvent>>): [ | ||
State<TContext, TEvent, any, TTypestate>, | ||
Interpreter<TContext, any, TEvent, TTypestate>['send'], | ||
Interpreter<TContext, any, TEvent, TTypestate> | ||
]; | ||
declare type RestParams<TMachine> = TMachine extends StateMachine<infer TContext, any, infer TEvent, any, any, infer TResolvedTypesMeta> ? AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? [ | ||
options: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta, true> | ||
] : [ | ||
options?: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta> | ||
] : never; | ||
declare type UseMachineReturn<TMachine extends StateMachine<any, any, any, any, any, any>, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter]; | ||
export declare function useMachine<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>; | ||
export {}; | ||
//# sourceMappingURL=useMachine.d.ts.map |
@@ -49,4 +49,8 @@ var __read = (this && this.__read) || function (o, n) { | ||
} | ||
export function useMachine(getMachine, options) { | ||
if (options === void 0) { options = {}; } | ||
export function useMachine(getMachine) { | ||
var _a = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
_a[_i - 1] = arguments[_i]; | ||
} | ||
var _b = __read(_a, 1), _c = _b[0], options = _c === void 0 ? {} : _c; | ||
var listener = useCallback(function (nextState) { | ||
@@ -58,4 +62,3 @@ // Only change the current state if: | ||
// The "live" initial state will have .changed === undefined. | ||
var initialStateChanged = nextState.changed === undefined && | ||
Object.keys(nextState.children).length; | ||
var initialStateChanged = nextState.changed === undefined && Object.keys(nextState.children).length; | ||
if (nextState.changed || initialStateChanged) { | ||
@@ -66,3 +69,3 @@ setState(nextState); | ||
var service = useInterpret(getMachine, options, listener); | ||
var _a = __read(useState(function () { | ||
var _d = __read(useState(function () { | ||
var initialState = service.machine.initialState; | ||
@@ -72,4 +75,4 @@ return (options.state | ||
: initialState); | ||
}), 2), state = _a[0], setState = _a[1]; | ||
}), 2), state = _d[0], setState = _d[1]; | ||
return [state, service.send, service]; | ||
} |
@@ -1,3 +0,2 @@ | ||
import { EventObject, State, Interpreter, Typestate } from 'xstate'; | ||
import { PayloadSender } from './types'; | ||
import { EventObject, Interpreter, PayloadSender, State, TypegenConstraint, TypegenDisabled, Typestate } from 'xstate'; | ||
export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state']; | ||
@@ -13,3 +12,6 @@ /** | ||
context: TContext; | ||
}>(service: Interpreter<TContext, any, TEvent, TTypestate>): [State<TContext, TEvent, any, TTypestate>, PayloadSender<TEvent>]; | ||
}, TTypesMeta extends TypegenConstraint = TypegenDisabled>(service: Interpreter<TContext, any, TEvent, TTypestate, TTypesMeta>): [ | ||
State<TContext, TEvent, any, TTypestate, TTypesMeta>, | ||
PayloadSender<TEvent> | ||
]; | ||
//# sourceMappingURL=useService.d.ts.map |
import { ActionMeta, ActionObject, EventObject, State, StateConfig } from 'xstate'; | ||
export declare type Sender<TEvent extends EventObject> = (event: TEvent) => void; | ||
export interface Subscription { | ||
unsubscribe(): void; | ||
} | ||
export interface Observer<T> { | ||
next?: (value: T) => void; | ||
error?: (errorValue: any) => void; | ||
complete: () => void; | ||
} | ||
export interface Subscribable<T> { | ||
subscribe(observer: Observer<T>): Subscription; | ||
subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription; | ||
} | ||
export declare type MaybeLazy<T> = T | (() => T); | ||
declare type ExcludeType<A> = { | ||
[K in Exclude<keyof A, 'type'>]: A[K]; | ||
}; | ||
declare type ExtractExtraParameters<A, T> = A extends { | ||
type: T; | ||
} ? ExcludeType<A> : never; | ||
declare type ExtractSimple<A> = A extends any ? {} extends ExcludeType<A> ? A : never : never; | ||
declare type NeverIfEmpty<T> = {} extends T ? never : T; | ||
export interface PayloadSender<TEvent extends EventObject> { | ||
/** | ||
* Send an event object or just the event type, if the event has no other payload | ||
*/ | ||
(event: TEvent | ExtractSimple<TEvent>['type']): void; | ||
/** | ||
* Send an event type and its payload | ||
*/ | ||
<K extends TEvent['type']>(eventType: K, payload: NeverIfEmpty<ExtractExtraParameters<TEvent, K>>): void; | ||
} | ||
export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Subscribable<TEmitted> { | ||
send: Sender<TEvent>; | ||
} | ||
export declare type NoInfer<T> = [T][T extends any ? 0 : any]; | ||
export declare type Prop<T, K> = K extends keyof T ? T[K] : never; | ||
export declare enum ReactEffectType { | ||
@@ -63,3 +31,2 @@ Effect = 1, | ||
]; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,3 +0,2 @@ | ||
import { Sender } from './types'; | ||
import { ActorRef, EventObject } from 'xstate'; | ||
import { ActorRef, EventObject, Sender } from 'xstate'; | ||
export declare function isActorWithState<T extends ActorRef<any>>(actorRef: T): actorRef is T & { | ||
@@ -4,0 +3,0 @@ state: any; |
@@ -1,8 +0,13 @@ | ||
import { EventObject, StateMachine, State, Interpreter, InterpreterOptions, MachineOptions, Typestate, Observer } from 'xstate'; | ||
import { StateMachine, State, InterpreterFrom, InterpreterOptions, Observer, AreAllImplementationsAssumedToBeProvided, MaybeTypegenMachineOptions, BaseActionObject } from 'xstate'; | ||
import { MaybeLazy } from './types'; | ||
import { UseMachineOptions } from './useMachine'; | ||
export declare function useInterpret<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = { | ||
value: any; | ||
context: TContext; | ||
}>(getMachine: MaybeLazy<StateMachine<TContext, any, TEvent, TTypestate>>, options?: Partial<InterpreterOptions> & Partial<UseMachineOptions<TContext, TEvent>> & Partial<MachineOptions<TContext, TEvent>>, observerOrListener?: Observer<State<TContext, TEvent, any, TTypestate>> | ((value: State<TContext, TEvent, any, TTypestate>) => void)): Interpreter<TContext, any, TEvent, TTypestate>; | ||
declare type RestParams<TMachine> = TMachine extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate, any, infer TResolvedTypesMeta> ? AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? [ | ||
options: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta, true>, | ||
observerOrListener?: Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>> | ((value: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>) => void) | ||
] : [ | ||
options?: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta>, | ||
observerOrListener?: Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>> | ((value: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>) => void) | ||
] : never; | ||
export declare function useInterpret<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>; | ||
export {}; | ||
//# sourceMappingURL=useInterpret.d.ts.map |
@@ -60,4 +60,8 @@ "use strict"; | ||
} | ||
function useInterpret(getMachine, options, observerOrListener) { | ||
if (options === void 0) { options = {}; } | ||
function useInterpret(getMachine) { | ||
var _a = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
_a[_i - 1] = arguments[_i]; | ||
} | ||
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1]; | ||
var machine = useConstant_1.default(function () { | ||
@@ -68,3 +72,3 @@ return typeof getMachine === 'function' ? getMachine() : getMachine; | ||
typeof getMachine !== 'function') { | ||
var _a = __read(react_1.useState(machine), 1), initialMachine = _a[0]; | ||
var _d = __read(react_1.useState(machine), 1), initialMachine = _d[0]; | ||
if (machine !== initialMachine) { | ||
@@ -75,3 +79,5 @@ console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' + | ||
} | ||
var context = options.context, guards = options.guards, actions = options.actions, activities = options.activities, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "activities", "services", "delays", "state"]); | ||
var context = options.context, guards = options.guards, actions = options.actions, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "services", "delays", "state"]); | ||
// it's not defined in `TypegenMachineOptions` so we can't just unpack this property here freely | ||
var activities = options.activities; | ||
var service = useConstant_1.default(function () { | ||
@@ -78,0 +84,0 @@ var machineConfig = { |
@@ -1,3 +0,3 @@ | ||
import { EventObject, StateMachine, State, Interpreter, InterpreterOptions, MachineOptions, StateConfig, Typestate, ActionFunction } from 'xstate'; | ||
import { MaybeLazy, ReactActionFunction } from './types'; | ||
import { ActionFunction, AreAllImplementationsAssumedToBeProvided, BaseActionObject, EventObject, InterpreterFrom, InterpreterOptions, MaybeTypegenMachineOptions, StateConfig, StateFrom, StateMachine } from 'xstate'; | ||
import { MaybeLazy, Prop, ReactActionFunction } from './types'; | ||
export declare function asEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>; | ||
@@ -16,10 +16,10 @@ export declare function asLayoutEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>; | ||
} | ||
export declare function useMachine<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = { | ||
value: any; | ||
context: TContext; | ||
}>(getMachine: MaybeLazy<StateMachine<TContext, any, TEvent, TTypestate>>, options?: Partial<InterpreterOptions> & Partial<UseMachineOptions<TContext, TEvent>> & Partial<MachineOptions<TContext, TEvent>>): [ | ||
State<TContext, TEvent, any, TTypestate>, | ||
Interpreter<TContext, any, TEvent, TTypestate>['send'], | ||
Interpreter<TContext, any, TEvent, TTypestate> | ||
]; | ||
declare type RestParams<TMachine> = TMachine extends StateMachine<infer TContext, any, infer TEvent, any, any, infer TResolvedTypesMeta> ? AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? [ | ||
options: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta, true> | ||
] : [ | ||
options?: InterpreterOptions & UseMachineOptions<TContext, TEvent> & MaybeTypegenMachineOptions<TContext, TEvent, BaseActionObject, TResolvedTypesMeta> | ||
] : never; | ||
declare type UseMachineReturn<TMachine extends StateMachine<any, any, any, any, any, any>, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter]; | ||
export declare function useMachine<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>; | ||
export {}; | ||
//# sourceMappingURL=useMachine.d.ts.map |
@@ -54,4 +54,8 @@ "use strict"; | ||
exports.asLayoutEffect = asLayoutEffect; | ||
function useMachine(getMachine, options) { | ||
if (options === void 0) { options = {}; } | ||
function useMachine(getMachine) { | ||
var _a = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
_a[_i - 1] = arguments[_i]; | ||
} | ||
var _b = __read(_a, 1), _c = _b[0], options = _c === void 0 ? {} : _c; | ||
var listener = react_1.useCallback(function (nextState) { | ||
@@ -63,4 +67,3 @@ // Only change the current state if: | ||
// The "live" initial state will have .changed === undefined. | ||
var initialStateChanged = nextState.changed === undefined && | ||
Object.keys(nextState.children).length; | ||
var initialStateChanged = nextState.changed === undefined && Object.keys(nextState.children).length; | ||
if (nextState.changed || initialStateChanged) { | ||
@@ -71,3 +74,3 @@ setState(nextState); | ||
var service = useInterpret_1.useInterpret(getMachine, options, listener); | ||
var _a = __read(react_1.useState(function () { | ||
var _d = __read(react_1.useState(function () { | ||
var initialState = service.machine.initialState; | ||
@@ -77,5 +80,5 @@ return (options.state | ||
: initialState); | ||
}), 2), state = _a[0], setState = _a[1]; | ||
}), 2), state = _d[0], setState = _d[1]; | ||
return [state, service.send, service]; | ||
} | ||
exports.useMachine = useMachine; |
@@ -1,3 +0,2 @@ | ||
import { EventObject, State, Interpreter, Typestate } from 'xstate'; | ||
import { PayloadSender } from './types'; | ||
import { EventObject, Interpreter, PayloadSender, State, TypegenConstraint, TypegenDisabled, Typestate } from 'xstate'; | ||
export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state']; | ||
@@ -13,3 +12,6 @@ /** | ||
context: TContext; | ||
}>(service: Interpreter<TContext, any, TEvent, TTypestate>): [State<TContext, TEvent, any, TTypestate>, PayloadSender<TEvent>]; | ||
}, TTypesMeta extends TypegenConstraint = TypegenDisabled>(service: Interpreter<TContext, any, TEvent, TTypestate, TTypesMeta>): [ | ||
State<TContext, TEvent, any, TTypestate, TTypesMeta>, | ||
PayloadSender<TEvent> | ||
]; | ||
//# sourceMappingURL=useService.d.ts.map |
{ | ||
"name": "@xstate/react", | ||
"version": "1.6.1", | ||
"version": "2.0.0-pr2674-202191175724", | ||
"description": "XState tools for React", | ||
@@ -21,2 +21,9 @@ "keywords": [ | ||
"types": "lib/index.d.ts", | ||
"typesVersions": { | ||
"<4.0": { | ||
"lib/index.d.ts": [ | ||
"index.v3.d.ts" | ||
] | ||
} | ||
}, | ||
"sideEffects": false, | ||
@@ -51,3 +58,3 @@ "directories": { | ||
"react": "^16.8.0 || ^17.0.0", | ||
"xstate": "^4.11.0" | ||
"xstate": "^4.26.0-pr2674-202191175724" | ||
}, | ||
@@ -54,0 +61,0 @@ "peerDependenciesMeta": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112493
56
1570
3