chore: replace all cn comments of fe to en version by volc api (#320)
This commit is contained in:
@@ -18,12 +18,12 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
import { RemoteWebWorker, register } from '../src/index';
|
||||
|
||||
// 获取模拟函数
|
||||
// Get the simulation function
|
||||
const mockCreateObjectURL = vi.mocked(URL.createObjectURL);
|
||||
|
||||
describe('RemoteWebWorker', () => {
|
||||
beforeEach(() => {
|
||||
// 清除模拟调用记录
|
||||
// Clear simulated call record
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
@@ -63,10 +63,10 @@ describe('RemoteWebWorker', () => {
|
||||
|
||||
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
|
||||
|
||||
// 验证创建的 Blob 内容
|
||||
// Verify the content of the created blob
|
||||
const blobArg = mockCreateObjectURL.mock.calls[0][0];
|
||||
expect(blobArg).toBeInstanceOf(Blob);
|
||||
// 由于 Blob 的内容无法直接访问,我们只能验证它被创建了
|
||||
// Since the content of the blob cannot be accessed directly, we can only verify that it was created
|
||||
});
|
||||
|
||||
it('应该处理非字符串 URL', () => {
|
||||
@@ -84,14 +84,14 @@ describe('RemoteWebWorker', () => {
|
||||
|
||||
describe('register', () => {
|
||||
it('应该将全局 Worker 替换为 RemoteWebWorker', () => {
|
||||
// 创建一个模拟的全局对象
|
||||
// Create a simulated global object
|
||||
const mockGlobal = {
|
||||
worker() {
|
||||
/* 空函数 */
|
||||
/* empty function */
|
||||
},
|
||||
};
|
||||
|
||||
// 将 worker 属性重命名为 Worker,以便测试 register 函数
|
||||
// Rename the worker property to Worker to test the register function
|
||||
Object.defineProperty(mockGlobal, 'Worker', {
|
||||
get() {
|
||||
return this.worker;
|
||||
|
||||
@@ -16,27 +16,27 @@
|
||||
|
||||
import { vi } from 'vitest';
|
||||
|
||||
// 定义一个模拟的 Worker 类
|
||||
// Define a simulated Worker class
|
||||
class MockWorker {
|
||||
constructor(
|
||||
public scriptURL: string,
|
||||
public options: any,
|
||||
) {}
|
||||
|
||||
// 添加 Worker 接口所需的方法
|
||||
// Methods required to add a Worker interface
|
||||
terminate(): void {
|
||||
// 空实现
|
||||
// empty implementation
|
||||
}
|
||||
|
||||
postMessage(): void {
|
||||
// 空实现
|
||||
// empty implementation
|
||||
}
|
||||
|
||||
onmessage = null;
|
||||
onmessageerror = null;
|
||||
}
|
||||
|
||||
// 全局模拟
|
||||
// global simulation
|
||||
global.Worker = MockWorker as any;
|
||||
global.URL = {
|
||||
createObjectURL: vi.fn().mockReturnValue('blob:mocked-object-url'),
|
||||
|
||||
@@ -33,7 +33,7 @@ export class RemoteWebWorker extends Worker {
|
||||
const remoteWorkerUrl =
|
||||
url.includes('://') &&
|
||||
!url.startsWith(location.origin) &&
|
||||
// 适配 @byted/uploader 等底层库的worker 调用
|
||||
// Adapt worker calls to underlying libraries such as @byted/uploader
|
||||
!url.startsWith('blob:')
|
||||
? URL.createObjectURL(
|
||||
new Blob(
|
||||
@@ -51,10 +51,10 @@ export class RemoteWebWorker extends Worker {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: 这种实现很脏,会篡改全局实例容易引发意外,但短期内为了向后兼容,暂时保留,后续需要:
|
||||
// 1. 将业务代码中的 worker 调用切换为 RemoteWebWorker 调用
|
||||
// 2. 这个 package 本身需要增加 ut
|
||||
// 3. 增加 lint 规则,不允许直接调用 Worker,统一使用 RemoteWebWorker 版本
|
||||
// TODO: This implementation is very dirty, and it is easy to cause accidents by tampering with the global instance, but for backward compatibility in the short term, it is temporarily reserved. Later needs:
|
||||
// 1. Switch worker calls in business code to RemoteWebWorker calls
|
||||
// 2. The package itself needs to add ut.
|
||||
// 3. Add the lint rule, do not allow direct calls to the Worker, and use the RemoteWebWorker version uniformly
|
||||
/**
|
||||
* @deprecated Do not use this function!!!
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user