2024-10-25 18:09:59 +08:00

71 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 消息类型
const messageType = {
config: 0,
writeFile: 1, // 写文件
};
// @ts-ignore SDK实际有暴露这几个API但是协议里没有
const { createSharedArrayBuffer, getFileSystemManager } = worker;
const fs = getFileSystemManager ? getFileSystemManager() : null;
function compareVersion(_v1, _v2) {
return (_v1
.split('.')
.map(v => v.padStart(2, '0'))
.join('')
>= _v2
.split('.')
.map(v => v.padStart(2, '0'))
.join(''));
}
worker.onMessage((res) => {
const { type, payload } = res;
if (type === messageType.writeFile) {
const { filePath, data, isSharedBuffer } = payload;
let content = data;
if (isSharedBuffer) {
content = data.buffer;
}
if (!fs) {
console.error('getFileSystemManager不存在');
return;
}
fs.writeFile({
filePath,
data: content,
success: () => {
worker.postMessage({
type: messageType.writeFile,
payload: {
isok: true,
filePath,
},
});
},
fail: (err) => {
worker.postMessage({
type: messageType.writeFile,
payload: {
isok: false,
filePath,
err,
},
});
},
});
}
if (type === messageType.config) {
const { systemInfo } = payload;
const { platform, version } = systemInfo;
// 安卓才需要使用worker写文件
const isAndroid = platform.toLocaleLowerCase() === 'android';
// 8.0.18以下版本出现写文件报错
const isClientValid = compareVersion(version, '8.0.18');
worker.postMessage({
type: messageType.config,
payload: {
supportWorkerFs: isAndroid && !!fs && isClientValid,
supportSharedBuffer: isAndroid && !!createSharedArrayBuffer,
},
});
}
});