Skip to content

Commit 21bb1fc

Browse files
committed
Docs, add types
1 parent b7f942b commit 21bb1fc

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

README.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _NBD: It actually doesn't **have** to use JSON Schema, but it's suggested_
1111
- Zero-dependency, extremely lightweight (under 2kb minzipped)
1212
- Runs everywhere
1313
- Nested conditions allow for controlling rule evaluation order
14-
- Memoization makes it fast
14+
- [Memoization] makes it fast
1515
- No thrown errors - errors are emitted, never thrown
1616

1717
## Installation
@@ -27,7 +27,7 @@ or, use it directly in the browser
2727
```html
2828
<script src="https://cdn.jsdelivr.net/npm/json-schema-rules-engine"></script>
2929
<script>
30-
const engine = jsonSchemaRulesEngine({ facts, actions, rules, validator });
30+
const engine = jsonSchemaRulesEngine(validator, { facts, actions, rules });
3131
</script>
3232
```
3333

@@ -93,7 +93,7 @@ const validator = async (subject, schema) => {
9393
return { result };
9494
};
9595

96-
const engine = createRulesEngine({ facts, rules, actions, validator });
96+
const engine = createRulesEngine(validator, { facts, rules, actions });
9797

9898
engine.run({
9999
hotTemp: 20,
@@ -169,6 +169,8 @@ const weather = async ({ query, appId, units }) => {
169169
};
170170
```
171171

172+
It's important to note that all functional facts are memoized during each run of the rule engine, based on **shallow equality** of their argument. Currently, functions that accept an argument that contains values that are objects or arrays are not memoized. This will be fixed in an upcoming release.
173+
172174
Static facts are simply the values of the context object
173175

174176
### Actions
@@ -227,7 +229,7 @@ engine.run({ age: 32, name: 'Joe' }); // fires the log action with { message: 'H
227229

228230
#### Nesting Rules
229231

230-
The `then` or `otherwise` property can consist of either `actions`, but it can also contain another `when` clause. All functional facts in all [FactMaps](#factmaps) are evaluated simultaneously. By nesting `when`'s, you can cause facts to be executed serially.
232+
The `then` or `otherwise` property can consist of either `actions`, but it can also contain a nested rule. All functional facts in all [FactMaps](#factmaps) are evaluated simultaneously. By nesting `when`'s, you can cause facts to be executed serially.
231233

232234
```js
233235
const myRule = {
@@ -300,7 +302,7 @@ const myRule = {
300302

301303
#### FactMap
302304

303-
A fact map is a plain object whose keys are facts (static or functional) and values are [`Evaluator`'s](#evaluator).
305+
A FactMap is a plain object whose keys are facts (static or functional) and values are [`Evaluator`'s](#evaluator).
304306

305307
**NOTE: `factMapId` is a reserved word in a `FactMap`. It is used internally to allow easy access to the results of a `FactMap` for interpolation in the `then` or `otherwise` clauses. For this reason `factMapId` _CANNOT_ be given as a fact or context**.
306308

@@ -384,10 +386,38 @@ The errors that can be emitted are:
384386
- `FactEvaluationError` - errors thrown during the evaluation of facts/results from facts
385387
- `ActionExecutionError` - errors thrown during the execution of actions
386388

389+
## API/Types
390+
391+
- **`createRulesEngine(validator: Validator, options?: Options): RulesEngine`**
392+
393+
```ts
394+
type Options = {
395+
facts?: Record<string,Fact>;
396+
rules?: Record<string,Rule>;
397+
actions?: Record<string,Action>;
398+
pattern?: RegExp; // for interpolation
399+
resolver?: (subject: Record<string,any>, path: string) => any
400+
};
401+
402+
interface RulesEngine {
403+
setRules(rulesPatch: Patch<Rules>): void;
404+
setFacts(factsPatch: Patch<Facts>): void;
405+
setActions(actionsPatch: Patch<Actions>): void;
406+
on('debug', subscriber: DebugSubscriber): Unsubscribe
407+
on('error', subscriber: ErrorSubscriber): Unsubscribe
408+
on('start', subscriber: StartSubscriber): Unsubscribe
409+
on('complete', subscriber: CompleteSubscriber): Unsubscribe
410+
run(context: Record<string, any>): Promise<void>;
411+
}
412+
413+
type PatchFunction<T> = (o: T) => T;
414+
type Patch<T> = PatchFunction<T> | Partial<T>;
415+
```
416+
387417
## License
388418

389419
[MIT](./LICENSE)
390420

391421
## Contributing
392422

393-
Help wanted! I'd like to really create great advanced types around the content of the facts, actions, and context given to the engine. Reach out [@akmjenkins](https://twitter.com/akmjenkins) or [[email protected]](mailto:[email protected])
423+
Help wanted! I'd like to create really great advanced types around the content of the facts, actions, and context given to the engine. Reach out [@akmjenkins](https://twitter.com/akmjenkins) or [[email protected]](mailto:[email protected])

0 commit comments

Comments
 (0)