UNPKG

@chix/flow

Version:
244 lines 8.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _debug = require("debug"); var json_ptr_1 = require("json-ptr"); var forOf = require("object-forof"); var ContainerPacket_1 = require("./ContainerPacket"); var debug = _debug('chix:packet'); var nr = 10000000000; var Packet = (function () { function Packet(data, type, n, c, pp) { this.trail = []; this.typeTrail = []; this.pointerPath = '/.'; this._meta = {}; this.c = 0; this.updated_at = null; this.chi = {}; if (pp) { this.pointerPath = pp; } this.type = type || typeof data; this.__data = data instanceof ContainerPacket_1.ContainerPacket ? data : new ContainerPacket_1.ContainerPacket(data); this.nr = n || nr++; if (c) { this.c = c; } this.pointer = json_ptr_1.JsonPointer.create(this.pointerPath); Object.defineProperty(this, 'data', { get: function () { throw Error('data property should not be accessed'); }, set: function () { throw Error('data property should not be written to'); }, }); this.created_at = new Date(); } Packet.create = function (data, type, n, c, pp) { return new Packet(data, type, n, c, pp); }; Packet.metaMerge = function (target, source) { forOf(function (ns, name, val) { if (!target.hasOwnProperty(ns)) { target[ns] = {}; } if (!target[ns].hasOwnProperty(name) || target[ns][name] === val) { target[ns][name] = val; } else { console.log('tgt:', target, 'src:', source); console.log('oldValue: %s, newValue: %s', target[ns][name], val); throw Error("Refusing to overwrite meta property " + ns + ":" + name); } }, source); }; Packet.isPacket = function (packet) { return (packet && typeof packet === 'object' && (packet instanceof Packet || packet.constructor.name === 'Packet')); }; Packet.prototype.point = function (owner, pointer) { if (this.isOwner(owner)) { if (pointer === undefined || pointer === '') { this.pointerPath = '/.'; } else if (pointer[0] === '/') { this.pointerPath = '/.' + pointer; } else { this.pointerPath = this.pointer.pointer + '/' + pointer; } this.pointer = json_ptr_1.JsonPointer.create(this.pointerPath); } return this; }; Packet.prototype.read = function (owner) { if (!this.hasOwner() || this.isOwner(owner)) { return this.pointer.get(this.__data); } throw Error('Not allowed to read packet'); }; Packet.prototype.write = function (owner, data, type) { if (this.isOwner(owner)) { this.updated_at = new Date(); this.pointer.set(this.__data, data); if (type) { this.type = type; } return this; } throw Error('Not allowed to write packet'); }; Packet.prototype.clone = function (owner) { if (this.isOwner(owner)) { var packet = new Packet(null, undefined, this.nr, this.c + 1, this.pointerPath); packet.setOwner(owner); if (!this.type) { throw Error('Refusing to clone substance of unknown type'); } if (this.type === 'function' || /[A-Z]/.test(this.type)) { packet.__data = this.__data; } else { try { packet.__data = JSON.parse(JSON.stringify(this.__data)); } catch (e) { console.log('ERROR', this.__data, e); throw e; } } packet.type = this.type; packet._meta = JSON.parse(JSON.stringify(this._meta)); return packet; } throw Error('Packet is not owned by this owner, refusing to clone.'); }; Packet.prototype.setType = function (owner, type) { if (this.isOwner(owner)) { this.typeTrail.push(this.type); this.type = type; return this; } return this; }; Packet.prototype.release = function (owner) { if (this.isOwner(owner)) { this.trail.push({ id: owner.id, pid: owner.pid, }); this.owner = undefined; return this; } return this; }; Packet.prototype.setOwner = function (newOwner) { if (this.owner === undefined) { this.owner = newOwner; return this; } if (newOwner === this.owner) { debug('Warning: Packet already owned by this owner'); return this; } throw Error('Refusing to overwrite owner'); }; Packet.prototype.hasOwner = function () { return this.owner !== undefined; }; Packet.prototype.isOwner = function (owner) { if (owner === this.owner) { return true; } if (!owner) { throw Error('Packet.isOwner expects an owner object as parameter'); } else if (this.owner === undefined) { debug('Warning: Packet is unclaimed, claim it first'); this.setOwner(owner); return true; } debug('Warning: Packet is not owned by this instance. REQUESTOR: %s:%s OWNER: %s:%s', owner.constructor.name, owner.name || owner, this.owner.constructor.name, this.owner.name || this.owner); this.release(this.owner); this.setOwner(owner); return true; }; Packet.prototype.dump = function () { return JSON.stringify(this, null, 2); }; Packet.prototype.set = function (prop, value) { ; this[prop] = value; return this; }; Packet.prototype.get = function (prop) { return this[prop]; }; Packet.prototype.del = function (prop) { delete this[prop]; return this; }; Packet.prototype.has = function (prop) { return this.hasOwnProperty(prop); }; Packet.prototype.meta = function (ns, key, val) { if (val !== undefined) { if (!this._meta[ns]) { this._meta[ns] = {}; } this._meta[ns][key] = val; return this; } if (!this._meta.hasOwnProperty(ns)) { throw Error("No such key " + ns); } if (key) { if (!this._meta[ns].hasOwnProperty(key)) { throw Error("No such key [" + ns + "][" + key + "]"); } return this._meta[ns][key]; } return this._meta[ns]; }; Packet.prototype.hasMeta = function (ns, key) { if (!this._meta.hasOwnProperty(ns)) { return false; } return this._meta[ns].hasOwnProperty(key); }; Packet.prototype.removeMeta = function (ns, key) { if (this._meta.hasOwnProperty(ns)) { if (key) { if (this._meta[ns].hasOwnProperty(key)) { delete this._meta[ns][key]; } } else { delete this._meta[ns]; } } }; Packet.prototype.toJSON = function () { return this.pointer.get(this.__data); }; Packet.prototype.export = function () { return { c: this.c, data: this.pointer.get(this.__data), meta: this._meta, nr: this.nr, owner: this.owner ? this.owner.id : null, pointerPath: this.pointerPath, trail: this.trail, type: this.type, typeTrail: this.typeTrail, }; }; return Packet; }()); exports.Packet = Packet; //# sourceMappingURL=packet.js.map