Open 981377660LMT opened 7 months ago
export interface IFailedFileStore {
add: (f: IFailedFile) => void;
remove: (f: IFailedFile) => void;
clear: () => void;
update: (f: IFailedFile, updateProps: Partial<IFailedFile> | ((f: IFailedFile) => void)) => void;
getAll: () => IFailedFile[];
}
class FailedFileStore implements IFailedFileStore {
@observable private _failedFiles: IFailedFile[] = [];
constructor () {
extendObservable(this, {
// How to check if object is in Mobx observable array
// https://stackoverflow.com/a/43509952
_failedFiles: observable.array(this._failedFiles, { deep: false })
});
}
add = action((f: IFailedFile): void => {
this._failedFiles.push(f);
});
remove = action((f: IFailedFile): void => {
const index = this._failedFiles.indexOf(f);
if (index !== -1) {
this._failedFiles[index].dispose();
this._failedFiles.splice(index, 1);
} else {
console.warn(`FailedFileStore: remove failed file not found: ${f}`);
}
});
clear = action((): void => {
this._failedFiles.forEach((f) => f.dispose());
this._failedFiles.splice(0);
});
update = action((f: IFailedFile, updateProps: Partial<IFailedFile> | ((recipe: IFailedFile) => void)): void => {
const index = this._failedFiles.indexOf(f);
if (index !== -1) {
this._failedFiles.splice(index, 1);
} else {
console.warn(`FailedFileStore: update failed file not found: ${f}`);
return;
}
if (typeof updateProps === 'function') {
updateProps(f);
} else {
Object.assign(f, updateProps);
}
this._failedFiles.splice(index, 0, f);
});
getAll = (): IFailedFile[] => {
return this._failedFiles.slice();
};
}