Question: How to get a list of all Reflux actions names and a preEmit hook for all of them?

Question

How to get a list of all Reflux actions names and a preEmit hook for all of them?

Answers 1
Added at 2016-12-09 19:12
Tags
Question

I know using Reflux.__keep.createdActions I get a list of all actions created. Is there a way to know the name of these actions?

Is there a way to define a preEmit hook for all actions?

Answers to

How to get a list of all Reflux actions names and a preEmit hook for all of them?

nr: #1 dodano: 2016-12-09 20:12

Important Note: Reflux.__keep was actually originally created to support another feature that never materialized. However it was also creating memory leaks in some programs. Therefore it was recently made to NOT store anything by default. To make it store anything you have to use Reflux.__keep.useKeep() in latest versions of reflux and reflux-core. Reflux.__keep is not a documented part of the API, and as such changes to it do not necessarily follow semantic versioning. From v5.0.2 of Reflux onward the useKeep() is needed for Reflux.__keep to store anything.

On to the question though:


1) In Reflux.__keep there is a createdActions property, which is an Array holding all created actions so far (if you did the useKeep() thing, of course). Every action should have on it an actionName property telling you that action's name which you supplied when you created it:

Reflux.__keep.useKeep()

Reflux.createActions(['firstAction', 'secondAction']);

console.log(Reflux.__keep.createdActions[0].actionName) // <-- firstAction
console.log(Reflux.__keep.createdActions[1].actionName) // <-- secondAction

2) preEmit hooks can be assigned to actions after-the-fact, so assigning them to actions within Reflux.__keep.createdActions would be a simple matter of a loop:

Reflux.__keep.useKeep()

var Actions = Reflux.createActions(['firstAction', 'secondAction']);

var total = Reflux.__keep.createdActions.length;

for (var i=0; i<total; i++) {
    Reflux.__keep.createdActions[i].preEmit = function(arg) { console.log(arg); };
}

Actions.firstAction('Hello');   // <- preEmit outputs "Hello"
Actions.secondAction('World!'); // <- preEmit outputs "World!"
Source Show
◀ Wstecz