|
|
@ -11,9 +11,12 @@ import FileManager from './filemanager'; |
|
|
|
//import { logger } from './polyfill';
|
|
|
|
//import { logger } from './polyfill';
|
|
|
|
|
|
|
|
|
|
|
|
export default class CacheStorageController { |
|
|
|
export default class CacheStorageController { |
|
|
|
|
|
|
|
public static STORAGES: CacheStorageController[] = []; |
|
|
|
//public dbName = 'cachedFiles';
|
|
|
|
//public dbName = 'cachedFiles';
|
|
|
|
public openDbPromise: Promise<Cache>; |
|
|
|
public openDbPromise: Promise<Cache>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public useStorage = true; |
|
|
|
|
|
|
|
|
|
|
|
//private log: ReturnType<typeof logger> = logger('CS');
|
|
|
|
//private log: ReturnType<typeof logger> = logger('CS');
|
|
|
|
|
|
|
|
|
|
|
|
constructor(public dbName: string) { |
|
|
|
constructor(public dbName: string) { |
|
|
@ -22,6 +25,7 @@ export default class CacheStorageController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.openDatabase(); |
|
|
|
this.openDatabase(); |
|
|
|
|
|
|
|
CacheStorageController.STORAGES.push(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public openDatabase(): Promise<Cache> { |
|
|
|
public openDatabase(): Promise<Cache> { |
|
|
@ -33,8 +37,8 @@ export default class CacheStorageController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public delete(entryName: string) { |
|
|
|
public delete(entryName: string) { |
|
|
|
return this.timeoutOperation(async(cache) => { |
|
|
|
return this.timeoutOperation((cache) => { |
|
|
|
const deleted = await cache.delete('/' + entryName); |
|
|
|
return cache.delete('/' + entryName); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -43,12 +47,16 @@ export default class CacheStorageController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public save(entryName: string, response: Response) { |
|
|
|
public save(entryName: string, response: Response) { |
|
|
|
|
|
|
|
if(!this.useStorage) return Promise.reject('STORAGE_OFFLINE'); |
|
|
|
|
|
|
|
|
|
|
|
return this.timeoutOperation((cache) => { |
|
|
|
return this.timeoutOperation((cache) => { |
|
|
|
return cache.put('/' + entryName, response); |
|
|
|
return cache.put('/' + entryName, response); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public saveFile(fileName: string, blob: Blob | Uint8Array) { |
|
|
|
public saveFile(fileName: string, blob: Blob | Uint8Array) { |
|
|
|
|
|
|
|
if(!this.useStorage) return Promise.reject('STORAGE_OFFLINE'); |
|
|
|
|
|
|
|
|
|
|
|
//return Promise.resolve(blobConstruct([blob]));
|
|
|
|
//return Promise.resolve(blobConstruct([blob]));
|
|
|
|
if(!(blob instanceof Blob)) { |
|
|
|
if(!(blob instanceof Blob)) { |
|
|
|
blob = blobConstruct(blob) as Blob; |
|
|
|
blob = blobConstruct(blob) as Blob; |
|
|
@ -64,6 +72,8 @@ export default class CacheStorageController { |
|
|
|
} */ |
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
|
|
public getFile(fileName: string, method: 'blob' | 'json' | 'text' = 'blob'): Promise<any> { |
|
|
|
public getFile(fileName: string, method: 'blob' | 'json' | 'text' = 'blob'): Promise<any> { |
|
|
|
|
|
|
|
if(!this.useStorage) return Promise.reject('STORAGE_OFFLINE'); |
|
|
|
|
|
|
|
|
|
|
|
/* if(method === 'blob') { |
|
|
|
/* if(method === 'blob') { |
|
|
|
return Promise.reject(); |
|
|
|
return Promise.reject(); |
|
|
|
} */ |
|
|
|
} */ |
|
|
@ -120,6 +130,16 @@ export default class CacheStorageController { |
|
|
|
|
|
|
|
|
|
|
|
return Promise.resolve(fakeWriter); |
|
|
|
return Promise.resolve(fakeWriter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static toggleStorage(enabled: boolean) { |
|
|
|
|
|
|
|
return Promise.all(this.STORAGES.map(storage => { |
|
|
|
|
|
|
|
storage.useStorage = enabled; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!enabled) { |
|
|
|
|
|
|
|
return storage.deleteAll(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//const cacheStorage = new CacheStorageController();
|
|
|
|
//const cacheStorage = new CacheStorageController();
|
|
|
|