diff --git a/lib/db.js b/lib/db.js index a1fe32c..c1a4275 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,13 +1,44 @@ -const Tapable = require('tapable') +const { + AsyncParallelBailHook, + SyncHook, + SyncWaterfallHook, +} = require("tapable"); -class DB extends Tapable { - constructor() { - // TODO +class DB { + constructor(options = {}) { + this.options = options; + this.hooks = { + endpointHook: new AsyncParallelBailHook(["options"]), + optionHook: new SyncHook(["options"]), + judgeHook: new SyncWaterfallHook(["res"]), + }; } - request() { - // TODO + plugin(name, cb) { + if (name === "endpoint") { + this.hooks.endpointHook.tapPromise( + name, + async (options) => await cb(options) + ); + } else if (name === "options") { + this.hooks.optionHook.tap(name, cb); + } else if (name === "judge") { + this.hooks.judgeHook.tap(name, cb); + } + } + + async request(options = {}) { + this.options = Object.assign({}, this.options, options); + + this.hooks.optionHook.call(this.options); + const result = await this.hooks.endpointHook.promise(this.options); + + if (this.hooks.judgeHook.call(result) === true || result === undefined) { + return Promise.reject(result); + } + + return result; } } -module.exports = DB \ No newline at end of file +module.exports = DB; diff --git a/package.json b/package.json index aad5780..c0e6d62 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "webpack": "^3.5.3" }, "dependencies": { - "puppeteer": "^16.2.0" + "puppeteer": "^16.2.0", + "tapable": "^2.2.1" } }