yog-bigpipe
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -6,2 +6,3 @@ var BigPipe = require('./lib/bigpipe.js'); | ||
return function (req, res, next) { | ||
options = options || {}; | ||
var bigpipe = res.bigpipe = new BigPipe(options); | ||
@@ -28,2 +29,4 @@ var pagelet = req.query.pagelet; | ||
res.locals.isQuicklingMode = bigpipe.isQuicklingMode(); | ||
// 检查是否是爬虫模式 | ||
bigpipe.isSpiderMode = options.isSpiderMode ? options.isSpiderMode(req) : false; | ||
// 拼写兼容 | ||
@@ -30,0 +33,0 @@ res.locals.isQuickingMode = res.locals.isQuicklingMode; |
@@ -7,2 +7,3 @@ var Pagelet = require('./pagelet.js'); | ||
var util = require('util'); | ||
var Promise = require('bluebird'); | ||
@@ -19,2 +20,4 @@ var BigPipe = module.exports = function BigPipe(options) { | ||
this.parentQuicklings = []; | ||
this.Pagelet = Pagelet; | ||
this.pageletData = {}; | ||
@@ -60,2 +63,8 @@ this.on('pagelet:after', this._onPageletDone); | ||
BigPipe.prototype.bind = function (id, fn) { | ||
if (fn.length >= 1 && id !== 'all') { | ||
fn = Promise.promisify(fn); | ||
} | ||
else if (fn.length >= 2 && id === 'all') { | ||
fn = Promise.promisify(fn); | ||
} | ||
this.sources[id] = fn; | ||
@@ -95,2 +104,6 @@ return this; | ||
} | ||
else if (obj.id === 'all') { | ||
this.emit('error', new Error('all is a preserved word for pagelet')); | ||
return; | ||
} | ||
@@ -175,2 +188,37 @@ if (!this.isQuicklingMode() && obj.mode !== mode.pipeline && obj.mode !== mode.async) { | ||
BigPipe.prototype.prepareAllSources = function () { | ||
var sources = this.sources; | ||
var promiseSources = {}; | ||
var me = this; | ||
// this.emit('pagelet:source', '*', function (_source) { | ||
// if (_source.length >= 2) { | ||
// _source = Promise.promisify(_source); | ||
// } | ||
// sources.all = _source; | ||
// }); | ||
// 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; | ||
return data; | ||
}); | ||
} | ||
for (var id in sources) { | ||
if (sources.hasOwnProperty(id)) { | ||
// 屏蔽所有异常 | ||
promiseSources[id] = sources[id]().catch(function (err) { | ||
return { | ||
BigPipeFailed: true, | ||
err: err | ||
}; | ||
}); | ||
} | ||
} | ||
return Promise.props(promiseSources).then(function (data) { | ||
me.pageletData = data; | ||
return data; | ||
}); | ||
}; | ||
BigPipe.prototype.renderPagelet = function (pagelet) { | ||
@@ -180,2 +228,3 @@ var sources = this.sources; | ||
// 屏蔽无法调整为同步的 bigpipe 绑定策略 | ||
if (!source && typeof sources.all === 'function') { | ||
@@ -186,7 +235,15 @@ source = sources.all.bind(null, pagelet.id); | ||
// hook | ||
this.emit('pagelet:source', pagelet.id, function (_source) { | ||
source = _source; | ||
}); | ||
// this.emit('pagelet:source', pagelet.id, function (_source) { | ||
// if (_source.length >= 2) { | ||
// _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); | ||
} | ||
}; | ||
@@ -193,0 +250,0 @@ |
var util = require("util"); | ||
var EventEmitter = require("events").EventEmitter; | ||
var _ = require('./util.js'); | ||
var Promise = require('bluebird'); | ||
var Pagelet = module.exports = function Pagelet(obj) { | ||
this.model = obj.model; | ||
@@ -70,5 +72,4 @@ this.container = obj['container'] || obj['for']; | ||
*/ | ||
Pagelet.prototype.start = function (provider) { | ||
Pagelet.prototype.start = function (provider, sync) { | ||
var self = this; | ||
var eventname; | ||
@@ -84,22 +85,13 @@ if (this.state !== status.pending) { | ||
if (!provider && this.locals && (eventname = 'onPagelet' + _.ucfirst(this.id)) && | ||
typeof this.locals[eventname] === 'function') { | ||
provider = this.locals[eventname]; | ||
if (sync) { | ||
return self._render(provider); | ||
} | ||
if (!provider && this.locals && typeof this.locals['onPagelet'] === 'function') { | ||
provider = this.locals['onPagelet'].bind(null, this.id); | ||
} | ||
if (provider) { | ||
provider(function (err, val) { | ||
// 被销毁后,不再触发渲染 | ||
if (self.destroyed) { | ||
return; | ||
} | ||
if (err) { | ||
return self.emit('error', err); | ||
} | ||
self._render(val); | ||
provider().then(function (data) { | ||
self._render(data); | ||
}).catch(function (err) { | ||
self._render({ | ||
BigPipeFailed: true, | ||
err: err | ||
}); | ||
}); | ||
@@ -112,4 +104,9 @@ } | ||
// don't call this directly. | ||
Pagelet.prototype._render = function (model) { | ||
var self = this; | ||
if (self.destroyed) { | ||
return; | ||
} | ||
var locals = this.locals || {}; | ||
@@ -116,0 +113,0 @@ var fn = this.compiled; |
{ | ||
"name": "yog-bigpipe", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "An express.js middleware for fis widget pipline output.", | ||
@@ -32,3 +32,6 @@ "main": "index.js", | ||
"should": "~4.0.4" | ||
}, | ||
"dependencies": { | ||
"bluebird": "3.0.6" | ||
} | ||
} |
@@ -139,3 +139,8 @@ (function (root) { | ||
if (this.readyState == 4) { | ||
cb(this.responseText); | ||
if (this.status !== 200) { | ||
cb(this.responseText); | ||
} | ||
else { | ||
cb(null, this.responseText); | ||
} | ||
} | ||
@@ -473,2 +478,3 @@ }; | ||
// - param extra params for the ajax call. | ||
// - search replace location.search for the ajax call, without '?'. | ||
// - container by default, the pagelet will be rendered in | ||
@@ -529,2 +535,3 @@ // some document node with the same id. With this | ||
obj.search && args.push(obj.search); | ||
obj.param && args.push(obj.param); | ||
if (obj.url) { | ||
@@ -534,3 +541,8 @@ url = obj.url + (obj.url.indexOf('?') === -1 ? '?' : '&') + args.join('&'); | ||
else { | ||
url = (location.search ? location.search + '&' : '?') + args.join('&'); | ||
if (!obj.search) { | ||
url = (location.search ? location.search + '&' : '?') + args.join('&'); | ||
} | ||
else { | ||
url = '?' + args.join('&'); | ||
} | ||
} | ||
@@ -560,3 +572,6 @@ BigPipe.on('pageletdone', function (pagelet, res) { | ||
else { | ||
Util.ajax(url, function (res) { | ||
Util.ajax(url, function (err, res) { | ||
if (err) { | ||
return obj.cb && obj.cb(err); | ||
} | ||
// if the page url has been moved. | ||
@@ -563,0 +578,0 @@ if (currentPageUrl !== location.href) { |
40677
1070
1
+ Addedbluebird@3.0.6
+ Addedbluebird@3.0.6(transitive)