Skip to content

Commit

Permalink
Synchronize commitUpdate (#3012)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel authored Nov 14, 2023
1 parent 20e2c02 commit a0229e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
15 changes: 13 additions & 2 deletions client/src/app/gateways/repositories/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { auditTime, BehaviorSubject, filter, Observable, Subject, Subscription }
import { HasSequentialNumber, Identifiable } from 'src/app/domain/interfaces';
import { OnAfterAppsLoaded } from 'src/app/infrastructure/definitions/hooks/after-apps-loaded';
import { ListUpdateData } from 'src/app/infrastructure/utils';
import { Deferred } from 'src/app/infrastructure/utils/promises';
import { OsSortProperty } from 'src/app/site/base/base-sort.service';
import { SortListService } from 'src/app/ui/modules/list';

Expand Down Expand Up @@ -40,6 +41,7 @@ enum PipelineActionType {
interface UpdatePipelineAction {
funct: () => Promise<void>;
type: PipelineActionType;
defer?: Deferred;
key?: string;
}

Expand Down Expand Up @@ -319,7 +321,8 @@ export abstract class BaseRepository<V extends BaseViewModel, M extends BaseMode
*
* @param ids All model ids.
*/
public changedModels(ids: Id[], changedModels: BaseModel<M>[]): void {
public changedModels(ids: Id[], changedModels: BaseModel<M>[]): Promise<void> {
const defer = new Deferred();
this.pushToPipeline({
funct: async () => {
const newViewModels: V[] = [];
Expand All @@ -344,9 +347,11 @@ export abstract class BaseRepository<V extends BaseViewModel, M extends BaseMode
await this.initChangeBasedResorting(newModels, updatedModels, newViewModels, updatedViewModels);
}
},
type: PipelineActionType.General
type: PipelineActionType.General,
defer
});
this.activatePipeline();
return defer;
}

/**
Expand Down Expand Up @@ -565,11 +570,17 @@ export abstract class BaseRepository<V extends BaseViewModel, M extends BaseMode
this.updateActionPipeline[priority][index].key ===
this.updateActionPipeline[priority][i].key)
) {
if (this.updateActionPipeline[priority][i].defer) {
this.updateActionPipeline[priority][i].defer.resolve();
}
this.updateActionPipeline[priority].splice(i, 1);
}
}
}
}
if (this.updateActionPipeline[priority][index].defer) {
this.updateActionPipeline[priority][index].defer.resolve();
}
this.updateActionPipeline[priority].splice(index, 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ export class DataStoreUpdateManagerService {
*
* @param slot The slot to commit
*/
public commit(slot: UpdateSlot, changedModels: ChangedModels): void {
public async commit(slot: UpdateSlot, changedModels: ChangedModels): Promise<void> {
if (!this.currentUpdateSlot || !this.currentUpdateSlot.equal(slot)) {
throw new Error(`No or wrong update slot to be finished!`);
}
this.currentUpdateSlot = null;

// notify repositories in two phases
const repositories = this.mapperService.getAllRepositories();
const modelUpdates = [];

// Phase 1: deleting and creating of view models (in this order)
for (const repo of repositories) {
Expand All @@ -184,9 +185,10 @@ export class DataStoreUpdateManagerService {

const changedModelIds = slot.getChangedModelIdsForCollection(repo.collection);
if (changedModelIds?.length) {
repo.changedModels(changedModelIds, changedModels[repo.COLLECTION]);
modelUpdates.push(repo.changedModels(changedModelIds, changedModels[repo.COLLECTION]));
}
}
await Promise.all(modelUpdates);

// Phase 2: updating all repositories
for (const repo of repositories) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class ViewModelStoreUpdateService {
await this.DS.addOrUpdate(changedModels[collection]);
}

this.DSUpdateService.commit(updateSlot, changedModels);
await this.DSUpdateService.commit(updateSlot, changedModels);
}

/**
Expand Down

0 comments on commit a0229e7

Please sign in to comment.