mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
43 lines (34 loc) • 1 kB
text/typescript
import { isObservableMap, ObservableMap } from "mobx"
import { namespace } from "../../utils"
import { ActionCallArgumentSerializer, cannotSerialize } from "./core"
export const mapSerializer: ActionCallArgumentSerializer<
Map<any, any> | ObservableMap<any, any>,
[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
},
}