Created
May 13, 2015 09:36
-
-
Save mistadikay/27e574ba4007ef606fd4 to your computer and use it in GitHub Desktop.
flux in ES7 by @roman01la
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
| function example(){ | |
| class Actions { | |
| @Observable | |
| static processSomething | |
| } | |
| class Store { | |
| constructor(name){ | |
| this.name = name; | |
| } | |
| @Observable | |
| updater | |
| @Observe(Actions.processSomething) | |
| processHandler = function(data) { | |
| this.updater.notify(this.name + " processed " + data) | |
| } | |
| } | |
| var myStoreA = new Store("Store A"); | |
| var myStoreB = new Store("Store B"); | |
| var print = function(result){ | |
| console.log(result); | |
| } | |
| myStoreA.updater.on(print) | |
| myStoreB.updater.on(print) | |
| Actions.processSomething.notify("1010110") | |
| } | |
| ///All the magic happens down here | |
| class ObservableClass { | |
| subscriptions = []; | |
| on = function(handler){ | |
| this.subscriptions.push(handler) | |
| } | |
| notify = function(){ | |
| var args = arguments; | |
| for(var i = 0 ; i < this.subscriptions.length; i++){ | |
| this.subscriptions[i].apply(null,args) | |
| } | |
| } | |
| } | |
| function Observable(target,key,e) { | |
| e.initializer = function(){ return new ObservableClass(); } | |
| } | |
| function Singleton(target,key,e) { | |
| e.initializer = function(){ return new target(); } | |
| } | |
| function Observe(observable) { | |
| return function(classType,key,d) { | |
| var tempInitializer = d.initializer; | |
| var blah = function() {}; | |
| blah.initializer = function(){ | |
| var val = tempInitializer.apply(arguments); | |
| observable.on(val.bind(this)) | |
| return val; | |
| } | |
| return blah; | |
| } | |
| } | |
| example(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment