Skip to content

Commit

Permalink
enable bind.all and prepare:source
Browse files Browse the repository at this point in the history
  • Loading branch information
hefangshi committed Dec 14, 2015
1 parent 6a1b481 commit 66e5843
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 24 deletions.
44 changes: 34 additions & 10 deletions lib/bigpipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ BigPipe.prototype._read = function (n) {
// bind pagelet data source.
// accept fucntion and model name.
BigPipe.prototype.bind = function (id, fn) {
if (fn.length === 1) {
if (fn.length >= 1) {
fn = Promise.promisify(fn);
}
this.sources[id] = fn;
Expand Down Expand Up @@ -95,6 +95,10 @@ BigPipe.prototype.addPagelet = function (obj) {
this.emit('error', new Error('Id is required when add pagelet'));
return;
}
else if (obj.id === 'all') {
this.emit('error', new Error('all is a preserved word for pagelet'));
return;
}

if (!this.isQuicklingMode() && obj.mode !== mode.pipeline && obj.mode !== mode.async) {
// 非 quickling 请求,只接收 pipeline 和 async 模式的 pagelet.
Expand Down Expand Up @@ -174,10 +178,22 @@ BigPipe.prototype.render = function () {
this._checkFinish();
};

BigPipe.prototype.prepareAllSources = function (cb) {
BigPipe.prototype.prepareAllSources = function () {
var sources = this.sources;
var promiseSources = {};
var me = this;
// when using res.bigpipe.bind('all'), pass * to cb to get all data
if (sources.all) {
return sources.all('*').then(function (data) {
me.pageletData = data;
});
}
this.emit('pagelet:source', '*', function (_sources) {
if (_sources.length >= 1) {
_sources = Promise.promisify(_sources);
}
sources = _sources;
});
for (var id in sources) {
if (sources.hasOwnProperty(id)) {
// 屏蔽所有异常
Expand All @@ -199,16 +215,24 @@ BigPipe.prototype.renderPagelet = function (pagelet) {
var source = sources[pagelet.id];

// 屏蔽无法调整为同步的 bigpipe 绑定策略
// if (!source && typeof sources.all === 'function') {
// source = sources.all.bind(null, pagelet.id);
// }
if (!source && typeof sources.all === 'function') {
source = sources.all.bind(null, pagelet.id);
}

// // hook
// this.emit('pagelet:source', pagelet.id, function (_source) {
// source = _source;
// });
// hook
this.emit('pagelet:source', pagelet.id, function (_source) {
if (_source.length >= 1) {
_source = Promise.promisify(_source);
}
source = _source;
});

pagelet.start(source);
if (this.pageletData[pagelet.id]) {
pagelet.start(this.pageletData[pagelet.id], true);
}
else {
pagelet.start(source);
}
};

BigPipe.prototype.destroy = function () {
Expand Down
5 changes: 2 additions & 3 deletions lib/pagelet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var _ = require('./util.js');
var Promise = require('bluebird');

var Pagelet = module.exports = function Pagelet(obj) {

this.model = obj.model;
this.container = obj['container'] || obj['for'];
this.mode = obj.mode;
Expand Down Expand Up @@ -84,9 +85,7 @@ Pagelet.prototype.start = function (provider, sync) {
return self._render(provider);
}
if (provider) {
new Promise(function (resolve, reject) {
resolve(provider());
}).then(function (data) {
provider().then(function (data) {
self._render(data);
}).catch(function (err) {
self._render({
Expand Down
190 changes: 179 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,91 @@ describe('render', function () {
});
});

describe('prepareAllSources', function () {
it('should finish pagelet data', function (done) {
var app = express();

app.use(middleware({
tpl: {
_default: '<%= this.html %>'
}
}));
app.use(function (req, res) {
var afterPrepare = false;
res.bigpipe.bind('pageletA', function () {
return new Promise(function (resolve) {
assert.equal(afterPrepare, false);
resolve({
msg: 'Hello world!'
});
});
});
res.bigpipe.prepareAllSources().then(function () {
afterPrepare = true;
res.bigpipe.addPagelet({
id: 'pageletA',
mode: 'async',
compiled: function (locals) {
return 'BigPipeFailed: ' + !!locals.BigPipeFailed;
}
});
res.bigpipe.pipe(res);
});

});

request(app.listen())
.get('/')
.end(function (err, res) {
if (err) return done(err);
assert.equal(res.text, 'BigPipeFailed: false');
done();
});
});

it('should render pagelet in sync', function (done) {
var app = express();

app.use(middleware({
tpl: {
_default: '<%= this.html %>'
}
}));
app.use(function (req, res) {
var afterPrepare = false;
res.bigpipe.bind('pageletA', function () {
return new Promise(function (resolve) {
assert.equal(afterPrepare, false);
resolve({
msg: 'Hello world!'
});
});
});
res.bigpipe.prepareAllSources().then(function () {
afterPrepare = true;
var pagelet = new res.bigpipe.Pagelet({
id: 'pageletA',
mode: 'async',
compiled: function (locals) {
return 'BigPipeFailed: ' + !!locals.BigPipeFailed;
}
});
pagelet.start(res.bigpipe.pageletData.pageletA, true);
res.end(pagelet.html);
});

});

request(app.listen())
.get('/')
.end(function (err, res) {
if (err) return done(err);
assert.equal(res.text, 'BigPipeFailed: false');
done();
});
});
});

describe('render template', function () {

it('template', function (done) {
Expand Down Expand Up @@ -593,8 +678,7 @@ describe('Provider', function () {
});
});

// disabled for sync render
it.skip('bigpipe.bind all', function (done) {
it('bigpipe.bind all', function (done) {
var app = express();

app.use(middleware({
Expand All @@ -606,6 +690,13 @@ describe('Provider', function () {
app.use(function (req, res) {
var bigpipe = res.bigpipe;

bigpipe.bind('all', function (id, next) {
assert.equal(id, 'pageletA');
next(null, {
content: 'test123'
});
});

bigpipe.addPagelet({
id: 'pageletA',
mode: 'async',
Expand All @@ -614,13 +705,6 @@ describe('Provider', function () {
}
});

bigpipe.bind('all', function (id, next) {
assert.equal(id, 'pageletA');
next(null, {
content: 'test123'
});
});

bigpipe.pipe(res);
});

Expand All @@ -634,8 +718,7 @@ describe('Provider', function () {
});
});

// disabled for sync render
it.skip('prepare:source', function (done) {
it('prepare:source', function (done) {
var app = express();

app.use(middleware({
Expand Down Expand Up @@ -1254,4 +1337,89 @@ describe('some incorrect usage.', function () {
done();
});
});

it('use a pagelet named all', function (done) {
var app = express();

app.use(middleware({
tpl: {
_default: '<%= this.id %>'
}
}));

app.use(function (req, res) {
var bigpipe = res.bigpipe;

bigpipe.on('pagelet:after', function (pagelet) {
pagelet.start();
});

bigpipe.on('error', function (e) {
assert.equal(e.message, 'all is a preserved word for pagelet');
});

bigpipe.addPagelet({
id: 'all',
mode: 'async',
locals: {
key: '123'
},
compiled: function () {
return 'whatever';
}
});


bigpipe.pipe(res);
});

request(app.listen())
.get('/')
.end(function (err, res) {
if (!err) return done();
done();
});
});

it('add a pagelet with null id', function (done) {
var app = express();

app.use(middleware({
tpl: {
_default: '<%= this.id %>'
}
}));

app.use(function (req, res) {
var bigpipe = res.bigpipe;

bigpipe.on('pagelet:after', function (pagelet) {
pagelet.start();
});

bigpipe.on('error', function (e) {
assert.equal(e.message, 'Id is required when add pagelet');
});

bigpipe.addPagelet({
mode: 'async',
locals: {
key: '123'
},
compiled: function () {
return 'whatever';
}
});


bigpipe.pipe(res);
});

request(app.listen())
.get('/')
.end(function (err, res) {
if (!err) return done();
done();
});
});
});

0 comments on commit 66e5843

Please sign in to comment.