Skip to content

Commit

Permalink
Fixes #251 - Output of render should be Promise compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkelleher committed Nov 12, 2016
1 parent d7ccc6b commit 83c84f7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
23 changes: 23 additions & 0 deletions runtime/OutMixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ module.exports = {
return this;
},

then: function(fn, fnErr) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self.on('error', reject);
self.on('finish', function(data) {
try {
resolve(fn(data));
} catch(err) {
reject(err);
}
});
});

if (fnErr) {
promise = promise.catch(fnErr);
}
return promise;
},

catch: function(fnErr) {
return this.then(undefined, fnErr);
},

appendTo: function(referenceEl) {
var newNode = this.getNode(referenceEl.ownerDocument);
dom.appendTo(newNode, referenceEl);
Expand Down
6 changes: 3 additions & 3 deletions runtime/html/AsyncStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ var proto = AsyncStream.prototype = {
if (state.writer.end) {
state.writer.end();
} else {
state.events.emit('finish');
state.events.emit('finish', this);
}
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ var proto = AsyncStream.prototype = {
var state = this._state;

if (event === 'finish' && state.finished) {
callback();
callback(this);
return this;
}

Expand All @@ -302,7 +302,7 @@ var proto = AsyncStream.prototype = {
var state = this._state;

if (event === 'finish' && state.finished) {
callback();
callback(this);
return this;
}

Expand Down
37 changes: 37 additions & 0 deletions test/AsyncStream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ describe('async-writer' , function() {
});
});

it('should resolve promise upon finish', function() {
var out = new AsyncStream();

out.write('1');
out.write('2');

return out.end().then((data) => {
const output = out.getOutput();
expect(output).to.equal('12');
expect(data.getOutput()).to.equal('12');
});
});

it('should render a series of sync and async calls correctly', function(done) {
var out = new AsyncStream();
out.write('1');
Expand Down Expand Up @@ -250,6 +263,30 @@ describe('async-writer' , function() {
});
});

it('should catch error in promise catch', function(done) {
const out = new AsyncStream();

let errors = [];

out.on('error', function(e) {
errors.push(e);
});

out.write('1');

let asyncOut = out.beginAsync();
setTimeout(function() {
asyncOut.error(new Error('test'));
}, 10);

out.write('3');
out.end().catch((err) => {
expect(errors.length).to.equal(1);
expect(err).to.be.an('error');
done();
});
});

it('should support chaining', function(done) {
var errors = [];
var out = new AsyncStream()
Expand Down
9 changes: 9 additions & 0 deletions test/AsyncVDOMBuilder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ it('async', function(done) {
});
});

it('promise', function(done) {
const out = new AsyncVDOMBuilder();
out.element('div', {}, 0);
out.end().then((tree) => {
expect(tree.childNodes.length).to.equal(1);
done();
});
});

it('async flush', function(done) {
var out = new AsyncVDOMBuilder();
out.on('update', function(tree) {
Expand Down
1 change: 1 addition & 0 deletions test/autotests/api/load-render-promise/template.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Hello ${data.name}!
16 changes: 16 additions & 0 deletions test/autotests/api/load-render-promise/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const nodePath = require('path');

exports.check = function(marko, markoCompiler, expect, done) {
let template = marko.load(nodePath.join(__dirname, 'template.marko'));

template.render({
name: 'John'
}).then((out) => {
expect(out.getOutput()).to.equal('Hello John!');
done();
}).catch((err) => {
done(err);
});
};

0 comments on commit 83c84f7

Please sign in to comment.