@@ -20,12 +20,11 @@ import { tracked } from '@glimmer/tracking';
20
20
import { DEBUG } from '@glimmer/env' ;
21
21
import { later , cancel } from '@ember/runloop' ;
22
22
import { getOwner , setOwner } from '@ember/application' ;
23
- import { warn } from '@ember/debug' ;
23
+ import { assert , warn } from '@ember/debug' ;
24
24
import { action } from '@ember/object' ;
25
25
26
26
import { use , Resource } from 'ember-could-get-used-to-this' ;
27
27
28
-
29
28
export const ARGS_STATE_CHANGE_WARNING =
30
29
'A change to passed `args` or a local state change triggered an update to a `useMachine`-usable. You can send a dedicated event to the machine or restart it so this is handled. This is done via the `.update`-hook of the `useMachine`-usable.' ;
31
30
@@ -58,25 +57,40 @@ export type UsableStatechart<
58
57
| MachineConfig < TContext , TStateSchema , TEvent >
59
58
| StateMachine < TContext , TStateSchema , TEvent , TTypestate > ;
60
59
60
+ type Args <
61
+ TContext ,
62
+ TStateSchema ,
63
+ TEvent extends EventObject ,
64
+ TTypestate extends Typestate < TContext >
65
+ > = {
66
+ machine : UsableStatechart < TContext , TStateSchema , TEvent , TTypestate > ;
67
+ interpreterOptions : Partial < InterpreterOptions > ;
68
+ onTransition ?: StateListener < TContext , TEvent , TStateSchema , TTypestate > ;
69
+ } ;
70
+
61
71
export class Statechart <
62
72
TContext ,
63
73
TStateSchema extends StateSchema ,
64
74
TEvent extends EventObject ,
65
75
TTypestate extends Typestate < TContext >
66
- > extends Resource {
67
- @tracked service ! : Interpreter < TContext , TStateSchema , TEvent , TTypestate > ;
68
- @tracked _state ! : State < TContext , TEvent , TStateSchema , TTypestate > ;
76
+ > extends Resource < Args < TContext , TStateSchema , TEvent , TTypestate > > {
77
+ @tracked service ?: Interpreter < TContext , TStateSchema , TEvent , TTypestate > = undefined ;
78
+ // current state of the machine,
79
+ // set onTransition, which is configured during setup
80
+ @tracked _state ?: State < TContext , TEvent , TStateSchema , TTypestate > = undefined ;
69
81
70
82
machine : StateMachine < TContext , TStateSchema , TEvent , TTypestate > ;
71
83
interpreterOptions : Partial < InterpreterOptions > ;
72
- _onTransition : StateListener < TContext , TEvent , TStateSchema , TTypestate > | undefined = undefined ;
73
84
74
- constructor (
75
- machine : UsableStatechart < TContext , TStateSchema , TEvent , TTypestate > ,
76
- interpreterOptions : Partial < InterpreterOptions > ,
77
- onTransition ?: StateListener < TContext , TEvent , TStateSchema , TTypestate >
78
- ) {
79
- super ( ...arguments ) ;
85
+ declare _onTransition : StateListener < TContext , TEvent , TStateSchema , TTypestate > | undefined ;
86
+ declare _config : Partial < MachineOptions < TContext , TEvent > > ;
87
+ declare _context : TContext ;
88
+
89
+ constructor ( owner : unknown , args : Args < TContext , TStateSchema , TEvent , TTypestate > ) {
90
+ super ( owner , args ) ;
91
+
92
+ let { machine } = args ;
93
+ const { interpreterOptions, onTransition } = args ;
80
94
81
95
machine = machine instanceof StateNode ? machine : createMachine ( machine ) ;
82
96
@@ -85,26 +99,28 @@ export class Statechart<
85
99
this . _onTransition = onTransition ;
86
100
}
87
101
88
- get state ( ) : {
89
- state : State < TContext , TEvent , TStateSchema , TTypestate > ;
90
- send : Send < TContext , TStateSchema , TEvent , TTypestate > ;
91
- service : Interpreter < TContext , TStateSchema , TEvent , TTypestate > ;
92
- } {
102
+ get state ( ) {
103
+ if ( ! this . service ) {
104
+ this . setup ( ) ;
105
+ }
106
+
107
+ assert ( `Machine setup failed` , this . service ) ;
108
+
93
109
return {
94
110
state : this . _state ,
95
111
send : this . service . send ,
96
112
service : this . service ,
97
113
} ;
98
114
}
99
115
116
+ // used when this Resource is used as a helper
100
117
get value ( ) {
101
- // TODO: lazily call setup, as we don't know if the call site has .withContext or .withConfig
102
- return "foo" ;
118
+ return this . state ;
103
119
}
104
120
105
121
@action
106
- send ( ) {
107
- this . service . send ( ...arguments ) ;
122
+ send ( ... args : Parameters < Interpreter < TContext , TStateSchema , TEvent , TTypestate > [ 'send' ] > ) {
123
+ this . state . service . send ( ...args ) ;
108
124
}
109
125
110
126
@action
@@ -113,7 +129,6 @@ export class Statechart<
113
129
return this ;
114
130
}
115
131
116
-
117
132
@action
118
133
withContext ( context : TContext ) {
119
134
this . _context = context ;
@@ -128,7 +143,6 @@ export class Statechart<
128
143
return this ;
129
144
}
130
145
131
-
132
146
setup (
133
147
setupOptions : {
134
148
initialState ?: State < TContext , TEvent , TStateSchema , TTypestate > | StateValue ;
@@ -159,6 +173,8 @@ export class Statechart<
159
173
}
160
174
161
175
teardown ( ) : void {
176
+ if ( ! this . service ) return ;
177
+
162
178
this . service . stop ( ) ;
163
179
}
164
180
}
0 commit comments