From 865ee090c8c23376b018bf0d109912ac20eaafc4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 1 Dec 2017 22:44:06 -0600 Subject: Add class utils --- packages/0x.js/src/utils/class_utils.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/0x.js/src/utils/class_utils.ts (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/class_utils.ts b/packages/0x.js/src/utils/class_utils.ts new file mode 100644 index 000000000..de7e99cd9 --- /dev/null +++ b/packages/0x.js/src/utils/class_utils.ts @@ -0,0 +1,17 @@ +import * as _ from 'lodash'; + +export const classUtils = { + bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { + for (const key of Object.getOwnPropertyNames(self)) { + const val = self[key]; + if (!_.includes(exclude, key)) { + if (_.isFunction(val)) { + self[key] = val.bind(thisArg || self); + } else if (_.isObject(val)) { + classUtils.bindAll(val, exclude, self); + } + } + } + return self; + }, +}; -- cgit v1.2.3 From 9485e4f4f38133141c32af3545a33c267222b851 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 1 Dec 2017 22:44:18 -0600 Subject: Add promisify --- packages/0x.js/src/utils/promisify.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 packages/0x.js/src/utils/promisify.ts (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/promisify.ts b/packages/0x.js/src/utils/promisify.ts new file mode 100644 index 000000000..8021a3c9f --- /dev/null +++ b/packages/0x.js/src/utils/promisify.ts @@ -0,0 +1,24 @@ +import * as _ from 'lodash'; + +/** + * Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into + * an ES6-compatible Promise. Promisify provides a default callback of the form (error, result) + * and rejects when `error` is not null. You can also supply settings object as the second argument. + */ +export function promisify( + originalFn: ( + ...args: any[], + // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453 + ) => void, + target: any, +): (...callArgs: any[]) => Promise { + const promisifiedFunction = (...callArgs: any[]): Promise => { + return new Promise((resolve, reject) => { + const callback = (err: Error|null, data?: T) => { + _.isNull(err) ? resolve(data) : reject(err); + }; + originalFn.apply(target, [...callArgs, callback]); + }); + }; + return promisifiedFunction; +} -- cgit v1.2.3 From 042caa3363b15ed298486afe4236d665fc254065 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 1 Dec 2017 22:55:36 -0600 Subject: Add async prefix --- packages/0x.js/src/utils/promisify.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/promisify.ts b/packages/0x.js/src/utils/promisify.ts index 8021a3c9f..1eb1bf29f 100644 --- a/packages/0x.js/src/utils/promisify.ts +++ b/packages/0x.js/src/utils/promisify.ts @@ -12,7 +12,7 @@ export function promisify( ) => void, target: any, ): (...callArgs: any[]) => Promise { - const promisifiedFunction = (...callArgs: any[]): Promise => { + const promisifiedFunction = async (...callArgs: any[]): Promise => { return new Promise((resolve, reject) => { const callback = (err: Error|null, data?: T) => { _.isNull(err) ? resolve(data) : reject(err); -- cgit v1.2.3 From 5673b42ec47342a1e4be2dd77a5676407a60e401 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 1 Dec 2017 23:11:55 -0600 Subject: Make target optional --- packages/0x.js/src/utils/promisify.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/promisify.ts b/packages/0x.js/src/utils/promisify.ts index 1eb1bf29f..49024c0e9 100644 --- a/packages/0x.js/src/utils/promisify.ts +++ b/packages/0x.js/src/utils/promisify.ts @@ -10,7 +10,7 @@ export function promisify( ...args: any[], // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453 ) => void, - target: any, + target?: any, ): (...callArgs: any[]) => Promise { const promisifiedFunction = async (...callArgs: any[]): Promise => { return new Promise((resolve, reject) => { -- cgit v1.2.3 From c0cf47138ec2efe4c0a27de9a503bb0d74d99d5f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 5 Dec 2017 16:59:36 +0300 Subject: Add a comment --- packages/0x.js/src/utils/class_utils.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/class_utils.ts b/packages/0x.js/src/utils/class_utils.ts index de7e99cd9..b89d3ba32 100644 --- a/packages/0x.js/src/utils/class_utils.ts +++ b/packages/0x.js/src/utils/class_utils.ts @@ -1,6 +1,7 @@ import * as _ from 'lodash'; export const classUtils = { + // This is usefull for classes that have nested methods. Nested methods don't get binded out of the box. bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { for (const key of Object.getOwnPropertyNames(self)) { const val = self[key]; -- cgit v1.2.3 From f88ecaa035352a511fbee1ff1846cdacd03ca169 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 5 Dec 2017 17:01:24 +0300 Subject: Fix a comment --- packages/0x.js/src/utils/promisify.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/promisify.ts b/packages/0x.js/src/utils/promisify.ts index 49024c0e9..c114cf32f 100644 --- a/packages/0x.js/src/utils/promisify.ts +++ b/packages/0x.js/src/utils/promisify.ts @@ -1,23 +1,23 @@ import * as _ from 'lodash'; /** - * Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into - * an ES6-compatible Promise. Promisify provides a default callback of the form (error, result) - * and rejects when `error` is not null. You can also supply settings object as the second argument. + * Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into an ES6-compatible Promise. + * Promisify provides a default callback of the form (error, result) and rejects when `error` is not null. You can also + * supply thisArg object as the second argument which will be passed to `apply`. */ export function promisify( originalFn: ( ...args: any[], // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453 ) => void, - target?: any, + thisArg?: any, ): (...callArgs: any[]) => Promise { const promisifiedFunction = async (...callArgs: any[]): Promise => { return new Promise((resolve, reject) => { const callback = (err: Error|null, data?: T) => { _.isNull(err) ? resolve(data) : reject(err); }; - originalFn.apply(target, [...callArgs, callback]); + originalFn.apply(thisArg, [...callArgs, callback]); }); }; return promisifiedFunction; -- cgit v1.2.3 From c64ec92fb23fd130d0c54a4d42147bb468e434d9 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 5 Dec 2017 22:02:27 +0300 Subject: Address feedback --- packages/0x.js/src/utils/class_utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/0x.js/src/utils') diff --git a/packages/0x.js/src/utils/class_utils.ts b/packages/0x.js/src/utils/class_utils.ts index b89d3ba32..04e60ee57 100644 --- a/packages/0x.js/src/utils/class_utils.ts +++ b/packages/0x.js/src/utils/class_utils.ts @@ -1,7 +1,7 @@ import * as _ from 'lodash'; export const classUtils = { - // This is usefull for classes that have nested methods. Nested methods don't get binded out of the box. + // This is useful for classes that have nested methods. Nested methods don't get bound out of the box. bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { for (const key of Object.getOwnPropertyNames(self)) { const val = self[key]; -- cgit v1.2.3