44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
/**
|
|
* Polyfills for compatibility
|
|
* 提供兼容性支持的 polyfill 函数
|
|
*/
|
|
|
|
/**
|
|
* structuredClone polyfill for environments that don't support it
|
|
* 为不支持 structuredClone 的环境提供兼容实现
|
|
*/
|
|
export function initPolyfills(): void {
|
|
// structuredClone polyfill
|
|
if (typeof (globalThis as any).structuredClone === 'undefined') {
|
|
(globalThis as any).structuredClone = function(obj: any): any {
|
|
if (obj === null || typeof obj !== 'object') {
|
|
return obj;
|
|
}
|
|
|
|
if (obj instanceof Date) {
|
|
return new Date(obj.getTime());
|
|
}
|
|
|
|
if (Array.isArray(obj)) {
|
|
return obj.map((item: any) => (globalThis as any).structuredClone(item));
|
|
}
|
|
|
|
if (typeof obj === 'object') {
|
|
const cloned: any = {};
|
|
for (const key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
cloned[key] = (globalThis as any).structuredClone(obj[key]);
|
|
}
|
|
}
|
|
return cloned;
|
|
}
|
|
|
|
return obj;
|
|
};
|
|
|
|
console.log('[Polyfill] structuredClone polyfill loaded');
|
|
}
|
|
}
|
|
|
|
// 自动初始化
|
|
initPolyfills(); |