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

Feature: Custom evaluateGuard function #249

Closed
webstacker opened this issue Nov 22, 2018 · 2 comments
Closed

Feature: Custom evaluateGuard function #249

webstacker opened this issue Nov 22, 2018 · 2 comments

Comments

@webstacker
Copy link

(Feature) Potential implementation:

I'm working on a module that allows rules to be serialized as JSON. It would be nice to do something like this so that I can use it to evaluate the conditions:

const myMachine = Machine(
    {
        context: {
            foo: {
                bar: ['baz']
            }
        },
        initial: 'a',
        states: {
            a: {
                on: {
                    FOO: {
                        target: 'b',
                        // only transition to 'b' if ctx.foo.bar[0] === 'baz'
                        cond: ["==", "$.foo.bar.0", "baz"]
                    }
                }
            },
            b: {}
        }
    },  
    {
        evaluateGuard: (cond, ctx, eventObject, interimState) => {
            // cond: ["==", "$.foo.bar.0", "baz"]
            return jr.evaluate(cond, ctx);
        }
    }
);

myMachine.transition('a', 'FOO').value; // b
myMachine.transition('a', 'FOO', {foo:{bar:['bag']}}).value; // a

Is this something that would be considered? Thanks

@davidkpiano
Copy link
Member

For sure - the lowest-common denominator is using a string-as-condition and mapping it to a real transition.

Some more discussion about this in #57, but I think that it would be reasonable to allow any JSON-serializable value, not just strings, in cond: ....

@davidkpiano
Copy link
Member

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