概述批量写入前需统一权限校验并准备恢复策略。本文提供封装与错误处理实践。批量校验与写入async function requestRW(handle) {
if (typeof handle.requestPermission === 'function') {
const s = await handle.requestPermission({ mode: 'readwrite' });
return s === 'granted';
}
return true;
}
async function ensureAllPermissions(handles) {
for (const h of handles) {
const ok = await requestRW(h);
if (!ok) throw new Error('permission denied');
}
}
async function writeBatch(entries) {
try {
await ensureAllPermissions(entries.map(e => e.handle));
for (const e of entries) {
const w = await e.handle.createWritable();
await w.write(e.bytes);
await w.close();
}
} catch (err) {
// 记录失败项并重试或提示用户重新授权
}
}

发表评论 取消回复