node-jet
Version:
Jet Realtime Message Bus for the Web. Daemon and Peer implementation.
80 lines (79 loc) • 2.13 kB
JavaScript
import { EventEmitter } from '../../1_socket/index.js';
import { Subscription } from '../daemon/subscription.js';
export class Fetcher extends EventEmitter {
message = { id: '' };
valueRules = {};
constructor() {
super();
this.setMaxListeners(0);
}
addListener = (eventName, listener) => {
super.addListener(eventName, listener);
return this;
};
path = (key, value) => {
if (!this.message.path) {
this.message.path = {};
}
this.message.path[key] = value;
return this;
};
value = (operator, value, field = '') => {
if (!this.message.value) {
this.message.value = {};
}
this.message.value[field] = {
operator,
value
};
return this;
};
matches = (path, value) => {
const sub = new Subscription(this.message);
return sub.matchesPath(path) && sub.matchesValue(value);
};
differential = () => {
if (!this.message.sort) {
this.message.sort = {};
}
this.message.sort.asArray = false;
return this;
};
ascending = () => {
if (!this.message.sort) {
this.message.sort = {};
}
this.message.sort.descending = false;
return this;
};
descending = () => {
if (!this.message.sort) {
this.message.sort = {};
}
this.message.sort.descending = true;
return this;
};
sortByValue = (key = '') => {
if (!this.message.sort) {
this.message.sort = {};
}
this.message.sort.by = key ? `value.${key}` : 'value';
return this;
};
sortByPath = () => {
if (!this.message.sort) {
this.message.sort = {};
}
this.message.sort.by = 'path';
return this;
};
range = (_from, _to) => {
if (!this.message.sort) {
this.message.sort = {};
}
this.message.sort.from = _from;
this.message.sort.to = _to;
return this;
};
}
export default Fetcher;