UNPKG

diffusion

Version:

Diffusion JavaScript client

124 lines (102 loc) 3.47 kB
var Range = require('features/time-series/range'); function StreamStructure(id, name) { this.id = id; this.name = name; this.toString = function() { return name; }; } var StreamStructures = { VALUE_EVENT_STREAM : new StreamStructure(1, "VALUE_EVENT_STREAM"), EDIT_EVENT_STREAM : new StreamStructure(2, "EDIT_EVENT_STREAM") }; function QueryType(id, primaryOperator, editRangeOperator, streamStructure) { this.id = id; this.primaryOperator = primaryOperator; this.editRangeOperator = editRangeOperator; this.streamStructure = streamStructure; } var QueryTypes = { VALUES : new QueryType(0, ".forValues()", ".editRange()", StreamStructures.VALUE_EVENT_STREAM), ALL_EDITS : new QueryType(1, ".forEdits()", ".allEdits()", StreamStructures.EDIT_EVENT_STREAM), LATEST_EDITS : new QueryType(2, ".forEdits()", ".latestEdits()", StreamStructures.EDIT_EVENT_STREAM) }; function requireNonNegative(i, what) { if (i < 0) { throw new Error(what + " is negative: " + i); } return i; } function RangeQueryParameters(queryType, viewRange, editRange, limit) { this.queryType = queryType; this.viewRange = viewRange; this.editRange = editRange; this.limit = limit; this.withViewRange = function(range) { return new RangeQueryParameters( this.queryType, range, this.editRange, this.limit ); }; this.withEditRange = function(range) { return new RangeQueryParameters( this.queryType, this.viewRange, range, this.limit ); }; this.withLimit = function(limit) { return new RangeQueryParameters( this.queryType, this.viewRange, this.editRange, requireNonNegative(limit, "limit") ); }; this.withQueryType = function(type) { return new RangeQueryParameters( type, this.viewRange, this.editRange, this.limit ); }; } RangeQueryParameters.prototype.equals = function(other) { if (other && other instanceof RangeQueryParameters) { return this.queryType === other.queryType && this.viewRange.equals(other.viewRange) && this.editRange.equals(other.editRange) && this.limit === other.limit; } return false; }; RangeQueryParameters.QueryType = QueryTypes; RangeQueryParameters.StreamStructure = StreamStructures; RangeQueryParameters.DEFAULT_RANGE_QUERY = new RangeQueryParameters( QueryTypes.VALUES, Range.DEFAULT_RANGE, Range.DEFAULT_RANGE, 9007199254740991); RangeQueryParameters.prototype.toString = function() { var s = "rangeQuery()"; if (this.queryType !== QueryTypes.VALUES) { s += this.queryType.primaryOperator; } if (!this.viewRange.equals(RangeQueryParameters.DEFAULT_RANGE_QUERY.viewRange)) { s += this.viewRange; } if (!this.editRange.equals(RangeQueryParameters.DEFAULT_RANGE_QUERY.editRange)) { s += this.queryType.editRangeOperator + this.editRange; } else if (this.queryType === QueryTypes.LATEST_EDITS) { s += QueryTypes.LATEST_EDITS.editRangeOperator; } if (this.limit !== RangeQueryParameters.DEFAULT_RANGE_QUERY.limit) { s += ".limit(" + this.limit + ")"; } return s; }; module.exports = RangeQueryParameters;