From 3bacf29d012e99fa540f66a1270276cb3b840c35 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Tue, 10 Dec 2024 20:04:26 +0530 Subject: [PATCH] chore: move types to a separate file --- examples/experimental/railgun-repro/index.ts | 23 +------- .../railgun-repro/service-worker.ts | 41 +------------- examples/experimental/railgun-repro/types.ts | 53 +++++++++++++++++++ 3 files changed, 57 insertions(+), 60 deletions(-) create mode 100644 examples/experimental/railgun-repro/types.ts diff --git a/examples/experimental/railgun-repro/index.ts b/examples/experimental/railgun-repro/index.ts index 50333fd..9ed3396 100644 --- a/examples/experimental/railgun-repro/index.ts +++ b/examples/experimental/railgun-repro/index.ts @@ -1,24 +1,5 @@ import { type DecodedMessage } from "@waku/sdk"; - -const CONTENT_TOPICS = [ - "/railgun/v2/0-1-fees/json", - "/railgun/v2/0-56-fees/json", - "/railgun/v2/0-137-fees/json", - "/railgun/v2/0-42161-fees/json", - "/railgun/v2/0-421$1-transact-response/json", - "/railgun/v2/encrypted-metrics-pong/json" -] as const; - -interface ServiceWorkerMessage { - type: string; - message?: string; - status?: string; - peerId?: string; - error?: string; - timestamp?: string; - contentTopic?: string; - topics?: readonly string[]; -} +import { WAKU_CONFIG, type ServiceWorkerMessage } from './types'; let lastMessageTimestamp: number | null = null; @@ -184,7 +165,7 @@ class Railgun { navigator.serviceWorker.controller.postMessage({ type: 'subscribe', - payload: { topics: CONTENT_TOPICS } + payload: { topics: WAKU_CONFIG.CONTENT_TOPICS } }); } diff --git a/examples/experimental/railgun-repro/service-worker.ts b/examples/experimental/railgun-repro/service-worker.ts index 5b2195e..63cf651 100644 --- a/examples/experimental/railgun-repro/service-worker.ts +++ b/examples/experimental/railgun-repro/service-worker.ts @@ -1,45 +1,8 @@ import { createDecoder, createEncoder, createLightNode, type LightNode, Protocols } from '@waku/sdk'; +import { WAKU_CONFIG, type MessagePayload, type ClientMessage, type WakuMessage } from './types'; declare const self: ServiceWorkerGlobalScope; -const WAKU_CONFIG = { - PUBSUB_TOPIC: "/waku/2/rs/0/1", - CONTENT_TOPICS: [ - "/railgun/v2/0-1-fees/json", - "/railgun/v2/0-56-fees/json", - "/railgun/v2/0-137-fees/json", - "/railgun/v2/0-42161-fees/json", - "/railgun/v2/0-421$1-transact-response/json", - "/railgun/v2/encrypted-metrics-pong/json" - ] as const, - NETWORK: { - CLUSTER_ID: 0, - SHARD: 1, - RAILGUN_MA: '/dns4/railgun.ivansete.xyz/tcp/8000/wss/p2p/16Uiu2HAmExcXDvdCr2XxfCeQY1jhCoJ1HodgKauRatCngQ9Q1X61' - } -} as const; - -interface SendMessagePayload { - message: string; -} - -interface SubscribePayload { - topics: readonly string[]; -} - -type MessagePayload = { - type: 'sendMessage'; - payload: SendMessagePayload; -} | { - type: 'subscribe'; - payload: SubscribePayload; -}; - -type ClientMessage = { - type: string; - [key: string]: any; -}; - let wakuNode: LightNode | null = null; async function notifyClients(message: ClientMessage): Promise { @@ -142,7 +105,7 @@ async function subscribeToTopics(): Promise { } } -async function handleIncomingMessage(message: { payload: Uint8Array; contentTopic: string }): Promise { +async function handleIncomingMessage(message: WakuMessage): Promise { console.log('📨 Message received on topic:', message.contentTopic); const content = new TextDecoder().decode(message.payload); await notifyClients({ diff --git a/examples/experimental/railgun-repro/types.ts b/examples/experimental/railgun-repro/types.ts new file mode 100644 index 0000000..5f0f62a --- /dev/null +++ b/examples/experimental/railgun-repro/types.ts @@ -0,0 +1,53 @@ +export const WAKU_CONFIG = { + PUBSUB_TOPIC: "/waku/2/rs/0/1", + CONTENT_TOPICS: [ + "/railgun/v2/0-1-fees/json", + "/railgun/v2/0-56-fees/json", + "/railgun/v2/0-137-fees/json", + "/railgun/v2/0-42161-fees/json", + "/railgun/v2/0-421$1-transact-response/json", + "/railgun/v2/encrypted-metrics-pong/json" + ] as const, + NETWORK: { + CLUSTER_ID: 0, + SHARD: 1, + RAILGUN_MA: '/dns4/railgun.ivansete.xyz/tcp/8000/wss/p2p/16Uiu2HAmExcXDvdCr2XxfCeQY1jhCoJ1HodgKauRatCngQ9Q1X61' + } +} as const; + +export interface SendMessagePayload { + message: string; +} + +export interface SubscribePayload { + topics: readonly string[]; +} + +export type MessagePayload = { + type: 'sendMessage'; + payload: SendMessagePayload; +} | { + type: 'subscribe'; + payload: SubscribePayload; +}; + +export type ClientMessage = { + type: string; + [key: string]: any; +}; + +export interface ServiceWorkerMessage { + type: string; + message?: string; + status?: string; + peerId?: string; + error?: string; + timestamp?: string; + contentTopic?: string; + topics?: readonly string[]; +} + +export interface WakuMessage { + payload: Uint8Array; + contentTopic: string; +} \ No newline at end of file