Skip to content

Commit

Permalink
Switch to localStorage (onflow#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Sep 28, 2023
1 parent 809009c commit 3d037e8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-peaches-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl": minor
---

Use localStorage as default & export LOCAL_STORAGE/SESSION_STORAGE as helpers for fcl.storage.default configuration key
6 changes: 4 additions & 2 deletions packages/fcl/src/fcl.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from './shared-exports';
export * from "./shared-exports"

import {getMutate} from "./exec/mutate"
export const mutate = getMutate({platform: "web"})

import {getCurrentUser} from "./current-user"
const currentUser = getCurrentUser({platform:"web"})
const currentUser = getCurrentUser({platform: "web"})

export {currentUser}

Expand All @@ -19,6 +19,8 @@ export const logIn = (opts = {}) => currentUser().authenticate(opts)

export const authz = currentUser().authorization

export {LOCAL_STORAGE, SESSION_STORAGE} from "./utils/web"

import {config} from "@onflow/config"
import {getDefaultConfig, coreStrategies} from "./utils/web"
import {initServiceRegistry} from "./current-user/exec-service/plugins"
Expand Down
17 changes: 2 additions & 15 deletions packages/fcl/src/utils/web/default-config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
const isServerSide = () => typeof window === "undefined"

const getSessionStorage = () => {
try {
const SESSION_STORAGE = {
can: !isServerSide(),
get: async key => JSON.parse(sessionStorage.getItem(key)),
put: async (key, value) => sessionStorage.setItem(key, JSON.stringify(value)),
}
return SESSION_STORAGE
} catch (error) {
return null
}
}
import {LOCAL_STORAGE} from "./storage"

export const getDefaultConfig = () => {
return {
"discovery.wallet.method.default": "IFRAME/RPC",
"fcl.storage.default": getSessionStorage(),
"fcl.storage.default": LOCAL_STORAGE,
}
}
11 changes: 6 additions & 5 deletions packages/fcl/src/utils/web/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {renderFrame} from './render-frame'
export {renderPop} from './render-pop'
export {renderTab} from './render-tab'
export {getDefaultConfig} from './default-config'
export {coreStrategies} from './coreStrategies'
export {renderFrame} from "./render-frame"
export {renderPop} from "./render-pop"
export {renderTab} from "./render-tab"
export {getDefaultConfig} from "./default-config"
export {coreStrategies} from "./coreStrategies"
export {LOCAL_STORAGE, SESSION_STORAGE} from "./storage"
13 changes: 13 additions & 0 deletions packages/fcl/src/utils/web/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const isServerSide = () => typeof window === "undefined"

export const SESSION_STORAGE = {
can: !isServerSide(),
get: async key => JSON.parse(sessionStorage.getItem(key)),
put: async (key, value) => sessionStorage.setItem(key, JSON.stringify(value)),
}

export const LOCAL_STORAGE = {
can: !isServerSide(),
get: async key => JSON.parse(localStorage.getItem(key)),
put: async (key, value) => localStorage.setItem(key, JSON.stringify(value)),
}

0 comments on commit 3d037e8

Please sign in to comment.