Skip to content

Commit

Permalink
new loading state + CatchingUp for browser-indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Jan 31, 2024
1 parent 454e4d2 commit 6fb8b96
Show file tree
Hide file tree
Showing 32 changed files with 168 additions and 26 deletions.
6 changes: 6 additions & 0 deletions examples/event-processor-bleeps/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# event-processor-bleeps

## 0.0.54

### Patch Changes

- ethereum-indexer-js-processor@0.6.28

## 0.0.53

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-bleeps/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-bleeps",
"private": true,
"version": "0.0.53",
"version": "0.0.54",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
6 changes: 6 additions & 0 deletions examples/event-processor-conquest-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# event-processor-conquest-eth

## 0.0.54

### Patch Changes

- ethereum-indexer-js-processor@0.6.28

## 0.0.53

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-conquest-eth/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-conquest-eth",
"private": true,
"version": "0.0.53",
"version": "0.0.54",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
6 changes: 6 additions & 0 deletions examples/event-processor-conquest-fplay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# event-processor-conquest-fplay

## 0.0.54

### Patch Changes

- ethereum-indexer-js-processor@0.6.28

## 0.0.53

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-conquest-fplay/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-conquest-fplay",
"private": true,
"version": "0.0.53",
"version": "0.0.54",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
6 changes: 6 additions & 0 deletions examples/event-processor-nfts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# event-processor-nfts

## 0.0.54

### Patch Changes

- ethereum-indexer-js-processor@0.6.28

## 0.0.53

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-nfts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-nfts",
"private": true,
"version": "0.0.53",
"version": "0.0.54",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
12 changes: 12 additions & 0 deletions examples/mud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# web-demo

## 0.1.60

### Patch Changes

- Updated dependencies
- ethereum-indexer-browser@0.6.29
- ethereum-indexer@0.6.19
- ethereum-indexer-js-processor@0.6.28
- event-processor-bleeps@0.0.54
- event-processor-conquest-eth@0.0.54
- event-processor-nfts@0.0.54

## 0.1.59

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/mud/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mud-demo",
"private": true,
"version": "0.1.59",
"version": "0.1.60",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
11 changes: 11 additions & 0 deletions examples/web-demo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# web-demo

## 0.1.60

### Patch Changes

- Updated dependencies
- ethereum-indexer-browser@0.6.29
- ethereum-indexer@0.6.19
- event-processor-bleeps@0.0.54
- event-processor-conquest-eth@0.0.54
- event-processor-nfts@0.0.54

## 0.1.59

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/web-demo/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web-demo",
"private": true,
"version": "0.1.59",
"version": "0.1.60",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
8 changes: 8 additions & 0 deletions packages/ethereum-indexer-browser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# ethereum-indexer-browser

## 0.6.29

### Patch Changes

- new loading state + CatchingUp for browser-indexer
- Updated dependencies
- ethereum-indexer@0.6.19

## 0.6.28

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-browser",
"version": "0.6.28",
"version": "0.6.29",
"publishConfig": {
"access": "public"
},
Expand Down
42 changes: 34 additions & 8 deletions packages/ethereum-indexer-browser/src/IndexerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type SyncingState<ABI extends Abi> = {
};

export type StatusState = {
state: 'Idle' | 'Loading' | 'Fetching' | 'Processing' | 'Loaded';
state: 'Idle' | 'Loading' | 'FetchingEventStream' | 'ProcessingEventStream' | 'CatchingUp' | 'IndexingLatest';
};

type InitFunction<ABI extends Abi, ProcessorConfig = undefined> = ProcessorConfig extends undefined
Expand All @@ -59,6 +59,7 @@ type InitFunction<ABI extends Abi, ProcessorConfig = undefined> = ProcessorConfi
export function createIndexerState<ABI extends Abi, ProcessResultType, ProcessorConfig = undefined>(
processor: EventProcessorWithInitialState<ABI, ProcessResultType, ProcessorConfig>,
options?: {
catchupThreshold?: number;
trackNumRequests?: boolean;
logRequests?: boolean;
keepState?: KeepState<ABI, ProcessResultType, unknown, ProcessorConfig>;
Expand Down Expand Up @@ -188,20 +189,27 @@ export function createIndexerState<ABI extends Abi, ProcessResultType, Processor
throw new Error(`no indexer`);
}
indexer.onLoad = async (loadingState) => {
setStatus({state: loadingState});

if (loadingState === 'Loading') {
} else if (loadingState === 'Fetching') {
setStatus({state: 'Loading'});
} else if (loadingState === 'FetchingEventStream') {
setSyncing({fetchingLogs: true});
} else if (loadingState === 'Processing') {
setStatus({state: 'FetchingEventStream'});
} else if (loadingState === 'ProcessingEventStream') {

setSyncing({fetchingLogs: false, processingFetchedLogs: true});
setStatus({state: 'ProcessingEventStream'});
} else if (loadingState === 'Loaded') {
setSyncing({processingFetchedLogs: false});
setSyncing({catchingUp: true});
setStatus({state: 'CatchingUp'});
}
await wait(0.001); // allow propagation if the whole proces is synchronous
};
indexer.onLastSyncUpdated = (lastSync) => {
// should we also wait ?
setLastSync(lastSync);
setCatchup(lastSync);
};
indexer.onStateUpdated = (state) => {
setState(state);
Expand All @@ -224,6 +232,7 @@ export function createIndexerState<ABI extends Abi, ProcessResultType, Processor
}
const lastSync = await indexer.indexMore();
setLastSync(lastSync);
setCatchup(lastSync);
return lastSync;
}

Expand All @@ -235,6 +244,7 @@ export function createIndexerState<ABI extends Abi, ProcessResultType, Processor

const lastSync = await indexer.indexMore();
setLastSync(lastSync);
setCatchup(lastSync);

if (lastSync.lastToBlock !== lastSync.latestBlock) {
return indexToLatest();
Expand All @@ -243,17 +253,33 @@ export function createIndexerState<ABI extends Abi, ProcessResultType, Processor
return lastSync;
}

function setCatchup(lastSync: LastSync<ABI>) {
if (lastSync.latestBlock - lastSync.lastToBlock > (options?.catchupThreshold || 20)) {
if (!$syncing.catchingUp) {
setSyncing({catchingUp: true});
setStatus({state: 'CatchingUp'});
}
} else {
if ($syncing.catchingUp) {
setSyncing({catchingUp: false});
setStatus({state: 'IndexingLatest'});
}
}
}

async function indexToLatest() {
let lastSync: LastSync<ABI> = await setupIndexing();
setLastSync(lastSync);
setCatchup(lastSync);
if (!indexer) {
throw new Error(`no indexer`);
}

setSyncing({catchingUp: true});

try {
lastSync = await indexer.indexMore();
setLastSync(lastSync);
setCatchup(lastSync);
} catch (err) {
lastSync = await new Promise((resolve) => {
setTimeout(async () => {
Expand All @@ -267,19 +293,19 @@ export function createIndexerState<ABI extends Abi, ProcessResultType, Processor
throw new Error(`no lastSync`);
}


while (lastSync.lastToBlock !== lastSync.latestBlock) {
try {
lastSync = await indexer.indexMore();
setLastSync(lastSync);
setCatchup(lastSync);
} catch (err) {
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
}
}
setSyncing({
catchingUp: false,
});

return lastSync;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/ethereum-indexer-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# ethereum-indexer-cli

## 0.6.25

### Patch Changes

- Updated dependencies
- ethereum-indexer@0.6.19
- ethereum-indexer-utils@0.6.11

## 0.6.24

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-cli",
"version": "0.6.24",
"version": "0.6.25",
"description": "",
"keywords": [],
"author": "",
Expand Down
8 changes: 8 additions & 0 deletions packages/ethereum-indexer-db-processors/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# ethereum-indexer-db-processors

## 0.6.19

### Patch Changes

- Updated dependencies
- ethereum-indexer@0.6.19
- ethereum-indexer-db-utils@0.6.19

## 0.6.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-db-processors/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-db-processors",
"version": "0.6.18",
"version": "0.6.19",
"description": "",
"publishConfig": {
"access": "public"
Expand Down
7 changes: 7 additions & 0 deletions packages/ethereum-indexer-db-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ethereum-indexer-db-utils

## 0.6.19

### Patch Changes

- Updated dependencies
- ethereum-indexer@0.6.19

## 0.6.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-db-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-db-utils",
"version": "0.6.18",
"version": "0.6.19",
"description": "",
"publishConfig": {
"access": "public"
Expand Down
7 changes: 7 additions & 0 deletions packages/ethereum-indexer-fs-cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ethereum-indexer-fs-event-store

## 0.6.19

### Patch Changes

- Updated dependencies
- ethereum-indexer@0.6.19

## 0.6.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-fs-cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-fs-cache",
"version": "0.6.18",
"version": "0.6.19",
"description": "",
"publishConfig": {
"access": "public"
Expand Down
7 changes: 7 additions & 0 deletions packages/ethereum-indexer-js-processor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ethereum-indexer-js-processor

## 0.6.28

### Patch Changes

- Updated dependencies
- ethereum-indexer@0.6.19

## 0.6.27

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-js-processor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-js-processor",
"version": "0.6.27",
"version": "0.6.28",
"description": "",
"keywords": [],
"author": "",
Expand Down
Loading

0 comments on commit 6fb8b96

Please sign in to comment.