From a29268426de711f776fb357065c95669194c6645 Mon Sep 17 00:00:00 2001 From: Simon Chan <1330321+yume-chan@users.noreply.github.com> Date: Thu, 24 Oct 2024 04:28:55 +0800 Subject: [PATCH] refactor: improve tree-shaking --- libraries/adb-scrcpy/package.json | 2 +- libraries/adb/package.json | 2 +- libraries/android-bin/src/logcat.ts | 4 +- libraries/event/package.json | 2 +- .../scrcpy-decoder-tinyh264/package.json | 2 +- libraries/scrcpy/package.json | 2 +- libraries/stream-extra/package.json | 2 +- libraries/struct/src/types/bigint.ts | 12 ++- libraries/struct/src/types/buffer/base.ts | 5 +- .../struct/src/types/number-namespace.ts | 70 +++++++++++++++ .../struct/src/types/number-reexports.ts | 13 +++ libraries/struct/src/types/number.ts | 85 +------------------ toolchain/eslint-config/package.json | 2 +- 13 files changed, 107 insertions(+), 96 deletions(-) create mode 100644 libraries/struct/src/types/number-namespace.ts create mode 100644 libraries/struct/src/types/number-reexports.ts diff --git a/libraries/adb-scrcpy/package.json b/libraries/adb-scrcpy/package.json index 7344a7dd..6d994c37 100644 --- a/libraries/adb-scrcpy/package.json +++ b/libraries/adb-scrcpy/package.json @@ -33,7 +33,7 @@ }, "dependencies": { "@yume-chan/adb": "workspace:^", - "@yume-chan/async": "^2.2.0", + "@yume-chan/async": "^4.0.0", "@yume-chan/event": "workspace:^", "@yume-chan/scrcpy": "workspace:^", "@yume-chan/stream-extra": "workspace:^", diff --git a/libraries/adb/package.json b/libraries/adb/package.json index 98958a96..ccc84d09 100644 --- a/libraries/adb/package.json +++ b/libraries/adb/package.json @@ -32,7 +32,7 @@ "test": "run-test" }, "dependencies": { - "@yume-chan/async": "^2.2.0", + "@yume-chan/async": "^4.0.0", "@yume-chan/event": "workspace:^", "@yume-chan/no-data-view": "workspace:^", "@yume-chan/stream-extra": "workspace:^", diff --git a/libraries/android-bin/src/logcat.ts b/libraries/android-bin/src/logcat.ts index 867284e4..8077561f 100644 --- a/libraries/android-bin/src/logcat.ts +++ b/libraries/android-bin/src/logcat.ts @@ -33,7 +33,7 @@ export type LogId = (typeof LogId)[keyof typeof LogId]; const LogIdName = /* #__PURE__ */ - Object.fromEntries(Object.entries(LogId).map(([k, v]) => [v, k])); + (() => Object.fromEntries(Object.entries(LogId).map(([k, v]) => [v, k])))(); // https://cs.android.com/android/platform/superproject/+/master:system/logging/liblog/include/android/log.h;l=73;drc=82b5738732161dbaafb2e2f25cce19cd26b9157d export const AndroidLogPriority = { @@ -96,7 +96,7 @@ export interface LogcatOptions { ids?: LogId[]; } -const NANOSECONDS_PER_SECOND = BigInt(1e9); +const NANOSECONDS_PER_SECOND = /* #__PURE__ */ BigInt(1e9); // https://cs.android.com/android/platform/superproject/+/master:system/logging/liblog/include/log/log_read.h;l=39;drc=82b5738732161dbaafb2e2f25cce19cd26b9157d export const LoggerEntry = diff --git a/libraries/event/package.json b/libraries/event/package.json index e7d275d0..40ce3534 100644 --- a/libraries/event/package.json +++ b/libraries/event/package.json @@ -33,7 +33,7 @@ "test": "run-test" }, "dependencies": { - "@yume-chan/async": "^2.2.0" + "@yume-chan/async": "^4.0.0" }, "devDependencies": { "@types/node": "^22.7.7", diff --git a/libraries/scrcpy-decoder-tinyh264/package.json b/libraries/scrcpy-decoder-tinyh264/package.json index e8edc87b..1a5c1570 100644 --- a/libraries/scrcpy-decoder-tinyh264/package.json +++ b/libraries/scrcpy-decoder-tinyh264/package.json @@ -33,7 +33,7 @@ "prepublishOnly": "npm run build" }, "dependencies": { - "@yume-chan/async": "^2.2.0", + "@yume-chan/async": "^4.0.0", "@yume-chan/event": "workspace:^", "@yume-chan/scrcpy": "workspace:^", "@yume-chan/stream-extra": "workspace:^", diff --git a/libraries/scrcpy/package.json b/libraries/scrcpy/package.json index d3af69f6..79d3efc7 100644 --- a/libraries/scrcpy/package.json +++ b/libraries/scrcpy/package.json @@ -33,7 +33,7 @@ "test": "run-test" }, "dependencies": { - "@yume-chan/async": "^2.2.0", + "@yume-chan/async": "^4.0.0", "@yume-chan/no-data-view": "workspace:^", "@yume-chan/stream-extra": "workspace:^", "@yume-chan/struct": "workspace:^" diff --git a/libraries/stream-extra/package.json b/libraries/stream-extra/package.json index 534bd733..adff254d 100644 --- a/libraries/stream-extra/package.json +++ b/libraries/stream-extra/package.json @@ -32,7 +32,7 @@ "test": "run-test" }, "dependencies": { - "@yume-chan/async": "^2.2.0", + "@yume-chan/async": "^4.0.0", "@yume-chan/struct": "workspace:^" }, "devDependencies": { diff --git a/libraries/struct/src/types/bigint.ts b/libraries/struct/src/types/bigint.ts index feec81d7..1cb96e8e 100644 --- a/libraries/struct/src/types/bigint.ts +++ b/libraries/struct/src/types/bigint.ts @@ -47,9 +47,17 @@ export class BigIntFieldVariant { this.serialize = serialize; } - static readonly Int64 = new BigIntFieldVariant(8, getInt64, setInt64); + static readonly Int64 = /* #__PURE__ */ new BigIntFieldVariant( + 8, + getInt64, + setInt64, + ); - static readonly Uint64 = new BigIntFieldVariant(8, getUint64, setUint64); + static readonly Uint64 = /* #__PURE__ */ new BigIntFieldVariant( + 8, + getUint64, + setUint64, + ); } export class BigIntFieldDefinition< diff --git a/libraries/struct/src/types/buffer/base.ts b/libraries/struct/src/types/buffer/base.ts index dff3f916..76d2cb1e 100644 --- a/libraries/struct/src/types/buffer/base.ts +++ b/libraries/struct/src/types/buffer/base.ts @@ -53,7 +53,8 @@ export abstract class BufferFieldConverter< export class Uint8ArrayBufferFieldConverter< TTypeScriptType = Uint8Array, > extends BufferFieldConverter { - static readonly Instance = new Uint8ArrayBufferFieldConverter(); + static readonly Instance = + /* #__PURE__ */ new Uint8ArrayBufferFieldConverter(); protected constructor() { super(); @@ -76,7 +77,7 @@ export class Uint8ArrayBufferFieldConverter< export class StringBufferFieldConverter< TTypeScriptType = string, > extends BufferFieldConverter { - static readonly Instance = new StringBufferFieldConverter(); + static readonly Instance = /* #__PURE__ */ new StringBufferFieldConverter(); override toBuffer(value: string): Uint8Array { return encodeUtf8(value); diff --git a/libraries/struct/src/types/number-namespace.ts b/libraries/struct/src/types/number-namespace.ts new file mode 100644 index 00000000..16d1ac81 --- /dev/null +++ b/libraries/struct/src/types/number-namespace.ts @@ -0,0 +1,70 @@ +import { + getInt16, + getInt32, + getInt8, + getUint16, + getUint32, + setInt16, + setInt32, + setUint16, + setUint32, +} from "@yume-chan/no-data-view"; +import type { NumberFieldVariant } from "./number-reexports.js"; + +export const Uint8: NumberFieldVariant = { + signed: false, + size: 1, + deserialize(array) { + return array[0]!; + }, + serialize(array, offset, value) { + array[offset] = value; + }, +}; + +export const Int8: NumberFieldVariant = { + signed: true, + size: 1, + deserialize(array) { + return getInt8(array, 0); + }, + serialize(array, offset, value) { + array[offset] = value; + }, +}; + +export const Uint16: NumberFieldVariant = { + signed: false, + size: 2, + deserialize(array, littleEndian) { + return getUint16(array, 0, littleEndian); + }, + serialize: setUint16, +}; + +export const Int16: NumberFieldVariant = { + signed: true, + size: 2, + deserialize(array, littleEndian) { + return getInt16(array, 0, littleEndian); + }, + serialize: setInt16, +}; + +export const Uint32: NumberFieldVariant = { + signed: false, + size: 4, + deserialize(array, littleEndian) { + return getUint32(array, 0, littleEndian); + }, + serialize: setUint32, +}; + +export const Int32: NumberFieldVariant = { + signed: true, + size: 4, + deserialize(array, littleEndian) { + return getInt32(array, 0, littleEndian); + }, + serialize: setInt32, +}; diff --git a/libraries/struct/src/types/number-reexports.ts b/libraries/struct/src/types/number-reexports.ts new file mode 100644 index 00000000..5b9e663c --- /dev/null +++ b/libraries/struct/src/types/number-reexports.ts @@ -0,0 +1,13 @@ +export * as NumberFieldVariant from "./number-namespace.js"; + +export interface NumberFieldVariant { + signed: boolean; + size: number; + deserialize(array: Uint8Array, littleEndian: boolean): number; + serialize( + array: Uint8Array, + offset: number, + value: number, + littleEndian: boolean, + ): void; +} diff --git a/libraries/struct/src/types/number.ts b/libraries/struct/src/types/number.ts index e1a903f7..caaaeeb1 100644 --- a/libraries/struct/src/types/number.ts +++ b/libraries/struct/src/types/number.ts @@ -1,15 +1,3 @@ -import { - getInt16, - getInt32, - getInt8, - getUint16, - getUint32, - setInt16, - setInt32, - setUint16, - setUint32, -} from "@yume-chan/no-data-view"; - import type { AsyncExactReadable, ExactReadable, @@ -19,78 +7,9 @@ import type { import { StructFieldDefinition, StructFieldValue } from "../basic/index.js"; import { SyncPromise } from "../sync-promise.js"; import type { ValueOrPromise } from "../utils.js"; +import type { NumberFieldVariant } from "./number-reexports.js"; -export interface NumberFieldVariant { - signed: boolean; - size: number; - deserialize(array: Uint8Array, littleEndian: boolean): number; - serialize( - array: Uint8Array, - offset: number, - value: number, - littleEndian: boolean, - ): void; -} - -export namespace NumberFieldVariant { - export const Uint8: NumberFieldVariant = { - signed: false, - size: 1, - deserialize(array) { - return array[0]!; - }, - serialize(array, offset, value) { - array[offset] = value; - }, - }; - - export const Int8: NumberFieldVariant = { - signed: true, - size: 1, - deserialize(array) { - return getInt8(array, 0); - }, - serialize(array, offset, value) { - array[offset] = value; - }, - }; - - export const Uint16: NumberFieldVariant = { - signed: false, - size: 2, - deserialize(array, littleEndian) { - return getUint16(array, 0, littleEndian); - }, - serialize: setUint16, - }; - - export const Int16: NumberFieldVariant = { - signed: true, - size: 2, - deserialize(array, littleEndian) { - return getInt16(array, 0, littleEndian); - }, - serialize: setInt16, - }; - - export const Uint32: NumberFieldVariant = { - signed: false, - size: 4, - deserialize(array, littleEndian) { - return getUint32(array, 0, littleEndian); - }, - serialize: setUint32, - }; - - export const Int32: NumberFieldVariant = { - signed: true, - size: 4, - deserialize(array, littleEndian) { - return getInt32(array, 0, littleEndian); - }, - serialize: setInt32, - }; -} +export * from "./number-reexports.js"; export class NumberFieldDefinition< TVariant extends NumberFieldVariant = NumberFieldVariant, diff --git a/toolchain/eslint-config/package.json b/toolchain/eslint-config/package.json index c1298dd6..41d8a345 100644 --- a/toolchain/eslint-config/package.json +++ b/toolchain/eslint-config/package.json @@ -13,7 +13,7 @@ "eslint": "^9.13.0", "eslint-plugin-import-x": "^4.3.1", "typescript": "^5.6.3", - "typescript-eslint": "^8.10.0" + "typescript-eslint": "^8.11.0" }, "devDependencies": { "prettier": "^3.3.3"