From 1e10ea20783d47b4f0cd2f4ef30fd61eaaaafdb9 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 28 Dec 2015 14:57:21 +0800 Subject: [PATCH] feat: support generator --- package.json | 2 ++ src/plugin.js | 6 +++++ test/fixtures/plugin-run/plugin-async.js | 21 ++++++++++++++++ test/fixtures/plugin-run/plugin-generator.js | 23 ++++++++++++++++++ .../{plugin-a.js => plugin-sync.js} | 4 ++-- test/index-test.js | 24 +++++++++++++++---- test/plugin-test.js | 15 ++++++++++++ 7 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/plugin-run/plugin-async.js create mode 100644 test/fixtures/plugin-run/plugin-generator.js rename test/fixtures/plugin-run/{plugin-a.js => plugin-sync.js} (80%) diff --git a/package.json b/package.json index 11e658d..0bca721 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/plugin.js b/src/plugin.js index fad16e7..7fd4ef7 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -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) === '.'; @@ -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 { diff --git a/test/fixtures/plugin-run/plugin-async.js b/test/fixtures/plugin-run/plugin-async.js new file mode 100644 index 0000000..6562797 --- /dev/null +++ b/test/fixtures/plugin-run/plugin-async.js @@ -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; + }; + }, +}; diff --git a/test/fixtures/plugin-run/plugin-generator.js b/test/fixtures/plugin-run/plugin-generator.js new file mode 100644 index 0000000..7bfe7c9 --- /dev/null +++ b/test/fixtures/plugin-run/plugin-generator.js @@ -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; + }; + }, +}; diff --git a/test/fixtures/plugin-run/plugin-a.js b/test/fixtures/plugin-run/plugin-sync.js similarity index 80% rename from test/fixtures/plugin-run/plugin-a.js rename to test/fixtures/plugin-run/plugin-sync.js index b85621f..3fd9a74 100644 --- a/test/fixtures/plugin-run/plugin-a.js +++ b/test/fixtures/plugin-run/plugin-sync.js @@ -4,7 +4,7 @@ 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'); @@ -12,7 +12,7 @@ module.exports = { }, 'middleware': function() { return function *(next) { - if (this.url.indexOf('/foo') > -1) { + if (this.url.indexOf('/sync') > -1) { this.body = a; return; } diff --git a/test/index-test.js b/test/index-test.js index b559388..6e1dec7 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -10,7 +10,9 @@ 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'), @@ -18,9 +20,23 @@ describe('index', () => { }, 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(); }); }); diff --git a/test/plugin-test.js b/test/plugin-test.js index e4eda2d..cafeee8 100644 --- a/test/plugin-test.js +++ b/test/plugin-test.js @@ -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(); + }); + }); + });