Skip to content
Myr

map.ts

This file

Path

src/map.ts

200 lines · 4.2 KiB · TypeScript

Last changed by

added set and iteror stuffMichel Weststratebefore adoption08f4682

History of this file

Take it

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 1–200why, one step back
1import {
2 each,3 has,4 is,5 isDraft,6 isDraftable,7 isEnumerable,8 isMap,9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13 iterateMapValues,14 makeIterable,15 makeIterateSetValues,16 latest17} from "./common"1819// TODO: kill:20import {21 assertUnrevoked,22 ES5Draft,23} from "./es5"24import { ImmerScope } from "./scope";25import { Immer } from "./immer";2627// TODO: create own states28// TODO: clean up the maps and such from ES5 / Proxy states2930export interface MapState {31 parent: any; // TODO: type32 scope: ImmerScope;33 modified: boolean;34 finalizing: boolean;35 finalized: boolean;36 copy: Map<any, any> | undefined;37 assigned: Map<any, boolean> | undefined;38 base: Map<any, any>;39 revoke(): void;40 draft: ES5Draft;41}4243function prepareCopy(state: MapState) {44 if (!state.copy) {45 state.assigned = new Map()46 state.copy = new Map(state.base);47 }48}4950// Make sure DraftMap declarion doesn't die if Map is not avialable...51const MapBase: MapConstructor = typeof Map !== "undefined" ? Map : function FakeMap() {} as any5253// TODO: fix types for drafts54export class DraftMap<K, V> extends MapBase implements Map<K, V> {55 [DRAFT_STATE]: MapState56 constructor(target, parent) {57 super()58 this[DRAFT_STATE] = {59 parent,60 scope: parent ? parent.scope : ImmerScope.current,61 modified: false,62 finalized: false,63 finalizing: false,64 copy: undefined,65 assigned: undefined,66 base: target,67 draft: this as any, // TODO: fix typing68 revoke() {69 // TODO: make sure this marks the Map as revoked, and assert everywhere70 }71 };72 }7374 get size(): number {75 return latest(this[DRAFT_STATE]).size76 }7778 has(key: K): boolean {79 return latest(this[DRAFT_STATE]).has(key)80 }8182 set(key: K, value: V): this {83 const state = this[DRAFT_STATE];84 if (latest(state).get(key) !== value) {85 prepareCopy(state)86 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly87 state.assigned!.set(key, true)88 state.copy!.set(key, value)89 state.assigned!.set(key, true);90 }91 return this92 }9394 delete(key: K): boolean {95 if (!this.has(key)) {96 return false;97 }9899 const state = this[DRAFT_STATE];100 prepareCopy(state)101 state.scope.immer.markChanged(state)102 state.assigned!.set(key, false)103 state.copy!.delete(key)104 return true105 }106107 clear() {108 const state = this[DRAFT_STATE];109 prepareCopy(state)110 state.scope.immer.markChanged(state)111 state.assigned = new Map()112 for (const key of latest(state).keys()) {113 state.assigned.set(key, false)114 }115 return state.copy!.clear()116 }117118 // @ts-ignore TODO:119 forEach(cb) {120 const state = this[DRAFT_STATE];121 latest(state).forEach((_value, key, _map) => {122 cb(this.get(key), key, this)123 })124 }125126 get(key: K): V /* TODO: Draft<V> */ {127 const state = this[DRAFT_STATE];128 const value = latest(state).get(key)129 if (state.finalizing || state.finalized || !isDraftable(value)) {130 return value131 }132 if (value !== state.base.get(key)) {133 return value // either already drafted or reassigned134 }135 const draft = state.scope.immer.createProxy(value, state)136 prepareCopy(state)137 state.copy!.set(key, draft)138 return draft139 }140141 keys() {142 return latest(this[DRAFT_STATE]).keys();143 }144145 // TODO: make these normal functions146 // TODO: values and entries iterators147 // @ts-ignore TODO:148 values = iterateMapValues149 // @ts-ignore TODO:150 entries = iterateMapValues151}152153154if (hasSymbol) {155 Object.defineProperty(156 DraftMap.prototype,157 Symbol.iterator,158 // @ts-ignore TODO fix159 proxyMethod(iterateMapValues)160 )161}162163export function proxyMap(target, parent) {164 if (target instanceof DraftMap) {165 return target; // TODO: or copy?166 }167 return new DraftMap(target, parent)168 // Object.defineProperties(target, mapTraps)169170}171172// TODO: kill?173function proxyMethod(trap, key) {174 return {175 get() {176 return function(this: ES5Draft, ...args) {177 const state = this[DRAFT_STATE]178 assertUnrevoked(state)179 return trap(state, key, state.draft)(...args)180 }181 }182 }183}184185// TODO: used?186export function hasMapChanges(state) {187 const {base, draft} = state188189 if (base.size !== draft.size) return true190191 // IE11 supports only forEach iteration192 let hasChanges = false193 // TODO: optimize: break on first difference194 draft.forEach(function(value, key) {195 if (!hasChanges) {196 hasChanges = isDraftable(value) ? value.modified : value !== base.get(key)197 }198 })199 return hasChanges200}