Skip to content

Commit

Permalink
feat(store-sync): remove unused generics (#3218)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Sep 23, 2024
1 parent 2cfddff commit 7c7bdb2
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 71 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-poems-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/common": patch
"@latticexyz/store-sync": patch
---

Removed unused generics and ensure that we're only passing around the generics we need, when we need them. Hopefully this improves TS performance in MUD projects.
6 changes: 3 additions & 3 deletions packages/common/src/actions/transactionQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export type TransactionQueueOptions<chain extends Chain> = {
queueConcurrency?: number;
};

export function transactionQueue<chain extends Chain, account extends Account>(
export function transactionQueue<chain extends Chain>(
opts: TransactionQueueOptions<chain> = {},
): (
client: Client<Transport, chain, account>,
): <transport extends Transport, account extends Account | undefined = Account | undefined>(
client: Client<transport, chain, account>,
) => Pick<WalletActions<chain, account>, "writeContract" | "sendTransaction"> {
return (client) => ({
// Applies to: `client.writeContract`, `getContract(client, ...).write`
Expand Down
7 changes: 1 addition & 6 deletions packages/store-sync/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from "@latticexyz/protocol-parser/internal";
import storeConfig from "@latticexyz/store/mud.config";
import worldConfig from "@latticexyz/world/mud.config";
import { Store as StoreConfig } from "@latticexyz/store";
import { Table as ConfigTable, Schema } from "@latticexyz/config";
import { configToTables } from "./configToTables";

Expand Down Expand Up @@ -66,11 +65,7 @@ export type SyncFilter = {
key1?: Hex;
};

export type SyncOptions<config extends StoreConfig = StoreConfig> = {
/**
* MUD config
*/
config?: config;
export type SyncOptions = {
/**
* [viem `PublicClient`][0] used for fetching logs from the RPC.
*
Expand Down
10 changes: 3 additions & 7 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ import { SyncStep } from "./SyncStep";
import { bigIntMax, chunk, isDefined, waitForIdle } from "@latticexyz/common/utils";
import { getSnapshot } from "./getSnapshot";
import { fetchAndStoreLogs } from "./fetchAndStoreLogs";
import { Store as StoreConfig } from "@latticexyz/store";

const debug = parentDebug.extend("createStoreSync");

const defaultFilters: SyncFilter[] = internalTableIds.map((tableId) => ({ tableId }));

type CreateStoreSyncOptions<config extends StoreConfig = StoreConfig> = SyncOptions<config> & {
type CreateStoreSyncOptions = SyncOptions & {
storageAdapter: StorageAdapter;
onProgress?: (opts: {
step: SyncStep;
Expand All @@ -50,7 +49,7 @@ type CreateStoreSyncOptions<config extends StoreConfig = StoreConfig> = SyncOpti
}) => void;
};

export async function createStoreSync<config extends StoreConfig = StoreConfig>({
export async function createStoreSync({
storageAdapter,
onProgress,
publicClient,
Expand All @@ -63,7 +62,7 @@ export async function createStoreSync<config extends StoreConfig = StoreConfig>(
initialState,
initialBlockLogs,
indexerUrl,
}: CreateStoreSyncOptions<config>): Promise<SyncResult> {
}: CreateStoreSyncOptions): Promise<SyncResult> {
const filters: SyncFilter[] =
initialFilters.length || tableIds.length
? [...initialFilters, ...tableIds.map((tableId) => ({ tableId })), ...defaultFilters]
Expand Down Expand Up @@ -292,15 +291,12 @@ export async function createStoreSync<config extends StoreConfig = StoreConfig>(
}
} catch (e) {
const error = e as GetTransactionReceiptErrorType;

if (error.name === "TransactionReceiptNotFoundError") {
return;
}

throw error;
}
}),
tap((result) => debug("has tx?", tx, result)),
);

return await firstValueFrom(hasTransaction$.pipe(filter(isDefined)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Hex, PublicClient, concatHex, getAddress } from "viem";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { and, eq } from "drizzle-orm";
import { buildTable } from "./buildTable";
import { Store as StoreConfig } from "@latticexyz/store";
import { debug } from "./debug";
import { StorageAdapter, StorageAdapterBlock } from "../common";
import { isTableRegistrationLog } from "../isTableRegistrationLog";
Expand All @@ -22,16 +21,14 @@ export type PostgresStorageAdapter = {
cleanUp: () => Promise<void>;
};

export async function createStorageAdapter<config extends StoreConfig = StoreConfig>({
export async function createStorageAdapter({
database,
publicClient,
config,
}: {
database: PgDatabase<QueryResultHKT>;
publicClient: PublicClient;
config?: config;
}): Promise<PostgresStorageAdapter> {
const bytesStorageAdapter = await createBytesStorageAdapter({ database, publicClient, config });
const bytesStorageAdapter = await createBytesStorageAdapter({ database, publicClient });
const cleanUp: (() => Promise<void>)[] = [];

async function postgresStorageAdapter({ blockNumber, logs }: StorageAdapterBlock): Promise<void> {
Expand Down
17 changes: 5 additions & 12 deletions packages/store-sync/src/postgres-decoded/syncToPostgres.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Store as StoreConfig } from "@latticexyz/store";
import { PgDatabase } from "drizzle-orm/pg-core";
import { SyncOptions, SyncResult } from "../common";
import { createStorageAdapter } from "./createStorageAdapter";
import { createStoreSync } from "../createStoreSync";

export type SyncToPostgresOptions<config extends StoreConfig = StoreConfig> = SyncOptions<config> & {
export type SyncToPostgresOptions = SyncOptions & {
/**
* [Postgres database object from Drizzle][0].
*
Expand All @@ -25,20 +24,14 @@ export type SyncToPostgresResult = SyncResult & {
* @param {CreateIndexerOptions} options See `CreateIndexerOptions`.
* @returns A function to unsubscribe from the block stream, effectively stopping the indexer.
*/
export async function syncToPostgres<config extends StoreConfig = StoreConfig>({
config,
export async function syncToPostgres({
database,
publicClient,
startSync = true,
...syncOptions
}: SyncToPostgresOptions<config>): Promise<SyncToPostgresResult> {
const { storageAdapter } = await createStorageAdapter({ database, publicClient, config });
const storeSync = await createStoreSync({
storageAdapter,
config,
publicClient,
...syncOptions,
});
}: SyncToPostgresOptions): Promise<SyncToPostgresResult> {
const { storageAdapter } = await createStorageAdapter({ database, publicClient });
const storeSync = await createStoreSync({ storageAdapter, publicClient, ...syncOptions });

const sub = startSync ? storeSync.storedBlockLogs$.subscribe() : null;
const stopSync = (): void => {
Expand Down
4 changes: 1 addition & 3 deletions packages/store-sync/src/postgres/createStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PublicClient, encodePacked, size } from "viem";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { and, eq } from "drizzle-orm";
import { Store as StoreConfig } from "@latticexyz/store";
import { debug } from "./debug";
import { tables } from "./tables";
import { spliceHex } from "@latticexyz/common";
Expand All @@ -17,13 +16,12 @@ export type PostgresStorageAdapter = {
cleanUp: () => Promise<void>;
};

export async function createStorageAdapter<config extends StoreConfig = StoreConfig>({
export async function createStorageAdapter({
database,
publicClient,
}: {
database: PgDatabase<QueryResultHKT>;
publicClient: PublicClient;
config?: config;
}): Promise<PostgresStorageAdapter> {
const cleanUp: (() => Promise<void>)[] = [];

Expand Down
11 changes: 4 additions & 7 deletions packages/store-sync/src/postgres/syncToPostgres.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Store as StoreConfig } from "@latticexyz/store";
import { PgDatabase } from "drizzle-orm/pg-core";
import { SyncOptions, SyncResult } from "../common";
import { createStorageAdapter } from "./createStorageAdapter";
import { createStoreSync } from "../createStoreSync";

export type SyncToPostgresOptions<config extends StoreConfig = StoreConfig> = SyncOptions<config> & {
export type SyncToPostgresOptions = SyncOptions & {
/**
* [Postgres database object from Drizzle][0].
*
Expand All @@ -25,17 +24,15 @@ export type SyncToPostgresResult = SyncResult & {
* @param {CreateIndexerOptions} options See `CreateIndexerOptions`.
* @returns A function to unsubscribe from the block stream, effectively stopping the indexer.
*/
export async function syncToPostgres<config extends StoreConfig = StoreConfig>({
config,
export async function syncToPostgres({
database,
publicClient,
startSync = true,
...syncOptions
}: SyncToPostgresOptions<config>): Promise<SyncToPostgresResult> {
const { storageAdapter } = await createStorageAdapter({ database, publicClient, config });
}: SyncToPostgresOptions): Promise<SyncToPostgresResult> {
const { storageAdapter } = await createStorageAdapter({ database, publicClient });
const storeSync = await createStoreSync({
storageAdapter,
config,
publicClient,
...syncOptions,
});
Expand Down
9 changes: 4 additions & 5 deletions packages/store-sync/src/recs/syncToRecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { SyncStep } from "../SyncStep";
import { configToTables } from "../configToTables";
import { merge } from "@ark/util";

export type SyncToRecsOptions<config extends StoreConfig = StoreConfig, extraTables extends Tables = Tables> = Omit<
SyncOptions,
"config"
> & {
export type SyncToRecsOptions<
config extends StoreConfig = StoreConfig,
extraTables extends Tables = Tables,
> = SyncOptions & {
world: RecsWorld;
config: config;
tables?: extraTables;
Expand Down Expand Up @@ -46,7 +46,6 @@ export async function syncToRecs<config extends StoreConfig, extraTables extends

const storeSync = await createStoreSync({
storageAdapter,
config,
...syncOptions,
onProgress: ({ step, percentage, latestBlockNumber, lastBlockNumberProcessed, message }) => {
// already live, no need for more progress updates
Expand Down
4 changes: 1 addition & 3 deletions packages/store-sync/src/sqlite/sqliteStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { and, eq, sql } from "drizzle-orm";
import { sqliteTableToSql } from "./sqliteTableToSql";
import { buildTable } from "./buildTable";
import { Store as StoreConfig } from "@latticexyz/store";
import { debug } from "./debug";
import { getTableName } from "./getTableName";
import { chainState, mudStoreTables } from "./internalTables";
Expand All @@ -17,13 +16,12 @@ import { KeySchema, decodeKey, decodeValueArgs } from "@latticexyz/protocol-pars

// TODO: upgrade drizzle and use async sqlite interface for consistency

export async function sqliteStorage<config extends StoreConfig = StoreConfig>({
export async function sqliteStorage({
database,
publicClient,
}: {
database: BaseSQLiteDatabase<"sync", void>;
publicClient: PublicClient;
config?: config;
}): Promise<StorageAdapter> {
const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());

Expand Down
11 changes: 4 additions & 7 deletions packages/store-sync/src/sqlite/syncToSqlite.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Store as StoreConfig } from "@latticexyz/store";
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { SyncOptions, SyncResult } from "../common";
import { sqliteStorage } from "./sqliteStorage";
import { createStoreSync } from "../createStoreSync";

export type SyncToSqliteOptions<config extends StoreConfig = StoreConfig> = SyncOptions<config> & {
export type SyncToSqliteOptions = SyncOptions & {
/**
* [SQLite database object from Drizzle][0].
*
Expand All @@ -25,16 +24,14 @@ export type SyncToSqliteResult = SyncResult & {
* @param {SyncToSqliteOptions} options See `SyncToSqliteOptions`.
* @returns A function to unsubscribe from the block stream, effectively stopping the indexer.
*/
export async function syncToSqlite<config extends StoreConfig = StoreConfig>({
config,
export async function syncToSqlite({
database,
publicClient,
startSync = true,
...syncOptions
}: SyncToSqliteOptions<config>): Promise<SyncToSqliteResult> {
}: SyncToSqliteOptions): Promise<SyncToSqliteResult> {
const storeSync = await createStoreSync({
storageAdapter: await sqliteStorage({ database, publicClient, config }),
config,
storageAdapter: await sqliteStorage({ database, publicClient }),
publicClient,
...syncOptions,
});
Expand Down
13 changes: 4 additions & 9 deletions packages/store-sync/src/stash/syncToStash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getRecord, setRecord, registerTable, Stash } from "@latticexyz/stash/internal";
import { Address, Client, publicActions } from "viem";
import { createStorageAdapter } from "./createStorageAdapter";
import { defineTable } from "@latticexyz/store/config/v2";
import { SyncStep } from "../SyncStep";
import { SyncResult } from "../common";
import { SyncOptions, SyncResult } from "../common";
import { createStoreSync } from "../createStoreSync";
import { getSchemaPrimitives, getValueSchema } from "@latticexyz/protocol-parser/internal";

Expand All @@ -28,10 +27,8 @@ export const initialProgress = {
message: "Connecting",
} satisfies getSchemaPrimitives<getValueSchema<typeof SyncProgress>>;

export type SyncToStashOptions = {
export type SyncToStashOptions = Omit<SyncOptions, "config"> & {
stash: Stash;
client: Client;
address: Address;
startSync?: boolean;
};

Expand All @@ -41,18 +38,16 @@ export type SyncToStashResult = SyncResult & {

export async function syncToStash({
stash,
client,
address,
startSync = true,
...opts
}: SyncToStashOptions): Promise<SyncToStashResult> {
registerTable({ stash, table: SyncProgress });

const storageAdapter = createStorageAdapter({ stash });

const sync = await createStoreSync({
...opts,
storageAdapter,
publicClient: client.extend(publicActions) as never,
address,
onProgress: (nextValue) => {
const currentValue = getRecord({ stash, table: SyncProgress, key: {} });
// update sync progress until we're caught up and live
Expand Down
5 changes: 1 addition & 4 deletions packages/store-sync/src/zustand/syncToZustand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { Tables } from "@latticexyz/config";
import { merge } from "@ark/util";
import { configToTables } from "../configToTables";

export type SyncToZustandOptions<config extends StoreConfig, extraTables extends Tables> = Omit<
SyncOptions,
"address" | "config"
> & {
export type SyncToZustandOptions<config extends StoreConfig, extraTables extends Tables> = SyncOptions & {
// require address for now to keep the data model + retrieval simpler
address: Address;
config: config;
Expand Down

0 comments on commit 7c7bdb2

Please sign in to comment.