mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
refactor(client): retain ES2022 compatibility
This commit is contained in:
parent
ee3156715d
commit
6e16f77161
4 changed files with 22 additions and 5 deletions
|
|
@ -15,6 +15,7 @@ import {
|
|||
} from "@earendil-works/pi-protocol";
|
||||
import { Connection } from "./connection.ts";
|
||||
import { PiDisconnectedError, PiServerError, PiSessionDetachedError, toError } from "./errors.ts";
|
||||
import { createPromiseResolvers } from "./promise.ts";
|
||||
import { ClientState } from "./state.ts";
|
||||
import type {
|
||||
ConnectionState,
|
||||
|
|
@ -191,7 +192,7 @@ export class PiClient {
|
|||
#request<const TCommand extends Command>(command: TCommand): Promise<ResultForCommand<TCommand>> {
|
||||
if (!this.connected) return Promise.reject(new PiDisconnectedError());
|
||||
const id = `request-${++this.#requestSequence}`;
|
||||
const { promise, resolve, reject } = Promise.withResolvers<CommandResult>();
|
||||
const { promise, resolve, reject } = createPromiseResolvers<CommandResult>();
|
||||
this.#pendingRequests.set(id, { command, resolve, reject });
|
||||
let frame: Uint8Array;
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import {
|
|||
type ServerSnapshot,
|
||||
} from "@earendil-works/pi-protocol";
|
||||
import { PiDisconnectedError, PiServerError, toDisconnectedError, toError } from "./errors.ts";
|
||||
import { createPromiseResolvers, type PromiseResolvers } from "./promise.ts";
|
||||
import type { ByteTransport, ByteTransportFactory, ByteTransportHandlers } from "./transport.ts";
|
||||
import type { ConnectionState, ConnectionStateChange } from "./types.ts";
|
||||
|
||||
|
|
@ -21,11 +22,11 @@ type ActiveConnection = {
|
|||
|
||||
type ConnectionLifecycle =
|
||||
| { state: "disconnected" }
|
||||
| ({ state: "connecting"; handshake: PromiseWithResolvers<ServerSnapshot> } & ActiveConnection)
|
||||
| ({ state: "connecting"; handshake: PromiseResolvers<ServerSnapshot> } & ActiveConnection)
|
||||
| ({
|
||||
state: "connected";
|
||||
transport: ByteTransport;
|
||||
handshake: PromiseWithResolvers<ServerSnapshot> | undefined;
|
||||
handshake: PromiseResolvers<ServerSnapshot> | undefined;
|
||||
} & ActiveConnection);
|
||||
|
||||
interface ConnectionOptions {
|
||||
|
|
@ -68,7 +69,7 @@ export class Connection {
|
|||
return Promise.reject(new PiDisconnectedError(`PiClient is already ${this.#lifecycle.state}`));
|
||||
}
|
||||
const id = ++this.#sequence;
|
||||
const handshake = Promise.withResolvers<ServerSnapshot>();
|
||||
const handshake = createPromiseResolvers<ServerSnapshot>();
|
||||
this.#lifecycle = {
|
||||
state: "connecting",
|
||||
id,
|
||||
|
|
|
|||
15
packages/client/src/promise.ts
Normal file
15
packages/client/src/promise.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
export interface PromiseResolvers<T> {
|
||||
promise: Promise<T>;
|
||||
resolve(value: T | PromiseLike<T>): void;
|
||||
reject(reason?: unknown): void;
|
||||
}
|
||||
|
||||
export function createPromiseResolvers<T>(): PromiseResolvers<T> {
|
||||
let resolve!: PromiseResolvers<T>["resolve"];
|
||||
let reject!: PromiseResolvers<T>["reject"];
|
||||
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
|
||||
resolve = resolvePromise;
|
||||
reject = rejectPromise;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "Node16",
|
||||
"lib": ["ES2024"],
|
||||
"lib": ["ES2022"],
|
||||
"strict": true,
|
||||
"erasableSyntaxOnly": true,
|
||||
"esModuleInterop": true,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue