Skip to content

Commit

Permalink
provider: check availability of window object
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed May 18, 2023
1 parent ca6ba5a commit 9c81e4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions packages/provider/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,23 @@ export class LocalStorage {
private constructor() {}

static getInstance(): ItemStore {
if (!LocalStorage._instance) {
LocalStorage._instance = {
getItem: (key: string) => Promise.resolve(window.localStorage.getItem(key)),
setItem: (key: string, value: string) => Promise.resolve(window.localStorage.setItem(key, value)),
removeItem: (key: string) => Promise.resolve(window.localStorage.removeItem(key))
if (typeof window === 'object') {
if (!LocalStorage._instance) {
LocalStorage._instance = {
getItem: (key: string) => Promise.resolve(window.localStorage.getItem(key)),
setItem: (key: string, value: string) => Promise.resolve(window.localStorage.setItem(key, value)),
removeItem: (key: string) => Promise.resolve(window.localStorage.removeItem(key))
}
}
} else {
// noop local storage if window is not defined
// TODO: perhaps add an in-memory local storage if we need?
if (!LocalStorage._instance) {
LocalStorage._instance = {
getItem: (key: string) => Promise.resolve(null),
setItem: (key: string, value: string) => Promise.resolve(),
removeItem: (key: string) => Promise.resolve()
}
}
}
return this._instance
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Wallet implements WalletProvider {
this.transport.messageProvider = new MuxMessageProvider()

// multiple message provider setup, first one to connect will be the main transport
if (this.config.transports?.windowTransport?.enabled) {
if (this.config.transports?.windowTransport?.enabled && typeof window === 'object') {
this.transport.windowMessageProvider = new WindowMessageProvider(this.config.walletAppURL)
this.transport.messageProvider.add(this.transport.windowMessageProvider)
}
Expand Down

0 comments on commit 9c81e4f

Please sign in to comment.