nope/lib/observables/nopeRemoteObservable.ts
Martin Karkowski 27e5f5569b Adding Comments
Adding Possiblity to generate a Documentation

Signed-off-by: Martin Karkowski <m.karkowski@dozema.local>
2020-09-11 09:36:14 +02:00

93 lines
2.4 KiB
TypeScript

import { Observable } from 'rxjs';
import { nopeDispatcher } from "../dispatcher/nopeDispatcher";
export type pipe<T, K> = (scope: { [index: string]: any }, observable: Observable<T>) => Observable<K>
/**
* A Remote Observable.
* This function acts a wrapper to access a Remote-Observable.
* The wrapper can be used to manipulate the item.
*
* @export
* @class nopeRemoteObservable
* @template T
* @template S
* @template G
*/
export class nopeRemoteObservable<T, S = T, G = T> {
protected _pathes: {
get: string,
set: string,
subscribe: string,
}
/**
* Creates a Remote Dispatcher.
* @param _dispatcher
* @param options
*/
constructor(
protected _dispatcher: nopeDispatcher,
public options: {
path: string,
}
) {
// Assing the Pathes
this._pathes = {
get: this.options.path + '.get',
set: this.options.path + '.set',
subscribe: this.options.path + '.subscribe'
}
}
/**
* Currently used remote Value.
*
* @readonly
* @memberof nopeRemoteObservable
*/
public get currentRemoteValue() {
return this.getRemoteValue()
}
/**
* Manually Funciton for getting the Remote Value.
*
* @return {*}
* @memberof nopeRemoteObservable
*/
public async getRemoteValue() {
return await this._dispatcher.performCall<G>(this._pathes.get, [])
}
/**
* Updates the Remote Value. (This will although trigger a Subscription update)
*
* @param {S} value The Value to use
* @return {*} Function doesnt return something
* @memberof nopeRemoteObservable
*/
public async setRemoteValue(value: S) {
// Perform an Update.
return await this._dispatcher.performCall<void>(this._pathes.set, [value]);
}
/**
* Function to Subscribe a NopeObservable on a Remote.
*
* @template K
* @param {(data: K) => void} next the callback, which will be called on new Data.
* @param {{
* scope?: { [index: string]: any },
* pipe?: pipe<G, K>
* }} [options={}] Options to enhance the Subscription. You can provide a Pipe. This pipe will be executed on the remote to reduce networktraffic.
* @return {*}
* @memberof nopeRemoteObservable
*/
public async subscribe<K>(next: (data: K) => void, options: {
scope?: { [index: string]: any },
pipe?: pipe<G, K>
} = {}) {
return await this._dispatcher.performCall<() => void>(this._pathes.subscribe, [next, options]);
}
}