code optimize

This commit is contained in:
2020-01-16 09:59:19 +08:00
parent d82010686d
commit c78f593c70
8 changed files with 20 additions and 14 deletions

View File

@ -1,6 +1,6 @@
import { Actions, Request } from "../common"; import { Actions, Request } from "../common";
import { sendMessage } from "./messaging"; import { sendMessage } from "./messaging";
import { logger } from "./common"; import { logger } from "./logger";
/** /**
* redirect tab to url. * redirect tab to url.

View File

@ -1,4 +1,4 @@
import { logger } from "./common"; import { logger } from "./logger";
import { Actions } from "../common"; import { Actions } from "../common";
import { messageSubscribers } from "./messaging"; import { messageSubscribers } from "./messaging";
@ -27,3 +27,5 @@ export class Caches {
}, 30000); }, 30000);
} }
} }
export const caches = new Caches();

View File

@ -1,6 +0,0 @@
import { Logger, LOGGER_LEVEL } from "./logger";
import { Caches } from "./caches";
export const caches = new Caches();
export const logger = new Logger(LOGGER_LEVEL.DEBUG, LOGGER_LEVEL.DISABLED);
export const URL_REG = /^\s*(https?):\/\//im;

View File

@ -1,8 +1,9 @@
import { Task } from "./task"; import { Task } from "./task";
import { saveFile } from "./tools"; import { saveFile } from "./tools";
import { createTab, getActiveTab, ping } from "./actions"; import { createTab, getActiveTab, ping } from "./actions";
import { logger, caches } from "./common";
import { ExtractResult } from "./result"; import { ExtractResult } from "./result";
import { logger } from "./logger";
import { caches } from "./caches";
export class Extractor { export class Extractor {
private _tasks: Task[] = []; private _tasks: Task[] = [];

View File

@ -71,3 +71,5 @@ export class Logger {
); );
} }
} }
export const logger = new Logger(LOGGER_LEVEL.DEBUG, LOGGER_LEVEL.DISABLED);

View File

@ -1,6 +1,6 @@
import { Request, Actions } from "../common"; import { Request, Actions } from "../common";
import { getTabByID } from "./actions"; import { getTabByID } from "./actions";
import { logger } from "./common"; import { logger } from "./logger";
/** /**
* Sending a message to target tab repeatedly until the response is not undefined. * Sending a message to target tab repeatedly until the response is not undefined.
@ -49,7 +49,13 @@ export function sendMessage<T>(
let result: T = r; let result: T = r;
if (dataChecker) { if (dataChecker) {
let pms = dataChecker(r, err, count); let pms: T | Promise<T>;
try {
pms = dataChecker(r, err, count);
} catch (error) {
reject(error);
return;
}
// don't catch if it's not a Promise // don't catch if it's not a Promise
if (pms instanceof Promise) { if (pms instanceof Promise) {
let checkerError: any; let checkerError: any;

View File

@ -4,7 +4,7 @@ import { testArgs, signitures } from "./signiture";
import { ExtractResult } from "./result"; import { ExtractResult } from "./result";
import { messageSubscribers, ActionSubscriber } from "./messaging"; import { messageSubscribers, ActionSubscriber } from "./messaging";
import { Actions } from "../common"; import { Actions } from "../common";
import { logger } from "./common"; import { logger } from "./logger";
export class Task { export class Task {
private _data: { [key: string]: string[][] } = {}; private _data: { [key: string]: string[][] } = {};

View File

@ -1,6 +1,7 @@
import { URL_REG } from "./common";
import { ExtractResult } from "./result"; import { ExtractResult } from "./result";
const URL_REG = /^\s*(https?):\/\//im;
export function parseUrls(...args) { export function parseUrls(...args) {
if (!args.length) return []; if (!args.length) return [];
let arg = args.shift(); let arg = args.shift();