import { nopeDispatcher } from "../dispatcher/nopeDispatcher"; import { IPipe } from "../types/nopeObservable.interface"; /** * A Remote Observable. (Should be used inside of the GUI) * This function acts a wrapper to access a Remote-Observable. * The wrapper can be used to manipulate the item. * * It provides a full async API. It should ideally used to create * interact with the UI. * * @export * @class nopeRemoteObservable * @template T * @template S * @template G */ export class nopeRemoteObservable { 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(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(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?: IPipe * }} [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(next: (data: K) => void, options: { scope?: { [index: string]: any }, pipe?: IPipe } = {}) { return await this._dispatcher.performCall<() => void>(this._pathes.subscribe, [next, options]); } }