tchen-vuelayers
Version:
Web map Vue components with the power of OpenLayers
148 lines (122 loc) • 3.46 kB
JavaScript
/**
* VueLayers
* Web map Vue components with the power of OpenLayers
*
* @package vuelayers
* @author Vladimir Vershinin <ghettovoice@gmail.com>
* @version 0.11.1
* @license MIT
* @copyright (c) 2017-2019, Vladimir Vershinin <ghettovoice@gmail.com>
*/
import _Object$keys from '@babel/runtime-corejs2/core-js/object/keys';
import _Object$create from '@babel/runtime-corejs2/core-js/object/create';
import _classCallCheck from '@babel/runtime-corejs2/helpers/esm/classCallCheck';
import _createClass from '@babel/runtime-corejs2/helpers/esm/createClass';
import _defineProperty from '@babel/runtime-corejs2/helpers/esm/defineProperty';
/**
* Simple Identity map with refs count
*/
var IdentityMap =
/*#__PURE__*/
function () {
function IdentityMap() {
_classCallCheck(this, IdentityMap);
_defineProperty(this, "pools", _Object$create(null));
}
_createClass(IdentityMap, [{
key: "_preparePool",
/**
* @param {string} pool
* @private
*/
value: function _preparePool(pool) {
this.pools[pool] || (this.pools[pool] = _Object$create(null));
}
/**
* @param {string} id
* @param {mixed} value
* @param {string} pool
*/
}, {
key: "set",
value: function set(id, value) {
var pool = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'default';
if (value == null) return;
this._preparePool(pool);
this.pools[pool][id] = {
value: value,
refs: 1
};
}
/**
* @param {string} id
* @param {string} pool
*/
}, {
key: "get",
value: function get(id) {
var pool = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
this._preparePool(pool);
var rec = this.pools[pool][id];
if (!rec || rec.value == null) return;
rec.refs++;
this.pools[pool][id] = rec;
return rec.value;
}
/**
* @param {string} id
* @param {string} pool
*/
}, {
key: "unset",
value: function unset(id) {
var pool = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
this._preparePool(pool);
var rec = this.pools[pool][id];
if (!rec || rec.value == null) return;
rec.refs--;
if (rec.refs === 0) {
delete this.pools[pool][id];
} else {
this.pools[pool][id] = rec;
}
}
/**
* @param {string} id
* @param {string} pool
* @return {boolean}
*/
}, {
key: "has",
value: function has(id) {
var pool = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
this._preparePool(pool);
return !!this.pools[pool][id];
}
/**
* @param {string} pool
* @return {string[]}
*/
}, {
key: "ids",
value: function ids() {
var pool = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
this._preparePool(pool);
return _Object$keys(this.pools[pool]);
}
/**
* @param {string} id
* @param {string} pool
* @return {*}
*/
}, {
key: "refs",
value: function refs(id) {
var pool = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
this._preparePool(pool);
return this.has(id, pool) ? this.pools[pool][id].refs : undefined;
}
}]);
return IdentityMap;
}();
export default IdentityMap;