import { isObservableMap, type ObservableMap } from "mobx" import { namespace } from "../../utils" import { type ActionCallArgumentSerializer, cannotSerialize } from "./core" export const mapSerializer: ActionCallArgumentSerializer< Map | ObservableMap, [any, any][] > = { id: `${namespace}/mapAsArray`, serialize(map, serialize) { if (!(map instanceof Map || isObservableMap(map))) { return cannotSerialize } const arr: [any, any][] = [] const iter = map.keys() let cur = iter.next() while (!cur.done) { const k = cur.value const v = map.get(k) arr.push([serialize(k), serialize(v)]) cur = iter.next() } return arr }, deserialize(arr, deserialize) { const map = new Map() const len = arr.length for (let i = 0; i < len; i++) { const k = arr[i][0] const v = arr[i][1] map.set(deserialize(k), deserialize(v)) } return map }, }