This repository has been archived by the owner on Feb 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from buildo/add-autobound
add autobound
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
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,33 @@ | ||
'use strict'; | ||
|
||
exports.__esModule = true; | ||
exports['default'] = autobound; | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
|
||
var _tcomb = require('tcomb'); | ||
|
||
var _tcomb2 = _interopRequireDefault(_tcomb); | ||
|
||
function autobound(target, key, descriptor) { | ||
var value = descriptor.value; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
_tcomb2['default'].assert(_tcomb2['default'].Func.is(value), '@autobound decorator can only be applied to methods'); | ||
} | ||
|
||
return { | ||
configurable: true, | ||
get: function get() { | ||
var boundValue = value.bind(this); | ||
Object.defineProperty(this, key, { | ||
value: boundValue, | ||
configurable: true, | ||
writable: true | ||
}); | ||
return boundValue; | ||
} | ||
}; | ||
} | ||
|
||
module.exports = exports['default']; |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import t from 'tcomb'; | ||
|
||
export default function autobound(target, key, descriptor) { | ||
const { value } = descriptor; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
t.assert(t.Func.is(value), `@autobound decorator can only be applied to methods`); | ||
} | ||
|
||
return { | ||
configurable: true, | ||
get() { | ||
const boundValue = value.bind(this); | ||
Object.defineProperty(this, key, { | ||
value: boundValue, | ||
configurable: true, | ||
writable: true | ||
}); | ||
return boundValue; | ||
} | ||
}; | ||
} | ||
|
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