From e317601fc5945a42fb6ae111b5644a66218f888c Mon Sep 17 00:00:00 2001 From: Christos Panagiotakopoulos Date: Tue, 27 Sep 2022 16:06:04 +0200 Subject: [PATCH] feat: expose start stop --- src/ipfs.ts | 24 ++++++++++++++++++++++++ src/types.ts | 2 ++ 2 files changed, 26 insertions(+) diff --git a/src/ipfs.ts b/src/ipfs.ts index 887d1d4..b9bb8fa 100644 --- a/src/ipfs.ts +++ b/src/ipfs.ts @@ -89,6 +89,9 @@ function createIPFSInterface(bootstrapNodes: string[], ipfsConfig: Options = def function _maintainConnection() { setTimeout(async () => { + if (!ipfsInitialised) { + return; + } await ensureConnectedToBootstrapNodes(); _maintainConnection(); }, 10000); @@ -179,12 +182,33 @@ function createIPFSInterface(bootstrapNodes: string[], ipfsConfig: Options = def _resolveCachedPromises(); }); + const stop = () => { + if (!node) { + throw new Error(`Not initialised`); + } + + ipfsInitialised = false; + return node.stop(); + }; + + const start = async () => { + if (!node) { + throw new Error('Not initialised'); + } + await node.start(); + ipfsInitialised = true; + _maintainConnection(); + _resolveCachedPromises(); + }; + return { getJSONData: (cid: string) => _promiseWrapper(getJSONData, cid), sendJSONData: (content: T) => _promiseWrapper(sendJSONData, content), sendData: (content: string | ArrayBuffer) => _promiseWrapper(sendData, content), getData: (cid: string) => _promiseWrapper(getData, cid), getNodes: () => _promiseWrapper(getNodes), + stop, + start, loadingResult, initResult, startResult, diff --git a/src/types.ts b/src/types.ts index e502907..4778037 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,4 +26,6 @@ export interface IPFSInterface { loadingResult: Promise<{ create: (options?: Options | undefined) => Promise }>; initResult: Promise<{ ipfs: IPFS; CIDObj: typeof Multiformats.CID }>; startResult: Promise; + stop: () => Promise; + start: () => Promise; }