Immer
https://immerjs.github.io/immer/
https://github.com/immerjs/immer
/nishio/Immer
/mrsekut-p/Immer
draftをまるごと入れ替える場合は、returnで返す https://immerjs.github.io/immer/return/
でないと更新されない
参照は壊されてしまうようだ
setAutoFreeze()をfalseにしても同じ
$ deno test -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Immer/map_test.ts
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Immer/map_test.ts
code:map_test.ts
import { enableMapSet, produce } from "./mod.ts";
import {
assertEquals,
assertNotStrictEquals,
assertStrictEquals,
} from "https://deno.land/std@0.173.0/testing/asserts.ts";
enableMapSet();
Deno.test("Immer", async (t) => {
const before = new Map("key1", { title: "test" });
const after = new Map("key1", { title: "test" });
// 構造は同じだが別の参照
assertEquals(before, after);
assertNotStrictEquals(before, after);
await t.step("objectを入れ替えるときはreturnする", () => {
const next = produce<typeof before>(before, () => after);
assertNotStrictEquals(before, next);
assertStrictEquals(after, next);
});
await t.step("何か破壊的操作があると、値が同じでも別のobjectになる", () => {
{
const next = produce<typeof before>(before, (draft) => {
draft.clear();
for (const key, value of after) {
draft.set(key, value);
}
});
assertNotStrictEquals(before, next);
}
{
const next = produce<typeof before>(before, (draft) => {
const value = draft.get("key1");
if (!value) return;
draft.clear();
draft.set("key1", value);
});
assertNotStrictEquals(before, next);
}
});
await t.step("変更がなければ同じobjectを返す", () => {
const next = produce<typeof before>(before, (draft) => {});
assertStrictEquals(before, next);
});
});
2024-06-04 18:47:19 v9からv10になって、型定義が変わった?
というわけではなさそう。
単に修正し忘れてただけか。
code:mod.ts
export * from "https://esm.sh/immer@10.1.1";
#2024-06-04 18:47:16
#2023-01-23 12:54:30
#2023-01-15 22:24:50