mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-06 22:04:06 +00:00
progress
This commit is contained in:
@@ -278,3 +278,52 @@ class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const engine = new Engine();
|
export const engine = new Engine();
|
||||||
|
|
||||||
|
export type FileStatesCallback = (files: Map<string, Readable<FileState>>) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells when file stores appear and disappear, so that their subscribers know when to
|
||||||
|
* subscribe and unsubscribe. `onFilesAdded` is called with the stores of the new files only
|
||||||
|
* (including the ones already present when the observer is created).
|
||||||
|
*/
|
||||||
|
export class FileStateCollectionObserver {
|
||||||
|
private _fileIds = new Set<string>();
|
||||||
|
private _onFilesAdded: FileStatesCallback;
|
||||||
|
private _onFileRemoved: (fileId: string) => void;
|
||||||
|
private _onDestroy: () => void;
|
||||||
|
private _unsubscribe: () => void;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
onFilesAdded: FileStatesCallback,
|
||||||
|
onFileRemoved: (fileId: string) => void,
|
||||||
|
onDestroy: () => void
|
||||||
|
) {
|
||||||
|
this._onFilesAdded = onFilesAdded;
|
||||||
|
this._onFileRemoved = onFileRemoved;
|
||||||
|
this._onDestroy = onDestroy;
|
||||||
|
|
||||||
|
this._unsubscribe = engine.files.subscribe((files) => {
|
||||||
|
this._fileIds.forEach((fileId) => {
|
||||||
|
if (!files.has(fileId)) {
|
||||||
|
this._onFileRemoved(fileId);
|
||||||
|
this._fileIds.delete(fileId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const newFiles = new Map<string, Readable<FileState>>();
|
||||||
|
files.forEach((store, fileId) => {
|
||||||
|
if (!this._fileIds.has(fileId)) {
|
||||||
|
newFiles.set(fileId, store);
|
||||||
|
this._fileIds.add(fileId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (newFiles.size > 0) {
|
||||||
|
this._onFilesAdded(newFiles);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
this._onDestroy();
|
||||||
|
this._unsubscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user