Refactor: Bun.peek polyfill for Node.js

This commit is contained in:
SukkaW
2024-07-23 15:46:38 +08:00
parent eed0d58697
commit b1481c87f2
6 changed files with 20 additions and 4 deletions

13
Build/lib/bun.ts Normal file
View File

@@ -0,0 +1,13 @@
interface Peek {
<T = undefined>(promise: T | Promise<T>): Promise<T> | T,
status<T = undefined>(
promise: T | Promise<T>,
): 'pending' | 'fulfilled' | 'rejected' | 'unknown'
}
const noopPeek = <T = undefined>(_: Promise<T>) => _;
noopPeek.status = () => 'unknown';
export const peek: Peek = typeof Bun !== 'undefined'
? Bun.peek
: noopPeek as Peek;

View File

@@ -5,6 +5,7 @@ import path from 'path';
import { mkdirSync } from 'fs';
import picocolors from 'picocolors';
import { fastStringArrayJoin } from './misc';
import { peek } from 'bun';
const identity = (x: any) => x;
@@ -178,7 +179,7 @@ export class Cache<S = string> {
const serializer = 'serializer' in opt ? opt.serializer : identity;
const promise = fn();
const peeked = Bun.peek(promise);
const peeked = peek(promise);
if (peeked === promise) {
return promise.then((value) => {