diff --git a/src/sdk/dm-sdk.js b/src/sdk/dm-sdk.js index 722b41f0..74f6d70c 100644 --- a/src/sdk/dm-sdk.js +++ b/src/sdk/dm-sdk.js @@ -449,7 +449,7 @@ export class DataManager { const annotationID = annotation?.id ?? task.lastAnnotation?.id; // this.lsf.loadTask(task.id, annotationID); - this.lsf.selectTask(task, annotationID); + await this.lsf.selectTask(task, annotationID); } } diff --git a/src/sdk/lsf-sdk.js b/src/sdk/lsf-sdk.js index d7b65c2c..de537eaf 100644 --- a/src/sdk/lsf-sdk.js +++ b/src/sdk/lsf-sdk.js @@ -258,7 +258,7 @@ export class LSFWrapper { // for preload it's good to always load the first one const annotation = task.annotations[0]; - this.selectTask(task, annotation?.id, true); + await this.selectTask(task, annotation?.id, true); } return false; @@ -294,7 +294,7 @@ export class LSFWrapper { }); // Add new data from received task - if (newTask) this.selectTask(newTask, annotationID, fromHistory); + if (newTask) await this.selectTask(newTask, annotationID, fromHistory); }; if (isFF(FF_DEV_2887) && this.lsf?.commentStore?.hasUnsaved) { @@ -316,14 +316,14 @@ export class LSFWrapper { this.datamanager.invoke("navigate", 'projects'); } - selectTask(task, annotationID, fromHistory = false) { + async selectTask(task, annotationID, fromHistory = false) { const needsAnnotationsMerge = task && this.task?.id === task.id; const annotations = needsAnnotationsMerge ? [...this.annotations] : []; this.task = task; if (needsAnnotationsMerge) { - this.task.mergeAnnotations(annotations); + await this.task.mergeAnnotations(annotations); } this.loadUserLabels(); @@ -596,7 +596,7 @@ export class LSFWrapper { /** @private */ onUpdateAnnotation = async (ls, annotation, extraData) => { const { task } = this; - const serializedAnnotation = this.prepareData(annotation); + const serializedAnnotation = await this.prepareData(annotation); const exitStream = this.shouldExitStream(); Object.assign(serializedAnnotation, extraData); @@ -705,7 +705,9 @@ export class LSFWrapper { onSubmitDraft = async (studio, annotation, params = {}) => { const annotationDoesntExist = !annotation.pk; - const data = { body: this.prepareData(annotation, { draft: true }) }; // serializedAnnotation + const data = { + body: await this.prepareData(annotation, { draft: true }), + }; // serializedAnnotation const hasChanges = this.needsDraftSave(annotation); const showToast = params?.useToast && hasChanges; // console.log('onSubmitDraft', params?.useToast, hasChanges); @@ -788,7 +790,9 @@ export class LSFWrapper { body: { annotation: null }, }); } else { - const annotationData = { body: this.prepareData(currentAnnotation) }; + const annotationData = { + body: await this.prepareData(currentAnnotation), + }; await this.datamanager.apiCall("createDraftForTask", { taskID: this.task.id, @@ -857,7 +861,7 @@ export class LSFWrapper { async submitCurrentAnnotation(eventName, submit, includeId = false, loadNext = true, exitStream) { const { taskID, currentAnnotation } = this; const unique_id = this.task.unique_lock_id; - const serializedAnnotation = this.prepareData(currentAnnotation, { includeId }); + const serializedAnnotation = await this.prepareData(currentAnnotation, { includeId }); if (unique_id) { serializedAnnotation.unique_id = unique_id; @@ -900,7 +904,7 @@ export class LSFWrapper { } /** @private */ - prepareData(annotation, { includeId, draft } = {}) { + async prepareData(annotation, { includeId, draft } = {}) { const userGenerate = !annotation.userGenerate || annotation.sentUserGenerate; @@ -911,7 +915,7 @@ export class LSFWrapper { const result = { lead_time, - result: (draft ? annotation.versions.draft : annotation.serializeAnnotation()) ?? [], + result: (draft ? annotation.versions.draft : await annotation.serializeAnnotation()) ?? [], draft_id: annotation.draftId, parent_prediction: annotation.parent_prediction, parent_annotation: annotation.parent_annotation, diff --git a/src/sdk/lsf-utils.ts b/src/sdk/lsf-utils.ts index c03a512b..bd00ebe1 100644 --- a/src/sdk/lsf-utils.ts +++ b/src/sdk/lsf-utils.ts @@ -76,10 +76,10 @@ export const annotationToServer = ( }; }; -export const getAnnotationSnapshot = (c: LSFAnnotation) => ({ +export const getAnnotationSnapshot = async (c: LSFAnnotation) => ({ id: c.id, pk: c.pk, - result: c.serializeAnnotation(), + result: await c.serializeAnnotation(), leadTime: c.leadTime, userGenerate: !!c.userGenerate, sentUserGenerate: !!c.sentUserGenerate, diff --git a/src/stores/DataStores/tasks.js b/src/stores/DataStores/tasks.js index fdd76210..2f82bfc2 100644 --- a/src/stores/DataStores/tasks.js +++ b/src/stores/DataStores/tasks.js @@ -48,9 +48,8 @@ export const create = (columns) => { }, })) .actions((self) => ({ - mergeAnnotations(annotations) { - // skip drafts, they'll be added later - self.annotations = annotations.filter(a => a.pk).map((c) => { + mergeAnnotations: flow(function *(annotations) { + const merged = annotations.filter(a => a.pk).map(async (c) => { const existingAnnotation = self.annotations.find( (ec) => ec.id === Number(c.pk), ); @@ -62,26 +61,31 @@ export const create = (columns) => { id: c.id, pk: c.pk, draftId: c.draftId, - result: c.serializeAnnotation(), + result: await c.serializeAnnotation(), leadTime: c.leadTime, userGenerate: !!c.userGenerate, sentUserGenerate: !!c.sentUserGenerate, }; } }); - }, - updateAnnotation(annotation) { + // skip drafts, they'll be added later + self.annotations = yield Promise.all(merged); + }), + + updateAnnotation: flow(function *(annotation) { const existingAnnotation = self.annotations.find((c) => { return c.id === Number(annotation.pk) || c.pk === annotation.pk; }); + const snapshot = yield getAnnotationSnapshot(annotation); + if (existingAnnotation) { - Object.assign(existingAnnotation, getAnnotationSnapshot(annotation)); + Object.assign(existingAnnotation, snapshot); } else { - self.annotations.push(getAnnotationSnapshot(annotation)); + self.annotations.push(snapshot); } - }, + }), deleteAnnotation(annotation) { const index = self.annotations.findIndex((c) => { diff --git a/src/types/Task.ts b/src/types/Task.ts index f6fea008..69114308 100644 --- a/src/types/Task.ts +++ b/src/types/Task.ts @@ -98,5 +98,5 @@ export interface LSFAnnotation extends LSFAnnotationData { // editable: boolean; - serializeAnnotation(): APIResult[]; + serializeAnnotation(): Promise; }