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
40 changes: 36 additions & 4 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
const Tapable = require('tapable')
const { AsyncSeriesWaterfallHook } = require('tapable');

class DB extends Tapable {
constructor() {
class DB {
constructor(options) {
// TODO
this.options =
Object.prototype.toString.call(options) === '[object Object]'
? options
: {};
this.hooks = {
pluginHook: new AsyncSeriesWaterfallHook(['name']),
};
}

plugin(name, cb) {
const { pluginHook } = this.hooks;
pluginHook.tapPromise(name, (options) => {
return new Promise(async (resolve, reject) => {
const isJudge = name === 'judge';
const cbBack = cb(options);
if (cbBack instanceof Promise) cbBack.catch((err) => reject(err));
let res = await cbBack;
let isErr = false;
if (res === true && isJudge) isErr = true;
if (res === true || isJudge) res = options;
if (isErr) reject(res);
else resolve(res);
});
});
}

request() {
request(options) {
// TODO
return new Promise((resolve, reject) => {
const { pluginHook } = this.hooks;
const optionsAssigned = Object.assign({}, this.options, options);
pluginHook
.promise(optionsAssigned)
.then((res) => resolve(res))
.catch((err) => reject(err));
});
}
}

Expand Down
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"
}
}