Delving deep into the world of decorators, I stumbled upon some fascinating ideas for incorporating them into my reflux implementation. My concept involves tagging a store's class method with an action, so that whenever that action is triggered, it automatically calls all associated methods.
My first step is to tag the Store method:
@Action
public setData(data: FakeData) {
console.log(this);
this.state.data = data;
}
Next, in the action class, I plan to register that method in an array:
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));
}
}
Unfortunately, within the constructor, I'm unable to access the actual store instance. While I can obtain the constructor, it doesn't seem like I can use that to achieve my intended functionality. Perhaps making all stores strictly static would be a better approach?
It's evident that I still have much to learn about how to effectively utilize these decorators, so any guidance or insights are more than welcome!