Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exercise4): 使用 tapable 2.x 完成 request 封装训练 #115

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.db.js
package-lock.json
dbback.js
dbback.js
yarn.lock
87 changes: 81 additions & 6 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,88 @@
const Tapable = require('tapable')
const { AsyncSeriesWaterfallHook, SyncWaterfallHook } = require("tapable");

class DB extends Tapable {
class HooksAdapter {
constructor() {
// TODO
this.hooks = {
endpoint: new AsyncSeriesWaterfallHook(["options"]),
options: new SyncWaterfallHook(["options"]),
judge: new SyncWaterfallHook(["res"]),
};
}

request() {
// TODO
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);
};
}

request(options) {
if (options) {
this.modifyOptions(options);
}

return this.hooksAdapter
.request(this.options)
.then((res) => {
return Promise.resolve(res);
})
.catch((err) => {
return Promise.reject(err);
})
}

modifyOptions(options = {}) {
if (typeof options !== "object") {
throw new Error("argument should be a Object");
}
Object.assign(this.options, options);
this.hooksAdapter.syncOptions(this.options)
}
}

module.exports = DB
module.exports = DB;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "test",
"main": "index.js",
"scripts": {
"test": "karma start"
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -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",
Expand All @@ -28,6 +29,7 @@
"webpack": "^3.5.3"
},
"dependencies": {
"puppeteer": "^16.2.0"
"puppeteer": "^16.2.0",
"tapable": "^2.2.1"
}
}