From 09108d92842fd3c90f562cae1097a87ad48a2073 Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Thu, 27 Feb 2025 14:00:28 +0100 Subject: [PATCH] feat: enable auto start upon node creation (#2291) * feat: enable auto start upon node creation * update the state --- packages/interfaces/src/protocols.ts | 8 ++++++++ packages/sdk/src/create/create.ts | 9 ++++++++- packages/sdk/src/waku/waku.ts | 20 +++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 67ec66e9d1..741f52056a 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -33,6 +33,14 @@ export type CreateNodeOptions = { */ userAgent?: string; + /** + * Starts Waku node automatically upon creations. + * Calls {@link @waku/sdk!WakuNode.start} before returning {@link @waku/sdk!WakuNode} + * + * @default true + */ + autoStart?: boolean; + /** * Configuration for determining the network in use. * Network configuration refers to the shards and clusters used in the network. diff --git a/packages/sdk/src/create/create.ts b/packages/sdk/src/create/create.ts index ccd519e7bc..3f11e56048 100644 --- a/packages/sdk/src/create/create.ts +++ b/packages/sdk/src/create/create.ts @@ -14,9 +14,16 @@ export async function createLightNode( ): Promise { const { libp2p, pubsubTopics } = await createLibp2pAndUpdateOptions(options); - return new WakuNode(pubsubTopics, options, libp2p, { + const node = new WakuNode(pubsubTopics, options, libp2p, { store: true, lightpush: true, filter: true }) as LightNode; + + // only if `false` is passed explicitly + if (options?.autoStart !== false) { + await node.start(); + } + + return node; } diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index d1b0a43a9c..62013a1a0e 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -40,6 +40,10 @@ export class WakuNode implements IWaku { public connectionManager: ConnectionManager; public health: HealthIndicator; + // needed to create a lock for async operations + private _nodeStateLock = false; + private _nodeStarted = false; + private readonly peerManager: PeerManager; public constructor( @@ -190,18 +194,32 @@ export class WakuNode implements IWaku { } public async start(): Promise { + if (this._nodeStateLock || this.isStarted()) return; + + this._nodeStateLock = true; + await this.libp2p.start(); this.peerManager.start(); this.health.start(); this.lightPush?.start(); + + this._nodeStateLock = false; + this._nodeStarted = true; } public async stop(): Promise { + if (this._nodeStateLock || !this.isStarted()) return; + + this._nodeStateLock = true; + this.lightPush?.stop(); this.health.stop(); this.peerManager.stop(); this.connectionManager.stop(); await this.libp2p.stop(); + + this._nodeStateLock = false; + this._nodeStarted = false; } public async getConnectedPeers(): Promise { @@ -216,7 +234,7 @@ export class WakuNode implements IWaku { } public isStarted(): boolean { - return this.libp2p.status == "started"; + return this._nodeStarted && this.libp2p.status === "started"; } public isConnected(): boolean {