Skip to content

Commit

Permalink
feat: support generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 28, 2015
1 parent 9099a1c commit 1e10ea2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
],
"dependencies": {
"async": "^1.5.0",
"co": "^4.6.0",
"commander": "~2.9.0",
"dora-util-is-async": "^0.1.0",
"internal-ip": "~1.1.0",
"is-generator-fn": "^1.0.0",
"is-plain-object": "~2.0.1",
"koa": "~1.1.2",
"koa-serve-index": "~1.1.1",
Expand Down
6 changes: 6 additions & 0 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import resolve from './resolve';
import spmLog from 'spm-log';
import _isAsync from 'dora-util-is-async';
import reduceAsync from './reduceAsync';
import isGeneratorFn from 'is-generator-fn';
import co from 'co';

function isRelative(filepath) {
return filepath.charAt(0) === '.';
Expand Down Expand Up @@ -85,6 +87,10 @@ export function applyPlugins(plugins, name, context, pluginArgs, _callback = fun
if (name === 'middleware') {
context.app.use(func.call(context));
callback();
} else if (isGeneratorFn(func)) {
co.wrap(func).call(context).then((val) => {
callback(null, val);
}, callback);
} else if (isAsync(func)) {
func.call(context, memo);
} else {
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/plugin-run/plugin-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

var a = '';

module.exports = {
name: 'async',
'middleware.before': function() {
process.nextTick(() => {
a = `async-${this.query.affix}`;
this.callback();
});
},
'middleware': function() {
return function *(next) {
if (this.url.indexOf('/async') > -1) {
this.body = a;
return;
}
yield next;
};
},
};
23 changes: 23 additions & 0 deletions test/fixtures/plugin-run/plugin-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var a = '';

module.exports = {
name: 'async',
*'middleware.before'() {
yield new Promise((resolve) => {
process.nextTick(() => {
a = `generator-${this.query.affix}`;
resolve();
});
});
},
'middleware'() {
return function *(next) {
if (this.url.indexOf('/generator') > -1) {
this.body = a;
return;
}
yield next;
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ var a = '';
module.exports = {
name: 'a',
'middleware.before': function() {
a = 'foo';
a = `sync-${this.query.affix}`;
this.log.info('a');
this.log.warn('a');
this.log.debug('a');
this.log.error('a');
},
'middleware': function() {
return function *(next) {
if (this.url.indexOf('/foo') > -1) {
if (this.url.indexOf('/sync') > -1) {
this.body = a;
return;
}
Expand Down
24 changes: 20 additions & 4 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@ describe('index', () => {
before(done => {
dora({
plugins: [
'./plugin-a',
'./plugin-sync?affix=s',
'./plugin-async?affix=a',
'./plugin-generator?affix=g',
],
port,
cwd: join(__dirname, 'fixtures/plugin-run'),
verbose: true,
}, done);
});

it('plugin-a middleware', (done) => {
request(`http://localhost:${port}/foo`, (err, res, body) => {
expect(body).toEqual('foo');
it('plugin-sync', (done) => {
request(`http://localhost:${port}/sync`, (err, res, body) => {
expect(body).toEqual('sync-s');
done();
});
});

it('plugin-async', (done) => {
request(`http://localhost:${port}/async`, (err, res, body) => {
expect(body).toEqual('async-a');
done();
});
});

it('plugin-generator', (done) => {
request(`http://localhost:${port}/generator`, (err, res, body) => {
expect(body).toEqual('generator-g');
done();
});
});
Expand Down
15 changes: 15 additions & 0 deletions test/plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,19 @@ describe('plugin', () => {
});
});

it('applyPlugins generator', () => {
const result = applyPlugins([
{ test(memo) { return memo + '1'; } },
{ *test(memo) {
yield new Promise((resolve) => {
process.nextTick(() => { this.callback(null, memo + '2'); });
});
}
},
], 'test', {}, '0', (err, result) => {
expect(result).toEqual('012');
done();
});
});

});

0 comments on commit 1e10ea2

Please sign in to comment.