"Fossies" - the Fresh Open Source Software Archive 
Member "jitsi-meet-7564/react/features/base/redux/functions.ts" (3 Oct 2023, 5179 Bytes) of package /linux/misc/jitsi-meet-7564.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 import _ from 'lodash';
2
3 import { IReduxState, IStore } from '../../app/types';
4 import { IStateful } from '../app/types';
5
6 /**
7 * Sets specific properties of a specific state to specific values and prevents
8 * unnecessary state changes.
9 *
10 * @param {T} target - The state on which the specified properties are to
11 * be set.
12 * @param {T} source - The map of properties to values which are to be set
13 * on the specified target.
14 * @returns {T} The specified target if the values of the specified
15 * properties equal the specified values; otherwise, a new state constructed
16 * from the specified target by setting the specified properties to the
17 * specified values.
18 */
19 export function assign<T extends Object>(target: T, source: Partial<T>): T {
20 let t = target;
21
22 for (const property in source) { // eslint-disable-line guard-for-in
23 t = _set(t, property, source[property], t === target);
24 }
25
26 return t;
27 }
28
29 /**
30 * Determines whether {@code a} equals {@code b} according to deep comparison
31 * (which makes sense for Redux and its state definition).
32 *
33 * @param {*} a - The value to compare to {@code b}.
34 * @param {*} b - The value to compare to {@code a}.
35 * @returns {boolean} True if {@code a} equals {@code b} (according to deep
36 * comparison); false, otherwise.
37 */
38 export function equals(a: any, b: any) {
39 return _.isEqual(a, b);
40 }
41
42 /**
43 * Sets a specific property of a specific state to a specific value. Prevents
44 * unnecessary state changes (when the specified {@code value} is equal to the
45 * value of the specified {@code property} of the specified {@code state}).
46 *
47 * @param {T} state - The (Redux) state from which a new state is to be
48 * constructed by setting the specified {@code property} to the specified
49 * {@code value}.
50 * @param {string} property - The property of {@code state} which is to be
51 * assigned the specified {@code value} (in the new state).
52 * @param {*} value - The value to assign to the specified {@code property}.
53 * @returns {T} The specified {@code state} if the value of the specified
54 * {@code property} equals the specified <tt>value/tt>; otherwise, a new state
55 * constructed from the specified {@code state} by setting the specified
56 * {@code property} to the specified {@code value}.
57 */
58 export function set<T extends Object>(state: T, property: keyof T, value: any): T {
59 return _set(state, property, value, /* copyOnWrite */ true);
60 }
61
62 /**
63 * Sets a specific property of a specific state to a specific value. Prevents
64 * unnecessary state changes (when the specified {@code value} is equal to the
65 * value of the specified {@code property} of the specified {@code state}).
66 *
67 * @param {T} state - The (Redux) state from which a state is to be
68 * constructed by setting the specified {@code property} to the specified
69 * {@code value}.
70 * @param {string} property - The property of {@code state} which is to be
71 * assigned the specified {@code value}.
72 * @param {*} value - The value to assign to the specified {@code property}.
73 * @param {boolean} copyOnWrite - If the specified {@code state} is to not be
74 * modified, {@code true}; otherwise, {@code false}.
75 * @returns {T} The specified {@code state} if the value of the specified
76 * {@code property} equals the specified <tt>value/tt> or {@code copyOnWrite}
77 * is truthy; otherwise, a new state constructed from the specified
78 * {@code state} by setting the specified {@code property} to the specified
79 * {@code value}.
80 */
81 function _set<T extends Object>(
82 state: T,
83 property: keyof T,
84 value: any,
85 copyOnWrite: boolean): T {
86 // Delete state properties that are to be set to undefined. (It is a matter
87 // of personal preference, mostly.)
88 if (typeof value === 'undefined'
89 && Object.prototype.hasOwnProperty.call(state, property)) {
90 const newState = copyOnWrite ? { ...state } : state;
91
92 if (delete newState[property]) {
93 return newState;
94 }
95 }
96
97 if (state[property] !== value) {
98 if (copyOnWrite) {
99 return {
100 ...state,
101 [property]: value
102 };
103 }
104
105 state[property] = value;
106 }
107
108 return state;
109 }
110
111 /* eslint-enable max-params */
112
113 /**
114 * Whether or not the entity is of type IStore.
115 *
116 * @param {IStateful} stateful - The entity to check.
117 * @returns {boolean}
118 */
119 function isStore(stateful: IStateful): stateful is IStore {
120 return 'getState' in stateful && typeof stateful.getState === 'function';
121 }
122
123 /**
124 * Returns redux state from the specified {@code stateful} which is presumed to
125 * be related to the redux state (e.g. The redux store, the redux
126 * {@code getState} function).
127 *
128 * @param {Function|IStore} stateful - The entity such as the redux store or the
129 * redux {@code getState} function from which the redux state is to be
130 * returned.
131 * @returns {Object} The redux state.
132 */
133 export function toState(stateful: IStateful): IReduxState {
134 if (stateful) {
135 if (typeof stateful === 'function') {
136 return stateful();
137 }
138
139 if (isStore(stateful)) {
140 return stateful.getState();
141 }
142 }
143
144 return stateful;
145 }