yog-bigpipe
Advanced tools
Comparing version 0.0.2 to 0.0.3
10
index.js
@@ -9,3 +9,10 @@ var BigPipe = require('./lib/bigpipe.js'); | ||
var pagelets = req.param('pagelets'); | ||
var destroy = function() { | ||
res.removeListener('finish', destroy); | ||
res.removeListener('close', destroy); | ||
bigpipe.destroy(); | ||
bigpipe = res.bigpipe = null; | ||
}; | ||
if (!Array.isArray(pagelets)) { | ||
@@ -18,4 +25,7 @@ pagelets = pagelets ? [ pagelets ] : []; | ||
res.on('finish', destroy); | ||
res.on('close', destroy); | ||
next(); | ||
} | ||
} |
@@ -5,9 +5,9 @@ var Pagelet = require('./pagelet.js'); | ||
function mixin(a, b){ | ||
if (a && b) { | ||
for (var key in b) { | ||
a[key] = b[key]; | ||
function mixin(a, b) { | ||
if (a && b) { | ||
for (var key in b) { | ||
a[key] = b[key]; | ||
} | ||
} | ||
} | ||
return a; | ||
return a; | ||
} | ||
@@ -34,2 +34,4 @@ | ||
// bind pagelet data source. | ||
// accept fucntion and model name. | ||
BigPipe.prototype.bind = function(id, fn) { | ||
@@ -90,3 +92,2 @@ this.sources[id] = fn; | ||
if (!pagelets.length) { | ||
this.destroy(); | ||
process.nextTick(cb); | ||
@@ -102,3 +103,9 @@ } | ||
BigPipe.prototype.destroy = function() { | ||
this.stream = this.cb = null; | ||
this.stream = this.cb = this.sources = null; | ||
this.rendered.concat(this.pagelets).forEach(function(pagelet) { | ||
pagelet.destroy(); | ||
}); | ||
this.rendered = this.pipelines = this.pagelets = this.quicklings = null; | ||
}; | ||
@@ -108,20 +115,21 @@ | ||
BigPipe.prototype._onPageletReady = function(pagelet) { | ||
var content, cb, idx, item; | ||
var cb, idx, item; | ||
if (pagelet.mode ===mode.pipeline) { | ||
// 必须按顺序 | ||
idx = this.pipelines.indexOf(pagelet); | ||
// idx = this.pipelines.indexOf(pagelet); | ||
// // if this is not the first pipline pagelet. | ||
// if (idx !== 0) { | ||
// return; | ||
// } | ||
// 必须是第一个才开始吐数据。否则等待。 | ||
if (idx===0) { | ||
while((item = this.pipelines[0]) && item.state === status.fulfilled) { | ||
content = this.format(item); | ||
content && this.stream.write(content); | ||
this._markPageletRendered(item); | ||
} | ||
while((item = this.pipelines[0]) && | ||
item.state === status.fulfilled) { | ||
this.outputPagelet(item); | ||
} | ||
} else { | ||
content = this.format(pagelet); | ||
content && this.stream.write(content); | ||
this._markPageletRendered(pagelet); | ||
this.outputPagelet(pagelet); | ||
} | ||
@@ -133,3 +141,2 @@ | ||
this.state = status.fulfilled; | ||
this.destroy(); | ||
cb(null, ''); | ||
@@ -139,2 +146,8 @@ } | ||
BigPipe.prototype.outputPagelet = function(pagelet) { | ||
var content = this.format(pagelet); | ||
content && this.stream.write(content); | ||
this._markPageletRendered(pagelet); | ||
}; | ||
BigPipe.prototype.format = function(pagelet) { | ||
@@ -141,0 +154,0 @@ var tpl = this.options.tpl; |
@@ -59,2 +59,5 @@ var util = require("util"); | ||
Pagelet.prototype._render = function(model) { | ||
if (this.destroyed) { | ||
return; | ||
} | ||
var locals = this.locals; | ||
@@ -105,4 +108,4 @@ var fn = this.compiled; | ||
scripts.splice.apply(scripts, [scripts.length, 0].concat(fis.scripts)); | ||
fis.sync['js'] && (js = fis.sync['js'].concat()); | ||
fis.sync['css'] && (css = fis.sync['css'].concat()); | ||
fis.sync['js'] && (js = this.js = fis.sync['js'].concat()); | ||
fis.sync['css'] && (css = this.css = fis.sync['css'].concat()); | ||
@@ -123,2 +126,9 @@ this.html = output; | ||
}; | ||
} | ||
Pagelet.prototype.destroy = function() { | ||
this.destroyed = true; | ||
this.removeAllListeners(); | ||
this.fis = this.mode = this.id = this.locals = this.compiled = this.state = this.html = null; | ||
this.scripts = this.js = this.css = this.styles = null; | ||
} |
{ | ||
"name": "yog-bigpipe", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "An express.js middleware for fis widget pipline output.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
21203
8
538