Skip to content
Myr

classless es5 impl

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 388f4f1 · Browse files · 1 file+44 added−44 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+44 added−44 removed

  • other:+44 added−44 removed

1 file+44 added−44 removed

modified:src/es5.js+44 added-44 removed
40 unmodified lines
41 let finished = false42 const descriptors = {}43 const states = []4445 class State {46 constructor(parent, proxy, base) {47 this.modified = false48 this.hasCopy = false49 this.parent = parent50 this.base = base51 this.proxy = proxy52 this.copy = undefined45 function createState(parent, proxy, base) {46 return {47 modified: false,48 hasCopy: false,49 parent,50 base,51 proxy,52 copy: undefined53 }54 }5555 get source() {56 return this.hasCopy ? this.copy : this.base57 }56 function source(state) {57 return state.hasCopy ? state.copy : state.base58 }5959 get(prop) {60 assertUnfinished()61 const value = this.source[prop]62 if (!finalizing && !isProxy(value) && isProxyable(value)) {63 this.prepareCopy()64 return (this.copy[prop] = createProxy(this, value))65 }66 return value60 function get(state, prop) {61 assertUnfinished(state)62 const value = source(state)[prop]63 if (!finalizing && !isProxy(value) && isProxyable(value)) {64 prepareCopy(state)65 return (state.copy[prop] = createProxy(state, value))66 }67 return value68 }6969 set(prop, value) {70 assertUnfinished()71 if (!this.modified) {72 if (Object.is(this.source[prop], value)) return73 this.markChanged()74 }75 this.prepareCopy()76 this.copy[prop] = value70 function set(state, prop, value) {71 assertUnfinished()72 if (!state.modified) {73 if (Object.is(source(state)[prop], value)) return74 markChanged(state)75 }76 prepareCopy(state)77 state.copy[prop] = value78 }7979 markChanged() {80 if (!this.modified) {81 this.modified = true82 if (this.parent) this.parent.markChanged()83 }80 function markChanged(state) {81 if (!state.modified) {82 state.modified = true83 if (state.parent) markChanged(state.parent)84 }85 }8686 prepareCopy() {87 if (this.hasCopy) return88 this.hasCopy = true89 this.copy = Array.isArray(this.base)90 ? this.base.slice()91 : Object.assign({}, this.base)92 }87 function prepareCopy(state) {88 if (state.hasCopy) return89 state.hasCopy = true90 state.copy = Array.isArray(state.base)91 ? state.base.slice()92 : Object.assign({}, state.base)93 }9495 // creates a proxy for plain objects / arrays96 function createProxy(parent, base) {97 let proxy98 if (Array.isArray(base)) proxy = createArrayProxy(base)99 else proxy = createObjectProxy(base)100 const state = new State(parent, proxy, base)100 const state = createState(parent, proxy, base)101 createHiddenProperty(proxy, PROXY_STATE, state)102 states.push(state)103 return proxy104 }
4 unmodified lines
109 (descriptors[prop] = {110 configurable: true,111 enumerable: true,112 get() {113 return this[PROXY_STATE].get(prop)113 return get(this[PROXY_STATE], prop)114 },115 set(value) {116 this[PROXY_STATE].set(prop, value)116 set(this[PROXY_STATE], prop, value)117 }118 })119 )120 }
30 unmodified lines
151 for (let i = states.length - 1; i >= 0; i--) {152 const state = states[i]153 if (state.modified === false) {154 if (Array.isArray(state.base)) {155 if (hasArrayChanges(state)) state.markChanged()156 } else if (hasObjectChanges(state)) state.markChanged()155 if (hasArrayChanges(state)) markChanged(state)156 } else if (hasObjectChanges(state)) markChanged(state)157 }158 }159 }160
136 unmodified lines