mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-09 09:13:10 +00:00
feat: enable auto start upon node creation (#2291)
* feat: enable auto start upon node creation * update the state
This commit is contained in:
parent
f199d92d60
commit
09108d9284
@ -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.
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user