Skip to content

Commit 96f979b

Browse files
refactor(bindFunctions): Use very late binding for bindFunctions -- switch to createProxyFunctions
- Allows `urlService.*` to delegate to `router.locationServices` once it's available
1 parent 315452b commit 96f979b

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/common/common.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ export interface Obj extends Object {
3333
}
3434

3535
/**
36-
* Binds and copies functions onto an object
36+
* Builds proxy functions on the `to` object which pass through to the `from` object.
3737
*
38-
* Takes functions from the 'from' object, binds those functions to the _this object, and puts the bound functions
39-
* on the 'to' object.
38+
* For each key in `fnNames`, creates a proxy function on the `to` object.
39+
* The proxy function calls the real function on the `from` object.
4040
*
41-
* This example creates an new class instance whose functions are prebound to the new'd object.
42-
* @example
43-
* ```
4441
*
42+
* #### Example:
43+
* This example creates an new class instance whose functions are prebound to the new'd object.
44+
* ```js
4545
* class Foo {
4646
* constructor(data) {
4747
* // Binds all functions from Foo.prototype to 'this',
@@ -60,8 +60,8 @@ export interface Obj extends Object {
6060
* logit(); // logs [1, 2, 3] from the myFoo 'this' instance
6161
* ```
6262
*
63+
* #### Example:
6364
* This example creates a bound version of a service function, and copies it to another object
64-
* @example
6565
* ```
6666
*
6767
* var SomeService = {
@@ -82,15 +82,18 @@ export interface Obj extends Object {
8282
* myOtherThing.log(); // logs [3, 4, 5] from SomeService's 'this'
8383
* ```
8484
*
85-
* @param from The object which contains the functions to be bound
85+
* @param from The object (or a function that returns the from object) which contains the functions to be bound
8686
* @param to The object which will receive the bound functions
87-
* @param bindTo The object which the functions will be bound to
87+
* @param bind The object (or a function that returns the object) which the functions will be bound to
8888
* @param fnNames The function names which will be bound (Defaults to all the functions found on the 'from' object)
8989
*/
90-
export function bindFunctions(from: Obj, to: Obj, bindTo: Obj, fnNames: string[] = Object.keys(from)): Obj {
91-
fnNames.filter(name => typeof from[name] === 'function')
92-
.forEach(name => to[name] = from[name].bind(bindTo));
93-
return to;
90+
export function createProxyFunctions(from: Obj|Function, to: Obj, bind: Obj|Function, fnNames: string[] = Object.keys(from)): Obj {
91+
const _from = isFunction(from) ? from : () => from;
92+
const _bind = isFunction(bind) ? bind : () => bind;
93+
const makePassthrough = fnName => function proxyFnCall() {
94+
return _from()[fnName].apply(_bind(), arguments);
95+
};
96+
return fnNames.reduce((acc, name) => (acc[name] = makePassthrough(name), acc), to);
9497
}
9598

9699

0 commit comments

Comments
 (0)