Skip to content
Myr

map.ts

This file

Path

src/map.ts

236 lines · 5.1 KiB · TypeScript

Last changed by

fixed most testsMichel Weststratebefore adoption385c0a4

History of this file

Take it

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 1–132why, 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 makeIterable,14 latest15} from "./common"1617// TODO: kill:18import {19 assertUnrevoked,20 ES5Draft,21} from "./es5"22import { ImmerScope } from "./scope";23import { Immer } from "./immer";2425// TODO: create own states26// TODO: clean up the maps and such from ES5 / Proxy states2728export interface MapState {29 parent: any; // TODO: type30 scope: ImmerScope;31 modified: boolean;32 finalizing: boolean;33 finalized: boolean;34 copy: Map<any, any> | undefined;35 assigned: Map<any, boolean> | undefined;36 base: Map<any, any>;37 revoke(): void;38 draft: ES5Draft;39}4041function prepareCopy(state: MapState) {42 if (!state.copy) {43 state.assigned = new Map()44 state.copy = new Map(state.base);45 }46}4748// Make sure DraftMap declarion doesn't die if Map is not avialable...49const MapBase: MapConstructor = typeof Map !== "undefined" ? Map : function FakeMap() {} as any5051// TODO: fix types for drafts52export class DraftMap<K, V> extends MapBase implements Map<K, V> {53 [DRAFT_STATE]: MapState54 constructor(target, parent) {55 super()56 this[DRAFT_STATE] = {57 parent,58 scope: parent ? parent.scope : ImmerScope.current,59 modified: false,60 finalized: false,61 finalizing: false,62 copy: undefined,63 assigned: undefined,64 base: target,65 draft: this as any, // TODO: fix typing66 revoke() {67 // TODO: make sure this marks the Map as revoked, and assert everywhere68 }69 };70 }7172 get size(): number {73 return latest(this[DRAFT_STATE]).size74 }7576 has(key: K): boolean {77 return latest(this[DRAFT_STATE]).has(key)78 }7980 set(key: K, value: V): this {81 const state = this[DRAFT_STATE];82 if (latest(state).get(key) !== value) {83 prepareCopy(state)84 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly85 state.assigned!.set(key, true)86 state.copy!.set(key, value)87 state.assigned!.set(key, true);88 }89 return this90 }9192 delete(key: K): boolean {93 if (!this.has(key)) {94 return false;95 }9697 const state = this[DRAFT_STATE];98 prepareCopy(state)99 state.scope.immer.markChanged(state)100 state.assigned!.set(key, false)101 state.copy!.delete(key)102 return true103 }104105 clear() {106 const state = this[DRAFT_STATE];107 prepareCopy(state)108 state.scope.immer.markChanged(state)109 state.assigned = new Map()110 for (const key of latest(state).keys()) {111 state.assigned.set(key, false)112 }113 return state.copy!.clear()114 }115116 // @ts-ignore TODO:117 forEach(cb) {118 const state = this[DRAFT_STATE];119 latest(state).forEach((_value, key, _map) => {120 cb(this.get(key), key, this)121 })122 }123124 get(key: K): V /* TODO: Draft<V> */ {125 const state = this[DRAFT_STATE];126 const value = latest(state).get(key)127 if (state.finalizing || state.finalized || !isDraftable(value)) {128 return value129 }130 if (value !== state.base.get(key)) {131 return value // either already drafted or reassigned132 }

fixed most testsMichel Weststratebefore adoption385c0a4

line 133why, one step back
133 // despite what it looks, this creates a draft only once, see above condition

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 134–143why, one step back
134 const draft = state.scope.immer.createProxy(value, state)
135 prepareCopy(state)136 state.copy!.set(key, draft)137 return draft138 }139140 keys() {141 return latest(this[DRAFT_STATE]).keys();142 }143

fixed most testsMichel Weststratebefore adoption385c0a4

lines 144–176why, one step back
144 values() {
145 const iterator = this.keys()146 return {147 [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly148 next: () => {149 const r = iterator.next()150 if (r.done) return r;151 const value = this.get(r.value);152 return {153 done: false, value154 }155 }156 } as any157 }158159 entries() {160 const iterator = this.keys()161 return {162 [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly163 next: () => {164 const r = iterator.next()165 if (r.done) return r;166 const value = this.get(r.value);167 return {168 done: false, value: [r.value, value]169 }170 }171 } as any172 }173174 [Symbol.iterator]() { // TODO: don't use symbol directly175 return this.entries()176 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 177–179why, one step back
177}
178179

fixed most testsMichel Weststratebefore adoption385c0a4

lines 180–194why, one step back
180/** Map.prototype.values _-or-_ Map.prototype.entries */
181export function iterateMapValues(state, prop, receiver) {182 const isEntries = prop !== "values"183 return () => {184 const iterator = latest(state)[Symbol.iterator]()185 return makeIterable(() => {186 const result = iterator.next()187 if (!result.done) {188 const [key] = result.value189 const value = receiver.get(key)190 result.value = isEntries ? [key, value] : value191 }192 return result193 })194 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 195why, one step back
195}

fixed most testsMichel Weststratebefore adoption385c0a4

lines 196–197why, one step back
196
197

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 198–236why, one step back
198
199export function proxyMap(target, parent) {200 if (target instanceof DraftMap) {201 return target; // TODO: or copy?202 }203 return new DraftMap(target, parent)204 // Object.defineProperties(target, mapTraps)205206}207208// TODO: kill?209function proxyMethod(trap, key) {210 return {211 get() {212 return function(this: ES5Draft, ...args) {213 const state = this[DRAFT_STATE]214 assertUnrevoked(state)215 return trap(state, key, state.draft)(...args)216 }217 }218 }219}220221// TODO: used?222export function hasMapChanges(state) {223 const {base, draft} = state224225 if (base.size !== draft.size) return true226227 // IE11 supports only forEach iteration228 let hasChanges = false229 // TODO: optimize: break on first difference230 draft.forEach(function(value, key) {231 if (!hasChanges) {232 hasChanges = isDraftable(value) ? value.modified : value !== base.get(key)233 }234 })235 return hasChanges236}