fix(share): onShare 加 isActive 守卫 + PixelMap 延到 setData 完成后释放(final review C1/M1)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
lanterngamescn
2026-06-27 14:29:43 +08:00
co-authored by Claude Sonnet 4.6
parent 55cab6df68
commit 90077a4b6e
@@ -73,7 +73,8 @@ export class ShareProvider implements CapabilityProvider {
// —— 入站 —— // —— 入站 ——
private onShare(data: string): void { private onShare(data: string): void {
if (this.ctx === undefined) { const bridge = this.bridge;
if (this.ctx === undefined || bridge === undefined || !bridge.isActive()) {
return; return;
} }
let bean: SharetypeBean; let bean: SharetypeBean;
@@ -294,15 +295,18 @@ export class ShareProvider implements CapabilityProvider {
private writeClipboardImage(filePath: string): void { private writeClipboardImage(filePath: string): void {
const source: image.ImageSource = image.createImageSource(filePath); const source: image.ImageSource = image.createImageSource(filePath);
source.createPixelMap().then((pm: image.PixelMap) => { source.createPixelMap().then((pm: image.PixelMap) => {
let data: pasteboard.PasteData;
try { try {
const data: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_PIXELMAP, pm); data = pasteboard.createData(pasteboard.MIMETYPE_PIXELMAP, pm);
pasteboard.getSystemPasteboard().setData(data)
.catch((e: BusinessError) => this.log.w(`clipboard image failed: ${e.code} ${e.message}`));
} catch (e) { } catch (e) {
this.log.w(`clipboard image init failed: ${(e as Error).message}`); this.log.w(`clipboard image init failed: ${(e as Error).message}`);
} finally {
pm.release(); pm.release();
return;
} }
// pm 须存活到 setData 完成(createData 可能持引用),故在 setData settle 后再 release。
pasteboard.getSystemPasteboard().setData(data)
.catch((e: BusinessError) => this.log.w(`clipboard image failed: ${e.code} ${e.message}`))
.finally(() => pm.release());
}).catch((e: BusinessError) => { }).catch((e: BusinessError) => {
this.log.w(`createPixelMap failed: ${e.code} ${e.message}`); this.log.w(`createPixelMap failed: ${e.code} ${e.message}`);
}).finally(() => { }).finally(() => {