From 2215117030057fc2d0f804df39828219de102b7b Mon Sep 17 00:00:00 2001 From: blankzz <454257312@qq.com> Date: Sun, 21 Aug 2022 10:29:06 +0800 Subject: [PATCH 1/4] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20tapable=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- package.json | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 17ef735..44a82f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .db.js package-lock.json -dbback.js \ No newline at end of file +dbback.js +yarn.lock \ No newline at end of file diff --git a/package.json b/package.json index 94bd5ff..83150da 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": "^2.2.1" } } From eecb2c6affa4a0b04c5286d644fc23a676280854 Mon Sep 17 00:00:00 2001 From: blankzz <454257312@qq.com> Date: Sun, 21 Aug 2022 13:10:43 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat(exercise4):=20=E5=AE=8C=E6=88=90=20req?= =?UTF-8?q?uest=20=E5=B0=81=E8=A3=85=E8=AE=AD=E7=BB=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/lib/db.js b/lib/db.js index a1fe32c..9c776ba 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,13 +1,67 @@ -const Tapable = require('tapable') +const { AsyncSeriesWaterfallHook, SyncWaterfallHook } = require("tapable"); -class DB extends Tapable { - constructor() { - // TODO +class DB { + constructor(options) { + this.options = options || {}; + + this.hooks = { + endpoint: new AsyncSeriesWaterfallHook(["options"]), + options: new SyncWaterfallHook(["options"]), + judge: new SyncWaterfallHook(["res"]), + }; + + this.plugin = function (hookName, cb) { + switch (hookName) { + case "endpoint": + this.hooks.endpoint.tapPromise("requestPlugin", (options) => { + const result = cb(options) + if(result) { + return result.catch(err => { + return Promise.reject(err) + }) + } else { + return Promise.resolve(options) + } + }); + break; + case "options": + this.hooks.options.tap("setOptionsPlugin", cb); + break; + case "judge": + this.hooks.judge.tap("parseResponsePlugin", cb); + break; + } + }; + } + + request(options) { + if (options) { + this.modifyOptions(options); + } + + return this.hooks.endpoint + .promise(this.options) + .then((res) => { + const judgeRes = this.hooks.judge.call(res); + + if (judgeRes === true) { + return Promise.reject(res); + } + + return res; + }) + .catch((err) => { + return Promise.reject(err); + }); } - request() { - // TODO + modifyOptions(options = {}) { + if (typeof options !== "object") { + throw new Error("argument should be a Object"); + } + Object.assign(this.options, options); + this.hooks.options.call(this.options); } } -module.exports = DB \ No newline at end of file +module.exports = DB; From 0ecbe61a5111f3a229dc32b0d71e4cccddc2e4e4 Mon Sep 17 00:00:00 2001 From: blankzz <454257312@qq.com> Date: Sun, 21 Aug 2022 13:28:47 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor(db):=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=BC=95=E5=85=A5=20HooksAdapter=20=E4=BD=BF?= =?UTF-8?q?=E5=85=B3=E6=B3=A8=E7=82=B9=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db.js | 91 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 35 deletions(-) diff --git a/lib/db.js b/lib/db.js index 9c776ba..16f6ff3 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,36 +1,63 @@ const { AsyncSeriesWaterfallHook, SyncWaterfallHook } = require("tapable"); -class DB { - constructor(options) { - this.options = options || {}; - +class HooksAdapter { + constructor() { this.hooks = { endpoint: new AsyncSeriesWaterfallHook(["options"]), options: new SyncWaterfallHook(["options"]), judge: new SyncWaterfallHook(["res"]), }; + } - this.plugin = function (hookName, cb) { - switch (hookName) { - case "endpoint": - this.hooks.endpoint.tapPromise("requestPlugin", (options) => { - const result = cb(options) - if(result) { - return result.catch(err => { - return Promise.reject(err) - }) - } else { - return Promise.resolve(options) - } - }); - break; - case "options": - this.hooks.options.tap("setOptionsPlugin", cb); - break; - case "judge": - this.hooks.judge.tap("parseResponsePlugin", cb); - break; + register(hookName, cb) { + switch (hookName) { + case "endpoint": + this.hooks.endpoint.tapPromise("requestPlugin", (options) => { + const result = cb(options); + if (result) { + return result.catch((err) => { + return Promise.reject(err); + }); + } else { + return Promise.resolve(options); + } + }); + break; + case "options": + this.hooks.options.tap("setOptionsPlugin", cb); + break; + case "judge": + this.hooks.judge.tap("parseResponsePlugin", cb); + break; + } + } + + request(options) { + return this.hooks.endpoint.promise(options).then(res => { + const judgeRes = this.hooks.judge.call(res); + + if (judgeRes === true) { + return Promise.reject(res); } + + return res; + }).catch((err) => { + return Promise.reject(err); + }); + } + + syncOptions (options) { + this.hooks.options.call(options); + } +} + +class DB { + constructor(options) { + this.options = options || {}; + this.hooksAdapter = new HooksAdapter(); + + this.plugin = function (hookName, cb) { + this.hooksAdapter.register(hookName, cb); }; } @@ -39,20 +66,14 @@ class DB { this.modifyOptions(options); } - return this.hooks.endpoint - .promise(this.options) + return this.hooksAdapter + .request(this.options) .then((res) => { - const judgeRes = this.hooks.judge.call(res); - - if (judgeRes === true) { - return Promise.reject(res); - } - - return res; + return Promise.resolve(res); }) .catch((err) => { return Promise.reject(err); - }); + }) } modifyOptions(options = {}) { @@ -60,7 +81,7 @@ class DB { throw new Error("argument should be a Object"); } Object.assign(this.options, options); - this.hooks.options.call(this.options); + this.hooksAdapter.syncOptions(this.options) } } From 914074ee71c03c8afeb877c7ac120aed1e0fd0e1 Mon Sep 17 00:00:00 2001 From: blankzz <454257312@qq.com> Date: Sun, 21 Aug 2022 13:33:25 +0800 Subject: [PATCH 4/4] =?UTF-8?q?chore(jest):=20=E4=BD=BF=E7=94=A8=20jest=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=20karma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 83150da..1f7bc69 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "test", "main": "index.js", "scripts": { - "test": "karma start" + "test": "jest" }, "repository": { "type": "git", @@ -20,6 +20,7 @@ }, "homepage": "https://github.com/FE-star/homework2#readme", "devDependencies": { + "jest": "^28.1.3", "karma": "^6.3.16", "karma-chrome-launcher": "^2.2.0", "karma-mocha": "^1.3.0",