UNPKG

marko

Version:

UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.

101 lines (87 loc) 2.32 kB
"use strict";var extend = require("raptor-util/extend"); function ensure(state, propertyName) { var proto = state.constructor.prototype; if (!(propertyName in proto)) { Object.defineProperty(proto, propertyName, { get: function () { return this.A_[propertyName]; }, set: function (value) { this.ar_(propertyName, value, false /* ensure:false */); } }); } } function State(component) { this.s_ = component; this.A_ = {}; this.al_ = false; this.aw_ = null; this.av_ = null; this.aR_ = null; // An object that we use to keep tracking of state properties that were forced to be dirty Object.seal(this); } State.prototype = { _t_: function () { this.al_ = false; this.aw_ = null; this.av_ = null; this.aR_ = null; }, ap_: function (newState) { var key; var rawState = this.A_; for (key in rawState) { if (!(key in newState)) { this.ar_( key, undefined, false /* ensure:false */, false /* forceDirty:false */ ); } } for (key in newState) { this.ar_( key, newState[key], true /* ensure:true */, false /* forceDirty:false */ ); } }, ar_: function (name, value, shouldEnsure, forceDirty) { var rawState = this.A_; if (shouldEnsure) { ensure(this, name); } if (forceDirty) { var forcedDirtyState = this.aR_ || (this.aR_ = {}); forcedDirtyState[name] = true; } else if (rawState[name] === value) { return; } if (!this.al_) { // This is the first time we are modifying the component state // so introduce some properties to do some tracking of // changes to the state this.al_ = true; // Mark the component state as dirty (i.e. modified) this.aw_ = rawState; this.A_ = rawState = extend({}, rawState); this.av_ = {}; this.s_.aq_(); } this.av_[name] = value; if (value === undefined) { // Don't store state properties with an undefined or null value delete rawState[name]; } else { // Otherwise, store the new value in the component state rawState[name] = value; } }, toJSON: function () { return this.A_; } }; module.exports = State;