Skip to content

Commit 86bae1b

Browse files
committed
replace state in plugin events with this
1 parent 5c748ad commit 86bae1b

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

plugins/plugin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import {TFullApiPost, TPost, TSearchResult, TSinglePost} from "..";
55
export type DType = TPost | TSinglePost | TFullApiPost | TSearchResult;
66

77
export interface IPlugin {
8-
constructionEvent?(state: instamancer.Instagram<DType>): void;
8+
constructionEvent?(this: instamancer.Instagram<DType>): void;
99

1010
requestEvent?(
11+
this: instamancer.Instagram<DType>,
1112
req: puppeteer.Request,
1213
overrides: puppeteer.Overrides,
13-
state: instamancer.Instagram<DType>,
1414
): void;
1515

1616
responseEvent?(
17+
this: instamancer.Instagram<DType>,
1718
res: puppeteer.Response,
1819
data: DType,
19-
state: instamancer.Instagram<DType>,
2020
): void;
2121

22-
postPageEvent?(data: DType, state: instamancer.Instagram<DType>): void;
22+
postPageEvent?(this: instamancer.Instagram<DType>, data: DType): void;
2323

24-
graftingEvent?(state: instamancer.Instagram<DType>): void;
24+
graftingEvent?(this: instamancer.Instagram<DType>): void;
2525
}

plugins/plugins/largeFirst.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@ import {Instagram} from "../../src/api/instagram";
55
import {DType, IPlugin} from "../plugin";
66

77
export class LargeFirst implements IPlugin {
8-
public constructionEvent(state: Instagram<DType>): void {
9-
state.jumpSize = 150;
8+
public constructionEvent(this: Instagram<DType>): void {
9+
this.jumpSize = 150;
1010
}
1111

12-
public requestEvent(
13-
req: Request,
14-
overrides: Overrides,
15-
state: Instagram<DType>,
16-
): void {
12+
public requestEvent(req: Request, overrides: Overrides): void {
1713
const url = overrides["url"] ? overrides["url"] : req.url();
1814
const parsedUrl = urlParse(url);
1915
const query = querystring.parse(parsedUrl.query);

src/api/instagram.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
Response,
1717
} from "puppeteer";
1818
import * as winston from "winston";
19-
import {IPlugin} from "../../plugins/plugin";
19+
import {IPlugin} from "../../plugins";
2020
import {IOptions} from "./api";
2121
import {PostIdSet} from "./postIdSet";
2222

@@ -192,7 +192,7 @@ export class Instagram<PostType> extends EventEmitter {
192192
this.validator = options.validator || validator;
193193

194194
this.addPlugins(options.plugins);
195-
this.emit("construction", this);
195+
this.emit("construction");
196196
}
197197

198198
/**
@@ -348,7 +348,7 @@ export class Instagram<PostType> extends EventEmitter {
348348
headers: this.graftHeaders,
349349
url: this.graftURL,
350350
};
351-
this.emit("request", req, overrides, this);
351+
this.emit("request", req, overrides);
352352
await req.continue(overrides);
353353

354354
// Reset grafting data
@@ -362,7 +362,7 @@ export class Instagram<PostType> extends EventEmitter {
362362
break;
363363
} else {
364364
const overrides = {};
365-
this.emit("request", req, overrides, this);
365+
this.emit("request", req, overrides);
366366
await req.continue(overrides);
367367
}
368368
}
@@ -400,7 +400,7 @@ export class Instagram<PostType> extends EventEmitter {
400400
}
401401

402402
// Emit event
403-
this.emit("response", res, data, this);
403+
this.emit("response", res, data);
404404

405405
// Check for rate limiting
406406
if (data && "status" in data && data["status"] === "fail") {
@@ -516,7 +516,7 @@ export class Instagram<PostType> extends EventEmitter {
516516
if (!parsed) {
517517
return;
518518
}
519-
this.emit("postPage", parsed, this);
519+
this.emit("postPage", parsed);
520520
await this.addToPostBuffer(parsed);
521521
}
522522

@@ -821,7 +821,7 @@ export class Instagram<PostType> extends EventEmitter {
821821

822822
await this.progress(Progress.GRAFTING);
823823

824-
this.emit("grafting", this);
824+
this.emit("grafting");
825825

826826
// Enable grafting
827827
this.graft = true;

tests/test.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,12 @@ describe("Search", () => {
533533
const searchRequestsSpy = jest.fn();
534534
class RequestCounter implements IPlugin {
535535
public requestEvent(
536+
this: Instagram<DType>,
536537
req: Request,
537538
overrides: Overrides,
538-
state: Instagram<DType>,
539539
): void {
540540
// @ts-ignore
541-
if (state.matchURL(req.url())) {
541+
if (this.matchURL(req.url())) {
542542
searchRequestsSpy();
543543
}
544544
}

0 commit comments

Comments
 (0)