Skip to content

Instantly share code, notes, and snippets.

@mattoni
Created July 22, 2016 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattoni/7d4232bce5246c969215ae66a7b8c230 to your computer and use it in GitHub Desktop.
Save mattoni/7d4232bce5246c969215ae66a7b8c230 to your computer and use it in GitHub Desktop.
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