Skip to content

Commit

Permalink
fix(storage): local api in specific cases
Browse files Browse the repository at this point in the history
Novout committed May 26, 2023

Verified

This commit was signed with the committer’s verified signature.
Novout Giovane Cardoso
1 parent 1fe3027 commit 9a91e0e
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
<div
class="flex justify-between items-center w-full bg-theme-aside-graph-background hover:bg-theme-aside-graph-background-hover active:bg-theme-aside-graph-background-active text-theme-aside-graph-text hover:text-theme-aside-graph-text-hover active:text-theme-aside-graph-text-active"
>
<InputColorPicker class="w-2 h-2" v-if="element.extra?.color" v-model="element.extra.color" />
<InputColorPicker v-if="element.extra?.color" v-model="element.extra.color" class="w-2 h-2" />
<div
class="truncate cursor-pointer"
@click.prevent.stop="schemas.onStart(element)"
17 changes: 14 additions & 3 deletions packages/client-storage/src/index.ts
Original file line number Diff line number Diff line change
@@ -3,17 +3,24 @@ import destr from 'destr'
import lz from 'lz-string'
import type { ClientStorageOptions, Maybe } from 'better-write-types'

// used for force indexeddb use in FF4 browsers and in specific TWA android build's. Even though indexeddb is not supported in some older versions, local saving will work correctly in any modern browser or environment.
const isLocalSupported = typeof Storage !== 'undefined'

export function set<T extends unknown>(
key: string,
data: T,
options: ClientStorageOptions
) {
if (!isLocalSupported) {
options.schema = 'indexeddb'
}

return new Promise((res) => {
const str = JSON.stringify(data)
const strResolved = options.compress ? lz.compress(str) : str

if (options.schema === 'indexeddb') {
localStorage.removeItem(key)
if(isLocalSupported) localStorage.removeItem(key)

indexeddb
.set(key, strResolved)
@@ -38,8 +45,12 @@ export function get<T extends unknown>(
key: string,
options: ClientStorageOptions
): Promise<Maybe<T>> {
if (!isLocalSupported) {
options.schema = 'indexeddb'
}

return new Promise((res) => {
const item = localStorage.getItem(key)
const item = isLocalSupported ? localStorage.getItem(key) : false

if (item)
res(
@@ -63,5 +74,5 @@ export function get<T extends unknown>(

export function exclude(key: string) {
indexeddb.del(key).catch(() => {})
localStorage.removeItem(key)
if (isLocalSupported) localStorage.removeItem(key)
}

0 comments on commit 9a91e0e

Please # to comment.