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:完成作业 #117

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
55 changes: 48 additions & 7 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -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
judge(res) {
let isJudged = this.hooks.judgeHooks.call(res);
if(isJudged !== true) {
isJudged = false
}
return isJudged;
}

}
module.exports = DB;
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": "^1.1.1"
}
}
58 changes: 23 additions & 35 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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) {
Expand Down