node-jet
Version:
Jet Realtime Message Bus for the Web. Daemon and Peer implementation.
47 lines (46 loc) • 1.94 kB
TypeScript
import type { ValueType } from '../types.js';
import type { JsonParams } from './index.js';
import { EventEmitter } from '../../1_socket/index.js';
/**
* Create a Jet State instance
* @class
* @classdesc A Jet State associates a unique path with any piece of
* cohesive data. The data may represent a Database entry, the configuration
* of a certain part of software or observations of "real" things (through sensors).
* A State can change its value at any time. It also can provide "behaviour" by providing
* a "set" event handler. This event handler can do whatever seems appropriate (validation etc) and may
* result in an auto-posted state change.
*
* @param {string} path A unique name, which identifies this State, e.g. 'persons/#23A51'
* @param {*} initialValue The initial value of the state.
* @param {object} [access] Access rights for this state. Per default unrestricted access to all Peers.
*
*/
export declare class State<T extends ValueType> extends EventEmitter {
_path: string;
_value: T;
_readGroup: string;
_writeGroup: string;
constructor(path: string, initialValue: T, readgroup?: string, writeGroup?: string);
/**
* Get the state's unchangable path.
*
* @returns {string} The state's path.
*
*/
path: () => string;
/**
* Replies to a 'set' request. Either set `response.value` or `response.error`.
* Only required for asynchronous working {State~setCallback}.
*
* @function State~reply
* @param {object} response The response to send to the invoker of the 'set' callback.
* @param {*} [response.value] The new State's value.
* @param {Boolean} [response.dontNotify=false] If set to true, no state change is posted.
* @param {string|object} [response.error] A error message or error object.
*
*/
value: (newValue?: T | undefined) => T;
toJson: () => JsonParams<T>;
}
export default State;