Skip to content

Commit

Permalink
feat: enable auto start upon node creation (#2291)
Browse files Browse the repository at this point in the history
* feat: enable auto start upon node creation

* update the state
  • Loading branch information
weboko authored Feb 27, 2025
1 parent f199d92 commit 09108d9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/src/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 19 additions & 1 deletion packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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[]> {
Expand All @@ -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 {
Expand Down

0 comments on commit 09108d9

Please sign in to comment.