diff --git a/ghost/promise/lib/pipeline.js b/ghost/promise/lib/pipeline.js new file mode 100644 index 00000000000..98be6651cba --- /dev/null +++ b/ghost/promise/lib/pipeline.js @@ -0,0 +1,31 @@ +/** + * # Pipeline Utility + * + * Based on pipeline.js from when.js: + * https://github.com/cujojs/when/blob/3.7.4/pipeline.js + */ +const Promise = require('bluebird'); + +function pipeline(tasks /* initial arguments */) { + const args = Array.prototype.slice.call(arguments, 1); + + let runTask = function (task, args) { + // Self-optimizing function to run first task with multiple + // args using apply, but subsequent tasks via direct invocation + runTask = function (task, arg) { + return task(arg); + }; + + return task.apply(null, args); + }; + + // Resolve any promises for the arguments passed in first + return Promise.all(args).then(function (args) { + // Iterate through the tasks passing args from one into the next + return Promise.reduce(tasks, function (arg, task) { + return runTask(task, arg); + }, args); + }); +} + +module.exports = pipeline; diff --git a/ghost/promise/lib/sequence.js b/ghost/promise/lib/sequence.js new file mode 100644 index 00000000000..c0046120080 --- /dev/null +++ b/ghost/promise/lib/sequence.js @@ -0,0 +1,26 @@ +const Promise = require('bluebird'); + +/** + * expects an array of functions returning a promise + */ +function sequence(tasks /* Any Arguments */) { + const args = Array.prototype.slice.call(arguments, 1); + + return Promise.reduce(tasks, function (results, task) { + const response = task.apply(this, args); + + if (response && response.then) { + return response.then(function (result) { + results.push(result); + return results; + }); + } else { + return Promise.resolve().then(() => { + results.push(response); + return results; + }); + } + }, []); +} + +module.exports = sequence; diff --git a/ghost/promise/test/pipeline_spec.js b/ghost/promise/test/pipeline_spec.js new file mode 100644 index 00000000000..11a72456e16 --- /dev/null +++ b/ghost/promise/test/pipeline_spec.js @@ -0,0 +1,100 @@ +const should = require('should'); +const sinon = require('sinon'); +const Promise = require('bluebird'); + +// Stuff we are testing +const pipeline = require('../../../../core/server/lib/promise/pipeline'); + +// These tests are based on the tests in https://github.com/cujojs/when/blob/3.7.4/test/pipeline-test.js +function createTask(y) { + return function (x) { + return x + y; + }; +} + +describe('Pipeline', function () { + afterEach(function () { + sinon.restore(); + }); + + it('should execute tasks in order', function () { + return pipeline([createTask('b'), createTask('c'), createTask('d')], 'a').then(function (result) { + result.should.eql('abcd'); + }); + }); + + it('should resolve to initial args when no tasks supplied', function () { + return pipeline([], 'a', 'b').then(function (result) { + result.should.eql(['a', 'b']); + }); + }); + + it('should resolve to empty array when no tasks and no args supplied', function () { + return pipeline([]).then(function (result) { + result.should.eql([]); + }); + }); + + it('should pass args to initial task', function () { + const expected = [1, 2, 3]; + const tasks = [sinon.spy()]; + + return pipeline(tasks, 1, 2, 3).then(function () { + tasks[0].calledOnce.should.be.true(); + tasks[0].firstCall.args.should.eql(expected); + }); + }); + + it('should allow initial args to be promises', function () { + const expected = [1, 2, 3]; + const tasks = [sinon.spy()]; + const Resolver = Promise.resolve; + + return pipeline(tasks, new Resolver(1), new Resolver(2), new Resolver(3)).then(function () { + tasks[0].calledOnce.should.be.true(); + tasks[0].firstCall.args.should.eql(expected); + }); + }); + + it('should allow tasks to be promises', function () { + const expected = [1, 2, 3]; + + const tasks = [ + sinon.stub().returns(new Promise.resolve(4)), + sinon.stub().returns(new Promise.resolve(5)), + sinon.stub().returns(new Promise.resolve(6)) + ]; + + return pipeline(tasks, 1, 2, 3).then(function (result) { + result.should.eql(6); + tasks[0].calledOnce.should.be.true(); + tasks[0].firstCall.args.should.eql(expected); + tasks[1].calledOnce.should.be.true(); + tasks[1].firstCall.calledWith(4).should.be.true(); + tasks[2].calledOnce.should.be.true(); + tasks[2].firstCall.calledWith(5).should.be.true(); + }); + }); + + it('should allow tasks and args to be promises', function () { + const expected = [1, 2, 3]; + + const tasks = [ + sinon.stub().returns(new Promise.resolve(4)), + sinon.stub().returns(new Promise.resolve(5)), + sinon.stub().returns(new Promise.resolve(6)) + ]; + + const Resolver = Promise.resolve; + + return pipeline(tasks, new Resolver(1), new Resolver(2), new Resolver(3)).then(function (result) { + result.should.eql(6); + tasks[0].calledOnce.should.be.true(); + tasks[0].firstCall.args.should.eql(expected); + tasks[1].calledOnce.should.be.true(); + tasks[1].firstCall.calledWith(4).should.be.true(); + tasks[2].calledOnce.should.be.true(); + tasks[2].firstCall.calledWith(5).should.be.true(); + }); + }); +}); diff --git a/ghost/promise/test/sequence_spec.js b/ghost/promise/test/sequence_spec.js new file mode 100644 index 00000000000..30d8b5bf63c --- /dev/null +++ b/ghost/promise/test/sequence_spec.js @@ -0,0 +1,28 @@ +const should = require('should'); +const sinon = require('sinon'); +const Promise = require('bluebird'); +const sequence = require('../../../../core/server/lib/promise/sequence'); + +describe('Unit: lib/promise/sequence', function () { + afterEach(function () { + sinon.restore(); + }); + + it('mixed tasks: promise and none promise', function () { + const tasks = [ + function a() { + return Promise.resolve('hello'); + }, + function b() { + return 'from'; + }, + function c() { + return Promise.resolve('chio'); + } + ]; + return sequence(tasks) + .then(function (result) { + result.should.eql(['hello','from', 'chio']); + }); + }); +});