Skip to content

Commit 3981024

Browse files
author
Ruben
committed
Allow an array of matches in the pattern matching rules
This allows setting multiple matches (a combination of subject, predicate and object) for one definition. The same can be achieved by creating multiple definitions with the same callback/options and a different single match pattern. Using an object (instead of array) for a `match` is still allowed: this is backwards compatible. This also changes two aspects of how deltas are sent through: When a delta matches multiple patterns (matches), it will send the same change sets multiple times to the same url, for every match that is in the rules. When using an array for `match`es, the change set will only be sent once. When using in conjunction with `sendMatchesOnly`, all patterns matching those defined in `match`es will be sent in one change set together. When not using an array for `match`es, multiple change sets with "one type of match" each would be sent to the url.
1 parent d77e06d commit 3981024

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default [
5858

5959
The exported property contains an array of definitions, each linking a match to a callback.
6060

61-
- `match`: Pattern to match against. Any supplied key must match, anything unspecified is ignored.
61+
- `match`: Pattern to match against. Any supplied key must match, anything unspecified is ignored. Can pass multiple patterns as a list, which will be combined as an "or" (one valid pattern results in a match).
6262
- `match.subject`: Matches the subject. Both `type` and `value` may be specified.
6363
- `match.predicate`: Matches the predicade. Both `type` and `value` may be specified.
6464
- `match.object`: Matches the object. Both `type` and `value` may be specified.
@@ -72,7 +72,7 @@ The exported property contains an array of definitions, each linking a match to
7272
- `options.ignoreFromSelf`: Don't inform about changes that originated from the microservice to be informed (based on the hostname).
7373
- `options.retry`: (experimental) How many times the request is sent again on failure. Defaults to 0. Warning: in case of retries, deltas may be received out of order!
7474
- `options.retryTimeout`: (experimental) How much time is left in between retries (in ms). Currently defaults to 250ms.
75-
- `options.sendMatchesOnly`: Only send triples that match, removing the other triples from the changes.
75+
- `options.sendMatchesOnly`: Only send triples that match the pattern(s), removing the other triples from the changes.
7676

7777
### Modifying quads
7878
#### Normalize datetime

matching.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { DEBUG_TRIPLE_MATCHES_SPEC } from './env';
66
* @param {Array<Object>} changeSets - An array of change set objects,
77
* each containing `insert` and `delete` properties.
88
* @param {Object} entry - An object containing the matching criteria.
9-
* @param {Array} entry.match - The pattern used to filter the triples
9+
* @param {Array} entry.match - The pattern(s) used to filter the triples
1010
* in the `insert` and `delete` arrays.
1111
* @returns {Array<Object>} A new array of change set objects with
1212
* filtered `insert` and `delete` properties.
@@ -27,14 +27,20 @@ export function filterChangesetsOnPattern(changeSets, entry) {
2727
return filteredChangesets;
2828
}
2929

30+
3031
export function tripleMatchesSpec( triple, matchSpec ) {
31-
// form of triple is {s, p, o}, same as matchSpec
32+
const matches = Array.isArray(matchSpec) ? matchSpec : [matchSpec];
3233
if(DEBUG_TRIPLE_MATCHES_SPEC)
3334
console.log(`Does ${JSON.stringify(triple)} match ${JSON.stringify(matchSpec)}?`);
3435

35-
for( let key in matchSpec ){
36-
// key is one of s, p, o
37-
const subMatchSpec = matchSpec[key];
36+
return matches.some((match) => tripleMatchesPattern(triple, match));
37+
}
38+
39+
function tripleMatchesPattern( triple, pattern ) {
40+
// form of triple is {s, p, o} or {subject, predicate, object}, same as pattern
41+
for( let key in pattern ){
42+
// key is one of subject, predicate, object
43+
const subMatchSpec = pattern[key];
3844
const subMatchValue = triple[key];
3945

4046
if( subMatchSpec && !subMatchValue )
@@ -45,7 +51,7 @@ export function tripleMatchesSpec( triple, matchSpec ) {
4551
if( subMatchSpec[subKey] !== subMatchValue[subKey] )
4652
return false;
4753
}
48-
return true; // no false matches found, let's send a response
54+
return true; // no false matches found
4955
}
5056

5157

0 commit comments

Comments
 (0)