Created
June 14, 2015 16:42
-
-
Save valff/c6f82fe817dbf6f856e8 to your computer and use it in GitHub Desktop.
React.Component Mixin
This file contains hidden or 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
createMergedResultFunction = (f, g) -> | |
-> | |
[a, b] = [f.apply(this, arguments), g.apply(this, arguments)] | |
return b unless a? | |
return a unless b? | |
Object.assign({}, a, b) | |
createChainedFunction = (f, g) -> | |
-> | |
f.apply(this, arguments) | |
g.apply(this, arguments) | |
return | |
@Mixin = (mixin) -> | |
for own key, value of mixin | |
switch key | |
when 'mixins' | |
@mixin object for object in value | |
when 'statics' | |
@[name] = property for own name, property of value | |
when 'displayName' | |
@[key] = value | |
when 'childContextTypes', 'contextTypes', 'propTypes' | |
@[key] = Object.assign({}, @[key], value) | |
when 'getDefaultProps' | |
if @[key] | |
@[key] = createMergedResultFunction(@[key], value) | |
else | |
@[key] = value | |
else | |
if @::[key] | |
switch key | |
when 'getInitialState', 'getChildContext' | |
@::[key] = createMergedResultFunction(@::[key], value) | |
when 'componentWillMount', 'componentDidMount', \ | |
'componentWillUpdate', 'componentDidUpdate', \ | |
'componentWillUnmount' | |
@::[key] = createChainedFunction(@::[key], value) | |
else | |
@::[key] = value | |
this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment