Ver código fonte

feat(StateListenerRegistry): add 'deepEquals' option

Adds an extra 'options' argument to the register method which
allows to use deep equality instead of a shallow one when comparing
the current and the previous selections.
j8
paweldomas 4 anos atrás
pai
commit
ad948bdbe2
1 arquivos alterados com 25 adições e 3 exclusões
  1. 25
    3
      react/features/base/redux/StateListenerRegistry.js

+ 25
- 3
react/features/base/redux/StateListenerRegistry.js Ver arquivo

@@ -2,6 +2,7 @@
2 2
 
3 3
 import type { Store } from 'redux';
4 4
 
5
+import { equals } from './functions';
5 6
 import logger from './logger';
6 7
 
7 8
 /**
@@ -37,6 +38,18 @@ type Listener
37 38
  */
38 39
 type Selector = (state: Object, prevSelection: any) => any;
39 40
 
41
+/**
42
+ * Options that can be passed to the register method.
43
+ */
44
+type RegistrationOptions = {
45
+
46
+    /**
47
+     * @property {boolean} [deepEquals=false] - whether or not a deep equals check should be performed on the selection
48
+     * returned by {@link Selector}.
49
+     */
50
+    deepEquals: ?boolean
51
+}
52
+
40 53
 /**
41 54
  * A type of a {@link Selector}-{@link Listener} association in which the
42 55
  * {@code Listener} listens to changes in the values derived from a redux
@@ -50,6 +63,11 @@ type SelectorListener = {
50 63
      */
51 64
     listener: Listener,
52 65
 
66
+    /**
67
+     * The {@link RegistrationOptions} passed during the registration to be applied on the listener.
68
+     */
69
+    options: ?RegistrationOptions,
70
+
53 71
     /**
54 72
      * The {@code Selector} which selects values whose changes are listened to
55 73
      * by {@link listener}.
@@ -94,8 +112,10 @@ class StateListenerRegistry {
94 112
                     = selectorListener.selector(
95 113
                         store.getState(),
96 114
                         prevSelection);
115
+                const useDeepEquals = selectorListener?.options?.deepEquals;
97 116
 
98
-                if (prevSelection !== selection) {
117
+                if ((useDeepEquals && !equals(prevSelection, selection))
118
+                        || (!useDeepEquals && prevSelection !== selection)) {
99 119
                     prevSelections.set(selectorListener, selection);
100 120
                     selectorListener.listener(selection, store, prevSelection);
101 121
                 }
@@ -117,12 +137,14 @@ class StateListenerRegistry {
117 137
      * @param {Function} listener - The listener to register with this
118 138
      * {@code StateListenerRegistry} so that it gets invoked when the value
119 139
      * returned by the specified {@code selector} changes.
140
+     * @param {RegistrationOptions} [options] - Any options to be applied to the registration.
120 141
      * @returns {void}
121 142
      */
122
-    register(selector: Selector, listener: Listener) {
143
+    register(selector: Selector, listener: Listener, options: ?RegistrationOptions) {
123 144
         this._selectorListeners.add({
124 145
             listener,
125
-            selector
146
+            selector,
147
+            options
126 148
         });
127 149
     }
128 150
 

Carregando…
Cancelar
Salvar