Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -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
module.exports = DB;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"webpack": "^3.5.3"
},
"dependencies": {
"puppeteer": "^16.2.0"
"puppeteer": "^16.2.0",
"tapable": "^2.2.1"
}
}