@servicetitan/standalone-root 1.4.0 → 1.6.0

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.
@@ -1,22 +1,20 @@
1
1
  import { makeObservable, observable, runInAction } from 'mobx';
2
2
 
3
3
  import {
4
- CurrentUserClientApi,
4
+ UserDetails,
5
5
  PartitionInfo,
6
6
  PartitionsClientApi,
7
- UserDetails,
7
+ CurrentUserClientApi,
8
8
  } from '@servicetitan/marketing-services-api/identity';
9
- import { LoadStatus, SymbolTokenDataType } from '@servicetitan/standalone-ui';
10
9
  import { inject, injectable } from '@servicetitan/react-ioc';
11
10
  import {
12
11
  AppConfigApi,
13
12
  AppSettings,
14
13
  } from '@servicetitan/marketing-services-api/dist/common/app-config.mrk.api';
14
+ import { LoadStatus, SymbolTokenDataType } from '@servicetitan/standalone-ui';
15
15
  import { TenantManagementApi, TenantModel } from '@servicetitan/standalone-tm-api';
16
16
  import { OnboardingApi, OnboardingResponse } from '@servicetitan/marketing-services-api/settings';
17
17
 
18
- import { Events } from '../utils/events';
19
- import { EventEmitter } from '../utils/event-emitter';
20
18
  import {
21
19
  storageKeyForUser,
22
20
  storageKeyForLocale,
@@ -26,10 +24,12 @@ import {
26
24
  storageKeyForAppSettings,
27
25
  storageKeyForTenantConfigs,
28
26
  } from '../utils/storage-keys';
29
- import { ModifiableRootSection } from '../enums';
30
- import { POSSIBLE_CHANGES_TOKEN } from '../utils/tokens';
27
+ import { RootSection } from '../enums';
28
+ import { Events } from '../utils/events';
29
+ import { EventEmitter } from '../utils/event-emitter';
30
+ import { REFRESH_SECTIONS_TOKEN, SIDE_EFFECTS_TOKEN } from '../utils/tokens';
31
31
  import { cultureKeyInCoreConfigs, defaultLocale, defaultTimezone } from '../utils/constants';
32
- import { eventEmitterSymbol, isInitializedSymbol, registeredChangesSymbol } from '../utils/symbols';
32
+ import { eventEmitterSymbol, isInitializedSymbol, sideEffectsSymbol } from '../utils/symbols';
33
33
 
34
34
  @injectable()
35
35
  export class StandaloneRootStore {
@@ -52,8 +52,10 @@ export class StandaloneRootStore {
52
52
  @inject(CurrentUserClientApi) private readonly currentUserClientApi: CurrentUserClientApi,
53
53
  @inject(OnboardingApi) private readonly onboardingApi: OnboardingApi,
54
54
  @inject(PartitionsClientApi) private readonly partitionsApi: PartitionsClientApi,
55
- @inject(POSSIBLE_CHANGES_TOKEN)
56
- private readonly possibleChanges: SymbolTokenDataType<typeof POSSIBLE_CHANGES_TOKEN>,
55
+ @inject(SIDE_EFFECTS_TOKEN)
56
+ private readonly sideEffects: SymbolTokenDataType<typeof SIDE_EFFECTS_TOKEN>,
57
+ @inject(REFRESH_SECTIONS_TOKEN)
58
+ private readonly refreshSections: SymbolTokenDataType<typeof REFRESH_SECTIONS_TOKEN>,
57
59
  ) {
58
60
  this.initializeEventEmitter();
59
61
  makeObservable(this);
@@ -100,12 +102,25 @@ export class StandaloneRootStore {
100
102
  const isSuccess = this.syncFromStorage();
101
103
 
102
104
  if (isSuccess) {
103
- if (this.registeredChanges?.length) {
105
+ const registeredSideEffects = this.registeredSideEffects;
106
+
107
+ if (registeredSideEffects?.length || this.refreshSections?.length) {
108
+ const distinctSections = Array.from(
109
+ new Set([
110
+ ...(this.registeredSideEffects ?? []),
111
+ ...(this.refreshSections ?? []),
112
+ ]),
113
+ );
114
+
104
115
  await Promise.all(
105
- this.registeredChanges.map(section =>
116
+ distinctSections.map(section =>
106
117
  this.mapModifiableSectionToFetcher[section](),
107
118
  ),
108
119
  );
120
+
121
+ if (registeredSideEffects?.length) {
122
+ this.unregisterSideEffects();
123
+ }
109
124
  }
110
125
  } else {
111
126
  await fetchAll();
@@ -116,8 +131,6 @@ export class StandaloneRootStore {
116
131
  this.setIsInitialized(true);
117
132
  }
118
133
 
119
- this.removeRegisteredChanges();
120
-
121
134
  const timezoneFetched = this.tenantConfigs.TimeZone;
122
135
 
123
136
  if (timezoneFetched) {
@@ -158,22 +171,22 @@ export class StandaloneRootStore {
158
171
  }
159
172
 
160
173
  dispose = () => {
161
- this.registerPossibleChanges();
174
+ this.registerSideEffects();
162
175
  this.unregisterGlobalSyncListener();
163
176
  };
164
177
 
165
- private registerPossibleChanges = () => {
166
- if (this.possibleChanges?.length) {
167
- (window as any)[registeredChangesSymbol] = this.possibleChanges;
178
+ private registerSideEffects = () => {
179
+ if (this.sideEffects?.length) {
180
+ (window as any)[sideEffectsSymbol] = this.sideEffects;
168
181
  }
169
182
  };
170
183
 
171
- private removeRegisteredChanges = () => {
172
- (window as any)[registeredChangesSymbol] = undefined;
184
+ private unregisterSideEffects = () => {
185
+ (window as any)[sideEffectsSymbol] = undefined;
173
186
  };
174
187
 
175
188
  private unregisterGlobalSyncListener = () => {
176
- this.eventEmitter.off(Events.SyncFromStorage, this.listenGlobalSync);
189
+ this.eventEmitter.off(Events.SyncFromStorage, this.globalSyncListener);
177
190
  };
178
191
 
179
192
  private initializeEventEmitter = () => {
@@ -188,10 +201,10 @@ export class StandaloneRootStore {
188
201
  };
189
202
 
190
203
  private registerGlobalSyncListener = () => {
191
- this.eventEmitter.on(Events.SyncFromStorage, this.listenGlobalSync);
204
+ this.eventEmitter.on(Events.SyncFromStorage, this.globalSyncListener);
192
205
  };
193
206
 
194
- private listenGlobalSync = (instanceSymbol: symbol) => {
207
+ private globalSyncListener = (instanceSymbol: symbol) => {
195
208
  if (instanceSymbol !== this.currentInstanceSymbol) {
196
209
  this.syncFromStorage();
197
210
  }
@@ -322,13 +335,18 @@ export class StandaloneRootStore {
322
335
  return !!(window as any)[isInitializedSymbol];
323
336
  }
324
337
 
325
- private get registeredChanges(): typeof this.possibleChanges {
326
- return (window as any)[registeredChangesSymbol];
338
+ private get registeredSideEffects(): typeof this.sideEffects {
339
+ return (window as any)[sideEffectsSymbol];
327
340
  }
328
341
 
329
- private get mapModifiableSectionToFetcher(): Record<ModifiableRootSection, () => void> {
342
+ private get mapModifiableSectionToFetcher(): Record<RootSection, () => void> {
330
343
  return {
331
- [ModifiableRootSection.Onboarding]: this.fetchOnboarding,
344
+ [RootSection.Tenant]: this.fetchTenant,
345
+ [RootSection.TenantConfigs]: this.fetchTenantConfigs,
346
+ [RootSection.User]: this.fetchUser,
347
+ [RootSection.AppSettings]: this.fetchAppSettings,
348
+ [RootSection.Onboarding]: this.fetchOnboarding,
349
+ [RootSection.Partitions]: this.fetchPartitions,
332
350
  };
333
351
  }
334
352
  }
@@ -1,7 +1,7 @@
1
1
  const rootSymbolKey = 'standalone-root-symbol-key';
2
2
  const eventEmitterSymbolKey = 'standalone-root-event-emitter-symbol-key';
3
- const registeredChangesSymbolKey = 'standalone-root-modifiable-changes-symbol-key';
3
+ const sideEffectsSymbolKey = 'standalone-root-side-effects-symbol-key';
4
4
 
5
5
  export const isInitializedSymbol = Symbol.for(rootSymbolKey);
6
6
  export const eventEmitterSymbol = Symbol.for(eventEmitterSymbolKey);
7
- export const registeredChangesSymbol = Symbol.for(registeredChangesSymbolKey);
7
+ export const sideEffectsSymbol = Symbol.for(sideEffectsSymbolKey);
@@ -1,7 +1,12 @@
1
1
  import { symbolToken } from '@servicetitan/react-ioc';
2
- import { ModifiableRootSection } from '../enums';
2
+ import { RootSection } from '../enums';
3
3
 
4
- export const POSSIBLE_CHANGES_TOKEN = symbolToken<ModifiableRootSection[] | undefined>(
5
- 'POSSIBLE_CHANGES_TOKEN',
4
+ export const SIDE_EFFECTS_TOKEN = symbolToken<RootSection[] | undefined>(
5
+ 'SIDE_EFFECTS_TOKEN',
6
+ false,
7
+ );
8
+
9
+ export const REFRESH_SECTIONS_TOKEN = symbolToken<RootSection[] | undefined>(
10
+ 'REFRESH_SECTIONS_TOKEN',
6
11
  false,
7
12
  );