Last active
August 29, 2015 14:19
-
-
Save hung-phan/10f2acb7386e26d55b10 to your computer and use it in GitHub Desktop.
Channel mixin for js-csp and react
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
'use strict'; | |
import SyntheticEvent from 'react/lib/SyntheticEvent'; | |
import {chan, putAsync} from 'js-csp'; | |
const ChannelMixin = { | |
channels: {}, | |
getEventChannel(channelName, ...args) { | |
return this.channels[channelName] || (this.channels[channelName] = chan(...args)); | |
}, | |
send(channelName, ...args) { | |
if (this[channelName] !== undefined && typeof(this[channelName]) === 'function') { | |
return this[channelName].bind(this, ...args); | |
} else { | |
let ch = this.getEventChannel(channelName, ...args); | |
return (eventArgs) => { | |
if (eventArgs instanceof SyntheticEvent && typeof(eventArgs.persist) === 'function') { | |
eventArgs.persist(); | |
} | |
putAsync(ch, eventArgs); | |
}; | |
} | |
} | |
}; | |
export default ChannelMixin; |
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
import React from 'react/addons'; | |
import reactMixin from 'react-mixin'; | |
import {go, CLOSED} from 'js-csp'; | |
class MyComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
} | |
componentDidMount() { | |
go(function*() { | |
let ch = this.getEventChannel('mouseMove'); | |
let value; | |
while((value = yield ch) !== CLOSED) { | |
console.log(value.clientX, value.clientY); | |
} | |
}.bind(this)); | |
} | |
render() { | |
return ( | |
<div onMouseMove={this.send('mouseMove')} style={{width: '100%', height: '100%'}}></div> | |
); | |
} | |
} | |
reactMixin(MyComponent.prototype, ChannelMixin); | |
export default window.MyComponent = MyComponent; |
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
import React from 'react/addons'; | |
import reactMixin from 'react-mixin'; | |
import {go, CLOSED} from 'js-csp'; | |
class MyComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.moveMove = (value) => { | |
console.log(value.clientX, value.clientY); | |
}; | |
} | |
render() { | |
return ( | |
<div onMouseMove={this.send('mouseMove')} style={{width: '100%', height: '100%'}}></div> | |
); | |
} | |
} | |
reactMixin(MyComponent.prototype, ChannelMixin); | |
export default window.MyComponent = MyComponent; |
https://github.com/facebook/react/blob/master/src/browser/syntheticEvents/SyntheticEvent.js#L40. Synthetic events are pooled. You can use .persist
.
Thanks @ubolonton
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's likely the event object was mutated by React.