Skip to content

Commit

Permalink
psync,etc: p-defer => Promise.withResolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Dec 3, 2024
1 parent 5a3d958 commit 2d13ed1
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 28 deletions.
1 change: 0 additions & 1 deletion pkg/psync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"mnemonist": "^0.39.8",
"murmurhash3js-revisited": "^3.0.0",
"obliterator": "^2.0.4",
"p-defer": "^4.0.1",
"pako": "^2.1.0",
"tslib": "^2.8.1",
"typescript-event-target": "^1.1.1"
Expand Down
5 changes: 2 additions & 3 deletions pkg/psync/src/psync/full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { GenericNumber } from "@ndn/naming-convention2";
import type { Component, Data, Interest, Name, Signer, Verifier } from "@ndn/packet";
import { type SyncNode, type SyncProtocol, SyncUpdate } from "@ndn/sync-api";
import { KeyMap, toHex, trackEventListener } from "@ndn/util";
import pDefer, { type DeferredPromise } from "p-defer";
import { TypedEventTarget } from "typescript-event-target";

import { computeInterval, type IntervalFunc, type IntervalRange } from "../detail/interval";
Expand All @@ -17,7 +16,7 @@ interface PendingInterest {
interest: Interest;
recvIblt: IBLT;
expire: NodeJS.Timeout | number;
defer: DeferredPromise<Data | undefined>;
defer: PromiseWithResolvers<Data | undefined>;
}

interface DebugEntry {
Expand Down Expand Up @@ -175,7 +174,7 @@ export class FullSync extends TypedEventTarget<EventMap> implements SyncProtocol
pending.defer.resolve(undefined);
}
}, interest.lifetime),
defer: pDefer<Data | undefined>(),
defer: Promise.withResolvers<Data | undefined>(),
};
this.pPendings.set(ibltComp, pending);
return pending.defer.promise;
Expand Down
5 changes: 2 additions & 3 deletions pkg/psync/src/psync/partial-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Data, type Interest, type Name, NameMap, type Signer } from "@ndn/packe
import type { SyncNode, SyncProtocol } from "@ndn/sync-api";
import { trackEventListener } from "@ndn/util";
import { BloomFilter } from "@yoursunny/psync-bloom";
import pDefer, { type DeferredPromise } from "p-defer";
import { TypedEventTarget } from "typescript-event-target";

import type { IBLT } from "../iblt";
Expand All @@ -17,7 +16,7 @@ interface PendingInterest {
recvIblt: IBLT;
bloom: BloomFilter;
expire: NodeJS.Timeout | number;
defer: DeferredPromise<Data | undefined>;
defer: PromiseWithResolvers<Data | undefined>;
}

interface DebugEntry {
Expand Down Expand Up @@ -166,7 +165,7 @@ export class PartialPublisher extends TypedEventTarget<EventMap> implements Sync
pending.defer.resolve(undefined);
}
}, interest.lifetime),
defer: pDefer<Data | undefined>(),
defer: Promise.withResolvers<Data | undefined>(),
};
this.sPendings.set(interest.name, pending);
return pending.defer.promise;
Expand Down
5 changes: 2 additions & 3 deletions pkg/psync/src/syncps/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { KeyMap, toHex, trackEventListener } from "@ndn/util";
import DefaultWeakMap from "mnemonist/default-weak-map.js";
import filter from "obliterator/filter.js";
import take from "obliterator/take.js";
import pDefer, { type DeferredPromise } from "p-defer";
import { TypedEventTarget } from "typescript-event-target";

import { IBLT } from "../iblt";
Expand All @@ -30,7 +29,7 @@ interface SyncInterestInfo {

interface PendingInterest extends SyncInterestInfo {
expire: NodeJS.Timeout | number;
defer: DeferredPromise<Data | undefined>;
defer: PromiseWithResolvers<Data | undefined>;
}

interface DebugEntry {
Expand Down Expand Up @@ -268,7 +267,7 @@ export class SyncpsPubsub extends TypedEventTarget<EventMap> implements Subscrib
pending.defer.resolve(undefined);
}
}, interest.lifetime),
defer: pDefer<Data | undefined>(),
defer: Promise.withResolvers<Data | undefined>(),
};
this.pPendings.set(ibltComp, pending);
return pending.defer.promise;
Expand Down
1 change: 0 additions & 1 deletion pkg/pyrepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@ndn/sync-api": "workspace:^",
"@ndn/tlv": "workspace:*",
"@ndn/util": "workspace:*",
"p-defer": "^4.0.1",
"streaming-iterables": "^8.0.1",
"tslib": "^2.8.1",
"type-fest": "^4.30.0",
Expand Down
3 changes: 1 addition & 2 deletions pkg/pyrepo/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { produce, ProducerOptions } from "@ndn/endpoint";
import type { Name } from "@ndn/packet";
import { DataStore as S } from "@ndn/repo-api";
import { Closers, delay } from "@ndn/util";
import pDefer from "p-defer";
import { collect } from "streaming-iterables";

import { PyRepoClient } from "./client";
Expand Down Expand Up @@ -67,7 +66,7 @@ export class PyRepoStore implements Disposable, S.Insert, S.Delete {
const pkts = await collect(S.Insert.parseArgs<{}>(args).pkts);

const retrieved = new Set<number>();
const answered = pDefer<void>();
const answered = Promise.withResolvers<void>();
const timeout = setTimeout(
() => answered.reject(new Error("no incoming Interest")),
this.incomingInterestTimeout,
Expand Down
1 change: 0 additions & 1 deletion pkg/repo-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@ndn/tlv": "workspace:*",
"@ndn/util": "workspace:*",
"is-stream": "^4.0.1",
"p-defer": "^4.0.1",
"p-event": "^6.0.1",
"streaming-iterables": "^8.0.1",
"tslib": "^2.8.1",
Expand Down
5 changes: 2 additions & 3 deletions pkg/repo-api/src/bulk-insert-initiator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Transport } from "@ndn/l3face";
import type { Data } from "@ndn/packet";
import { Encoder } from "@ndn/tlv";
import { pushable } from "@ndn/util";
import pDefer, { type DeferredPromise } from "p-defer";
import { consume } from "streaming-iterables";
import { TypedEventTarget } from "typescript-event-target";

import * as S from "./data-store";

interface Burst {
pkts: AsyncIterable<Data>;
defer: DeferredPromise<undefined>;
defer: PromiseWithResolvers<void>;
}

type EventMap = {
Expand Down Expand Up @@ -69,7 +68,7 @@ export class BulkInsertInitiator extends TypedEventTarget<EventMap> implements S
*/
public async insert(...args: S.Insert.Args<{}>): Promise<void> {
const { pkts } = S.Insert.parseArgs<{}>(args);
const defer = pDefer<undefined>();
const defer = Promise.withResolvers<void>();
this.queue.push({ pkts, defer });
return defer.promise;
}
Expand Down
1 change: 0 additions & 1 deletion pkg/segmented-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"it-keepalive": "^1.2.0",
"mnemonist": "^0.39.8",
"obliterator": "^2.0.4",
"p-defer": "^4.0.1",
"p-lazy": "^5.0.0",
"streaming-iterables": "^8.0.1",
"tslib": "^2.8.1",
Expand Down
7 changes: 3 additions & 4 deletions pkg/segmented-object/src/serve/data-producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { ProducerHandler } from "@ndn/endpoint";
import { Data, digestSigning, type Interest, type Name, type Signer } from "@ndn/packet";
import { assert } from "@ndn/util";
import DefaultMap from "mnemonist/default-map.js";
import pDefer, { type DeferredPromise } from "p-defer";
import { getIterator } from "streaming-iterables";

import { defaultSegmentConvention, type SegmentConvention } from "../convention";
Expand Down Expand Up @@ -84,9 +83,9 @@ class SequentialDataProducer extends DataProducer {
private requested = -1;
private final = Infinity;
private readonly buffer = new Map<number, Data>();
private readonly waitlist = new DefaultMap<number, DeferredPromise<void>>(() => pDefer());
private readonly waitlist = new DefaultMap<number, PromiseWithResolvers<void>>(() => Promise.withResolvers<void>());
private readonly generator: AsyncGenerator<Chunk, false>;
private pause?: DeferredPromise<void>;
private pause?: PromiseWithResolvers<void>;

constructor(source: ChunkSource, prefix: Name, opts: DataProducer.Options = {}) {
super(source, prefix, opts);
Expand Down Expand Up @@ -139,7 +138,7 @@ class SequentialDataProducer extends DataProducer {
assert(chunk.i === i, "unexpected chunk number");

if (i > this.requested + bufferAhead) {
this.pause = pDefer();
this.pause = Promise.withResolvers<void>();
await this.pause.promise;
this.pause = undefined;
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/svs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"devDependencies": {
"@ndn/keychain": "workspace:*",
"@ndn/l3face": "workspace:*",
"mnemonist": "^0.39.8",
"p-defer": "^4.0.1"
"mnemonist": "^0.39.8"
}
}
3 changes: 1 addition & 2 deletions pkg/svs/tests/svsps.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { generateSigningKey } from "@ndn/keychain";
import { Name, type NameLike } from "@ndn/packet";
import { DataArray } from "@ndn/repo-api";
import { Closers, console, crypto, delay } from "@ndn/util";
import pDefer from "p-defer";
import { afterEach, beforeAll, expect, test, vi } from "vitest";

import { type MappingEntry, type Subscription, SvPublisher, SvSubscriber, SvSync, TimedMappingEntry } from "..";
Expand Down Expand Up @@ -66,7 +65,7 @@ async function publishCheck(
const abort = new AbortController();
const received = Array.from<SvSubscriber.Update | undefined>({ length: expectReceive.length });
let nWaiting = expectReceive.length;
const allReceived = pDefer<void>();
const allReceived = Promise.withResolvers<void>();
for (const [i, sub] of expectReceive.entries()) {
sub.addEventListener("update", (update) => { // eslint-disable-line @typescript-eslint/no-loop-func
expect(received[i]).toBeUndefined();
Expand Down
1 change: 1 addition & 0 deletions pkg/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"@shigen/polyfill-symbol-dispose": "^1.0.1",
"@ungap/with-resolvers": "^0.1.0",
"event-iterator": "^2.0.0",
"streaming-iterables": "^8.0.1",
"tiny-invariant": "^1.3.3",
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/src/polyfill_node.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
import "@ungap/with-resolvers"; // needed for Node 20.x
9 changes: 8 additions & 1 deletion xo.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ function makePackageOverrides(config, ...pkgs) {

/** @type {XoOptions} */
module.exports = {
...js,
...merge(js, {
rules: {
"n/no-unsupported-features/es-syntax": ["error", {
version: require("./package.json").engines.node,
ignores: ["promise-withresolvers"],
}],
},
}),
overrides: [
...makePackageOverrides(merge(js, ts), ""),
...makePackageOverrides(merge(js, ts, web),
Expand Down

0 comments on commit 2d13ed1

Please sign in to comment.