1+ var __read = this && this . __read || function ( o , n ) {
2+ var m = typeof Symbol === "function" && o [ Symbol . iterator ] ;
3+ if ( ! m ) return o ;
4+ var i = m . call ( o ) ,
5+ r ,
6+ ar = [ ] ,
7+ e ;
8+ try {
9+ while ( ( n === void 0 || n -- > 0 ) && ! ( r = i . next ( ) ) . done ) ar . push ( r . value ) ;
10+ } catch ( error ) {
11+ e = { error : error } ;
12+ } finally {
13+ try {
14+ if ( r && ! r . done && ( m = i [ "return" ] ) ) m . call ( i ) ;
15+ } finally {
16+ if ( e ) throw e . error ;
17+ }
18+ }
19+ return ar ;
20+ } ;
21+ var __spread = this && this . __spread || function ( ) {
22+ for ( var ar = [ ] , i = 0 ; i < arguments . length ; i ++ ) ar = ar . concat ( __read ( arguments [ i ] ) ) ;
23+ return ar ;
24+ } ;
25+ import { EMPTY_ACTIVITY_MAP } from './constants' ;
26+ import { matchesState , keys } from './utils' ;
27+ export function stateValuesEqual ( a , b ) {
28+ if ( a === b ) {
29+ return true ;
30+ }
31+ var aKeys = keys ( a ) ;
32+ var bKeys = keys ( b ) ;
33+ return aKeys . length === bKeys . length && aKeys . every ( function ( key ) {
34+ return stateValuesEqual ( a [ key ] , b [ key ] ) ;
35+ } ) ;
36+ }
37+ var State = /** @class */ /*#__PURE__*/ function ( ) {
38+ /**
39+ * Creates a new State instance.
40+ * @param value The state value
41+ * @param context The extended state
42+ * @param historyValue The tree representing historical values of the state nodes
43+ * @param history The previous state
44+ * @param actions An array of action objects to execute as side-effects
45+ * @param activities A mapping of activities and whether they are started (`true`) or stopped (`false`).
46+ * @param meta
47+ * @param events Internal event queue. Should be empty with run-to-completion semantics.
48+ * @param tree
49+ */
50+ function State ( config ) {
51+ this . actions = [ ] ;
52+ this . activities = EMPTY_ACTIVITY_MAP ;
53+ this . meta = { } ;
54+ this . events = [ ] ;
55+ this . value = config . value ;
56+ this . context = config . context ;
57+ this . historyValue = config . historyValue ;
58+ this . history = config . history ;
59+ this . actions = config . actions || [ ] ;
60+ this . activities = config . activities || EMPTY_ACTIVITY_MAP ;
61+ this . meta = config . meta || { } ;
62+ this . events = config . events || [ ] ;
63+ Object . defineProperty ( this , 'tree' , {
64+ value : config . tree ,
65+ enumerable : false
66+ } ) ;
67+ }
68+ /**
69+ * Creates a new State instance for the given `stateValue` and `context`.
70+ * @param stateValue
71+ * @param context
72+ */
73+ State . from = function ( stateValue , context ) {
74+ if ( stateValue instanceof State ) {
75+ if ( stateValue . context !== context ) {
76+ return new State ( {
77+ value : stateValue . value ,
78+ context : context ,
79+ historyValue : stateValue . historyValue ,
80+ history : stateValue . history ,
81+ actions : [ ] ,
82+ activities : stateValue . activities ,
83+ meta : { } ,
84+ events : [ ] ,
85+ tree : stateValue . tree
86+ } ) ;
87+ }
88+ return stateValue ;
89+ }
90+ return new State ( {
91+ value : stateValue ,
92+ context : context ,
93+ historyValue : undefined ,
94+ history : undefined ,
95+ actions : [ ] ,
96+ activities : undefined ,
97+ meta : undefined ,
98+ events : [ ]
99+ } ) ;
100+ } ;
101+ /**
102+ * Creates a new State instance for the given `config`.
103+ * @param config The state config
104+ */
105+ State . create = function ( config ) {
106+ return new State ( config ) ;
107+ } ;
108+ /**
109+ * Creates a new State instance for the given `stateValue` and `context` with no actions (side-effects).
110+ * @param stateValue
111+ * @param context
112+ */
113+ State . inert = function ( stateValue , context ) {
114+ if ( stateValue instanceof State ) {
115+ if ( ! stateValue . actions . length ) {
116+ return stateValue ;
117+ }
118+ return new State ( {
119+ value : stateValue . value ,
120+ context : context ,
121+ historyValue : stateValue . historyValue ,
122+ history : stateValue . history ,
123+ activities : stateValue . activities ,
124+ tree : stateValue . tree
125+ } ) ;
126+ }
127+ return State . from ( stateValue , context ) ;
128+ } ;
129+ Object . defineProperty ( State . prototype , "nextEvents" , {
130+ /**
131+ * The next events that will cause a transition from the current state.
132+ */
133+ get : function ( ) {
134+ if ( ! this . tree ) {
135+ return [ ] ;
136+ }
137+ return this . tree . nextEvents ;
138+ } ,
139+ enumerable : true ,
140+ configurable : true
141+ } ) ;
142+ /**
143+ * Returns an array of all the string leaf state node paths.
144+ * @param stateValue
145+ * @param delimiter The character(s) that separate each subpath in the string state node path.
146+ */
147+ State . prototype . toStrings = function ( stateValue , delimiter ) {
148+ var _this = this ;
149+ if ( stateValue === void 0 ) {
150+ stateValue = this . value ;
151+ }
152+ if ( delimiter === void 0 ) {
153+ delimiter = '.' ;
154+ }
155+ if ( typeof stateValue === 'string' ) {
156+ return [ stateValue ] ;
157+ }
158+ var valueKeys = keys ( stateValue ) ;
159+ return valueKeys . concat . apply ( valueKeys , __spread ( valueKeys . map ( function ( key ) {
160+ return _this . toStrings ( stateValue [ key ] ) . map ( function ( s ) {
161+ return key + delimiter + s ;
162+ } ) ;
163+ } ) ) ) ;
164+ } ;
165+ /**
166+ * Whether the current state value is a subset of the given parent state value.
167+ * @param parentStateValue
168+ */
169+ State . prototype . matches = function ( parentStateValue ) {
170+ return matchesState ( parentStateValue , this . value ) ;
171+ } ;
172+ Object . defineProperty ( State . prototype , "changed" , {
173+ /**
174+ * Indicates whether the state has changed from the previous state. A state is considered "changed" if:
175+ *
176+ * - Its value is not equal to its previous value, or:
177+ * - It has any new actions (side-effects) to execute.
178+ *
179+ * An initial state (with no history) will return `undefined`.
180+ */
181+ get : function ( ) {
182+ if ( ! this . history ) {
183+ return undefined ;
184+ }
185+ return ! ! this . actions . length || ( typeof this . history . value !== typeof this . value ? true : typeof this . value === 'string' ? this . value !== this . history . value : stateValuesEqual ( this . value , this . history . value ) ) ;
186+ } ,
187+ enumerable : true ,
188+ configurable : true
189+ } ) ;
190+ return State ;
191+ } ( ) ;
192+ export { State } ;
0 commit comments