Skip to content

Commit

Permalink
fix: swarm.localStorage mimics window.localStorage with null and unde…
Browse files Browse the repository at this point in the history
…fined set (#72)
  • Loading branch information
nugaon authored Nov 29, 2021
1 parent e6b78d4 commit 700ac0c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 21 deletions.
11 changes: 3 additions & 8 deletions src/background/dapp-session.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,17 @@ class DappSecurityContext {

/** STORAGE FUNCTIONS */

public setStorageItem(keyName: string, keyValue: unknown): Promise<void> {
public async setStorageItem(keyName: string, keyValue: unknown): Promise<void> {
const key = this.enrichStorageKey(keyName)

return new Promise(resolve => this.storage.set({ [key]: keyValue, resolve }))
await new Promise(resolve => this.storage.set({ [key]: keyValue }, () => resolve(true)))
}

public getStorageItem(keyName: string): Promise<unknown> {
const key = this.enrichStorageKey(keyName)

return new Promise((resolve, reject) =>
return new Promise(resolve =>
this.storage.get([key], result => {
console.log('result', result)

if (!result[key]) {
reject(`LocalStorage: ${key} does not exist`)
}
resolve(result[key])
}),
)
Expand Down
2 changes: 1 addition & 1 deletion src/background/feeder/local-storage.feeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class LocalStorageFeeder {
) {
console.log(`LocalStorageFeeder: store set -> ${payload}`)

if (!payload || !payload[0] || !payload[1]) {
if (!payload || !payload[0]) {
response.error = `LocalStorageFeeder: wrong payload: Got ${payload}`

return response
Expand Down
6 changes: 1 addition & 5 deletions src/contentscript/document-start/messenger.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,9 @@ export class MessengerInterceptor {
throw new Error(message.error)
}

if (!message.answer) {
throw new Error(`No answer from message handler at key "${message.key}"`)
}

return {
key: message.key,
answer: message.answer,
answer: message.answer as T,
target: 'content',
sender: 'background',
}
Expand Down
8 changes: 1 addition & 7 deletions src/contentscript/swarm-library/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ export class LocalStorage extends MessengerInpage implements ILocalStorageMessag
// handle message
if (response.data.error) reject(response.data.error)

if (response.data.answer === undefined) {
const errorMessage = `LocalStorage: no answer. EventID: ${response.data.eventId}`
console.error(errorMessage, response)
reject(errorMessage)
} else {
resolve(response.data.answer)
}
resolve(response.data.answer)
}

window.addEventListener('message', handler)
Expand Down

0 comments on commit 700ac0c

Please sign in to comment.