@@ -17,7 +17,10 @@ export class BackgroundBehavior {
17
17
}
18
18
19
19
// ===========================================================================
20
- export class Behavior extends BackgroundBehavior {
20
+ export class Behavior < State , Opts = EmptyObject >
21
+ extends BackgroundBehavior
22
+ implements AbstractBehavior < State , Opts >
23
+ {
21
24
_running : Promise < void > | null ;
22
25
paused : any ;
23
26
_unpause : any ;
@@ -105,51 +108,66 @@ export class Behavior extends BackgroundBehavior {
105
108
}
106
109
}
107
110
108
- async * [ Symbol . asyncIterator ] ( ) {
111
+ async * [ Symbol . asyncIterator ] ( ) : AsyncGenerator <
112
+ State | undefined ,
113
+ void ,
114
+ void
115
+ > {
109
116
yield ;
110
117
}
111
118
}
112
119
113
120
// WIP: BehaviorRunner class allows for arbitrary behaviors outside of the
114
121
// library to be run through the BehaviorManager
115
122
116
- export type Context < State , Opts > = {
123
+ export type EmptyObject = Record < string , never > ;
124
+
125
+ export type Context < State , Opts = EmptyObject > = {
117
126
Lib : typeof Lib ;
118
127
state : State ;
119
128
opts : Opts ;
120
129
log : ( data : any , type ?: string ) => Promise < void > ;
121
130
} ;
122
131
123
- abstract class AbstractBehaviorInst < State , Opts , RunResult > {
124
- abstract run : ( ctx : Context < State , Opts > ) => AsyncIterable < RunResult | void > ;
132
+ export abstract class AbstractBehavior < State , Opts = EmptyObject > {
133
+ static id : String ;
134
+ static isMatch : ( ) => boolean ;
135
+ static init : ( ) => any ;
136
+
137
+ abstract run : ( ctx : Context < State , Opts > ) => AsyncIterable < any > ;
125
138
126
139
abstract awaitPageLoad ?: ( ctx : Context < State , Opts > ) => Promise < void > ;
127
140
}
128
141
129
- interface StaticAbstractBehavior {
130
- id : string ;
131
- isMatch : ( ) => boolean ;
132
- init : ( ) => any ;
133
- }
142
+ type StaticProps < T > = {
143
+ [ K in keyof T ] : T [ K ] ;
144
+ } ;
134
145
135
- export type AbstractBehavior < State , Opts , RunResult > =
136
- ( new ( ) => AbstractBehaviorInst < State , Opts , RunResult > ) &
137
- StaticAbstractBehavior ;
146
+ type StaticBehaviorProps = StaticProps < typeof AbstractBehavior > ;
138
147
139
- export class BehaviorRunner < State , Opts , RunResult > extends BackgroundBehavior {
140
- inst : AbstractBehaviorInst < State , Opts , RunResult > ;
141
- behaviorProps : StaticAbstractBehavior ;
148
+ // Non-abstract constructor type
149
+ type ConcreteBehaviorConstructor < State , Opts > = StaticBehaviorProps & {
150
+ new ( ) : AbstractBehavior < State , Opts > ;
151
+ } ;
152
+
153
+ export class BehaviorRunner <
154
+ State ,
155
+ Opts = EmptyObject ,
156
+ > extends BackgroundBehavior {
157
+ inst : AbstractBehavior < State , Opts > ;
158
+ behaviorProps : ConcreteBehaviorConstructor < State , Opts > ;
142
159
ctx : Context < State , Opts > ;
143
- _running : Promise < void > | null ;
160
+ _running : any ;
144
161
paused : any ;
145
- _unpause : ( ( value ?: unknown ) => void ) | null ;
162
+ _unpause : any ;
146
163
147
164
get id ( ) {
148
- return ( this . inst . constructor as any ) . id ;
165
+ return ( this . inst . constructor as ConcreteBehaviorConstructor < State , Opts > )
166
+ . id ;
149
167
}
150
168
151
169
constructor (
152
- behavior : AbstractBehavior < State , Opts , RunResult > ,
170
+ behavior : ConcreteBehaviorConstructor < State , Opts > ,
153
171
mainOpts = { } ,
154
172
) {
155
173
super ( ) ;
@@ -167,7 +185,7 @@ export class BehaviorRunner<State, Opts, RunResult> extends BackgroundBehavior {
167
185
state = state || { } ;
168
186
opts = opts ? { ...opts , ...mainOpts } : mainOpts ;
169
187
// eslint-disable-next-line @typescript-eslint/no-explicit-any
170
- const log = async ( data : any , type : string ) => this . wrappedLog ( data , type ) ;
188
+ const log = async ( data : any , type ? : string ) => this . wrappedLog ( data , type ) ;
171
189
172
190
this . ctx = { Lib, state, opts, log } ;
173
191
@@ -194,7 +212,7 @@ export class BehaviorRunner<State, Opts, RunResult> extends BackgroundBehavior {
194
212
this . _running = this . run ( ) ;
195
213
}
196
214
197
- async done ( ) {
215
+ done ( ) {
198
216
return this . _running ? this . _running : Promise . resolve ( ) ;
199
217
}
200
218
0 commit comments