rjrodger / patrun

A fast pattern matcher on JavaScript object properties.
MIT License
143 stars 19 forks source link

Behaviour when multiple patterns match is undocumented or undefined. #8

Closed AdrianRossouw closed 9 years ago

AdrianRossouw commented 9 years ago

When multiple patterns exactly match the message, it is not clear what order they are executed in.

This is an issue with a library like seneca, where patterns are re-used consistently. The entity system is very prone to this, where you have many things being triggered by saving specific entities.

If all functions were idempotent you could maybe get away with it, but this leads to problems when they are all modifiying some form of shared state.

AdrianRossouw commented 9 years ago

I looked at the patch, and i don't see how it resolves this.

What I would like to know is :

p1.add({a:0},'P')
//  some code happens here
p1.add({a:0},'Q')
// some code happens here
p1.add({a:0},'R')

How does it choose which to return? Does it return only the first, only the last, or an array of all of them. If an array, is it ordered first to last or last to first?

rjrodger commented 9 years ago

just 'R' - matches are unique, so "there can be only one" match for {a:0}

i.e. last wins

AdrianRossouw commented 9 years ago

thanks richard, that's much clearer.