forked from super-productivity/super-productivity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-providers.factory.ts
More file actions
72 lines (63 loc) · 2.56 KB
/
sync-providers.factory.ts
File metadata and controls
72 lines (63 loc) · 2.56 KB
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
69
70
71
72
import { SyncProviderId } from './provider.const';
import { SyncProviderBase } from './provider.interface';
import { DROPBOX_APP_KEY } from '../../imex/sync/dropbox/dropbox.const';
import { IS_ELECTRON } from '../../app.constants';
import { IS_ANDROID_WEB_VIEW } from '../../util/is-android-web-view';
import { environment } from '../../../environments/environment';
let _providersPromise: Promise<SyncProviderBase<SyncProviderId>[]> | null = null;
/**
* Lazily loads and instantiates all sync providers.
* Provider modules (Dropbox, WebDAV, SuperSync, LocalFile) are only imported
* when this function is first called, keeping them out of the initial bundle.
*/
export const loadSyncProviders = (): Promise<SyncProviderBase<SyncProviderId>[]> => {
if (!_providersPromise) {
_providersPromise = _createProviders().catch((err) => {
_providersPromise = null;
throw err;
});
}
return _providersPromise;
};
/**
* Narrow interface for LocalFile sync providers that expose directory picker methods.
* Used to avoid `as any` casts in sync-form.const.ts.
*/
export interface LocalFileSyncPicker {
pickDirectory(): Promise<string | void>;
setupSaf(): Promise<string>;
}
const _createProviders = async (): Promise<SyncProviderBase<SyncProviderId>[]> => {
const [
{ createDropboxProvider },
{ createWebdavProvider },
{ createSuperSyncProvider },
{ createNextcloudProvider },
] = await Promise.all([
import('./file-based/dropbox/dropbox'),
import('./file-based/webdav/webdav'),
import('./super-sync/super-sync'),
import('./file-based/webdav/nextcloud'),
]);
const extraPath = environment.production ? undefined : `/DEV`;
const providers: SyncProviderBase<SyncProviderId>[] = [
createDropboxProvider({
appKey: DROPBOX_APP_KEY,
basePath: environment.production ? `/` : `/DEV/`,
}) as SyncProviderBase<SyncProviderId>,
createWebdavProvider(extraPath) as SyncProviderBase<SyncProviderId>,
createSuperSyncProvider() as SyncProviderBase<SyncProviderId>,
createNextcloudProvider(extraPath) as SyncProviderBase<SyncProviderId>,
];
if (IS_ELECTRON) {
const { createLocalFileSyncElectron } =
await import('./file-based/local-file/local-file-sync-electron');
providers.push(createLocalFileSyncElectron() as SyncProviderBase<SyncProviderId>);
}
if (IS_ANDROID_WEB_VIEW) {
const { createLocalFileSyncAndroid } =
await import('./file-based/local-file/local-file-sync-android');
providers.push(createLocalFileSyncAndroid() as SyncProviderBase<SyncProviderId>);
}
return providers;
};