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.B_[propertyName]; }, set: function (value) { this.at_(propertyName, value, false /* ensure:false */); } }); } } function State(component) { this.s_ = component; this.B_ = {}; this.an_ = false; this.ay_ = null; this.ax_ = null; this.aT_ = null; // An object that we use to keep tracking of state properties that were forced to be dirty Object.seal(this); } State.prototype = { _v_: function () { this.an_ = false; this.ay_ = null; this.ax_ = null; this.aT_ = null; }, ar_: function (newState) { var key; var rawState = this.B_; for (key in rawState) { if (!(key in newState)) { this.at_( key, undefined, false /* ensure:false */, false /* forceDirty:false */ ); } } for (key in newState) { this.at_( key, newState[key], true /* ensure:true */, false /* forceDirty:false */ ); } }, at_: function (name, value, shouldEnsure, forceDirty) { var rawState = this.B_; if (shouldEnsure) { ensure(this, name); } if (forceDirty) { var forcedDirtyState = this.aT_ || (this.aT_ = {}); forcedDirtyState[name] = true; } else if (rawState[name] === value) { return; } if (!this.an_) { // 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.an_ = true; // Mark the component state as dirty (i.e. modified) this.ay_ = rawState; this.B_ = rawState = extend({}, rawState); this.ax_ = {}; this.s_.as_(); } this.ax_[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.B_; } }; module.exports = State;