Skip to content

Commit

Permalink
Use Array.fromAsync polyfill (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho authored Mar 15, 2024
1 parent 4f1171b commit 1e4476e
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 27 deletions.
1 change: 1 addition & 0 deletions apps/extension/array-from-async.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'array-from-async';
1 change: 1 addition & 0 deletions apps/minifront/array-from-async.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'array-from-async';
1 change: 1 addition & 0 deletions packages/polyfills/array-from-async.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'array-from-async';
3 changes: 3 additions & 0 deletions packages/polyfills/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"clean": "rm -rfv dist",
"lint": "eslint \"**/*.ts*\"",
"test": "vitest run"
},
"dependencies": {
"array-from-async": "^3.0.0"
}
}
27 changes: 15 additions & 12 deletions packages/polyfills/src/Array.fromAsync.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// just a type declaration for chrome's existing Array.fromAsync

type ArrayWithAsync = typeof Array & {
fromAsync<T, U = T>(
arrayLike: ArrayLike<T> | AsyncIterable<T>,
mapfn?: (v: T, k: number) => U,
thisArg?: any,
): Promise<U[]>;
};

export default Array as ArrayWithAsync;
import fromAsync from 'array-from-async';

type FromAsync = <T, U = T>(
arrayLike: ArrayLike<T> | AsyncIterable<T>,
mapfn?: (v: T, k: number) => U,
thisArg?: unknown,
) => Promise<U[]>;

type ArrayWithFromAsync = typeof Array & { fromAsync: FromAsync };

if (!('fromAsync' in Array)) {
Object.assign(Array, { fromAsync: fromAsync as FromAsync });
}

export default Array as ArrayWithFromAsync;
4 changes: 1 addition & 3 deletions packages/router/array-from-async.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
declare module 'array-from-async' {
export { default } from '@penumbra-zone/polyfills/src/Array.fromAsync';
}
declare module 'array-from-async';
3 changes: 0 additions & 3 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@
"@penumbra-zone/storage": "workspace:*",
"@penumbra-zone/transport-dom": "workspace:*",
"@penumbra-zone/wasm": "workspace:*"
},
"devDependencies": {
"array-from-async": "^3.0.0"
}
}
3 changes: 0 additions & 3 deletions packages/router/tests-setup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import fromAsync from 'array-from-async';
import { vi } from 'vitest';

Array.fromAsync = fromAsync;

// chrome.storage persistence middleware is run upon importing from `state/index.ts`.
// For tests, this is problematic as it uses globals. This mocks those out.
global.chrome = {
Expand Down
1 change: 1 addition & 0 deletions packages/transport-dom/array-from-async.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'array-from-async';
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e4476e

Please sign in to comment.