mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-10 07:36:29 +00:00
refactoring and tests
This commit is contained in:
1 parent
3bb553d6b5
commit
da9abbae5e
45 files changed
+3558
-1564
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import type { Readable } from 'svelte/store';
|
||||
import { engine } from './engine';
|
||||
import type { FileState, FileStatesCallback } from './types';
|
||||
|
||||
/**
|
||||
* 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