Skip to content

Commit

Permalink
Merge pull request #5 from withmandala/dev
Browse files Browse the repository at this point in the history
Modify source to comply with js standard
  • Loading branch information
Fadhli Dzil Ikram committed Nov 20, 2016
2 parents 89e355d + a9a8086 commit 8987751
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 252 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* Copyright (c) 2016 Fadhli Dzil Ikram
*/

module.exports = require('./lib/setlist');
module.exports = require('./lib/setlist')
110 changes: 55 additions & 55 deletions lib/setlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,173 +4,173 @@
* Copyright (c) 2016 Fadhli Dzil Ikram
*/

'use strict';
'use strict'

const lo = require('lodash');
const lo = require('lodash')

// Export modules
module.exports = run;
run.callbackify = callbackify;
run.promisify = promisify;
run.proxify = proxify;
run.isPromise = isPromise;
run.isGenerator = isGenerator;
run.isGeneratorFunction = isGeneratorFunction;
module.exports = run
run.callbackify = callbackify
run.promisify = promisify
run.proxify = proxify
run.isPromise = isPromise
run.isGenerator = isGenerator
run.isGeneratorFunction = isGeneratorFunction

// Main generator function
function run(fn) {
// Return directly if it is promise
if (isPromise(fn)) return fn;
if (isPromise(fn)) return fn
// Initialize generator function on GenFn input
else if (isGeneratorFunction(fn)) fn = fn();
else if (isGeneratorFunction(fn)) fn = fn()
// Throw unknown input
else if (!isGenerator(fn)) {
throw new Error('Unknown generator run input');
throw new Error('Unknown generator run input')
}

// Create new promise generator runner
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
// Run generator function
exec();
exec()

// Generator run function
function exec(val) {
try {
next(fn.next(val));
} catch(err) {
reject(err);
next(fn.next(val))
} catch (err) {
reject(err)
}
}

// Generator error function
function error(val) {
try {
next(fn.throw(val));
} catch(err) {
reject(err);
next(fn.throw(val))
} catch (err) {
reject(err)
}
}

// Generator next function
function next(r) {
let cont;
let fail;
let cont
let fail
if (r.done) {
cont = resolve;
fail = reject;
cont = resolve
fail = reject
} else {
cont = exec;
fail = error;
cont = exec
fail = error
}

if (isPromise(r.value)) {
r.value.then(cont).catch(fail);
r.value.then(cont).catch (fail)
} else if (isGenerator(r.value)) {
run(r.value).then(cont).catch(fail);
run(r.value).then(cont).catch (fail)
} else {
if (r.done) {
resolve(r.value);
resolve(r.value)
} else {
process.nextTick(() => exec(r.value));
process.nextTick(() => exec(r.value))
}
}
}
});
})
}

// Wrap generator function with async-with-callback style function
function callbackify(fn) {
// Check if the input was not generator function
if (!isGeneratorFunction(fn)) {
throw new Error('Callbackify input is not a Generator Function');
throw new Error('Callbackify input is not a Generator Function')
}

return function callbackifier() {
// Get arguments from callback handler (without callback function)
let args = Array.prototype.slice.call(arguments, 0, -1);
let args = Array.prototype.slice.call(arguments, 0, -1)
// Get callback function itself
let callback = arguments[arguments.length - 1];
let callback = arguments[arguments.length - 1]
// Run the generator function with o-generator
run(fn.apply(this, args))
// Then handler (result return)
.then((result) => callback(null, result))
// Catch handler (error return)
.catch((error) => callback(error, null));
.catch ((error) => callback(error, null))
}
}

// Wrap async-with-callback with function that returns promise
function promisify(fn) {
// Check if the input is not function
if (!lo.isFunction(fn)) {
throw new Error('Promisify input is not a Function');
throw new Error('Promisify input is not a Function')
}
// Return with new proxy function
return function promisifier() {
let self = this;
let self = this
// Get arguments from proxy
let args = Array.from(arguments);
let args = Array.from(arguments)
// Return promise to user
return new Promise((resolve, reject) => {
// Push callback handler as the last argument
args.push(callback);
args.push(callback)
// Run the callback function
fn.apply(self, args);
fn.apply(self, args)
// Define callback handler
function callback(err, r) {
// Get return value as Array
let rArray = Array.prototype.slice.call(arguments, 1);
let rArray = Array.prototype.slice.call(arguments, 1)
// Check error status
if (err) {
reject(err);
reject(err)
} else {
if (rArray.length > 1) {
// Resolve callback resolve as Array
resolve(rArray);
resolve(rArray)
} else {
resolve(r);
resolve(r)
}
}
}
});
})
}
}

// Wrap class and prototype method that contains generator function with
// promise proxy so it can be used without explicit usage of generator runner
function proxify(obj) {
if (!lo.isObject(obj)) {
throw new Error('Proxify input is not object');
throw new Error('Proxify input is not object')
}
// Proxify prototype if available
if ('prototype' in obj) proxify(obj.prototype);
if ('prototype' in obj) proxify(obj.prototype)
// Get all own property member of the object
let properties = Object.getOwnPropertyNames(obj);
let properties = Object.getOwnPropertyNames(obj)
for (let property of properties) {
// Skip constructor function
if (property === 'constructor') continue;
if (property === 'constructor') continue
// Skip if the property is not generator function
if (!isGeneratorFunction(obj[property])) continue;
if (!isGeneratorFunction(obj[property])) continue
// Store current function
let srcFunction = obj[property];
let srcFunction = obj[property]
// Inject object/class with proxified generator function
obj[property] = function proxifier() {
return run(srcFunction.apply(this, arguments));
return run(srcFunction.apply(this, arguments))
}
}
}

function isPromise(fn) {
return (lo.isObject(fn) && lo.isFunction(fn.then) &&
lo.isFunction(fn.catch));
lo.isFunction(fn.catch))
}

function isGenerator(fn) {
return (lo.isObject(fn) && lo.isFunction(fn.next) &&
lo.isFunction(fn.throw));
lo.isFunction(fn.throw))
}

function isGeneratorFunction(fn) {
return (lo.isFunction(fn) && lo.isObject(fn.constructor) &&
fn.constructor.name === 'GeneratorFunction');
fn.constructor.name === 'GeneratorFunction')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setlist",
"version": "0.2.1",
"version": "0.2.2",
"description": "Sequential-ish your asynchronous code with ES6 Generator Function and Promise",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 8987751

Please sign in to comment.