Skip to content

Commit

Permalink
feat: plugins 'onCreate' can only be init once
Browse files Browse the repository at this point in the history
  • Loading branch information
wqcstrong committed Jun 15, 2023
1 parent c936ca7 commit 06d31ba
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/plugins/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export default class ConsolePlugin implements PageSpyPlugin {

private console: Record<string, any> = {};

public onCreated() {
public static hasInitd = false;

// eslint-disable-next-line class-methods-use-this
public async onCreated() {
if (ConsolePlugin.hasInitd) return;
ConsolePlugin.hasInitd = true;

const type: SpyConsole.ProxyType[] = ['log', 'info', 'error', 'warn'];
type.forEach((item) => {
this.console[item] = window.console[item];
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import type PageSpyPlugin from './index';
export default class ErrorPlugin implements PageSpyPlugin {
public name = 'ErrorPlugin';

public static hasInitd = false;

public onCreated() {
if (ErrorPlugin.hasInitd) return;
ErrorPlugin.hasInitd = true;

this.onUncaughtError();
this.onResourceLoadError();
this.onUnhandledRejectionError();
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export default class NetworkPlugin implements PageSpyPlugin {

public beaconProxy: BeaconProxy | null = null;

public static hasInitd = false;

public onCreated() {
if (NetworkPlugin.hasInitd) return;
NetworkPlugin.hasInitd = true;

this.xhrProxy = new XhrProxy();
this.fetchProxy = new FetchProxy();
this.beaconProxy = new BeaconProxy();
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { makeMessage, DEBUG_MESSAGE_TYPE } from 'src/utils/message';
export default class PagePlugin implements PageSpyPlugin {
public name = 'PagePlugin';

public static hasInitd = false;

// eslint-disable-next-line class-methods-use-this
public onCreated() {
if (PagePlugin.hasInitd) return;
PagePlugin.hasInitd = true;

window.addEventListener('load', () => {
const msg = PagePlugin.collectHtml();
SocketStore.broadcastMessage(msg);
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import socketStore from '../utils/socket';
export class StoragePlugin implements PageSpyPlugin {
public name = 'StoragePlugin';

public static hasInitd = false;

// eslint-disable-next-line class-methods-use-this
public onCreated() {
if (StoragePlugin.hasInitd) return;
StoragePlugin.hasInitd = true;

const { sendStorageItem, initStorageProxy } = StoragePlugin;
const local = { ...localStorage };
Object.keys(local).forEach((name) => {
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ window.Modernizr.addTest(
);

export default class SystemPlugin implements PageSpyPlugin {
public name: string;
public name = 'SystemPlugin';

public constructor() {
this.name = 'SystemPlugin';
}
public static hasInitd = false;

// eslint-disable-next-line class-methods-use-this
public async onCreated() {
if (SystemPlugin.hasInitd) return;
SystemPlugin.hasInitd = true;

const id = getRandomId();
const features = await computeResult();
socketStore.broadcastMessage(
Expand Down

0 comments on commit 06d31ba

Please sign in to comment.