Skip to content

Commit a465226

Browse files
committed
feat: add addXStateService function
1 parent cecb9d7 commit a465226

File tree

16 files changed

+214
-115
lines changed

16 files changed

+214
-115
lines changed

.ncurc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"reject": ["standard"]
3+
}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [3.1.0] - 2022-07-15
9+
10+
### New
11+
- Add `addXStateService` function for registering XState services that can be retrieved by `getXStateService` function.
12+
813
## [3.0.2] - 2022-07-08
914

1015
### Fixes
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* global describe cy before it */
2+
describe('App with XState full version', () => {
3+
before(() => {
4+
cy.visit('app-with-xstate-full')
5+
})
6+
7+
it('has an add water button', () => {
8+
cy.get('add-water-button')
9+
.should('exist')
10+
})
11+
12+
it('should display water level', () => {
13+
cy.get('display-button')
14+
.should('have.text', 'You have filled 0 times!')
15+
})
16+
17+
function filling (expecting) {
18+
describe('adding water', () => {
19+
before(() => {
20+
cy.get('add-water-button button').click()
21+
})
22+
23+
it('should display water level', () => {
24+
cy.get('display-button')
25+
.should('have.text', expecting)
26+
})
27+
})
28+
}
29+
30+
[
31+
'You have filled 1 times!',
32+
'You have filled 2 times!',
33+
'You have filled 3 times!',
34+
'You have filled 4 times!',
35+
'You have filled 5 times!',
36+
'You have filled 6 times!',
37+
'You have filled 7 times!',
38+
'You have filled 8 times!',
39+
'You have filled 9 times!',
40+
'The glass is full!',
41+
'The glass is full!'
42+
].forEach(e => filling(e))
43+
})

dist/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1212
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1313
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1414
PERFORMANCE OF THIS SOFTWARE.
15-
***************************************************************************** */function t(e,n){var h="function"==typeof Symbol&&e[Symbol.iterator];if(!h)return e;var _,b,p=h.call(e),d=[];try{for(;(void 0===n||n-- >0)&&!(_=p.next()).done;)d.push(_.value)}catch(e){b={error:e}}finally{try{_&&!_.done&&(h=p.return)&&h.call(p)}finally{if(b)throw b.error}}return d}var b;!function(e){e[e.NotStarted=0]="NotStarted",e[e.Running=1]="Running",e[e.Stopped=2]="Stopped"}(b||(b={}));var p={type:"xstate.init"};function r(e){return void 0===e?[]:[].concat(e)}function i(e){return{type:"xstate.assign",assignment:e}}function o(e,n){return"string"==typeof(e="string"==typeof e&&n&&n[e]?n[e]:e)?{type:e}:"function"==typeof e?{type:e.name,exec:e}:e}function a(e){return function(n){return e===n}}function u(e){return"string"==typeof e?{type:e}:e}function c(e,n){return{value:e,context:n,actions:[],changed:!1,matches:a(e)}}function f(e,n,h){var _=n,b=!1;return[e.filter((function(e){if("xstate.assign"===e.type){b=!0;var n=Object.assign({},_);return"function"==typeof e.assignment?n=e.assignment(_,h):Object.keys(e.assignment).forEach((function(b){n[b]="function"==typeof e.assignment[b]?e.assignment[b](_,h):e.assignment[b]})),_=n,!1}return!0})),_,b]}function s(e,n){void 0===n&&(n={});var h=t(f(r(e.states[e.initial].entry).map((function(e){return o(e,n.actions)})),e.context,p),2),_=h[0],b=h[1],d={config:e,_options:n,initialState:{value:e.initial,actions:_,context:b,matches:a(e.initial)},transition:function(n,h){var _,b,p="string"==typeof n?{value:n,context:e.context}:n,S=p.value,g=p.context,m=u(h),v=e.states[S];if(v.on){var y=r(v.on[m.type]);try{for(var T=function(e){var n="function"==typeof Symbol&&Symbol.iterator,h=n&&e[n],_=0;if(h)return h.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&_>=e.length&&(e=void 0),{value:e&&e[_++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}(y),j=T.next();!j.done;j=T.next()){var w=j.value;if(void 0===w)return c(S,g);var x="string"==typeof w?{target:w}:w,k=x.target,C=x.actions,I=void 0===C?[]:C,N=x.cond,O=void 0===N?function(){return!0}:N,M=void 0===k,U=null!=k?k:S,D=e.states[U];if(O(g,m)){var E=t(f((M?r(I):[].concat(v.exit,I,D.entry).filter((function(e){return e}))).map((function(e){return o(e,d._options.actions)})),g,m),3),R=E[0],F=E[1],X=E[2],A=null!=k?k:S;return{value:A,context:F,actions:R,changed:k!==S||R.length>0||X,matches:a(A)}}}}catch(e){_={error:e}}finally{try{j&&!j.done&&(b=T.return)&&b.call(T)}finally{if(_)throw _.error}}}return c(S,g)}};return d}var l=function(e,n){return e.actions.forEach((function(h){var _=h.exec;return _&&_(e.context,n)}))};function interpret(e,n){const h=wrapXStateService(function(e){var n=e.initialState,h=b.NotStarted,_=new Set,d={_machine:e,send:function(p){h===b.Running&&(n=e.transition(n,p),l(n,u(p)),_.forEach((function(e){return e(n)})))},subscribe:function(e){return _.add(e),e(n),{unsubscribe:function(){return _.delete(e)}}},start:function(_){if(_){var S="object"==typeof _?_:{context:e.config.context,value:_};n={value:S.value,actions:[],context:S.context,matches:a(S.value)}}else n=e.initialState;return h=b.Running,l(n,p),d},stop:function(){return h=b.Stopped,_.clear(),d},get state(){return n},get status(){return h}};return d}(e));return n?function(e,n){return e._getterCache={},e.getters=new Proxy(n,{get(n,h){if(!e._getterCache[h]){const _=n[h](e._xstateService.state.context);e._getterCache[h]=_}return e._getterCache[h]}}),e.subscribe((()=>{e._getterCache={}})),e}(h,n):h}function createXStateService(e,n,h){const _=interpret(n,h);return globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.xstate=globalThis.__ficusjs__.xstate||{},globalThis.__ficusjs__.xstate[e]=_,_}function getXStateService(e){if(globalThis.__ficusjs__&&globalThis.__ficusjs__.xstate&&globalThis.__ficusjs__.xstate[e])return globalThis.__ficusjs__.xstate[e]}export{e as BasePersist,n as Store,i as assign,createAppState,s as createMachine,createPersist,createStateMachine,createXStateService,getAppState,getXStateService,interpret,withLocalState,withStateMachine,withStore,withWorkerStore,withXStateService,wrapXStateService};
15+
***************************************************************************** */function t(e,n){var h="function"==typeof Symbol&&e[Symbol.iterator];if(!h)return e;var _,b,p=h.call(e),d=[];try{for(;(void 0===n||n-- >0)&&!(_=p.next()).done;)d.push(_.value)}catch(e){b={error:e}}finally{try{_&&!_.done&&(h=p.return)&&h.call(p)}finally{if(b)throw b.error}}return d}var b;!function(e){e[e.NotStarted=0]="NotStarted",e[e.Running=1]="Running",e[e.Stopped=2]="Stopped"}(b||(b={}));var p={type:"xstate.init"};function r(e){return void 0===e?[]:[].concat(e)}function i(e){return{type:"xstate.assign",assignment:e}}function o(e,n){return"string"==typeof(e="string"==typeof e&&n&&n[e]?n[e]:e)?{type:e}:"function"==typeof e?{type:e.name,exec:e}:e}function a(e){return function(n){return e===n}}function u(e){return"string"==typeof e?{type:e}:e}function c(e,n){return{value:e,context:n,actions:[],changed:!1,matches:a(e)}}function f(e,n,h){var _=n,b=!1;return[e.filter((function(e){if("xstate.assign"===e.type){b=!0;var n=Object.assign({},_);return"function"==typeof e.assignment?n=e.assignment(_,h):Object.keys(e.assignment).forEach((function(b){n[b]="function"==typeof e.assignment[b]?e.assignment[b](_,h):e.assignment[b]})),_=n,!1}return!0})),_,b]}function s(e,n){void 0===n&&(n={});var h=t(f(r(e.states[e.initial].entry).map((function(e){return o(e,n.actions)})),e.context,p),2),_=h[0],b=h[1],d={config:e,_options:n,initialState:{value:e.initial,actions:_,context:b,matches:a(e.initial)},transition:function(n,h){var _,b,p="string"==typeof n?{value:n,context:e.context}:n,S=p.value,g=p.context,m=u(h),v=e.states[S];if(v.on){var y=r(v.on[m.type]);try{for(var T=function(e){var n="function"==typeof Symbol&&Symbol.iterator,h=n&&e[n],_=0;if(h)return h.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&_>=e.length&&(e=void 0),{value:e&&e[_++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}(y),j=T.next();!j.done;j=T.next()){var x=j.value;if(void 0===x)return c(S,g);var w="string"==typeof x?{target:x}:x,k=w.target,C=w.actions,I=void 0===C?[]:C,N=w.cond,O=void 0===N?function(){return!0}:N,M=void 0===k,U=null!=k?k:S,D=e.states[U];if(O(g,m)){var E=t(f((M?r(I):[].concat(v.exit,I,D.entry).filter((function(e){return e}))).map((function(e){return o(e,d._options.actions)})),g,m),3),R=E[0],X=E[1],F=E[2],A=null!=k?k:S;return{value:A,context:X,actions:R,changed:k!==S||R.length>0||F,matches:a(A)}}}}catch(e){_={error:e}}finally{try{j&&!j.done&&(b=T.return)&&b.call(T)}finally{if(_)throw _.error}}}return c(S,g)}};return d}var l=function(e,n){return e.actions.forEach((function(h){var _=h.exec;return _&&_(e.context,n)}))};function interpret(e,n){const h=wrapXStateService(function(e){var n=e.initialState,h=b.NotStarted,_=new Set,d={_machine:e,send:function(p){h===b.Running&&(n=e.transition(n,p),l(n,u(p)),_.forEach((function(e){return e(n)})))},subscribe:function(e){return _.add(e),e(n),{unsubscribe:function(){return _.delete(e)}}},start:function(_){if(_){var S="object"==typeof _?_:{context:e.config.context,value:_};n={value:S.value,actions:[],context:S.context,matches:a(S.value)}}else n=e.initialState;return h=b.Running,l(n,p),d},stop:function(){return h=b.Stopped,_.clear(),d},get state(){return n},get status(){return h}};return d}(e));return n?function(e,n){return e._getterCache={},e.getters=new Proxy(n,{get(n,h){if(!e._getterCache[h]){const _=n[h](e._xstateService.state.context);e._getterCache[h]=_}return e._getterCache[h]}}),e.subscribe((()=>{e._getterCache={}})),e}(h,n):h}function createXStateService(e,n,h){const _=interpret(n,h);return globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.xstate=globalThis.__ficusjs__.xstate||{},globalThis.__ficusjs__.xstate[e]=_,_}function addXStateService(e,n){return globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.xstate=globalThis.__ficusjs__.xstate||{},globalThis.__ficusjs__.xstate[e]=n,n}function getXStateService(e){if(globalThis.__ficusjs__&&globalThis.__ficusjs__.xstate&&globalThis.__ficusjs__.xstate[e])return globalThis.__ficusjs__.xstate[e]}export{e as BasePersist,n as Store,addXStateService,i as assign,createAppState,s as createMachine,createPersist,createStateMachine,createXStateService,getAppState,getXStateService,interpret,withLocalState,withStateMachine,withStore,withWorkerStore,withXStateService,wrapXStateService};

dist/xstate-service.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1212
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1313
PERFORMANCE OF THIS SOFTWARE.
1414
***************************************************************************** */
15-
function t(e,n){var v="function"==typeof Symbol&&e[Symbol.iterator];if(!v)return e;var _,g,h=v.call(e),p=[];try{for(;(void 0===n||n-- >0)&&!(_=h.next()).done;)p.push(_.value)}catch(e){g={error:e}}finally{try{_&&!_.done&&(v=h.return)&&v.call(h)}finally{if(g)throw g.error}}return p}var e;!function(e){e[e.NotStarted=0]="NotStarted",e[e.Running=1]="Running",e[e.Stopped=2]="Stopped"}(e||(e={}));var n={type:"xstate.init"};function r(e){return void 0===e?[]:[].concat(e)}function i(e){return{type:"xstate.assign",assignment:e}}function o(e,n){return"string"==typeof(e="string"==typeof e&&n&&n[e]?n[e]:e)?{type:e}:"function"==typeof e?{type:e.name,exec:e}:e}function a(e){return function(n){return e===n}}function u(e){return"string"==typeof e?{type:e}:e}function c(e,n){return{value:e,context:n,actions:[],changed:!1,matches:a(e)}}function f(e,n,v){var _=n,g=!1;return[e.filter((function(e){if("xstate.assign"===e.type){g=!0;var n=Object.assign({},_);return"function"==typeof e.assignment?n=e.assignment(_,v):Object.keys(e.assignment).forEach((function(g){n[g]="function"==typeof e.assignment[g]?e.assignment[g](_,v):e.assignment[g]})),_=n,!1}return!0})),_,g]}function s(e,v){void 0===v&&(v={});var _=t(f(r(e.states[e.initial].entry).map((function(e){return o(e,v.actions)})),e.context,n),2),g=_[0],h=_[1],p={config:e,_options:v,initialState:{value:e.initial,actions:g,context:h,matches:a(e.initial)},transition:function(n,v){var _,g,h="string"==typeof n?{value:n,context:e.context}:n,x=h.value,S=h.context,y=u(v),b=e.states[x];if(b.on){var d=r(b.on[y.type]);try{for(var m=function(e){var n="function"==typeof Symbol&&Symbol.iterator,v=n&&e[n],_=0;if(v)return v.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&_>=e.length&&(e=void 0),{value:e&&e[_++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}(d),T=m.next();!T.done;T=m.next()){var j=T.value;if(void 0===j)return c(x,S);var w="string"==typeof j?{target:j}:j,N=w.target,X=w.actions,E=void 0===X?[]:X,O=w.cond,R=void 0===O?function(){return!0}:O,C=void 0===N,I=null!=N?N:x,P=e.states[I];if(R(S,y)){var D=t(f((C?r(E):[].concat(b.exit,E,P.entry).filter((function(e){return e}))).map((function(e){return o(e,p._options.actions)})),S,y),3),k=D[0],z=D[1],A=D[2],G=null!=N?N:x;return{value:G,context:z,actions:k,changed:N!==x||k.length>0||A,matches:a(G)}}}}catch(e){_={error:e}}finally{try{T&&!T.done&&(g=m.return)&&g.call(m)}finally{if(_)throw _.error}}}return c(x,S)}};return p}var l=function(e,n){return e.actions.forEach((function(v){var _=v.exec;return _&&_(e.context,n)}))};function wrapXStateService(e){return{_xstateService:e,get status(){return this._xstateService?this._xstateService.status:v.INIT},get state(){return this._xstateService.state},subscribe(e){return this._xstateService.subscribe(e)},send(e){this._xstateService.send(e)},start(){this._xstateService.start()}}}const v=Object.freeze({INIT:-1,NOT_STARTED:0,RUNNING:1,STOPPED:2});function interpret(v,_){const g=wrapXStateService(function(v){var _=v.initialState,g=e.NotStarted,h=new Set,p={_machine:v,send:function(n){g===e.Running&&(_=v.transition(_,n),l(_,u(n)),h.forEach((function(e){return e(_)})))},subscribe:function(e){return h.add(e),e(_),{unsubscribe:function(){return h.delete(e)}}},start:function(h){if(h){var x="object"==typeof h?h:{context:v.config.context,value:h};_={value:x.value,actions:[],context:x.context,matches:a(x.value)}}else _=v.initialState;return g=e.Running,l(_,n),p},stop:function(){return g=e.Stopped,h.clear(),p},get state(){return _},get status(){return g}};return p}(v));return _?function(e,n){return e._getterCache={},e.getters=new Proxy(n,{get(n,v){if(!e._getterCache[v]){const _=n[v](e._xstateService.state.context);e._getterCache[v]=_}return e._getterCache[v]}}),e.subscribe((()=>{e._getterCache={}})),e}(g,_):g}function createXStateService(e,n,v){const _=interpret(n,v);return globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.xstate=globalThis.__ficusjs__.xstate||{},globalThis.__ficusjs__.xstate[e]=_,_}function getXStateService(e){if(globalThis.__ficusjs__&&globalThis.__ficusjs__.xstate&&globalThis.__ficusjs__.xstate[e])return globalThis.__ficusjs__.xstate[e]}export{v as XStateServiceStatus,i as assign,s as createMachine,createXStateService,getXStateService,interpret,wrapXStateService};
15+
function t(e,n){var _="function"==typeof Symbol&&e[Symbol.iterator];if(!_)return e;var g,v,h=_.call(e),S=[];try{for(;(void 0===n||n-- >0)&&!(g=h.next()).done;)S.push(g.value)}catch(e){v={error:e}}finally{try{g&&!g.done&&(_=h.return)&&_.call(h)}finally{if(v)throw v.error}}return S}var e;!function(e){e[e.NotStarted=0]="NotStarted",e[e.Running=1]="Running",e[e.Stopped=2]="Stopped"}(e||(e={}));var n={type:"xstate.init"};function r(e){return void 0===e?[]:[].concat(e)}function i(e){return{type:"xstate.assign",assignment:e}}function o(e,n){return"string"==typeof(e="string"==typeof e&&n&&n[e]?n[e]:e)?{type:e}:"function"==typeof e?{type:e.name,exec:e}:e}function a(e){return function(n){return e===n}}function u(e){return"string"==typeof e?{type:e}:e}function c(e,n){return{value:e,context:n,actions:[],changed:!1,matches:a(e)}}function f(e,n,_){var g=n,v=!1;return[e.filter((function(e){if("xstate.assign"===e.type){v=!0;var n=Object.assign({},g);return"function"==typeof e.assignment?n=e.assignment(g,_):Object.keys(e.assignment).forEach((function(v){n[v]="function"==typeof e.assignment[v]?e.assignment[v](g,_):e.assignment[v]})),g=n,!1}return!0})),g,v]}function s(e,_){void 0===_&&(_={});var g=t(f(r(e.states[e.initial].entry).map((function(e){return o(e,_.actions)})),e.context,n),2),v=g[0],h=g[1],S={config:e,_options:_,initialState:{value:e.initial,actions:v,context:h,matches:a(e.initial)},transition:function(n,_){var g,v,h="string"==typeof n?{value:n,context:e.context}:n,x=h.value,p=h.context,y=u(_),b=e.states[x];if(b.on){var d=r(b.on[y.type]);try{for(var m=function(e){var n="function"==typeof Symbol&&Symbol.iterator,_=n&&e[n],g=0;if(_)return _.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&g>=e.length&&(e=void 0),{value:e&&e[g++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}(d),T=m.next();!T.done;T=m.next()){var j=T.value;if(void 0===j)return c(x,p);var w="string"==typeof j?{target:j}:j,N=w.target,X=w.actions,E=void 0===X?[]:X,O=w.cond,R=void 0===O?function(){return!0}:O,C=void 0===N,I=null!=N?N:x,P=e.states[I];if(R(p,y)){var D=t(f((C?r(E):[].concat(b.exit,E,P.entry).filter((function(e){return e}))).map((function(e){return o(e,S._options.actions)})),p,y),3),k=D[0],z=D[1],A=D[2],G=null!=N?N:x;return{value:G,context:z,actions:k,changed:N!==x||k.length>0||A,matches:a(G)}}}}catch(e){g={error:e}}finally{try{T&&!T.done&&(v=m.return)&&v.call(m)}finally{if(g)throw g.error}}}return c(x,p)}};return S}var l=function(e,n){return e.actions.forEach((function(_){var g=_.exec;return g&&g(e.context,n)}))};function wrapXStateService(e){return{_xstateService:e,get status(){return this._xstateService?this._xstateService.status:_.INIT},get state(){return this._xstateService.state},subscribe(e){return this._xstateService.subscribe(e)},send(e){this._xstateService.send(e)},start(){this._xstateService.start()}}}const _=Object.freeze({INIT:-1,NOT_STARTED:0,RUNNING:1,STOPPED:2});function interpret(_,g){const v=wrapXStateService(function(_){var g=_.initialState,v=e.NotStarted,h=new Set,S={_machine:_,send:function(n){v===e.Running&&(g=_.transition(g,n),l(g,u(n)),h.forEach((function(e){return e(g)})))},subscribe:function(e){return h.add(e),e(g),{unsubscribe:function(){return h.delete(e)}}},start:function(h){if(h){var x="object"==typeof h?h:{context:_.config.context,value:h};g={value:x.value,actions:[],context:x.context,matches:a(x.value)}}else g=_.initialState;return v=e.Running,l(g,n),S},stop:function(){return v=e.Stopped,h.clear(),S},get state(){return g},get status(){return v}};return S}(_));return g?function(e,n){return e._getterCache={},e.getters=new Proxy(n,{get(n,_){if(!e._getterCache[_]){const g=n[_](e._xstateService.state.context);e._getterCache[_]=g}return e._getterCache[_]}}),e.subscribe((()=>{e._getterCache={}})),e}(v,g):v}function createXStateService(e,n,_){const g=interpret(n,_);return globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.xstate=globalThis.__ficusjs__.xstate||{},globalThis.__ficusjs__.xstate[e]=g,g}function addXStateService(e,n){return globalThis.__ficusjs__=globalThis.__ficusjs__||{},globalThis.__ficusjs__.xstate=globalThis.__ficusjs__.xstate||{},globalThis.__ficusjs__.xstate[e]=n,n}function getXStateService(e){if(globalThis.__ficusjs__&&globalThis.__ficusjs__.xstate&&globalThis.__ficusjs__.xstate[e])return globalThis.__ficusjs__.xstate[e]}export{_ as XStateServiceStatus,addXStateService,i as assign,s as createMachine,createXStateService,getXStateService,interpret,wrapXStateService};

0 commit comments

Comments
 (0)