UNPKG

babelute

Version:

Internal Domain Specific (Multi)Modeling javascript framework

114 lines (93 loc) 3.84 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } // removed in production /** * Inner-sentence-scopes manager : hold array as stacks for inner-scopes of sentences (if needed). It's only avaiable in pragmatics, while traversing, and is dependent of what pragmatics do. See htsl-view as an example of usage. * * So its a simple helper aimed to (while interpreting sentences) : * - give a space where store/access needed variables. * - manage "inner sentences scopes" (as current view container, current context, etc). * * It has to be used carefully after reading this : * * For certain output types (as in htsl diffing) it has to be "pure". * (in a functional way of thinking). * It means that it should contains nothing else * than "local" variables produced and managed while interpreting sentences. * No outside-sentence variables should be needed to perform the output. * And so, two output from the same sentence should be the same. * * So by example, htsl-view use it to keep track (for managing view's life cycle) * of views tree while rendering (with the scope facility prodived here). * * Views are inner-sentences objects, and so as needed, * two render on same htsl sentence will provide same output. * * For other DSL and outputs types, it depends what you want and implement, but be sure of what your doing * before introducing outside variables dependencies in sentences interpretations. */ var Scopes = function () { /** * @param {?Object} scope initial scope object */ function Scopes() { var scope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Scopes); /** * the scopes holder * @type {Object} * @protected */ this.scope = scope; } /** * push value to scope[name] * @param {string} name the scope name * @param {*} value the value to push * @return {number} the new length to be consistent with array push */ _createClass(Scopes, [{ key: 'push', value: function push(name, value) { this.scope[name] = this.scope[name] || []; return this.scope[name].push(value); } /** * pop value from scope[name] * @param {string} name the scope name to pop * @return {*} the popped value */ }, { key: 'pop', value: function pop(name) { if (!this.scope[name].length) return; return this.scope[name].pop(); } /** * get scope value by name * @param {string} name the scope name * @return {*} the top value * @throws {Error} If scope not found with name */ }, { key: 'get', value: function get(name) { var scope = this.scope[name]; if (!scope) throw new Error('scope not found with : ' + name); return scope[scope.length - 1]; } }]); return Scopes; }(); /** * @author Gilles Coomans * @licence MIT * @copyright 2016-2017 Gilles Coomans */ /******************************************************* ************** Babelute Acions Environment ************ *******************************************************/ exports.default = Scopes;