@zenflux/react-commander 0.0.15 → 0.0.17

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.
@@ -12,8 +12,7 @@ import {
12
12
  INTERNAL_ON_MOUNT,
13
13
  INTERNAL_ON_UPDATE,
14
14
  INTERNAL_ON_LOAD,
15
- INTERNAL_ON_CONTEXT_STATE_UPDATED,
16
- INTERNAL_STATE_UPDATED_EVENT
15
+ INTERNAL_ON_CONTEXT_STATE_UPDATED
17
16
  } from "./_internal/constants";
18
17
 
19
18
  import core from "./_internal/core";
@@ -66,63 +65,6 @@ export function withCommands(
66
65
  throw new Error( "Invalid arguments" );
67
66
  }
68
67
 
69
- function shallowEqual<T extends Record<string, unknown>>( a: T, b: T ): boolean {
70
- if ( a === b ) return true;
71
- if ( ! a || ! b ) return false;
72
-
73
- const aKeys = Object.keys( a );
74
- const bKeys = Object.keys( b );
75
- if ( aKeys.length !== bKeys.length ) return false;
76
-
77
- for ( const key of aKeys ) {
78
- if ( ( a as Record<string, unknown> )[ key ] !== ( b as Record<string, unknown> )[ key ] ) {
79
- return false;
80
- }
81
- }
82
- return true;
83
- }
84
-
85
- class Store {
86
- private silentState: any;
87
- private currentState: any;
88
- private prevState: any;
89
-
90
- public constructor( initialState: any ) {
91
- this.currentState = initialState;
92
- this.prevState = initialState;
93
- }
94
-
95
- public getState() {
96
- return this.silentState || this.currentState;
97
- }
98
-
99
- public getPrevState() {
100
- return this.prevState;
101
- }
102
-
103
- public setState( newState: any, silent = false ) {
104
- this.prevState = this.currentState;
105
-
106
- if ( silent ) {
107
- this.silentState = newState;
108
-
109
- return;
110
- }
111
-
112
- this.silentState = null;
113
- this.currentState = newState;
114
- }
115
-
116
- public hasChanged() {
117
- if ( this.prevState === this.currentState ) {
118
- return false;
119
- }
120
-
121
- return ! shallowEqual( this.prevState, this.currentState );
122
- }
123
-
124
- }
125
-
126
68
  if ( state ) {
127
69
  const originalFunction = Component,
128
70
  originalName = Component.displayName || Component.name || "Component";
@@ -144,7 +86,22 @@ export function withCommands(
144
86
 
145
87
  public context: DCommandComponentContextProps;
146
88
 
147
- private store: Store;
89
+ /**
90
+ * The state before its last change, for the update lifecycle handler.
91
+ */
92
+ private prevState: any = state;
93
+
94
+ /**
95
+ * Its state while it is unmounted: taken out of the store as it unmounts, put back if it mounts again (React
96
+ * mounts the same instance again in StrictMode, and as a Suspense shows what it hid). Undefined while mounted.
97
+ */
98
+ private unmountedState: any = undefined;
99
+
100
+ private readState() {
101
+ return this.unmountedState !== undefined
102
+ ? this.unmountedState
103
+ : commandsManager.getState( componentName, this.context.getNameUnique(), state );
104
+ }
148
105
 
149
106
  private $$commander = {
150
107
  isMounted: false,
@@ -161,7 +118,6 @@ export function withCommands(
161
118
  this.context = context;
162
119
 
163
120
  this.state = {};
164
- this.store = new Store( state );
165
121
 
166
122
  this.registerInternalContext();
167
123
  }
@@ -177,6 +133,9 @@ export function withCommands(
177
133
  return;
178
134
  }
179
135
 
136
+ // Its commands are `$e`'s from its first instance on: the app made its API by then.
137
+ commandsManager.manage( componentName );
138
+
180
139
  const self = this;
181
140
 
182
141
  core[ REGISTER_INTERNAL_SYMBOL ]( {
@@ -192,29 +151,33 @@ export function withCommands(
192
151
 
193
152
  getComponentContext: () => self.context,
194
153
 
195
- getState: () => this.store ? this.store.getState() : this.state,
196
- setState: ( state, callback ) => {
197
- const prevState = this.store.getState();
154
+ // The instance's state is in its `$e` component's store, which finds what changed; while it is unmounted
155
+ // (StrictMode's second mount, a Suspense that hid it), the instance holds it, as before.
156
+ getState: () => state ? this.readState() : this.state,
157
+ setState: ( newState, callback ) => {
158
+ const prevState = this.readState(),
159
+ patch = typeof newState === "function" ? newState( prevState ) : newState;
198
160
 
199
- this.store.setState(
200
- {
201
- ... prevState,
202
- ... state
203
- },
204
- ! this.isMounted(),
205
- );
161
+ let hasChanged = false;
206
162
 
207
- const currentState = this.store.getState();
163
+ if ( patch && this.unmountedState !== undefined ) {
164
+ this.unmountedState = { ... this.unmountedState, ... patch };
208
165
 
209
- if ( this.isMounted() ) {
210
- const hasChanged = ! shallowEqual( prevState, currentState );
166
+ hasChanged = true;
167
+ } else if ( patch ) {
168
+ hasChanged = commandsManager.setState( componentName, id, patch, state );
169
+ }
170
+
171
+ const currentState = this.readState();
172
+
173
+ if ( hasChanged ) {
174
+ this.prevState = prevState;
211
175
 
212
- if ( hasChanged ) {
176
+ if ( this.isMounted() ) {
213
177
  queueMicrotask( () => {
214
178
  if ( this.isMounted() ) {
215
179
  const ctx = core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() );
216
180
 
217
- ctx.emitter.emit( INTERNAL_STATE_UPDATED_EVENT );
218
181
  this.$$commander.lifecycleHandlers[ INTERNAL_ON_CONTEXT_STATE_UPDATED ]?.( ctx, true );
219
182
  }
220
183
  } );
@@ -241,7 +204,15 @@ export function withCommands(
241
204
 
242
205
  const componentNameUnique = this.context.getNameUnique();
243
206
 
207
+ commandsManager.unhookWithinComponent( componentNameUnique );
208
+
244
209
  core[ UNREGISTER_INTERNAL_SYMBOL ]( componentNameUnique );
210
+
211
+ if ( state ) {
212
+ this.unmountedState = commandsManager.getState( componentName, componentNameUnique, state );
213
+ }
214
+
215
+ commandsManager.removeState( componentName, componentNameUnique );
245
216
  }
246
217
 
247
218
  /**
@@ -255,6 +226,15 @@ export function withCommands(
255
226
 
256
227
  this.registerInternalContext();
257
228
 
229
+ // Mounted again: its state goes back to the store.
230
+ if ( this.unmountedState !== undefined ) {
231
+ const kept = this.unmountedState;
232
+
233
+ this.unmountedState = undefined;
234
+
235
+ commandsManager.setState( componentName, id, kept, state );
236
+ }
237
+
258
238
  core[ SET_TO_CONTEXT_SYMBOL ]( id, { props: this.props } );
259
239
 
260
240
  if ( this.$$commander.lifecycleHandlers[ INTERNAL_ON_MOUNT ] ) {
@@ -262,15 +242,15 @@ export function withCommands(
262
242
  }
263
243
  }
264
244
 
265
- public componentDidUpdate( prevProps: any, prevState: any, snapshot?: any ) {
245
+ public componentDidUpdate( prevProps: any, _prevState: any, snapshot?: any ) {
266
246
  if ( this.$$commander.lifecycleHandlers[ INTERNAL_ON_UPDATE ] ) {
267
247
  const context = core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() );
268
248
 
269
249
  this.$$commander.lifecycleHandlers[ INTERNAL_ON_UPDATE ]( context, {
270
250
  currentProps: this.props,
271
- currentState: this.store.getState(),
251
+ currentState: commandsManager.getState( componentName, this.context.getNameUnique(), state ),
272
252
  prevProps,
273
- prevState: this.store.getPrevState(),
253
+ prevState: this.prevState,
274
254
  snapshot,
275
255
  } );
276
256
  }