yog-bigpipe
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -14,3 +14,2 @@ var Pagelet = require('./pagelet.js'); | ||
var BigPipe = module.exports = function BigPipe(options) { | ||
@@ -45,7 +44,7 @@ this.options = mixin({}, BigPipe.options, options); | ||
var arr = this.quicklings; | ||
arr.push.apply(arr, pagelets); | ||
arr && arr.push.apply(arr, pagelets); | ||
}; | ||
BigPipe.prototype.isQuickingMode = function() { | ||
return this.quicklings.length; | ||
return !!(this.quicklings && this.quicklings.length); | ||
}; | ||
@@ -64,3 +63,4 @@ | ||
return false; | ||
} else if (this.isQuickingMode() && obj.mode !== mode.quickling) { | ||
} else if (this.isQuickingMode() && | ||
(obj.mode !== mode.quickling || !~this.quicklings.indexOf(obj.id))) { | ||
@@ -71,10 +71,11 @@ // quickling 请求,只接收 quickling 模式的 pagelet. | ||
if (this.quicklings.length && !~this.quicklings.indexOf(obj.id)) { | ||
// 不在列表里面。 | ||
return false; | ||
} | ||
var pagelet = new Pagelet(obj); | ||
var sources = this.sources; | ||
var source = sources[pagelet.id]; | ||
if (!source && sources.all) { | ||
source = sources.all.bind(null, pagelet.id); | ||
} | ||
source && pagelet.setSource(source); | ||
this.pagelets.push(pagelet); | ||
@@ -86,3 +87,2 @@ pagelet.mode === mode.pipeline && this.pipelines.push(pagelet); | ||
if (this.state === status.rendering) { | ||
sources[pagelet.id] && pagelet.setSource(sources[pagelet.id]); | ||
pagelet.render(); | ||
@@ -95,2 +95,3 @@ } | ||
var sources = this.sources; | ||
var self = this; | ||
@@ -106,3 +107,2 @@ this.stream = stream; | ||
pagelets.forEach(function(pagelet) { | ||
sources[pagelet.id] && pagelet.setSource(sources[pagelet.id]); | ||
pagelet.render(); | ||
@@ -122,3 +122,2 @@ }); | ||
BigPipe.prototype._onPageletReady = function(pagelet) { | ||
@@ -150,3 +149,3 @@ var cb, idx, item; | ||
BigPipe.prototype._checkFinish = function() { | ||
if (!this.pagelets.length) { | ||
if (!this.pagelets.length && this.state === status.rendering) { | ||
cb = this.cb; | ||
@@ -153,0 +152,0 @@ |
var util = require("util"); | ||
var EventEmitter = require("events").EventEmitter; | ||
function mixin(a, b) { | ||
if (a && b) { | ||
for (var key in b) { | ||
a[key] = b[key]; | ||
} | ||
} | ||
return a; | ||
} | ||
function ucfirst(str) { | ||
return str.substring(0, 1).toUpperCase() + str.substring(1); | ||
} | ||
var Pagelet = module.exports = function Pagelet(obj) { | ||
this.model = obj.model; | ||
this.alias = obj['for']; | ||
this.container = obj['for']; | ||
this.mode = obj.mode; | ||
@@ -17,2 +30,3 @@ this.id = obj.id; | ||
this.html = ''; | ||
// this.errorMsg = ''; | ||
}; | ||
@@ -25,3 +39,4 @@ | ||
rendering: 'rendering', | ||
fulfilled: 'fulfilled' | ||
fulfilled: 'fulfilled', | ||
failed: 'failed' | ||
}; | ||
@@ -35,3 +50,2 @@ | ||
Pagelet.prototype.setSource = function(source) { | ||
@@ -42,20 +56,32 @@ this.source = source; | ||
Pagelet.prototype.render = function() { | ||
var source = this.source; | ||
var fn = this.source; | ||
var eventname; | ||
var self = this; | ||
if (!fn && (eventname = 'onPagelet' + ucfirst(this.id)) && | ||
typeof this.locals[eventname] === 'function' ) { | ||
fn = this.locals[eventname]; | ||
} | ||
if (!fn && typeof this.locals['opPagelet'] === 'function') { | ||
fn = this.locals['opPagelet'].bind(null, this.id); | ||
} | ||
this.state = status.rendering; | ||
if (typeof source === 'function') { | ||
source.call(null, function(err, val) { | ||
// todo 支持 pagelet.model 自动关联。 | ||
if (fn) { | ||
fn(function(err, val) { | ||
if (err) { | ||
self.emit('error', err); | ||
// todo | ||
// self.state = status.failed; | ||
// self.errorMsg = err; | ||
return; | ||
} | ||
self.destroyed || self._render(val); | ||
self._render(val); | ||
}); | ||
} else if (typeof source ==='string' && source) { | ||
// todo 自动创建 model 与之关联 | ||
} else { | ||
this._render(); | ||
self._render(); | ||
} | ||
@@ -70,3 +96,3 @@ }; | ||
model && (locals['model'] = model); | ||
model && (mixin(locals, model), locals.model = model); | ||
@@ -133,3 +159,3 @@ this._yog = locals._yog = locals._yog.fork(); | ||
return { | ||
container: this.alias || '', | ||
container: this.container || '', | ||
id: this.id, | ||
@@ -136,0 +162,0 @@ html: this.html, |
{ | ||
"name": "yog-bigpipe", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "An express.js middleware for fis widget pipline output.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
22485
578