aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/utils
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-12-06 06:18:36 +0800
committerFabio Berger <me@fabioberger.com>2017-12-06 06:18:36 +0800
commit08168c6e7d52711aeb46e27444ba26970e16e244 (patch)
tree40a006de279221009d0ee05d73bfbb74f0a9ea91 /packages/0x.js/src/utils
parentb5030df4e3afe17b4e652b438d655edda79c5f54 (diff)
parent4441d76725af4e83f90eeb373983b600b6903e8e (diff)
downloaddexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar
dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.gz
dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.bz2
dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.lz
dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.xz
dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.zst
dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.zip
Merge branch 'development' into feature/addSubproviders
* development: (50 commits) Add PR number to changelog Address feedback Add requestId to subscription messages and update json-schemas Remove isomorphic-fetch types from contracts package Update README Regenerate files Make it private Change package name Update README Make fileExtension configurable Rename abi-gen to typed-contracts Add docs for typed-contracts Remove TODOs Introduce separate ContextData type and rework it Check ABI is defined Introduce a const for 'contract.mustache' Improve error message Reuse util Fix a typo Introduce a const for 'function' ... # Conflicts: # yarn.lock
Diffstat (limited to 'packages/0x.js/src/utils')
-rw-r--r--packages/0x.js/src/utils/class_utils.ts18
-rw-r--r--packages/0x.js/src/utils/promisify.ts24
2 files changed, 42 insertions, 0 deletions
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..04e60ee57
--- /dev/null
+++ b/packages/0x.js/src/utils/class_utils.ts
@@ -0,0 +1,18 @@
+import * as _ from 'lodash';
+
+export const classUtils = {
+ // 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];
+ if (!_.includes(exclude, key)) {
+ if (_.isFunction(val)) {
+ self[key] = val.bind(thisArg || self);
+ } else if (_.isObject(val)) {
+ classUtils.bindAll(val, exclude, self);
+ }
+ }
+ }
+ return self;
+ },
+};
diff --git a/packages/0x.js/src/utils/promisify.ts b/packages/0x.js/src/utils/promisify.ts
new file mode 100644
index 000000000..c114cf32f
--- /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 thisArg object as the second argument which will be passed to `apply`.
+ */
+export function promisify<T>(
+ originalFn: (
+ ...args: any[],
+ // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453
+ ) => void,
+ thisArg?: any,
+): (...callArgs: any[]) => Promise<T> {
+ const promisifiedFunction = async (...callArgs: any[]): Promise<T> => {
+ return new Promise<T>((resolve, reject) => {
+ const callback = (err: Error|null, data?: T) => {
+ _.isNull(err) ? resolve(data) : reject(err);
+ };
+ originalFn.apply(thisArg, [...callArgs, callback]);
+ });
+ };
+ return promisifiedFunction;
+}