@totalsoft/rules-algebra-react 5.0.22 → 5.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/hooks/useRulesLens.js +12 -4
- package/hooks/useRulesLens.js +10 -2
- package/package.json +3 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright (c) TotalSoft.
|
|
2
2
|
// This source code is licensed under the MIT license.
|
|
3
|
-
import { useStateLens, rmap, over } from '@totalsoft/react-state-lens';
|
|
4
|
-
import { useMemo, useCallback, useState } from 'react';
|
|
3
|
+
import { useStateLens, rmap, over, reuseCache } from '@totalsoft/react-state-lens';
|
|
4
|
+
import { useMemo, useCallback, useState, useRef } from 'react';
|
|
5
5
|
import { applyRule, logTo } from '@totalsoft/rules-algebra';
|
|
6
6
|
import { create, detectChanges, ensureArrayUIDsDeep } from '@totalsoft/change-tracking';
|
|
7
7
|
export function useRulesLens(rules, initialModel, {
|
|
@@ -20,14 +20,22 @@ export function useRulesLens(rules, initialModel, {
|
|
|
20
20
|
}, [rules, isLogEnabled, logger, ...deps]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
21
21
|
|
|
22
22
|
const stateLens = useStateLens(() => ensureArrayUIDsDeep(initialModel));
|
|
23
|
+
const prevProxy = useRef(null);
|
|
23
24
|
const rulesEngineLens = useMemo(() => {
|
|
24
25
|
var _stateLens;
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
const proxy = (_stateLens = stateLens, rmap((changedModel, prevModel) => {
|
|
27
28
|
const result = applyRule(rulesEngine, ensureArrayUIDsDeep(changedModel), prevModel);
|
|
28
29
|
setDirtyInfo(prevDirtyInfo => detectChanges(result, prevModel, prevDirtyInfo));
|
|
29
30
|
return result;
|
|
30
|
-
})(_stateLens);
|
|
31
|
+
})(_stateLens));
|
|
32
|
+
|
|
33
|
+
if (prevProxy.current) {
|
|
34
|
+
reuseCache(prevProxy.current, proxy);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
prevProxy.current = proxy;
|
|
38
|
+
return proxy;
|
|
31
39
|
}, [stateLens, rulesEngine]);
|
|
32
40
|
return [rulesEngineLens, dirtyInfo, // Reset
|
|
33
41
|
useCallback((newModel = undefined) => {
|
package/hooks/useRulesLens.js
CHANGED
|
@@ -48,16 +48,24 @@ function useRulesLens(rules, initialModel) {
|
|
|
48
48
|
var stateLens = (0, _reactStateLens.useStateLens)(function () {
|
|
49
49
|
return (0, _changeTracking.ensureArrayUIDsDeep)(initialModel);
|
|
50
50
|
});
|
|
51
|
+
var prevProxy = (0, _react.useRef)(null);
|
|
51
52
|
var rulesEngineLens = (0, _react.useMemo)(function () {
|
|
52
53
|
var _stateLens;
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
var proxy = (_stateLens = stateLens, (0, _reactStateLens.rmap)(function (changedModel, prevModel) {
|
|
55
56
|
var result = (0, _rulesAlgebra.applyRule)(rulesEngine, (0, _changeTracking.ensureArrayUIDsDeep)(changedModel), prevModel);
|
|
56
57
|
setDirtyInfo(function (prevDirtyInfo) {
|
|
57
58
|
return (0, _changeTracking.detectChanges)(result, prevModel, prevDirtyInfo);
|
|
58
59
|
});
|
|
59
60
|
return result;
|
|
60
|
-
})(_stateLens);
|
|
61
|
+
})(_stateLens));
|
|
62
|
+
|
|
63
|
+
if (prevProxy.current) {
|
|
64
|
+
(0, _reactStateLens.reuseCache)(prevProxy.current, proxy);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
prevProxy.current = proxy;
|
|
68
|
+
return proxy;
|
|
61
69
|
}, [stateLens, rulesEngine]);
|
|
62
70
|
return [rulesEngineLens, dirtyInfo, // Reset
|
|
63
71
|
(0, _react.useCallback)(function () {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totalsoft/rules-algebra-react",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.31",
|
|
4
4
|
"description": "React extensions for the \"rules-algebra\" business rules api",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"author": "TotalSoft",
|
|
@@ -15,13 +15,13 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@totalsoft/change-tracking-react": "^5.0.
|
|
18
|
+
"@totalsoft/change-tracking-react": "^5.0.31",
|
|
19
19
|
"@totalsoft/rules-algebra": "^5.0.22"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"react": ">= 16.9.0"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "e784fb7f64e22e9490a170f83f277031d2a88a59",
|
|
25
25
|
"private": false,
|
|
26
26
|
"module": "./esm/index.js",
|
|
27
27
|
"typings": "./index.d.ts"
|