Created
July 22, 2016 05:34
-
-
Save mattoni/7d4232bce5246c969215ae66a7b8c230 to your computer and use it in GitHub Desktop.
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
interface Method<T> { | |
(payload: T): void; | |
} | |
interface FakeData { | |
name: string; | |
} | |
class Action { | |
private static methods: Method<FakeData>[] = []; | |
public static register(method: Method<FakeData>) { | |
this.methods.push(method); | |
} | |
constructor(payload: FakeData); | |
constructor(store: Store, method: string); | |
constructor(payload: any, method?: string, descriptor?: PropertyDescriptor) { | |
if (method) { | |
//TODO need actual instance of class here.... | |
Action.register(payload[method].bind(payload)); | |
return; | |
} | |
this.trigger(payload); | |
} | |
public get payload() { | |
return Math.random(); | |
} | |
private trigger(payload: FakeData) { | |
Action.methods.forEach(m => m(payload)); | |
} | |
} | |
class Store { | |
private static state = { | |
data: <FakeData>undefined | |
}; | |
public static get data() { | |
return this.state.data; | |
} | |
@Action | |
public static setData(data: FakeData) { | |
console.log(this); | |
this.state.data = data; | |
} | |
} | |
console.log("Store before action: ", Store.data); | |
new Action({name: "TEST"}); | |
console.log("Store after action: ", Store.data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment