Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize commitUpdate #3012

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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