diff --git a/bun.lock b/bun.lock index 577b68f790a..b335be33101 100644 --- a/bun.lock +++ b/bun.lock @@ -83,7 +83,6 @@ "@solidjs/router": "catalog:", "@tanstack/solid-query": "5.91.4", "@tanstack/solid-virtual": "catalog:", - "core-js": "3.50.0", "effect": "catalog:", "fuzzysort": "catalog:", "qr-scanner": "1.4.2", @@ -3308,8 +3307,6 @@ "cookie-es": ["cookie-es@2.0.1", "", {}, "sha512-aVf4A4hI2w70LnF7GG+7xDQUkliwiXWXFvTjkip4+b64ygDQ2sJPRSKFDHbxn8o0xu9QzPkMuuiWIXyFSE2slA=="], - "core-js": ["core-js@3.50.0", "", {}, "sha512-BRWgOLKkFeCgRudR6zrs8p9XJZcE14grzKMMssoYrk6krtuEZ7MTKPIY5RzOnqsEKIR9kst7wNzphttraT+Yqw=="], - "core-js-compat": ["core-js-compat@3.50.0", "", { "dependencies": { "browserslist": "^4.28.7" } }, "sha512-XGpFGbMLHwSt74YLTKho7Ib242qi6O8MSX+sRokV4oz7iKXvQWGYZthjIhjRGMxjzVkAubBO512dKGYcefmX3Q=="], "core-util-is": ["core-util-is@1.0.3", "", {}, "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="], diff --git a/packages/app/package.json b/packages/app/package.json index f66020c8f71..20b16db82c5 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -81,7 +81,6 @@ "@solidjs/router": "catalog:", "@tanstack/solid-query": "5.91.4", "@tanstack/solid-virtual": "catalog:", - "core-js": "3.50.0", "effect": "catalog:", "fuzzysort": "catalog:", "qr-scanner": "1.4.2", diff --git a/packages/app/src/runtime/polyfills.test.ts b/packages/app/src/runtime/polyfills.test.ts new file mode 100644 index 00000000000..0c4c8d599f8 --- /dev/null +++ b/packages/app/src/runtime/polyfills.test.ts @@ -0,0 +1,61 @@ +import { afterEach, beforeEach, expect, test } from "bun:test" + +const native = { groupBy: Map.groupBy, withResolvers: Promise.withResolvers, try: Promise.try } + +const maps: Partial = Map + +const promises: Partial = Promise + +beforeEach(async () => { + delete maps.groupBy + delete promises.withResolvers + delete promises.try + await import(`./polyfills?${Math.random()}`) +}) + +afterEach(() => { + Map.groupBy = native.groupBy + Promise.withResolvers = native.withResolvers + Promise.try = native.try +}) + +test("Map.groupBy keeps first-seen key order and passes the index", () => { + const groups = Map.groupBy([1, 2, 3, 4, 5], (n) => (n % 2 ? "odd" : "even")) + expect([...groups]).toEqual([ + ["odd", [1, 3, 5]], + ["even", [2, 4]], + ]) + expect([...Map.groupBy(["a", "b"], (_, index) => index).keys()]).toEqual([0, 1]) +}) + +test("Promise.withResolvers settles from outside", async () => { + const resolved = Promise.withResolvers() + resolved.resolve(1) + expect(await resolved.promise).toBe(1) + + const rejected = Promise.withResolvers() + rejected.reject(new Error("no")) + + await expect(rejected.promise).rejects.toThrow("no") +}) + +test("Promise.try runs synchronously and isolates a throw", async () => { + let ran = false + + const value = Promise.try(() => { + ran = true + + return 2 + }) + + expect(ran).toBe(true) + expect(await value).toBe(2) + + expect(await Promise.try((a: number, b: number) => a + b, 1, 2)).toBe(3) + + await expect( + Promise.try(() => { + throw new Error("boom") + }), + ).rejects.toThrow("boom") +}) diff --git a/packages/app/src/runtime/polyfills.ts b/packages/app/src/runtime/polyfills.ts index cf609290df5..edf61c83dc4 100644 --- a/packages/app/src/runtime/polyfills.ts +++ b/packages/app/src/runtime/polyfills.ts @@ -1,4 +1,39 @@ -// Safari before 17.4 does not provide these runtime APIs; Promise.try arrived in Safari 18.2. -import "core-js/es/map/group-by" -import "core-js/es/promise/with-resolvers" -import "core-js/es/promise/try" +// Safari before 17.4 lacks Map.groupBy and Promise.withResolvers, and Promise.try arrived in Safari 18.2. Electron's +// Chromium has all three, so these only matter for the web app. +if (!Map.groupBy) { + Map.groupBy = function groupBy(items: Iterable, callback: (item: T, index: number) => K) { + const groups = new Map() + let index = 0 + + for (const item of items) { + const key = callback(item, index++) + const group = groups.get(key) + + if (group) group.push(item) + else groups.set(key, [item]) + } + + return groups + } +} + +if (!Promise.withResolvers) { + Promise.withResolvers = function withResolvers() { + let resolve!: (value: T | PromiseLike) => void + let reject!: (reason?: Error) => void + + const promise = new Promise((res, rej) => { + resolve = res + reject = rej + }) + + return { promise, resolve, reject } + } +} + +if (!Promise.try) { + // SAFETY: the callback's return type is carried through unchanged, which is all Promise.try promises. + Promise.try = function tryCall(callback: (...args: U) => T | PromiseLike, ...args: U) { + return new Promise((resolve) => resolve(callback(...args))) + } as typeof Promise.try +}