Last active
August 29, 2015 14:17
-
-
Save pinyin/cc240c148229df99f724 to your computer and use it in GitHub Desktop.
micro-framework for reactive react with actors
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
# actions handlers are actors running in dedicated web workers | |
# send_event is their only way to return result | |
emit = (params...) -> this.postMessage params | |
handler = (func)-> | |
this.onmessage = (e)-> | |
try | |
func.apply this, e.data | |
finally | |
close() | |
module.exports = {handler, emit} |
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
channel = require 'pubsub-js' | |
store = require 'framework/store' | |
handler = (evt_name, func) -> | |
channel.subscribe "event.#{evt_name}", (ignored, params)-> | |
try | |
func.apply store, params | |
finally | |
store.commit() | |
module.exports = {handler, store} |
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
# Demo | |
# commands/sign_up.coffee | |
# action handler | |
# can use synchronized IO | |
{handler, emit} = require 'framework/command' | |
handler (username, password)-> | |
emit 'signing_up', username, password | |
# events/signing_up.coffee | |
# changes store | |
{handler, store} = require 'framework/event' | |
handler 'signing_up', (username, password)-> | |
console.log 'signing_up', username, password | |
# sign_in.cjsx | |
# call @start() to emit actions | |
# actions runs in other threads | |
View = require 'framework/view' | |
module.exports = View | |
render: -> | |
<button onClick={=> @start 'sign_up', 'bob', 'alice'}/> |
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
React = require 'react' | |
channel = require 'pubsub-js' | |
store = require 'framework/store' | |
emit = require 'commands/emit' | |
# A React component mixin | |
react_mixin = | |
mixins: [store.mixin] | |
start: (cmd_name, params...)-> | |
worker = require.context('..')("./commands/#{cmd_name}") | |
handler = new worker | |
handler.onmessage = (e) -> | |
evt_name = e.data[0] | |
params = e.data[1..-1] | |
channel.publish "event.#{evt_name}", params | |
handler.postMessage(params) | |
emit: (params...)-> # shortcut | |
start emit, params | |
read: (query, params...)-> | |
query params | |
module.exports = (obj)-> | |
obj.mixins = [react_mixin].add obj.mixins | |
React.createClass obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment