Skip to content

Commit

Permalink
Merge pull request #27 from Onheiron/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Onheiron committed Jan 7, 2019
2 parents 8e1886c + 19ca652 commit 2c059c9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 54 deletions.
34 changes: 17 additions & 17 deletions lib/operation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
'use-strict';
const util = require("util");

const handleFunction = (fnc, data, promisify) => {
if (promisify) {
return util.promisify(fnc)(data);
} else {
return handleValue(fnc(data));
}
}

const handleValue = (value) => {
if (value instanceof Promise) return value;
if (value instanceof Error) return Promise.reject(value);
return Promise.resolve(value);
}

/**
* This class represents an atomic operation you want to handle.
* It might consist of a Promise to chain, a function that returns a fixed value or a Promise or a fixed value itself.
Expand All @@ -11,6 +25,7 @@ module.exports = class Operation {
this.value = value;
this.promisify = promisify;
}

/**
* Converts the wrapped value into a Promise.
* If the value is a function returning a Promise, it returns that Promise.
Expand All @@ -21,22 +36,7 @@ module.exports = class Operation {
*/
get(params) {
if (this.value instanceof Operation) return this.value.get(params);
if (this.value instanceof Promise) return this.value;
let result;
if (this.value instanceof Function) {
if (this.promisify) {
return util.promisify(this.value)(params);
} else {
result = this.value(params);
}
} else {
result = this.value;
}
if (result instanceof Promise) {
return result;
} else {
if(result instanceof Error) return Promise.reject(result);
else return Promise.resolve(result);
}
if (this.value instanceof Function) return handleFunction(this.value, params, this.promisify);
else return handleValue(this.value);
}
};
53 changes: 22 additions & 31 deletions lib/promise-mix-mux.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,39 @@ require("./promise-mix-logical");
const baseFunctions = require('./config').base_functions;
const logicalFunctions = require('./config').logical_functions;

const initPool = (inputs) => {
let pool;
if (Array.isArray(inputs)) pool = [];
else if (inputs instanceof Object) pool = {};
else throw TypeError(`PromiseMux input must be an object or an array. Current input type is ${typeof inputs}`);
for (let i in inputs) {
pool[i] = Promise.resolve(inputs[i]);
}
return pool;
}

/**
* This section contains more Promise extensions with methods to handle several Promises simultaneously.
*/
class PromiseMux {

const PromiseMux = function (inputs) {

this.init = function (inputs) {
if (Array.isArray(inputs)) {
this.pool = [];
} else if (inputs instanceof Object) {
this.pool = {};
} else {
throw TypeError(`PromiseMux input must be an object or an array. Current input type is ${typeof inputs}`);
}
for (let i in inputs) {
this.pool[i] = Promise.resolve(inputs[i]);
}
};

if (inputs) this.init(inputs);

const portFunc = (func, key) => {
return val => func(val, key);
};
constructor(inputs) {
this.pool = initPool(inputs);
}

this.then = function (func) {
then(func) {
for (let p in this.pool) {
this.pool[p] = this.pool[p].then(portFunc(func, p));
this.pool[p] = this.pool[p].then(func.bind({ key: p }));
}
return this;
};

this.deMux = function (func) {
deMux(func) {
let output;
if (Array.isArray(inputs)) {
output = Promise.all(this.pool);
} else {
output = Promise.aggregate(this.pool);
}
if (func) {
output = output.then(func);
}
if (Array.isArray(this.pool)) output = Promise.all(this.pool);
else output = Promise.aggregate(this.pool);

if (func) output = output.then(func);
return output;
};
};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "promise-mix",
"version": "1.8.0",
"version": "1.8.1",
"description": "Node module with some useful Promise composition functionalities.",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions test/derivates/mux.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe("Test promise explosion", () => {
(init, done) => done(null, init + 1),
(step1, done) => done(null, step1 * 2),
(step2, done) => done(null, step2 / 3)
], [2, 5, 8]).then((result, key) => {
return `Result is ${result} for key ${key}`;
], [2, 5, 8]).then(function(result) {
return `Result is ${result} for key ${this.key}`;
}).deMux(reports => {
should.exist(reports);
reports.length.should.equal(3);
Expand All @@ -74,8 +74,8 @@ describe("Test promise explosion", () => {
easy: 2,
normal: 5,
hard: 8
}).then((result, key) => {
return `Result is ${result} for ${key}`;
}).then(function(result) {
return `Result is ${result} for ${this.key}`;
}).deMux(reports => {
should.exist(reports);
should.exist(reports.easy);
Expand Down

0 comments on commit 2c059c9

Please sign in to comment.