curvature
Version:

234 lines (233 loc) • 9.6 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Bag = void 0;
var _Bindable = require("./Bindable");
var _Mixin = require("./Mixin");
var _EventTargetMixin = require("../mixin/EventTargetMixin");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var toId = function toId(_int) {
return Number(_int);
};
var fromId = function fromId(id) {
return parseInt(id);
};
var Mapped = Symbol('Mapped');
var Has = Symbol('Has');
var Add = Symbol('Add');
var Remove = Symbol('Remove');
var Delete = Symbol('Delete');
var Bag = /*#__PURE__*/function (_Mixin$with) {
_inherits(Bag, _Mixin$with);
var _super = _createSuper(Bag);
function Bag() {
var _this;
var changeCallback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
_classCallCheck(this, Bag);
_this = _super.call(this);
_this.changeCallback = changeCallback;
_this.content = new Map();
_this.current = 0;
_this.length = 0;
_this.list = _Bindable.Bindable.makeBindable([]);
_this.meta = Symbol('meta');
_this.type = undefined;
return _this;
}
_createClass(Bag, [{
key: "has",
value: function has(item) {
if (this[Mapped]) {
return this[Mapped].has(item);
}
return this[Has](item);
}
}, {
key: Has,
value: function value(item) {
return this.content.has(item);
}
}, {
key: "add",
value: function add(item) {
if (this[Mapped]) {
return this[Mapped].add(item);
}
return this[Add](item);
}
}, {
key: Add,
value: function value(item) {
if (item === undefined || !(item instanceof Object)) {
throw new Error('Only objects may be added to Bags.');
}
if (this.type && !(item instanceof this.type)) {
console.error(this.type, item);
throw new Error("Only objects of type ".concat(this.type, " may be added to this Bag."));
}
item = _Bindable.Bindable.make(item);
if (this.content.has(item)) {
return;
}
var adding = new CustomEvent('adding', {
detail: {
item: item
}
});
if (!this.dispatchEvent(adding)) {
return;
}
var id = toId(this.current++);
this.content.set(item, id);
this.list[id] = item;
if (this.changeCallback) {
this.changeCallback(item, this.meta, Bag.ITEM_ADDED, id);
}
var add = new CustomEvent('added', {
detail: {
item: item,
id: id
}
});
this.dispatchEvent(add);
this.length = this.size;
return id;
}
}, {
key: "remove",
value: function remove(item) {
if (this[Mapped]) {
return this[Mapped].remove(item);
}
return this[Remove](item);
}
}, {
key: Remove,
value: function value(item) {
if (item === undefined || !(item instanceof Object)) {
throw new Error('Only objects may be removed from Bags.');
}
if (this.type && !(item instanceof this.type)) {
console.error(this.type, item);
throw new Error("Only objects of type ".concat(this.type, " may be removed from this Bag."));
}
item = _Bindable.Bindable.make(item);
if (!this.content.has(item)) {
if (this.changeCallback) {
this.changeCallback(item, this.meta, 0, undefined);
}
return false;
}
var removing = new CustomEvent('removing', {
detail: {
item: item
}
});
if (!this.dispatchEvent(removing)) {
return;
}
var id = this.content.get(item);
delete this.list[id];
this.content["delete"](item);
if (this.changeCallback) {
this.changeCallback(item, this.meta, Bag.ITEM_REMOVED, id);
}
var remove = new CustomEvent('removed', {
detail: {
item: item,
id: id
}
});
this.dispatchEvent(remove);
this.length = this.size;
return item;
}
}, {
key: "delete",
value: function _delete(item) {
if (this[Mapped]) {
return this[Mapped]["delete"](item);
}
this[Delete](item);
}
}, {
key: Delete,
value: function value(item) {
this.remove(item);
}
}, {
key: "map",
value: function map() {
var mapper = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function (x) {
return x;
};
var filter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (x) {
return x;
};
var mappedItems = new WeakMap();
var mappedBag = new Bag();
mappedBag[Mapped] = this;
this.addEventListener('added', function (event) {
var item = event.detail.item;
if (!filter(item)) {
return;
}
if (mappedItems.has(item)) {
return;
}
var mapped = mapper(item);
mappedItems.set(item, mapped);
mappedBag[Add](mapped);
});
this.addEventListener('removed', function (event) {
var item = event.detail.item;
if (!mappedItems.has(item)) {
return;
}
var mapped = mappedItems.get(item);
mappedItems["delete"](item);
mappedBag[Remove](mapped);
});
return mappedBag;
}
}, {
key: "size",
get: function get() {
return this.content.size;
}
}, {
key: "items",
value: function items() {
return Array.from(this.content.entries()).map(function (entry) {
return entry[0];
});
}
}]);
return Bag;
}(_Mixin.Mixin["with"](_EventTargetMixin.EventTargetMixin));
exports.Bag = Bag;
Object.defineProperty(Bag, 'ITEM_ADDED', {
configurable: false,
enumerable: false,
writable: true,
value: 1
});
Object.defineProperty(Bag, 'ITEM_REMOVED', {
configurable: false,
enumerable: false,
writable: true,
value: -1
});