@@ -12,11 +12,7 @@ import {
12
12
addLink ,
13
13
checkToJsonOverride ,
14
14
} from "./lib/utils" ;
15
- import {
16
- type AbstractBehavior ,
17
- type Behavior ,
18
- BehaviorRunner ,
19
- } from "./lib/behavior" ;
15
+ import { type AbstractBehavior , BehaviorRunner } from "./lib/behavior" ;
20
16
import * as Lib from "./lib/utils" ;
21
17
22
18
import siteBehaviors from "./site" ;
@@ -63,18 +59,17 @@ type BehaviorClass =
63
59
| typeof AutoClick
64
60
| typeof AutoScroll
65
61
| typeof Autoplay
66
- | typeof AutoFetcher ;
62
+ | typeof AutoFetcher
63
+ | typeof BehaviorRunner < any , any > ;
67
64
68
65
type BehaviorInstance = InstanceType < BehaviorClass > ;
69
- type SiteSpecificBehaviorInstance = InstanceType <
70
- ( typeof siteBehaviors ) [ number ]
71
- > ;
72
66
73
67
export class BehaviorManager {
74
68
autofetch ?: AutoFetcher ;
75
69
behaviors : BehaviorInstance [ ] | null ;
76
- loadedBehaviors : { [ key in BehaviorClass [ "id" ] ] : BehaviorClass } ;
77
- mainBehavior : Behavior | BehaviorRunner < any , any > | null ;
70
+ loadedBehaviors : Record < string , BehaviorClass > ;
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ mainBehavior : BehaviorInstance | BehaviorRunner < any , any > | null ;
78
73
mainBehaviorClass ! : BehaviorClass ;
79
74
inited : boolean ;
80
75
started : boolean ;
@@ -84,12 +79,13 @@ export class BehaviorManager {
84
79
85
80
constructor ( ) {
86
81
this . behaviors = [ ] ;
87
- this . loadedBehaviors = siteBehaviors . reduce <
88
- Record < BehaviorClass [ "id" ] , BehaviorClass >
89
- > ( ( behaviors , next ) => {
90
- behaviors [ next . id ] = next ;
91
- return behaviors ;
92
- } , { } ) ;
82
+ this . loadedBehaviors = siteBehaviors . reduce < Record < string , BehaviorClass > > (
83
+ ( behaviors , next ) => {
84
+ behaviors [ next . id ] = next ;
85
+ return behaviors ;
86
+ } ,
87
+ { } ,
88
+ ) ;
93
89
this . mainBehavior = null ;
94
90
this . inited = false ;
95
91
this . started = false ;
@@ -181,11 +177,7 @@ export class BehaviorManager {
181
177
if ( opts ?. siteSpecific ) {
182
178
for ( const name in this . loadedBehaviors ) {
183
179
const siteBehaviorClass = this . loadedBehaviors [ name ] ;
184
- if (
185
- (
186
- siteBehaviorClass as unknown as SiteSpecificBehaviorInstance
187
- ) . isMatch ( )
188
- ) {
180
+ if ( "isMatch" in siteBehaviorClass && siteBehaviorClass . isMatch ( ) ) {
189
181
void behaviorLog ( "Using Site-Specific Behavior: " + name ) ;
190
182
this . mainBehaviorClass = siteBehaviorClass ;
191
183
const siteSpecificOpts =
@@ -209,14 +201,14 @@ export class BehaviorManager {
209
201
}
210
202
}
211
203
212
- if ( ! siteMatch && opts . autoscroll ) {
213
- behaviorLog ( "Using Autoscroll" ) ;
204
+ if ( ! siteMatch && opts ? .autoscroll ) {
205
+ void behaviorLog ( "Using Autoscroll" ) ;
214
206
this . mainBehaviorClass = AutoScroll ;
215
- this . mainBehavior = new AutoScroll ( this . autofetch ) ;
207
+ this . mainBehavior = new AutoScroll ( this . autofetch ! ) ;
216
208
}
217
209
218
210
if ( this . mainBehavior ) {
219
- this . behaviors . push ( this . mainBehavior ) ;
211
+ this . behaviors ! . push ( this . mainBehavior ) ;
220
212
221
213
if ( this . mainBehavior instanceof BehaviorRunner ) {
222
214
return this . mainBehavior . behaviorProps . id ;
@@ -226,9 +218,16 @@ export class BehaviorManager {
226
218
return "" ;
227
219
}
228
220
229
- load ( behaviorClass ) {
230
- if ( typeof behaviorClass . id !== "string" ) {
231
- behaviorLog (
221
+ load ( behaviorClass : unknown ) {
222
+ if ( typeof behaviorClass !== "function" ) {
223
+ void behaviorLog (
224
+ `Must pass a class object, got ${ behaviorClass } ` ,
225
+ "error" ,
226
+ ) ;
227
+ return ;
228
+ }
229
+ if ( ! ( "id" in behaviorClass ) || typeof behaviorClass . id !== "string" ) {
230
+ void behaviorLog (
232
231
'Behavior class must have a string string "id" property' ,
233
232
"error" ,
234
233
) ;
@@ -237,37 +236,34 @@ export class BehaviorManager {
237
236
238
237
const name = behaviorClass . id ;
239
238
240
- if ( typeof behaviorClass !== "function" ) {
241
- behaviorLog ( `Must pass a class object, got ${ behaviorClass } ` , "error" ) ;
242
- return ;
243
- }
244
-
245
239
if (
240
+ ! ( "isMatch" in behaviorClass ) ||
246
241
typeof behaviorClass . isMatch !== "function" ||
242
+ ! ( "init" in behaviorClass ) ||
247
243
typeof behaviorClass . init !== "function"
248
244
) {
249
- behaviorLog (
245
+ void behaviorLog (
250
246
"Behavior class must have an is `isMatch()` and `init()` static methods" ,
251
247
"error" ,
252
248
) ;
253
249
return ;
254
250
}
255
251
256
252
if ( ! this . isInTopFrame ( ) ) {
257
- if ( ! behaviorClass . runInIframe ) {
258
- behaviorLog (
253
+ if ( ! ( "runInIframe" in behaviorClass ) || ! behaviorClass . runInIframe ) {
254
+ void behaviorLog (
259
255
`Behavior class ${ name } : not running in iframes (.runInIframe not set)` ,
260
256
"debug" ,
261
257
) ;
262
258
return ;
263
259
}
264
260
}
265
261
266
- behaviorLog ( `Behavior class ${ name } : loaded` , "debug" ) ;
267
- this . loadedBehaviors [ name ] = behaviorClass ;
262
+ void behaviorLog ( `Behavior class ${ name } : loaded` , "debug" ) ;
263
+ this . loadedBehaviors [ name ] = behaviorClass as BehaviorClass ;
268
264
}
269
265
270
- async resolve ( target ) {
266
+ async resolve ( target : string ) {
271
267
const imported = await import ( `${ target } ` ) ; // avoid Webpack warning
272
268
if ( Array . isArray ( imported ) ) {
273
269
for ( const behavior of imported ) {
@@ -280,11 +276,16 @@ export class BehaviorManager {
280
276
281
277
async awaitPageLoad ( ) {
282
278
this . selectMainBehavior ( ) ;
283
- if ( this . mainBehavior ?. awaitPageLoad ) {
284
- behaviorLog ( "Waiting for custom page load via behavior" ) ;
279
+ if (
280
+ this . mainBehavior &&
281
+ "awaitPageLoad" in this . mainBehavior &&
282
+ ( this . mainBehavior as AbstractBehavior < any , any > ) . awaitPageLoad
283
+ ) {
284
+ void behaviorLog ( "Waiting for custom page load via behavior" ) ;
285
+ // @ts -expect-error TODO why isn't `log` passed in here? It seems like functions expect it to be
285
286
await this . mainBehavior . awaitPageLoad ( { Lib } ) ;
286
287
} else {
287
- behaviorLog ( "No custom wait behavior" ) ;
288
+ void behaviorLog ( "No custom wait behavior" ) ;
288
289
}
289
290
}
290
291
@@ -303,7 +304,7 @@ export class BehaviorManager {
303
304
304
305
await awaitLoad ( ) ;
305
306
306
- this . behaviors . forEach ( ( x ) => {
307
+ this . behaviors ! . forEach ( ( x ) => {
307
308
const id = x . id || x . constructor . id || "(Unnamed)" ;
308
309
behaviorLog ( "Starting behavior: " + id , "debug" ) ;
309
310
x . start ( ) ;
@@ -318,7 +319,7 @@ export class BehaviorManager {
318
319
) ;
319
320
320
321
if ( this . timeout ) {
321
- behaviorLog (
322
+ void behaviorLog (
322
323
`Waiting for behaviors to finish or ${ this . timeout } ms timeout` ,
323
324
"debug" ,
324
325
) ;
0 commit comments