Skip to content

Commit

Permalink
perf: prevent unnecessary duplication of args
Browse files Browse the repository at this point in the history
  • Loading branch information
yisraelx committed Jul 3, 2018
1 parent fc28328 commit bf1063a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
15 changes: 11 additions & 4 deletions modules/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ try {

export namespace Promises {
export function _setOnConstructor(key: string, fn: Function, wrap: boolean = true): void {
this[key] = this[key] || wrap ? function (...args) {
let result = fn(...args);
this[key] = this[key] || wrap ? function () {
let result = fn.apply(this, arguments);
return this.resolve(result);
} : fn;
}

export function _setOnPrototype(key: string, fn: Function, wrap: boolean = true): void {
this.prototype[key] = this.prototype[key] || wrap ? function (...args) {
let result = fn(this, ...args);
this.prototype[key] = this.prototype[key] || wrap ? function () {
let {length} = arguments;
let args = Array(length + 1);
args[0] = this;
for (let i = 1; i <= length; i++) {
args[i] = arguments[i - 1];
}
let result = fn.apply(this, args);
return this.constructor.resolve(result);
} : fn;
}
Expand All @@ -42,6 +48,7 @@ export namespace Promises {
*/
export interface Promises<T> {
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promises<TResult1 | TResult2>;

catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promises<T | TResult>;
}

Expand Down
5 changes: 3 additions & 2 deletions modules/promisify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ function promisify<P1, P2, P3, P4, P5, P6, P7, R>(fn: (p1: P1, p2: P2, p3: P3, p
function promisify<P1, P2, P3, P4, P5, P6, P7, P8, R>(fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, cb: (error: any, result: R) => void) => void, options?: { multi?: false, context?: any }): (p1: IOptionalPromise<P1>, p2: IOptionalPromise<P2>, p3: IOptionalPromise<P3>, p4: IOptionalPromise<P4>, p5: IOptionalPromise<P5>, p6: IOptionalPromise<P6>, p7: IOptionalPromise<P7>, p8: IOptionalPromise<P8>) => Promise<R>;
function promisify<P1, P2, P3, P4, P5, P6, P7, P8, P9, R>(fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, cb: (error: any, result: R) => void) => void, options?: { multi?: false, context?: any }): (p1: IOptionalPromise<P1>, p2: IOptionalPromise<P2>, p3: IOptionalPromise<P3>, p4: IOptionalPromise<P4>, p5: IOptionalPromise<P5>, p6: IOptionalPromise<P6>, p7: IOptionalPromise<P7>, p8: IOptionalPromise<P8>, p9: IOptionalPromise<P9>) => Promise<R>;
function promisify<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, R>(fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, cb: (error: any, result: R) => void) => void, options?: { multi?: false, context?: any }): (p1: IOptionalPromise<P1>, p2: IOptionalPromise<P2>, p3: IOptionalPromise<P3>, p4: IOptionalPromise<P4>, p5: IOptionalPromise<P5>, p6: IOptionalPromise<P6>, p7: IOptionalPromise<P7>, p8: IOptionalPromise<P8>, p9: IOptionalPromise<P9>, p10: IOptionalPromise<P10>) => Promise<R>;
function promisify(fn, {multi, context}: IPromisifyOptions = {}) {
function promisify(fn, { multi, context }: IPromisifyOptions = {}) {
return function (...args) {
return Promise.all(args).then((args) => {
return new Promise((resolve, reject) => {
fn.call(context || this, ...args, (error, ...result) => {
args.push((error, ...result) => {
if (error) return reject(error);
if (result && !multi) result = result[0];
resolve(result);
});
fn.apply(context || this, args);
});
});
};
Expand Down
2 changes: 1 addition & 1 deletion modules/spread/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function spread(array, fn) {
return (Promise as any).resolve(array).then((array) => {
return Array.isArray(array) ? Promise.all(array) : Object(array);
}).then((args) => {
return fn(...args);
return fn.apply(null, args);
});
}

Expand Down

0 comments on commit bf1063a

Please sign in to comment.