Skip to content

Commit

Permalink
feat(stash): add useRecord and useRecords hooks (#3450)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Jan 16, 2025
1 parent 70f224a commit 16242b7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .changeset/slimy-lemons-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@latticexyz/stash": patch
---

Added `useRecord` and `useRecords` hooks for convenience.

```ts
import { useRecords } from "@latticexyz/stash/react";

const players = useRecords({ stash, table: Position });
```

```ts
import { useRecord } from "@latticexyz/stash/react";

const player = useRecord({ stash, table: Position, key: { player: "0x..." } });
```
2 changes: 2 additions & 0 deletions packages/stash/src/exports/react.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { useStash, type UseStashOptions } from "../react/useStash";
export { useRecord, type UseRecordOptions, type UseRecordResult } from "../react/useRecord";
export { useRecords, type UseRecordsOptions, type UseRecordsResult } from "../react/useRecords";
8 changes: 8 additions & 0 deletions packages/stash/src/react/isArrayEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function isArrayEqual(a: readonly unknown[], b: readonly unknown[]): boolean {
if (a === b) return true;
if (a.length !== b.length) return false;
for (let i = a.length - 1; i >= 0; i--) {
if (a[i] !== b[i]) return false;
}
return true;
}
26 changes: 26 additions & 0 deletions packages/stash/src/react/useRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Table } from "@latticexyz/config";
import { TableRecord, Stash, Key } from "../common";
import { useStash } from "./useStash";
import { getRecord } from "../actions/getRecord";

export type UseRecordOptions<
table extends Table = Table,
defaultValue extends Omit<TableRecord<table>, keyof Key<table>> | undefined = undefined,
> = {
stash: Stash;
table: table;
key: Key<table>;
defaultValue?: defaultValue;
};

export type UseRecordResult<
table extends Table = Table,
defaultValue extends Omit<TableRecord<table>, keyof Key<table>> | undefined = undefined,
> = defaultValue extends undefined ? TableRecord<table> | undefined : TableRecord<table>;

export function useRecord<
const table extends Table,
const defaultValue extends Omit<TableRecord<table>, keyof Key<table>> | undefined = undefined,
>({ stash, ...args }: UseRecordOptions<table, defaultValue>): UseRecordResult<table, defaultValue> {
return useStash(stash, (state) => getRecord({ state, ...args }));
}
22 changes: 22 additions & 0 deletions packages/stash/src/react/useRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Table } from "@latticexyz/config";
import { Key, Stash, TableRecord } from "../common";
import { useStash } from "./useStash";
import { getRecords } from "../actions/getRecords";
import { isArrayEqual } from "./isArrayEqual";

export type UseRecordsOptions<table extends Table = Table> = {
stash: Stash;
table: table;
keys?: readonly Key<table>[];
};

export type UseRecordsResult<table extends Table = Table> = readonly TableRecord<table>[];

export function useRecords<const table extends Table>({
stash,
...args
}: UseRecordsOptions<table>): UseRecordsResult<table> {
return useStash(stash, (state) => Object.values(getRecords({ state, ...args })), {
isEqual: isArrayEqual,
});
}

0 comments on commit 16242b7

Please sign in to comment.