-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(proxy): cleanup proxy handler and base relation
- Loading branch information
1 parent
acb0b66
commit 78251ad
Showing
12 changed files
with
415 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* adonis-lucid | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
module.exports = function proxyGet (proxyOn, bindFn = false, customCallback = null) { | ||
return function (target, name) { | ||
/** | ||
* If value exists on the target object, return it | ||
*/ | ||
if (typeof (target[name]) !== 'undefined') { | ||
return target[name] | ||
} | ||
|
||
/** | ||
* If there is a custom callback to resolve values, call | ||
* the callback and return the value if it's not | ||
* undefined | ||
*/ | ||
if (typeof (customCallback) === 'function') { | ||
const result = customCallback.bind(this)(target, name) | ||
if (typeof (result) !== 'undefined') { | ||
return result | ||
} | ||
} | ||
|
||
/** | ||
* If there is no fallback then return undefined | ||
*/ | ||
if (!proxyOn) { | ||
return undefined | ||
} | ||
|
||
/** | ||
* Get access to source by looking up value on target. Also if | ||
* value is a function, the fn is executed and return value | ||
* is used as source. | ||
*/ | ||
const source = typeof (target[proxyOn]) === 'function' ? target[proxyOn]() : target[proxyOn] | ||
|
||
if (typeof (source[name]) === 'function') { | ||
/** | ||
* If bindFn is set to true, we disconnect the | ||
* proxy trap and instead return the instance | ||
* of source[name] | ||
*/ | ||
if (bindFn) { | ||
return source[name].bind(source) | ||
} | ||
|
||
/** | ||
* Otherwise wrap it inside a closure and return the value. | ||
* The reason we need to wrap it, so that the `proxyOn` | ||
* method should have correct reference to `this`. | ||
*/ | ||
return function (...args) { | ||
source[name](...args) | ||
return this | ||
} | ||
} | ||
|
||
/** | ||
* Finally fallback to `proxyOn` and return the value of property | ||
* and if value does not exists, `undefined` is returned | ||
*/ | ||
return source[name] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
'use strict' | ||
|
||
/* | ||
* adonis-lucid | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.