Skip to content

Commit

Permalink
feat(@aws-amplify/datastore): support indexeddb adapter on web worker (
Browse files Browse the repository at this point in the history
  • Loading branch information
wylfent authored and nubpro committed Oct 2, 2020
1 parent e986614 commit a562edb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/JS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ export const makeQuerablePromise = promise => {
return result;
};

export const isWebWorker = () => {
if (typeof self === 'undefined') {
return false;
}
const selfContext = self as { WorkerGlobalScope? };
return typeof selfContext.WorkerGlobalScope !== 'undefined' &&
self instanceof selfContext.WorkerGlobalScope;
};

export const browserOrNode = () => {
const isBrowser =
typeof window !== 'undefined' && typeof window.document !== 'undefined';
Expand Down Expand Up @@ -268,6 +277,7 @@ export class JS {
static isTextFile = isTextFile;
static generateRandomString = generateRandomString;
static makeQuerablePromise = makeQuerablePromise;
static isWebWorker = isWebWorker;
static browserOrNode = browserOrNode;
static transferKeyToLowerCase = transferKeyToLowerCase;
static transferKeyToUpperCase = transferKeyToUpperCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { browserOrNode } from '@aws-amplify/core';
import { browserOrNode, isWebWorker } from '@aws-amplify/core';
import { Adapter } from '..';

const getDefaultAdapter: () => Adapter = () => {
const { isBrowser } = browserOrNode();

if (isBrowser && window.indexedDB) {
if ((isBrowser && window.indexedDB) || (isWebWorker() && self.indexedDB)) {
return require('../indexeddb').default;
}

Expand Down

0 comments on commit a562edb

Please sign in to comment.