-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-sync): add client support for streaming logs from indexer (#…
- Loading branch information
Showing
7 changed files
with
90 additions
and
22 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/store-sync": patch | ||
--- | ||
|
||
Added support for streaming logs from the indexer. |
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,10 @@ | ||
import { Observable } from "rxjs"; | ||
|
||
export function fromEventSource<T>(url: string | URL): Observable<MessageEvent<T>> { | ||
return new Observable<MessageEvent>((subscriber) => { | ||
const eventSource = new EventSource(url); | ||
eventSource.onmessage = (ev): void => subscriber.next(ev); | ||
eventSource.onerror = (): void => subscriber.error(new Error("Event source failed" + new URL(url).origin)); | ||
return () => eventSource.close(); | ||
}); | ||
} |
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
10 changes: 10 additions & 0 deletions
10
packages/store-sync/src/indexer-client/isLogsApiResponse.ts
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,10 @@ | ||
import { StorageAdapterBlock } from "../common"; | ||
|
||
export type LogsApiResponse = Omit<StorageAdapterBlock, "blockNumber"> & { blockNumber: string }; | ||
|
||
export function isLogsApiResponse( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
data: any, | ||
): data is LogsApiResponse { | ||
return data && typeof data.blockNumber === "string" && Array.isArray(data.logs); | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/store-sync/src/indexer-client/toStorageAdapterBlock.ts
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,6 @@ | ||
import { StorageAdapterBlock } from "../common"; | ||
import { LogsApiResponse } from "./isLogsApiResponse"; | ||
|
||
export function toStorageAdatperBlock(data: LogsApiResponse): StorageAdapterBlock { | ||
return { ...data, blockNumber: BigInt(data.blockNumber) }; | ||
} |