Skip to content

Commit 1d6e853

Browse files
committed
wip
1 parent 684d40a commit 1d6e853

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

src/index.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import {
1212
addLink,
1313
checkToJsonOverride,
1414
} 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";
2016
import * as Lib from "./lib/utils";
2117

2218
import siteBehaviors from "./site";
@@ -63,18 +59,17 @@ type BehaviorClass =
6359
| typeof AutoClick
6460
| typeof AutoScroll
6561
| typeof Autoplay
66-
| typeof AutoFetcher;
62+
| typeof AutoFetcher
63+
| typeof BehaviorRunner<any, any>;
6764

6865
type BehaviorInstance = InstanceType<BehaviorClass>;
69-
type SiteSpecificBehaviorInstance = InstanceType<
70-
(typeof siteBehaviors)[number]
71-
>;
7266

7367
export class BehaviorManager {
7468
autofetch?: AutoFetcher;
7569
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;
7873
mainBehaviorClass!: BehaviorClass;
7974
inited: boolean;
8075
started: boolean;
@@ -84,12 +79,13 @@ export class BehaviorManager {
8479

8580
constructor() {
8681
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+
);
9389
this.mainBehavior = null;
9490
this.inited = false;
9591
this.started = false;
@@ -181,11 +177,7 @@ export class BehaviorManager {
181177
if (opts?.siteSpecific) {
182178
for (const name in this.loadedBehaviors) {
183179
const siteBehaviorClass = this.loadedBehaviors[name];
184-
if (
185-
(
186-
siteBehaviorClass as unknown as SiteSpecificBehaviorInstance
187-
).isMatch()
188-
) {
180+
if ("isMatch" in siteBehaviorClass && siteBehaviorClass.isMatch()) {
189181
void behaviorLog("Using Site-Specific Behavior: " + name);
190182
this.mainBehaviorClass = siteBehaviorClass;
191183
const siteSpecificOpts =
@@ -209,14 +201,14 @@ export class BehaviorManager {
209201
}
210202
}
211203

212-
if (!siteMatch && opts.autoscroll) {
213-
behaviorLog("Using Autoscroll");
204+
if (!siteMatch && opts?.autoscroll) {
205+
void behaviorLog("Using Autoscroll");
214206
this.mainBehaviorClass = AutoScroll;
215-
this.mainBehavior = new AutoScroll(this.autofetch);
207+
this.mainBehavior = new AutoScroll(this.autofetch!);
216208
}
217209

218210
if (this.mainBehavior) {
219-
this.behaviors.push(this.mainBehavior);
211+
this.behaviors!.push(this.mainBehavior);
220212

221213
if (this.mainBehavior instanceof BehaviorRunner) {
222214
return this.mainBehavior.behaviorProps.id;
@@ -226,9 +218,16 @@ export class BehaviorManager {
226218
return "";
227219
}
228220

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(
232231
'Behavior class must have a string string "id" property',
233232
"error",
234233
);
@@ -237,37 +236,34 @@ export class BehaviorManager {
237236

238237
const name = behaviorClass.id;
239238

240-
if (typeof behaviorClass !== "function") {
241-
behaviorLog(`Must pass a class object, got ${behaviorClass}`, "error");
242-
return;
243-
}
244-
245239
if (
240+
!("isMatch" in behaviorClass) ||
246241
typeof behaviorClass.isMatch !== "function" ||
242+
!("init" in behaviorClass) ||
247243
typeof behaviorClass.init !== "function"
248244
) {
249-
behaviorLog(
245+
void behaviorLog(
250246
"Behavior class must have an is `isMatch()` and `init()` static methods",
251247
"error",
252248
);
253249
return;
254250
}
255251

256252
if (!this.isInTopFrame()) {
257-
if (!behaviorClass.runInIframe) {
258-
behaviorLog(
253+
if (!("runInIframe" in behaviorClass) || !behaviorClass.runInIframe) {
254+
void behaviorLog(
259255
`Behavior class ${name}: not running in iframes (.runInIframe not set)`,
260256
"debug",
261257
);
262258
return;
263259
}
264260
}
265261

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;
268264
}
269265

270-
async resolve(target) {
266+
async resolve(target: string) {
271267
const imported = await import(`${target}`); // avoid Webpack warning
272268
if (Array.isArray(imported)) {
273269
for (const behavior of imported) {
@@ -280,11 +276,16 @@ export class BehaviorManager {
280276

281277
async awaitPageLoad() {
282278
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
285286
await this.mainBehavior.awaitPageLoad({ Lib });
286287
} else {
287-
behaviorLog("No custom wait behavior");
288+
void behaviorLog("No custom wait behavior");
288289
}
289290
}
290291

@@ -303,7 +304,7 @@ export class BehaviorManager {
303304

304305
await awaitLoad();
305306

306-
this.behaviors.forEach((x) => {
307+
this.behaviors!.forEach((x) => {
307308
const id = x.id || x.constructor.id || "(Unnamed)";
308309
behaviorLog("Starting behavior: " + id, "debug");
309310
x.start();
@@ -318,7 +319,7 @@ export class BehaviorManager {
318319
);
319320

320321
if (this.timeout) {
321-
behaviorLog(
322+
void behaviorLog(
322323
`Waiting for behaviors to finish or ${this.timeout}ms timeout`,
323324
"debug",
324325
);

src/lib/behavior.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ type ConcreteBehaviorConstructor<State, Opts> = StaticBehaviorProps & {
147147
new (): AbstractBehavior<State, Opts>;
148148
};
149149

150-
export class BehaviorRunner<
151-
State,
152-
Opts = EmptyObject,
153-
> extends BackgroundBehavior {
150+
export class BehaviorRunner<State, Opts>
151+
extends BackgroundBehavior
152+
implements AbstractBehavior<State, Opts>
153+
{
154154
inst: AbstractBehavior<State, Opts>;
155155
behaviorProps: ConcreteBehaviorConstructor<State, Opts>;
156156
ctx: Context<State, Opts>;

0 commit comments

Comments
 (0)