fix: #502
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent b2f23c4 · Browse files · 13 files·+115 added−131 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
13 files+115 added−131 removed
- tests:+15 added−0 removed
- other:+100 added−131 removed
Provenance
- Commit
8d4f723- Tree
a018d43- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
b2f23c4
13 files changed+115 added−131 removed
modified:__tests__/base.js+14 added
580 unmodified lines581 expect(entries).toEqual([[key, 4]])582 expect(entries[0][0]).toBe(key)583 expect(entries[0][0].a).toBe(2)584 })585+586+ it("does support instanceof Map", () => {587+ const map = new Map()588+ produce(map, d => {589+ expect(d instanceof Map).toBeTruthy()590+ })591+ })592 })593 594 describe("set drafts", () => {595 it("supports iteration", () => {259 unmodified lines855 expect(() => m.add("x")).toThrow(856 "Cannot use a proxy that has been revoked"857 )858 })859+860+ it("does support instanceof Set", () => {861+ const set = new Set()862+ produce(set, d => {863+ expect(d instanceof Set).toBeTruthy()864+ })865+ })866 })867 868 it("supports `immerable` symbol on constructor", () => {869 class One {}1105 unmodified linesmodified:__tests__/empty.ts+1 added
8 unmodified lines91011121314151617125 unmodified linesExpand all8 unmodified lines test("can assign set value", () => { setUseProxies(false)
const baseState = new Map([["x", 1]]) debugger const nextState = produce(baseState, s => { s.set("x", 2) }) expect(baseState.get("x")).toEqual(1)125 unmodified linesExpand allmodified:src/common.ts+2 added-42 removed
1+import {DRAFT_STATE, DRAFTABLE, hasSet} from "./env"2 import {3 Objectish,4 Drafted,5 AnyObject,3 unmodified lines9 ImmerState,10 ProxyType,11 Archtype12 } from "./types"13+import {isMap} from "./map"14 13-/** Use a class type for `nothing` so its type is unique */14-export class Nothing {15- // This lets us do `Exclude<T, Nothing>`16- // @ts-ignore17- private _!: unique symbol18-}19-20-const hasSymbol = typeof Symbol !== "undefined"21-export const hasMap = typeof Map !== "undefined"22-export const hasSet = typeof Set !== "undefined"23-24-/**25- * The sentinel value returned by producers to replace the draft with undefined.26- */27-export const NOTHING: Nothing = hasSymbol28- ? Symbol("immer-nothing")29- : ({["immer-nothing"]: true} as any)30-31-/**32- * To let Immer treat your class instances as plain immutable objects33- * (albeit with a custom prototype), you must define either an instance property34- * or a static property on each of your custom classes.35- *36- * Otherwise, your class instance will never be drafted, which means it won't be37- * safe to mutate in a produce callback.38- */39-export const DRAFTABLE: unique symbol = hasSymbol40- ? Symbol("immer-draftable")41- : ("__$immer_draftable" as any)42-43-export const DRAFT_STATE: unique symbol = hasSymbol44- ? Symbol("immer-state")45- : ("__$immer_state" as any)46-47-export const iteratorSymbol: typeof Symbol.iterator = hasSymbol48- ? Symbol.iterator49- : ("@@iterator" as any)50-15 /** Returns true if the given value is an Immer draft */16 export function isDraft(value: any): boolean {17 return !!value && !!value[DRAFT_STATE]18 }111 unmodified lines130 return x !== x && y !== y131 }132 }133 170-export function isMap(target: any): target is AnyMap {171- return hasMap && target instanceof Map172-}173-134 export function isSet(target: any): target is AnySet {135 return hasSet && target instanceof Set136 }137 68 unmodified linesmodified:src/es5.ts+1 added-1 removed
5 unmodified lines6 isDraft,7 isDraftable,8 isEnumerable,9 shallowCopy,10- DRAFT_STATE,10 latest,11 createHiddenProperty12 } from "./common"13 8 unmodified lines22 ProxyType23 } from "./types"24 import {MapState} from "./map"25 import {SetState} from "./set"26+import {DRAFT_STATE} from "./env"27 28 interface ES5BaseState extends ImmerBaseState {29 finalizing: boolean30 assigned: {[key: string]: any}276 unmodified linesmodified:src/finalize.ts+2 added-12 removed
1 import {Immer} from "./immer"2 import {ImmerState, Drafted, ProxyType} from "./types"3 import {ImmerScope} from "./scope"4-import {5- isSet,6- has,7- is,8- get,9- each,10- DRAFT_STATE,11- NOTHING,12- freeze,13- shallowCopy,14- set15-} from "./common"4+import {isSet, has, is, get, each, freeze, shallowCopy, set} from "./common"5 import {isDraft, isDraftable} from "./index"6 import {SetState} from "./set"7 import {generatePatches, PatchPath} from "./patches"8+import {DRAFT_STATE, NOTHING} from "./env"9 10 export function processResult(immer: Immer, result: any, scope: ImmerScope) {11 const baseDraft = scope.drafts![0]12 const isReplaced = result !== undefined && result !== baseDraft181 unmodified linesmodified:src/immer.ts+3 added-11 removed
1 import {createES5Proxy, willFinalizeES5, markChangedES5} from "./es5"2 import {createProxy, markChanged} from "./proxy"3 4 import {applyPatches} from "./patches"5-import {6- each,7- isDraft,8- isSet,9- isMap,10- isDraftable,11- DRAFT_STATE,12- NOTHING,13- die14-} from "./common"5+import {each, isDraft, isSet, isDraftable, die} from "./common"6 import {ImmerScope} from "./scope"7 import {8 ImmerState,9 IProduce,3 unmodified lines13 Draft,14 Patch,15 Drafted16 } from "./types"26-import {proxyMap} from "./map"17+import {proxyMap, isMap} from "./map"18 import {proxySet} from "./set"19 import {processResult, maybeFreeze} from "./finalize"20+import {NOTHING, DRAFT_STATE} from "./env"21 22 /* istanbul ignore next */23 function verifyMinified() {}24 244 unmodified linesmodified:src/map.ts+48 added-27 removed
1-import {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"1+import {__extends} from "./extends"2+import {isDraftable, latest} from "./common"3 4 import {ImmerScope} from "./scope"5 import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6 import {assertUnrevoked} from "./es5"7+import {DRAFT_STATE, iteratorSymbol, hasMap} from "./env"8 9 export interface MapState extends ImmerBaseState {10 type: ProxyType.Map11 copy: AnyMap | undefined2 unmodified lines14 revoked: boolean15 draft: Drafted<AnyMap, MapState>16 }17 16-// Make sure DraftMap declarion doesn't die if Map is not avialable...17-/* istanbul ignore next */18-const MapBase: MapConstructor =19- typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)20-21-export class DraftMap<K, V> extends MapBase implements Map<K, V> {22- [DRAFT_STATE]: MapState23- constructor(target: AnyMap, parent?: ImmerState) {24- super()18+const DraftMap = (function(_super) {19+ if (!_super) {20+ /* istanbul ignore next */21+ throw new Error("Map is not polyfilled")22+ }23+ __extends(DraftMap, _super)24+ // Create class manually, cause #50225+ function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {26 this[DRAFT_STATE] = {27 type: ProxyType.Map,28 parent,29 scope: parent ? parent.scope : ImmerScope.current!,1 unmodified line31 finalized: false,32 copy: undefined,33 assigned: undefined,34 base: target,34- draft: this,35+ draft: this as any,36 isManual: false,37 revoked: false38 }39+ return this40 }41+ const p = DraftMap.prototype42 43+ // TODO: smaller build size if we create a util for Object.defineProperty44+ Object.defineProperty(p, "size", {40- get size(): number {41- return latest(this[DRAFT_STATE]).size42- }45+ get: function() {46+ return latest(this[DRAFT_STATE]).size47+ },48+ enumerable: true,49+ configurable: true50+ })51 44- has(key: K): boolean {52+ p.has = function(key: any): boolean {53 return latest(this[DRAFT_STATE]).has(key)54 }55 48- set(key: K, value: V): this {56+ p.set = function(key: any, value: any) {57 const state = this[DRAFT_STATE]58 assertUnrevoked(state)59 if (latest(state).get(key) !== value) {60 prepareCopy(state)4 unmodified lines65 }66 return this67 }68 61- delete(key: K): boolean {69+ p.delete = function(key: any): boolean {70 if (!this.has(key)) {71 return false72 }73 5 unmodified lines79 state.copy!.delete(key)80 return true81 }82 75- clear() {83+ p.clear = function() {84 const state = this[DRAFT_STATE]85 assertUnrevoked(state)86 prepareCopy(state)87 state.scope.immer.markChanged(state)3 unmodified lines91 }92 return state.copy!.clear()93 }94 87- forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {95+ p.forEach = function(96+ cb: (value: any, key: any, self: any) => void,97+ thisArg?: any98+ ) {99 const state = this[DRAFT_STATE]89- latest(state).forEach((_value: V, key: K, _map: this) => {100+ latest(state).forEach((_value: any, key: any, _map: any) => {101 cb.call(thisArg, this.get(key), key, this)102 })103 }104 94- get(key: K): V {105+ p.get = function(key: any): any {106 const state = this[DRAFT_STATE]107 assertUnrevoked(state)108 const value = latest(state).get(key)109 if (state.finalized || !isDraftable(value)) {8 unmodified lines118 state.copy!.set(key, draft)119 return draft120 }121 111- keys(): IterableIterator<K> {122+ p.keys = function(): IterableIterator<any> {123 return latest(this[DRAFT_STATE]).keys()124 }125 115- values(): IterableIterator<V> {126+ p.values = function(): IterableIterator<any> {127 const iterator = this.keys()128 return {129 [iteratorSymbol]: () => this.values(),130 next: () => {8 unmodified lines139 }140 } as any141 }142 132- entries(): IterableIterator<[K, V]> {143+ p.entries = function(): IterableIterator<[any, any]> {144 const iterator = this.keys()145 return {146 [iteratorSymbol]: () => this.entries(),147 next: () => {8 unmodified lines156 }157 } as any158 }159 149- [iteratorSymbol]() {160+ p[iteratorSymbol] = function() {161 return this.entries()162 }152-}163 154-export function proxyMap(target: AnyMap, parent?: ImmerState) {164+ return DraftMap165+})(Map)166+167+export function proxyMap<T extends AnyMap>(168+ target: T,169+ parent?: ImmerState170+): T & {[DRAFT_STATE]: MapState} {171+ // @ts-ignore172 return new DraftMap(target, parent)173 }174 175 function prepareCopy(state: MapState) {1 unmodified line177 state.assigned = new Map()178 state.copy = new Map(state.base)179 }180 }181+182+export function isMap(target: any): target is AnyMap {183+ return hasMap && (target instanceof Map || target instanceof DraftMap) // TODO: fix184+}modified:src/patches.ts+2 added-2 removed
1-import {get, each, isMap, has, die, getArchtype} from "./common"1+import {get, each, has, die, getArchtype} from "./common"2 import {Patch, ImmerState, ProxyType, Archtype} from "./types"3 import {SetState} from "./set"4 import {ES5ArrayState, ES5ObjectState} from "./es5"5 import {ProxyArrayState, ProxyObjectState} from "./proxy"6-import {MapState} from "./map"6+import {MapState, isMap} from "./map"7 8 export type PatchPath = (string | number)[]9 10 export function generatePatches(240 unmodified linesmodified:src/proxy.ts+2 added-9 removed
1 "use strict"2-import {3- each,4- has,5- is,6- isDraftable,7- shallowCopy,8- DRAFT_STATE,9- latest10-} from "./common"2+import {each, has, is, isDraftable, shallowCopy, latest} from "./common"3 import {ImmerScope} from "./scope"4 import {5 AnyObject,6 Drafted,2 unmodified lines9 Objectish,10 ImmerBaseState,11 ProxyType12 } from "./types"13+import {DRAFT_STATE} from "./env"14 15 interface ProxyBaseState extends ImmerBaseState {16 assigned: {17 [property: string]: boolean235 unmodified linesmodified:src/scope.ts+1 added-1 removed
1-import {DRAFT_STATE} from "./common"1 import {Patch, PatchListener, Drafted, ProxyType} from "./types"2 import {Immer} from "./immer"3+import {DRAFT_STATE} from "./env"4 5 /** Each scope represents a `produce` call. */6 export class ImmerScope {7 static current?: ImmerScope56 unmodified linesmodified:src/set.ts+37 added-24 removed
1-import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"1+import {__extends} from "./extends"2+import {latest, isDraftable} from "./common"3 4 import {ImmerScope} from "./scope"5 import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6 import {assertUnrevoked} from "./es5"7+import {DRAFT_STATE, iteratorSymbol} from "./env"8 9 export interface SetState extends ImmerBaseState {10 type: ProxyType.Set11 copy: AnySet | undefined2 unmodified lines14 revoked: boolean15 draft: Drafted<AnySet, SetState>16 }17 16-// Make sure DraftSet declarion doesn't die if Map is not avialable...17-/* istanbul ignore next */18-const SetBase: SetConstructor =19- typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)20-21-export class DraftSet<K, V> extends SetBase implements Set<V> {22- [DRAFT_STATE]: SetState23- constructor(target: AnySet, parent?: ImmerState) {24- super()18+const DraftSet = (function(_super) {19+ if (!_super) {20+ /* istanbul ignore next */21+ throw new Error("Set is not polyfilled")22+ }23+ __extends(DraftSet, _super)24+ // Create class manually, cause #50225+ function DraftSet(this: any, target: AnySet, parent?: ImmerState) {26 this[DRAFT_STATE] = {27 type: ProxyType.Set,28 parent,29 scope: parent ? parent.scope : ImmerScope.current!,5 unmodified lines35 drafts: new Map(),36 revoked: false,37 isManual: false38 }39+ return this40 }41+ const p = DraftSet.prototype42 43+ Object.defineProperty(p, "size", {40- get size(): number {41- return latest(this[DRAFT_STATE]).size42- }44+ get: function() {45+ return latest(this[DRAFT_STATE]).size46+ },47+ enumerable: true,48+ configurable: true49+ })50 44- has(value: V): boolean {51+ p.has = function(value: any): boolean {52 const state = this[DRAFT_STATE]53 assertUnrevoked(state)54 // bit of trickery here, to be able to recognize both the value, and the draft of its value55 if (!state.copy) {4 unmodified lines60 return true61 return false62 }63 57- add(value: V): this {64+ p.add = function(value: any): any {65 const state = this[DRAFT_STATE]66 assertUnrevoked(state)67 if (state.copy) {68 state.copy.add(value)4 unmodified lines73 }74 return this75 }76 70- delete(value: V): boolean {77+ p.delete = function(value: any): any {78 if (!this.has(value)) {79 return false80 }81 8 unmodified lines90 : /* istanbul ignore next */ false)91 )92 }93 87- clear() {94+ p.clear = function() {95 const state = this[DRAFT_STATE]96 assertUnrevoked(state)97 prepareCopy(state)98 state.scope.immer.markChanged(state)99 return state.copy!.clear()100 }101 95- values(): IterableIterator<V> {102+ p.values = function(): IterableIterator<any> {103 const state = this[DRAFT_STATE]104 assertUnrevoked(state)105 prepareCopy(state)106 return state.copy!.values()107 }108 102- entries(): IterableIterator<[V, V]> {109+ p.entries = function entries(): IterableIterator<[any, any]> {110 const state = this[DRAFT_STATE]111 assertUnrevoked(state)112 prepareCopy(state)113 return state.copy!.entries()114 }115 109- keys(): IterableIterator<V> {116+ p.keys = function(): IterableIterator<any> {117 return this.values()118 }119 113- [iteratorSymbol]() {120+ p[iteratorSymbol] = function() {121 return this.values()122 }123 117- forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {124+ p.forEach = function forEach(cb: any, thisArg?: any) {125 const iterator = this.values()126 let result = iterator.next()127 while (!result.done) {128 cb.call(thisArg, result.value, result.value, this)129 result = iterator.next()130 }131 }125-}132 127-export function proxySet(target: AnySet, parent?: ImmerState) {133+ return DraftSet134+})(Set)135+136+export function proxySet<T extends AnySet>(137+ target: T,138+ parent?: ImmerState139+): T & {[DRAFT_STATE]: SetState} {140+ // @ts-ignore141 return new DraftSet(target, parent)142 }143 144 function prepareCopy(state: SetState) {14 unmodified linesmodified:src/types.ts+1 added-1 removed
1-import {Nothing, DRAFT_STATE} from "./common"1 import {SetState} from "./set"2 import {MapState} from "./map"3 import {ProxyObjectState, ProxyArrayState} from "./proxy"4 import {ES5ObjectState, ES5ArrayState} from "./es5"5 import {ImmerScope} from "./scope"6+import {DRAFT_STATE, Nothing} from "./env"7 8 export type Objectish = AnyObject | AnyArray | AnyMap | AnySet9 export type ObjectishNoSet = AnyObject | AnyArray | AnyMap10 206 unmodified linesmodified:tsconfig.json+1 added-1 removed
1 {2 "compilerOptions": {3 "lib": ["es2015"],4- "target": "ES6",4+ "target": "ES5",5 "strict": true,6 "declaration": true,7 "importHelpers": false,8 "noImplicitAny": true,9 unmodified lines