*switch to p-defer

This commit is contained in:
Derrick Hammer 2023-03-30 20:57:08 -04:00
parent fe6dac6e4a
commit b9b8040b50
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 10 additions and 10 deletions

View File

@ -39,6 +39,7 @@
"libkmodule": "^0.2.53", "libkmodule": "^0.2.53",
"libp2p": "^0.42.2", "libp2p": "^0.42.2",
"multiformats": "^11.0.2", "multiformats": "^11.0.2",
"p-defer": "^4.0.0",
"p-queue": "^7.3.4", "p-queue": "^7.3.4",
"private-ip": "^3.0.0", "private-ip": "^3.0.0",
"rewire": "^6.0.0", "rewire": "^6.0.0",

View File

@ -38,6 +38,7 @@ import { peerIdFromCID } from "@libp2p/peer-id";
import { bootstrap } from "@libp2p/bootstrap"; import { bootstrap } from "@libp2p/bootstrap";
import { IDBBlockstore } from "blockstore-idb"; import { IDBBlockstore } from "blockstore-idb";
import { IDBDatastore } from "datastore-idb"; import { IDBDatastore } from "datastore-idb";
import defer from "p-defer";
const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys( const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
bases bases
@ -49,10 +50,7 @@ const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
onmessage = handleMessage; onmessage = handleMessage;
let moduleLoadedResolve: Function; const moduleDefer = defer();
let moduleLoaded: Promise<void> = new Promise((resolve) => {
moduleLoadedResolve = resolve;
});
let swarm; let swarm;
let proxy: Proxy; let proxy: Proxy;
@ -205,7 +203,7 @@ async function handlePresentSeed() {
dht(PeerManager.instance.ipfs), dht(PeerManager.instance.ipfs),
pubsub(PeerManager.instance.ipfs as any), pubsub(PeerManager.instance.ipfs as any),
]); ]);
moduleLoadedResolve(); moduleDefer.resolve();
} }
async function handleStat(aq: ActiveQuery) { async function handleStat(aq: ActiveQuery) {
@ -316,7 +314,7 @@ async function handleIpnsResolve(aq: ActiveQuery) {
} }
async function ready() { async function ready() {
await moduleLoaded; await moduleDefer.promise;
await PeerManager.instance.ipfsReady; await PeerManager.instance.ipfsReady;
} }

View File

@ -5,6 +5,7 @@ import { fixed32, json, raw, uint } from "compact-encoding";
import { TcpSocketConnectOpts } from "net"; import { TcpSocketConnectOpts } from "net";
import { Helia } from "@helia/interface"; import { Helia } from "@helia/interface";
import { deserializeError } from "serialize-error"; import { deserializeError } from "serialize-error";
import defer from "p-defer";
import { import {
CloseSocketRequest, CloseSocketRequest,
ErrorSocketRequest, ErrorSocketRequest,
@ -127,14 +128,14 @@ export default class PeerManager {
this._ipfs = value as Helia; this._ipfs = value as Helia;
} }
private _ipfsReady?: Promise<void>; private _ipfsReady?: Promise<unknown>;
private _ipfsResolve?: () => void; private _ipfsResolve?: () => void;
get ipfsReady(): Promise<void> { get ipfsReady(): Promise<void> {
if (!this._ipfsReady) { if (!this._ipfsReady) {
this._ipfsReady = new Promise((resolve) => { let ipfsDefer = defer();
this._ipfsResolve = resolve; this._ipfsReady = ipfsDefer.promise;
}); this._ipfsResolve = ipfsDefer.resolve;
} }
return this._ipfsReady as Promise<any>; return this._ipfsReady as Promise<any>;