UNPKG

diffusion

Version:

Diffusion JavaScript client

194 lines (156 loc) 5.34 kB
var _implements = require('util/interface')._implements; var Set = require('core-js/library/fn/set'); var api = require('../../topic-update/update-constraint'); var DataTypes = require('data/datatypes'); var JSONPointer = require('data/json/json-pointer'); var requireNonNull = require("util/require-non-null"); var ConstraintType = { UNCONSTRAINED: 0, CONJUNCTION: 1, BINARY_VALUE: 2, NO_VALUE: 3, LOCKED: 4, NO_TOPIC: 5, PARTIAL_JSON: 6 }; var Unconstrained = _implements( api.UpdateConstraint, function Unconstrained() { this.and = andConstraint.bind(this); this.getConstraintType = function() { return 'UNCONSTRAINED'; }; } ); var Conjunction = _implements( api.UpdateConstraint, function Conjunction(constraints) { checkSatisfiability(constraints); this.and = function(other) { if (!Array.isArray(other) && other.getConstraintType() === 'CONJUNCTION') { return other.and(constraints); } var newConstraints = constraints.concat(other); checkSatisfiability(newConstraints); return new Conjunction(newConstraints); }; this.getConstraints = function() { return constraints.slice(); }; this.getConstraintType = function() { return 'CONJUNCTION'; }; } ); var BinaryValue = _implements( api.UpdateConstraint, function BinaryValue(bytes) { this.and = andConstraint.bind(this); this.getBytes = function() { return bytes; }; this.getConstraintType = function() { return 'BINARY_VALUE'; }; } ); var Locked = _implements( api.UpdateConstraint, function Locked(lock) { var lockName = lock.getName(); var sequence = lock.getSequence(); this.and = andConstraint.bind(this); this.getLockName = function() { return lockName; }; this.getSequence = function() { return sequence; }; this.getConstraintType = function() { return 'LOCKED'; }; } ); var NoTopic = _implements( api.UpdateConstraint, function NoTopic() { this.and = andConstraint.bind(this); this.getConstraintType = function() { return 'NO_TOPIC'; }; } ); var NoValue = _implements( api.UpdateConstraint, function NoValue() { this.and = andConstraint.bind(this); this.getConstraintType = function() { return 'NO_VALUE'; }; } ); var PartialJSONImpl = _implements( api.PartialJSON, function PartialJSONImpl(withVals, withoutVals) { var withValues = (withVals!==undefined)?withVals:{}; var withoutValues = (withoutVals!==undefined)?withoutVals:(new Set()); this.and = andConstraint.bind(this); this.getWithValues = function() { return Object.assign({}, withValues); }; this.getWithoutValues = function() { return new Set(withoutValues); }; this.with = function(pointer, value, dataType) { var valueType = (dataType!==undefined)?dataType:DataTypes.getByValue(value); requireNonNull(value, "value"); JSONPointer.parse(requireNonNull(pointer, "pointer")); var withProperty = {}; withProperty[pointer] = valueType.toBytes(value); var newWithValues = Object.assign({}, withValues, withProperty); return new PartialJSONImpl(newWithValues, withoutValues); }; this.without = function(pointer) { JSONPointer.parse(requireNonNull(pointer, "pointer")); var newWithoutValues = new Set(withoutValues); newWithoutValues.add(pointer); return new PartialJSONImpl(withValues, newWithoutValues); }; this.getConstraintType = function() { return 'PARTIAL_JSON'; }; } ); function andConstraint(other) { if (!Array.isArray(other) && other.getConstraintType() === 'CONJUNCTION') { return other.and(this); } var constraints = Array.isArray(other)?other.concat(this):[this, other]; checkSatisfiability(constraints); return new Conjunction(constraints); } function checkSatisfiability(constraints) { var previousConstraintOnValue; constraints.forEach(function(constraint) { if (isConstraintOnTopic(constraint)) { if (previousConstraintOnValue !== undefined) { throw new Error( "Multiple value constraints found: " + previousConstraintOnValue + " and " + constraint); } else { previousConstraintOnValue = constraint; } } }); } function isConstraintOnTopic(constraint) { return ['BINARY_VALUE', 'NO_VALUE', 'NO_TOPIC', 'PARTIAL_JSON'].includes(constraint.getConstraintType()); } module.exports.ConstraintType = ConstraintType; module.exports.Unconstrained = Unconstrained; module.exports.Conjunction = Conjunction; module.exports.BinaryValue = BinaryValue; module.exports.Locked = Locked; module.exports.NoTopic = NoTopic; module.exports.NoValue = NoValue; module.exports.PartialJSONImpl = PartialJSONImpl;