refactor: update roles immediately without waiting for full sync (#2294)

This commit is contained in:
Aman Harwara
2023-04-05 21:34:06 +05:30
committed by GitHub
parent b3556ef8d2
commit 41a5b1415a
2 changed files with 102 additions and 48 deletions

View File

@@ -88,7 +88,8 @@ export class SNFeaturesService
const {
payload: { userUuid, currentRoles },
} = data as UserRolesChangedEvent
await this.updateOnlineRolesAndFetchFeatures(userUuid, currentRoles)
await this.updateOnlineRoles(currentRoles)
await this.fetchFeatures(userUuid)
}
})
@@ -143,6 +144,9 @@ export class SNFeaturesService
return
}
const { userUuid, userRoles } = event.payload as MetaReceivedData
await this.updateOnlineRoles(userRoles.map((role) => role.name))
/**
* All user data must be downloaded before we map features. Otherwise, feature mapping
* may think a component doesn't exist and create a new one, when in reality the component
@@ -152,11 +156,7 @@ export class SNFeaturesService
return
}
const { userUuid, userRoles } = event.payload as MetaReceivedData
await this.updateOnlineRolesAndFetchFeatures(
userUuid,
userRoles.map((role) => role.name),
)
await this.fetchFeatures(userUuid)
}
}
@@ -400,7 +400,7 @@ export class SNFeaturesService
return hasFirstPartyOfflineSubscription || new URL(offlineRepo.content.offlineFeaturesUrl).hostname === 'localhost'
}
async updateOnlineRolesAndFetchFeatures(userUuid: UuidString, roles: string[]): Promise<void> {
async updateOnlineRoles(roles: string[]): Promise<void> {
const previousRoles = this.onlineRoles
const userRolesChanged =
@@ -412,9 +412,21 @@ export class SNFeaturesService
return
}
this.needsInitialFeaturesUpdate = false
if (userRolesChanged && !isInitialLoadRolesChange) {
if (this.onlineRolesIncludePaidSubscription()) {
await this.notifyEvent(FeaturesEvent.DidPurchaseSubscription)
}
}
await this.setOnlineRoles(roles)
}
async fetchFeatures(userUuid: UuidString): Promise<void> {
if (!this.needsInitialFeaturesUpdate) {
return
}
this.needsInitialFeaturesUpdate = false
const shouldDownloadRoleBasedFeatures = !this.hasOfflineRepo()
@@ -426,12 +438,6 @@ export class SNFeaturesService
await this.didDownloadFeatures(features)
}
}
if (userRolesChanged && !isInitialLoadRolesChange) {
if (this.onlineRolesIncludePaidSubscription()) {
await this.notifyEvent(FeaturesEvent.DidPurchaseSubscription)
}
}
}
async setOnlineRoles(roles: string[]): Promise<void> {