feat: enable auto start upon node creation (#2291)

* feat: enable auto start upon node creation

* update the state
This commit is contained in:
Sasha 2025-02-27 14:00:28 +01:00 committed by GitHub
parent f199d92d60
commit 09108d9284
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 2 deletions

View File

@ -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.

View File

@ -14,9 +14,16 @@ export async function createLightNode(
): Promise<LightNode> {
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;
}

View File

@ -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<void> {
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<void> {
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<Peer[]> {
@ -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 {