Skip to content

Commit

Permalink
Fix saving at webworker (#2647)
Browse files Browse the repository at this point in the history
BUG

This PR is to fix the bug while saving models at webworker env. (#2643)

Note: Under webworker, we can only save models via IndexedDB. Maybe we should mention it at the docs?
  • Loading branch information
WenheLI committed Feb 4, 2020
1 parent fdd4603 commit 5afa7c2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tfjs-core/src/io/indexed_db.ts
Expand Up @@ -57,7 +57,7 @@ function getIndexedDBFactory(): IDBFactory {
'is not a web browser.');
}
// tslint:disable-next-line:no-any
const theWindow: any = window;
const theWindow: any = window || self;
const factory = theWindow.indexedDB || theWindow.mozIndexedDB ||
theWindow.webkitIndexedDB || theWindow.msIndexedDB ||
theWindow.shimIndexedDB;
Expand Down
5 changes: 4 additions & 1 deletion tfjs-core/src/io/local_storage.ts
Expand Up @@ -38,6 +38,7 @@ const MODEL_METADATA_SUFFIX = 'model_metadata';
*/
export function purgeLocalStorageArtifacts(): string[] {
if (!env().getBool('IS_BROWSER') ||
typeof window === 'undefined' ||
typeof window.localStorage === 'undefined') {
throw new Error(
'purgeLocalStorageModels() cannot proceed because local storage is ' +
Expand Down Expand Up @@ -119,7 +120,8 @@ export class BrowserLocalStorage implements IOHandler {

constructor(modelPath: string) {
if (!env().getBool('IS_BROWSER') ||
typeof window.localStorage === 'undefined') {
typeof window === 'undefined' ||
typeof window.localStorage === 'undefined') {
// TODO(cais): Add more info about what IOHandler subtypes are
// available.
// Maybe point to a doc page on the web and/or automatically determine
Expand Down Expand Up @@ -307,6 +309,7 @@ export class BrowserLocalStorageManager implements ModelStoreManager {
env().getBool('IS_BROWSER'),
() => 'Current environment is not a web browser');
assert(
typeof window === 'undefined' ||
typeof window.localStorage !== 'undefined',
() => 'Current browser does not appear to support localStorage');
this.LS = window.localStorage;
Expand Down

0 comments on commit 5afa7c2

Please sign in to comment.