From 0b22450424d7be5c3ff596e6ebb08647eeff0f01 Mon Sep 17 00:00:00 2001 From: ienaga Date: Sat, 28 Nov 2026 13:53:49 +0900 Subject: [PATCH] =?UTF-8?q?fix(cache):=20=E5=85=B1=E6=9C=89=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=82=8B=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7?= =?UTF-8?q?=E3=83=A5=E3=82=92=E3=80=81=E3=81=BE=E3=81=A0=E6=8F=8F=E3=81=8B?= =?UTF-8?q?=E3=82=8C=E3=81=A6=E3=81=84=E3=82=8B=E3=82=AA=E3=83=96=E3=82=B8?= =?UTF-8?q?=E3=82=A7=E3=82=AF=E3=83=88=E3=81=8C=E3=81=82=E3=82=8B=E3=81=AE?= =?UTF-8?q?=E3=81=AB=E6=8D=A8=E3=81=A6=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同じ内容の表示オブジェクトは描画キャッシュを共有する。片方がステージから外れると 1 秒後にキャッシュが捨てられ、まだステージにあるもう片方は次の描画で全部作り直しに なっていた (画面を切り替えるたびに、両方の画面にある背景などが描き直される)。 表示オブジェクトは内側の Map を直接持ち、自分の ID の印があるかだけを見て使い続ける ため、CacheStore.get を通らず、削除の予約が取り消されなかった。削除を予約するときに 印を外し、まだ描かれているオブジェクトが次の描画で CacheStore.get を通るようにする。 描かれなければ、これまでどおり 1 秒後に捨てられる。毎フレームの処理は増えない。 --- .../CacheStoreRemoveTimerService.test.ts | 26 +++++++++++++++++++ .../service/CacheStoreRemoveTimerService.ts | 12 +++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.test.ts b/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.test.ts index 8846576a..bd7a2913 100644 --- a/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.test.ts +++ b/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.test.ts @@ -1,4 +1,5 @@ import { execute } from "./CacheStoreRemoveTimerService"; +import { execute as cacheStoreGetService } from "./CacheStoreGetService"; import { describe, expect, it } from "vitest"; import { $cacheStore } from "../.."; @@ -19,4 +20,29 @@ describe("CacheStoreRemoveTimerService.js test", () => expect(data.has("trash")).toBe(true); expect(trash.size).toBe(1); }); + + it("a cache shared with an object that is still drawn is not discarded", () => + { + const trash = new Map(); + const data = new Map(); + const store = new Map(); + store.set("3", data); + + // 表示オブジェクトが付ける自分の ID の印と、描画キャッシュ + data.set("3", true); + data.set("100", "node"); + + // 共有している片方がステージから外れる + execute($cacheStore, store, trash, "3"); + expect(trash.has("3")).toBe(true); + + // 印が外れるので、まだ描かれているもう片方は内側の Map を直接使い続けない + expect(data.has("3")).toBe(false); + expect(data.get("100")).toBe("node"); + + // 次の描画で CacheStore.get を通ると、削除の予約が取り消される + expect(cacheStoreGetService($cacheStore, store, trash, "3", "100")).toBe("node"); + expect(trash.has("3")).toBe(false); + expect(data.has("trash")).toBe(false); + }); }); \ No newline at end of file diff --git a/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.ts b/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.ts index cbef62a5..ec4ad84b 100644 --- a/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.ts +++ b/packages/cache/src/CacheStore/service/CacheStoreRemoveTimerService.ts @@ -31,6 +31,18 @@ export const execute = ( data.set("trash", true); trash_store.set(id, data); + // 同じ内容の表示オブジェクトは、このキャッシュを共有する。共有している別のオブジェクトがまだ + // ステージにあるときに捨ててしまうと、そのオブジェクトは次の描画で全部作り直しになる。 + // 表示オブジェクトは内側の Map を直接持ち、自分の ID の印があるかだけを見て使い続けるので、 + // ここで印を外す。まだ描かれているオブジェクトは、次の描画で印が無いことに気づいて + // CacheStore.get を通り、そこで削除の予約が取り消される。描かれなければ、従来どおり捨てられる。 + // Display objects with the same content share this cache. Discarding it while another object + // that shares it is still on the stage makes that object rebuild everything on its next draw. + // A display object keeps the inner Map directly and only checks its own ID marker, so the marker + // is removed here: an object that is still being drawn notices it on the next draw, goes through + // CacheStore.get and the scheduled removal is cancelled there. Otherwise it is discarded as before. + data.delete(id); + if (cache_store.$timerId !== null) { clearTimeout(cache_store.$timerId); }