UNPKG

1.71 kBJavaScriptView Raw
1!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("xstate"),require("jotai")):"function"==typeof define&&define.amd?define(["exports","xstate","jotai"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).jotaiXstate={},t.xstate,t.jotai)}(this,(function(t,n,e){"use strict";function i(){return i=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},i.apply(this,arguments)}var o=["guards","actions","activities","services","delays"];t.atomWithMachine=function(t,r){var a=e.atom(null),u=e.atom((function(e){var u=e(a);if(u)return u;var c=!0,f=function(t){if(c)return e(t);throw new Error("get not allowed after initialization")},s="function"==typeof t?t(f):t,l="function"==typeof r?r(f):r;c=!1;var v=l||{},d=v.guards,p=v.actions,y=v.activities,h=v.services,g=v.delays,m=function(t,n){if(null==t)return{};var e,i,o={},r=Object.keys(t);for(i=0;i<r.length;i++)e=r[i],n.indexOf(e)>=0||(o[e]=t[e]);return o}(v,o),j=i({},d&&{guards:d},p&&{actions:p},y&&{activities:y},h&&{services:h},g&&{delays:g}),x=s.withConfig(j,s.context);return{machine:x,service:n.interpret(x,m)}}),(function(t,n,e){n(a,t(u))}));u.onMount=function(t){t()};var c=e.atom(null),f=e.atom((function(t){var n;return null!=(n=t(c))?n:t(u).machine.initialState}),(function(t,n,e){var i=t(u).service;i.onTransition((function(t){n(c,t)})),i.start(),e((function(){i.stop()}))}));return f.onMount=function(t){var n;return t((function(t){!1===n?t():n=t})),function(){n&&n(),n=!1}},e.atom((function(t){return t(f)}),(function(t,n,e){t(u).service.send(e)}))},Object.defineProperty(t,"__esModule",{value:!0})}));