-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaseCacheService.ts
68 lines (59 loc) · 2.55 KB
/
BaseCacheService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { CacheDownloadMode } from 'utils/record-cache';
/**
* @desc Generic Base Class for All Caching actions using SQLite or LocalForage ensuring consistency between all Database implementations
* @property { RepoMetadata } _ All Details contained by a Metadata Entry
* @property { RepositoryDownloadRequestSpec } _ Information details for a Download e.g. API Url, Cache Targets, bounding boxes
* @property { ProgressCallbackParams } _ Details for Callback Functionality, used for Progress bars, updates
* @property { RepositoryStatusSchema } _ Enum for Cache Statuses, e.g. READY, CACHED, ERROR, etc
* @example Implementation abstract Class Demo extends BaseCacheService<
* RepoMetadataType
* RepositoryDownloadRequestSpecType
* RepoProgressCallbackParamsType
* RepositoryStatusSchemeType>
*/
abstract class BaseCacheService<
RepoMetadata,
RepositoryDownloadRequestSpec,
ProgressCallbackParams,
RepositoryStatusSchema
> {
/** Update any details for a Repository */
protected abstract addOrUpdateRepository(repositoryId: RepoMetadata): Promise<void>;
/** Remove one Repository from the collection along with its contentes */
public abstract deleteRepository(repositoryId: string): Promise<void>;
/** Pull metadata for one Repository in the Collection */
public abstract getRepository(repositoryId: string): Promise<RepoMetadata | null>;
/** List metadata for all Repositories */
public abstract listRepositories(): Promise<RepoMetadata[]>;
/** Change the status for a given Repository */
public abstract setRepositoryStatus(repositoryId: string, status: RepositoryStatusSchema): Promise<void>;
/** Download a new Repository along with its contents */
public abstract download(
spec: RepositoryDownloadRequestSpec,
progressCallback?: (currentProgress: ProgressCallbackParams) => void
): Promise<CacheDownloadMode | void>;
/**
* @desc Concurrency helper. If one call fails, makes a second attempt before failing over.
* @param executing Promises in progress
* @param promiseFn Promises to Execute
*/
protected async processNext(executing: Set<Promise<void>>, promiseFn: () => Promise<void>) {
const promise = promiseFn();
executing.add(promise);
try {
await promise;
} catch (e) {
console.error(e);
try {
await promise;
} catch (e) {
console.error('Failed second attempt at Promise');
throw e;
}
} finally {
executing.delete(promise);
}
}
protected constructor() {}
}
export default BaseCacheService;