Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback for map/reduce capability on actions #58

Closed
spoike opened this issue Sep 5, 2014 · 3 comments
Closed

Add callback for map/reduce capability on actions #58

spoike opened this issue Sep 5, 2014 · 3 comments
Milestone

Comments

@spoike
Copy link
Member

spoike commented Sep 5, 2014

From discussion in #57 it seems we may need to create actions together with a callback that works like the preEmit.

Add function as argument in createAction so you can do this:

var myAction = Reflux.createAction(function(arg) {
    console.log(arg);
    return arg + 1;
});

myAction.listen(function(arg) { console.log(arg); })

myAction("test");
// will output:
// test
// test1

Internally we could just pass the callback to the preEmit hook. Also the action needs to pass the return value preEmit does. By default, if preEmit does not return anything (i.e. returns undefined), the arguments are kept as is.

Creating multiple actions is done through an options object:

var Actions = Reflux.createActions({
    action1: true, // or any truthy value
    action2: function(arg) {
        console.log(arg);
        return arg + 1;
    }
});

Actions.action1(); // no preEmit hook, just a default action
Actions.action2("test"); // outputs test, passes on "test1"
@andreypopp
Copy link

my 2 cents here: I think actions is the place where additional validations might be useful, so what do you think if when creating actions developer can specify arg type validation, similar to propTypes in React.

@spoike
Copy link
Member Author

spoike commented Sep 5, 2014

@andreypopp I wonder if it is just easier to do the function approach anyway... and add a validator composer. E.g. like this:

// composing validator
var validatorFunc = Reflux.createValidator(
    Reflux.validators.isNumberIntervalOf(0, 100), 
    Reflux.validators.isNot(1));
// attach validator to action
var action = Reflux.createAction(validatorFunc);

Alternatively put that into the shouldEmit hook instead.

// attach validator to action
var action = Reflux.createAction();
action.shouldEmit = validatorFunc;

Also validators may need to "throw an error" by invoking relevant action(s) for an error handling flow. Probably add that to the validator as parameter:

// ErrorAction(s) defined earlier
var validatorFunc = Reflux.createValidator(
    Reflux.validators.isNot(-1, ErrorAction.wrongNumberOnTextField)
);

Does this make sense to you all?

@spoike
Copy link
Member Author

spoike commented Sep 23, 2014

Krawaller has implemented the passthrough on preEmit in #78 which is included in 0.1.8.

Closing this issue now, since I'm pretty sure you can use 3rd party argument matchers in javascript. So I'm dropping the idea for now until someone comes with a really good idea for validator implementation.

@spoike spoike closed this as completed Sep 23, 2014
@spoike spoike added this to the 0.1.8 milestone Sep 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants