From 2103c2b6dbffd694dd7e1f59610cb981654ff99b Mon Sep 17 00:00:00 2001 From: JackXie60 <1809697652@qq.com> Date: Sun, 21 Aug 2022 13:14:08 +0800 Subject: [PATCH 1/3] feat:homework2 --- lib/db.js | 55 ++++++++++++++++++++++++++++++++++++++++++------- package.json | 3 +++ test/test.js | 58 +++++++++++++++++++++------------------------------- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/lib/db.js b/lib/db.js index a1fe32c..0117d10 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 94bd5ff..fa2c14e 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,8 @@ "karma-webpack": "^2.0.4", "mocha": "^3.5.0", "webpack": "^3.5.3" + }, + "dependencies": { + "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) { From 7146067075b6078fd208dfd93df8cb6c7bac949b Mon Sep 17 00:00:00 2001 From: JackXie60 <1809697652@qq.com> Date: Sun, 21 Aug 2022 17:22:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=E6=8F=92=E4=BB=B6=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/db.js b/lib/db.js index 0117d10..9e5c1b2 100644 --- a/lib/db.js +++ b/lib/db.js @@ -26,7 +26,7 @@ class DB { return this.hooks.endpointHooks.promise(this.options).then(res=>{ const isJudge = this.judge(res); if(!isJudge) { - return res; + return res } return Promise.reject(res); }) From 8a530f494e7f0f216e26929a9ce18954f57cae79 Mon Sep 17 00:00:00 2001 From: JackXie60 <1809697652@qq.com> Date: Sun, 21 Aug 2022 17:51:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index fa2c14e..c41914a 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "webpack": "^3.5.3" }, "dependencies": { + "puppeteer": "^16.2.0", "tapable": "^1.1.1" } }