diff --git a/lib/db.js b/lib/db.js index a1fe32c..9e5c1b2 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,13 +1,54 @@ -const Tapable = require('tapable') +const {AsyncSeriesWaterfallHook, SyncWaterfallHook, SyncHook} = require('tapable') -class DB extends Tapable { - constructor() { +class DB { + constructor(options) { // TODO + this.options = options || {}; + this.hooks = { + endpointHooks: new AsyncSeriesWaterfallHook(['options']), + optionHooks: new SyncWaterfallHook(['options']), + judgeHooks: new SyncWaterfallHook(['resValue']) + } + + this.plugin = function(pluginName, fn) { + if(pluginName === 'endpoint') { + this.hooks.endpointHooks.tapPromise(pluginName, fn) + } else if(pluginName === 'options') { + this.hooks.optionHooks.tap(pluginName, fn) + } else if(pluginName === 'judge') { + this.hooks.judgeHooks.tap(pluginName, fn) + } + } } - request() { - // TODO + request(options) { + this.addOptions(options) + return this.hooks.endpointHooks.promise(this.options).then(res=>{ + const isJudge = this.judge(res); + if(!isJudge) { + return res + } + return Promise.reject(res); + }) + } + + addOptions(options) { + const totalOptions = this.options + if(options) { + for(let key in options) { + totalOptions[key] = options[key] + } + } + this.options = this.hooks.optionHooks.call(totalOptions) } -} -module.exports = DB \ No newline at end of file + judge(res) { + let isJudged = this.hooks.judgeHooks.call(res); + if(isJudged !== true) { + isJudged = false + } + return isJudged; + } + +} +module.exports = DB; \ No newline at end of file diff --git a/package.json b/package.json index aad5780..c41914a 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": "^1.1.1" } } diff --git a/test/test.js b/test/test.js index f8fecbb..6c849d5 100644 --- a/test/test.js +++ b/test/test.js @@ -39,28 +39,22 @@ describe('DB', function () { class AA extends DB { constructor(options) { super(options) - this.plugin('endpoint', function (options) { - if (options.type === 1) { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 1, msg: 'logout' }) - }, 0) - }) - } - }) - this.plugin('endpoint', function (options) { - if (options.type === 0) { - return new Promise((resolve) => { + + this.plugin('endpoint', (options) => { + return new Promise((resolve) => { + if (options.type === 1) { setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - } - }) + resolve({ retcode: 1 }); + }, 0); + } + if (options.type === 0) { + resolve({ retcode: 0 }); + } + }); + }); } - } - - const aa = new AA + } + const aa = new AA() // 如果 options.type === 1,则返回第一个答案 aa.request({ type: 1 }) .then(res => { @@ -153,22 +147,16 @@ describe('DB', function () { constructor(options) { super(options) this.plugin('endpoint', function (options) { - if (options.type === 1) { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 1, msg: 'logout' }) - }, 0) - }) - } - }) - this.plugin('endpoint', function (options) { - if (options.type === 0) { - return new Promise((resolve) => { + return new Promise((resolve) => { + if (options.type === 1) { setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - } + resolve({ retcode: 1, msg: 'logout' }); + }, 0); + } + if (options.type === 0) { + resolve({ retcode: 0, res: { msg: 'hello world' } }) + } + }); }) this.plugin('judge', function (res) {