-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from rainlanguage/2024-02-07-gui-refactor-stores
2024 02 07 gui refactor stores
- Loading branch information
Showing
9 changed files
with
97 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,7 @@ | ||
import { get, writable } from 'svelte/store'; | ||
import { get } from 'svelte/store'; | ||
import type { Order } from '$lib/typeshare/orderDetail'; | ||
import { invoke } from '@tauri-apps/api'; | ||
import { subgraphUrl } from '$lib/stores/settings'; | ||
import { detailStore } from '$lib/storesGeneric/detailStore'; | ||
|
||
function useOrderDetailStore() { | ||
const STORAGE_KEY = "orders.orderDetail"; | ||
|
||
const { subscribe, update } = writable<{[id: string]: Order}>(localStorage.getItem(STORAGE_KEY) ? JSON.parse(localStorage.getItem(STORAGE_KEY) as string) : {}); | ||
|
||
subscribe(value => { | ||
if(value) { | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(value)); | ||
} else { | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify({})); | ||
} | ||
}); | ||
|
||
async function refetch(id: string) { | ||
const res: Order = await invoke("order_detail", {id, subgraphArgs: { url: get(subgraphUrl)} }); | ||
update((value) => { | ||
return {... value, [id]: res}; | ||
}); | ||
} | ||
|
||
return { | ||
subscribe, | ||
refetch | ||
} | ||
} | ||
|
||
export const orderDetail = useOrderDetailStore(); | ||
export const orderDetail = detailStore<Order>("orders.orderDetail", (id: string) => invoke("order_detail", {id, subgraphArgs: { url: get(subgraphUrl)} })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,7 @@ | ||
import { get, writable } from 'svelte/store'; | ||
import type { TokenVault } from '$lib/typeshare/vaultDetail'; | ||
import { get } from 'svelte/store'; | ||
import { invoke } from '@tauri-apps/api'; | ||
import { subgraphUrl } from '$lib/stores/settings'; | ||
import { detailStore } from '$lib/storesGeneric/detailStore'; | ||
|
||
function useVaultDetailStore() { | ||
const STORAGE_KEY = "vaults.vaultsDetail"; | ||
|
||
const { subscribe, update } = writable<{[id: string]: TokenVault}>(localStorage.getItem(STORAGE_KEY) ? JSON.parse(localStorage.getItem(STORAGE_KEY) as string) : {}); | ||
|
||
subscribe(value => { | ||
if(value) { | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(value)); | ||
} else { | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify({})); | ||
} | ||
}); | ||
|
||
async function refetch(id: string) { | ||
const res: TokenVault = await invoke("vault_detail", {id, subgraphArgs: { url: get(subgraphUrl)} }); | ||
update((value) => { | ||
return {... value, [id]: res}; | ||
}); | ||
} | ||
|
||
return { | ||
subscribe, | ||
refetch | ||
} | ||
} | ||
|
||
export const vaultDetail = useVaultDetailStore(); | ||
export const vaultDetail = detailStore<TokenVault>("vaults.vaultsDetail", (id: string) => invoke("vault_detail", {id, subgraphArgs: { url: get(subgraphUrl)} })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { writable } from "svelte/store"; | ||
|
||
export function cachedWritableStore<T>( | ||
key: string, | ||
defaultValue: T, | ||
serialize: (value: T) => string, | ||
deserialize: (serialized: string) => T | ||
) { | ||
const getCache = () => { | ||
const cached = localStorage.getItem(key); | ||
return cached ? deserialize(cached) : defaultValue; | ||
} | ||
const setCache = (value?: T) => { | ||
if(value !== undefined) { | ||
localStorage.setItem(key, serialize(value)); | ||
} else { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
|
||
const data = writable<T>(getCache()); | ||
|
||
data.subscribe((value) => { | ||
setCache(value); | ||
}) | ||
|
||
return data; | ||
} | ||
|
||
export const cachedWritableString = (key: string, defaultValue = '') => cachedWritableStore<string>(key, defaultValue, (v) => v, (v) => v); | ||
export const cachedWritableInt = (key: string, defaultValue = 0) => cachedWritableStore<number>(key, defaultValue, (v) => v.toString(), (v) => parseInt(v)); | ||
|
||
|
||
export const cachedWritableOptionalStore = <T>( | ||
key: string, | ||
defaultValue: T | undefined = undefined, | ||
serialize: (value: T) => string, | ||
deserialize: (serialized: string) => T | ||
) => cachedWritableStore<T | undefined>(key, defaultValue, (v) => v ? serialize(v) : '', (v) => v ? deserialize(v) : undefined); | ||
|
||
export const cachedWritableIntOptional = (key: string, defaultValue = undefined) => cachedWritableOptionalStore<number>(key, defaultValue, (v) => v.toString(), (v) => parseInt(v)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { cachedWritableStore } from '$lib/storesGeneric/cachedWritableStore'; | ||
|
||
export function detailStore<T>(key: string, fetchById: (id: string) => Promise<T>) { | ||
const {subscribe, update} = cachedWritableStore<{[id: string]: T}>(key, {}, (value) => JSON.stringify(value), (value) => JSON.parse(value)); | ||
|
||
subscribe(value => { | ||
if(value) { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
} else { | ||
localStorage.setItem(key, JSON.stringify({})); | ||
} | ||
}); | ||
|
||
async function refetch(id: string) { | ||
const res: T = await fetchById(id); | ||
update((value) => { | ||
return {... value, [id]: res}; | ||
}); | ||
} | ||
|
||
return { | ||
subscribe, | ||
refetch | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters