Skip to content

Commit

Permalink
chore(tauri/ui): rename generic stores for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg committed Feb 7, 2024
1 parent cb83ee2 commit 536024a
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions tauri-app/src/lib/stores/orderDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ 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 { useDetailStore } from '$lib/storesGeneric/detailStore';
import { detailStore } from '$lib/storesGeneric/detailStore';

export const orderDetail = useDetailStore<Order>("orders.orderDetail", (id: string) => invoke("order_detail", {id, subgraphArgs: { url: get(subgraphUrl)} }));
export const orderDetail = detailStore<Order>("orders.orderDetail", (id: string) => invoke("order_detail", {id, subgraphArgs: { url: get(subgraphUrl)} }));
4 changes: 2 additions & 2 deletions tauri-app/src/lib/stores/ordersList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { get } from 'svelte/store';
import type { Order } from '$lib/typeshare/ordersList';
import { invoke } from '@tauri-apps/api';
import { subgraphUrl } from '$lib/stores/settings';
import { usePaginatedCachedStore } from '$lib/storesGeneric/paginatedStore';
import { listStore } from '$lib/storesGeneric/listStore';

export const ordersList = usePaginatedCachedStore<Order>(
export const ordersList = listStore<Order>(
'ordersList',
(page) => invoke("orders_list", {subgraphArgs: { url: get(subgraphUrl)}, paginationArgs: { page, page_size: 10 } }),
(path) => invoke("orders_list_write_csv", { path, subgraphArgs: { url: get(subgraphUrl)}, paginationArgs: { page: 1, page_size: 1000 } })
Expand Down
2 changes: 1 addition & 1 deletion tauri-app/src/lib/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { derived } from 'svelte/store';
import every from 'lodash/every';
import { isAddress } from 'viem';
import { updateChainId } from '$lib/stores/chain';
import { cachedWritableInt, cachedWritableString } from '$lib/storesGeneric/cachedWritable';
import { cachedWritableInt, cachedWritableString } from '$lib/storesGeneric/cachedWritableStore';

export const rpcUrl = cachedWritableString("settings.rpcUrl", '');
export const subgraphUrl = cachedWritableString("settings.subgraphUrl", '');
Expand Down
4 changes: 2 additions & 2 deletions tauri-app/src/lib/stores/vaultDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ 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 { useDetailStore } from '$lib/storesGeneric/detailStore';
import { detailStore } from '$lib/storesGeneric/detailStore';

export const vaultDetail = useDetailStore<TokenVault>("vaults.vaultsDetail", (id: string) => invoke("vault_detail", {id, subgraphArgs: { url: get(subgraphUrl)} }));
export const vaultDetail = detailStore<TokenVault>("vaults.vaultsDetail", (id: string) => invoke("vault_detail", {id, subgraphArgs: { url: get(subgraphUrl)} }));
5 changes: 3 additions & 2 deletions tauri-app/src/lib/stores/vaultListBalanceChanges.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { get } from 'svelte/store';
import { invoke } from '@tauri-apps/api';
import { subgraphUrl } from '$lib/stores/settings';
import { usePaginatedCachedStore } from '$lib/storesGeneric/paginatedStore';
import { listStore } from '$lib/storesGeneric/listStore';
import type { VaultBalanceChange } from '$lib/typeshare/vaultListBalanceChanges';
export const useVaultListBalanceChanges = (id: string) => usePaginatedCachedStore<VaultBalanceChange>(

export const useVaultListBalanceChanges = (id: string) => listStore<VaultBalanceChange>(
`vaultListBalanceChanges-${id}`,
(page) => invoke("vault_list_balance_changes", {subgraphArgs: { url: get(subgraphUrl)}, id, paginationArgs: { page, page_size: 10 } }),
(path) => invoke("vault_list_balance_changes_write_csv", {path, subgraphArgs: { url: get(subgraphUrl)}, id, paginationArgs: { page: 1, page_size: 1000 } })
Expand Down
4 changes: 2 additions & 2 deletions tauri-app/src/lib/stores/vaultsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { get } from 'svelte/store';
import type { TokenVault } from '$lib/typeshare/vaultsList';
import { invoke } from '@tauri-apps/api';
import { subgraphUrl } from '$lib/stores/settings';
import { usePaginatedCachedStore } from '$lib/storesGeneric/paginatedStore';
import { listStore } from '$lib/storesGeneric/listStore';


export const vaultsList = usePaginatedCachedStore<TokenVault>(
export const vaultsList = listStore<TokenVault>(
'vaultsList',
(page) => invoke("vaults_list", {subgraphArgs: { url: get(subgraphUrl)}, paginationArgs: { page, page_size: 10 } }),
(path) => invoke("vaults_list_write_csv", {path, subgraphArgs: { url: get(subgraphUrl)}, paginationArgs: { page: 1, page_size: 1000 } })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writable } from "svelte/store";

export function useCachedWritable<T>(
export function cachedWritableStore<T>(
key: string,
defaultValue: T,
serialize: (value: T) => string,
Expand All @@ -27,15 +27,15 @@ export function useCachedWritable<T>(
return data;
}

export const cachedWritableString = (key: string, defaultValue = '') => useCachedWritable<string>(key, defaultValue, (v) => v, (v) => v);
export const cachedWritableInt = (key: string, defaultValue = 0) => useCachedWritable<number>(key, defaultValue, (v) => v.toString(), (v) => parseInt(v));
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 useCachedWritableOptional = <T>(
export const cachedWritableOptionalStore = <T>(
key: string,
defaultValue: T | undefined = undefined,
serialize: (value: T) => string,
deserialize: (serialized: string) => T
) => useCachedWritable<T | undefined>(key, defaultValue, (v) => v ? serialize(v) : '', (v) => v ? deserialize(v) : undefined);
) => cachedWritableStore<T | undefined>(key, defaultValue, (v) => v ? serialize(v) : '', (v) => v ? deserialize(v) : undefined);

export const cachedWritableIntOptional = (key: string, defaultValue = undefined) => useCachedWritableOptional<number>(key, defaultValue, (v) => v.toString(), (v) => parseInt(v));
export const cachedWritableIntOptional = (key: string, defaultValue = undefined) => cachedWritableOptionalStore<number>(key, defaultValue, (v) => v.toString(), (v) => parseInt(v));
6 changes: 3 additions & 3 deletions tauri-app/src/lib/storesGeneric/detailStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCachedWritable } from '$lib/storesGeneric/cachedWritable';
import { cachedWritableStore } from '$lib/storesGeneric/cachedWritableStore';

export function useDetailStore<T>(key: string, fetchById: (id: string) => Promise<T>) {
const {subscribe, update} = useCachedWritable<{[id: string]: T}>(key, {}, (value) => JSON.stringify(value), (value) => JSON.parse(value));
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toasts } from '../stores/toasts';
import { save } from '@tauri-apps/api/dialog';
import dayjs from 'dayjs';
import { ToastMessageType } from '$lib/typeshare/toast';
import { useCachedWritable } from '$lib/storesGeneric/cachedWritable';
import { cachedWritableStore } from '$lib/storesGeneric/cachedWritableStore';

type Unsubscriber = () => void;

Expand All @@ -28,9 +28,9 @@ export interface AllPages<T> {
}


const cachedWritablePages = <T>(key: string) => useCachedWritable<AllPages<T>>(key, [], (value) => JSON.stringify(value), (value) => JSON.parse(value));
const cachedWritablePages = <T>(key: string) => cachedWritableStore<AllPages<T>>(key, [], (value) => JSON.stringify(value), (value) => JSON.parse(value));

export function usePaginatedCachedStore<T>(key: string, fetchPageHandler: (page: number) => Promise<Array<T>>, writeCsvHandler: (path: string) => Promise<void>) {
export function listStore<T>(key: string, fetchPageHandler: (page: number) => Promise<Array<T>>, writeCsvHandler: (path: string) => Promise<void>) {
const allPages = cachedWritablePages<T>(key);
const pageIndex = writable(1);
const isFetching = writable(false);
Expand Down

0 comments on commit 536024a

Please sign in to comment.