Skip to content

Commit

Permalink
feat(site): reply from latest height
Browse files Browse the repository at this point in the history
  • Loading branch information
cor committed Sep 28, 2023
1 parent 52ee5a2 commit 9a02318
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions site/src/lib/Xterm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { ApolloClient, InMemoryCache, gql } from '@apollo/client/core';
import type { Terminal } from 'xterm';
import type { ApolloQueryResult } from '@apollo/client';
import BlogLayout from '../mdsvex/BlogLayout.svelte';
import ButtonA from './ButtonA.svelte';
const client = new ApolloClient({
uri: 'https://graphql.union.build/v1/graphql',
Expand All @@ -18,8 +20,18 @@
}
`
const FETCH_LATEST_ID = gql`
query GetLatestId {
demo_txes(limit: 1, order_by: {id: desc}) {
id
}
}
`
let terminal: null | Terminal;
let terminalElement: HTMLElement;
let latestId: null | number = null;
const replayOffset = 300;
let logLines: {network: String, action: String, logLine: String}[] = [];
Expand Down Expand Up @@ -71,28 +83,34 @@
return { network, action, logLine: JSON.stringify(data)}
}
const worker = async () => {
for (let i = 862; i < 100000000; i++) {
await new Promise(r => setTimeout(r, 2000));
client.query({
query: FETCH_EVENT,
variables: {
id: i
},
}).then(async (result) => {
console.log(result)
const newLine = filter(result);
if (newLine != null) {
logLines = [newLine, ...logLines];
}
})
}
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
const worker = async (latestIdWorker) => {
const startHeight = latestIdWorker - replayOffset
let i = startHeight;
while (true) {
i++;
await sleep(2000);
const response = await client.query({
query: FETCH_EVENT,
variables: { id: i },
});
const newLine = filter(response);
if (newLine != null) {
logLines = [newLine, ...logLines];
}
if (i === (latestIdWorker - 1)) {
i = startHeight;
}
}
}
onMount(async () => {
worker();
let response = await client.query({ query: FETCH_LATEST_ID, });
latestId = response.data.demo_txes[0].id;
worker(latestId);
})
</script>

Expand All @@ -107,7 +125,6 @@
</div>
</div>


<style>
/* For Webkit-based browsers (Chrome, Safari and Opera) */
.scrollbar-hide::-webkit-scrollbar {
Expand Down

0 comments on commit 9a02318

Please sign in to comment.