Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Commit 8df21b1

Browse files
Lynnderegtd
authored andcommitted
detects worker context and returns proper window ref (#51)
1 parent a12263b commit 8df21b1

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/IndexedDbProvider.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export class IndexedDbProvider extends NoSqlProvider.DbProvider {
4545
this._dbFactory = explicitDbFactory;
4646
this._fakeComplicatedKeys = !explicitDbFactorySupportsCompoundKeys;
4747
} else {
48-
this._dbFactory = window._indexedDB || window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
48+
const win = this.getWindow();
49+
this._dbFactory = win._indexedDB || win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB;
4950

5051
if (typeof explicitDbFactorySupportsCompoundKeys !== 'undefined') {
5152
this._fakeComplicatedKeys = !explicitDbFactorySupportsCompoundKeys;
@@ -57,6 +58,20 @@ export class IndexedDbProvider extends NoSqlProvider.DbProvider {
5758
}
5859
}
5960

61+
/**
62+
* Gets global window object - whether operating in worker or UI thread context.
63+
* Adapted from: https://stackoverflow.com/questions/7931182/reliably-detect-if-the-script-is-executing-in-a-web-worker
64+
*/
65+
getWindow() {
66+
if (typeof window === 'object' && window.document) {
67+
return window;
68+
} else if (self && self.document === undefined) {
69+
return self;
70+
}
71+
72+
throw new Error('Undefined context');
73+
}
74+
6075
static WrapRequest<T>(req: IDBRequest): SyncTasks.Promise<T> {
6176
const task = SyncTasks.Defer<T>();
6277

0 commit comments

Comments
 (0)