2020-08-25 10:33:33 +00:00
|
|
|
import { nopeDispatcher } from "../dispatcher/nopeDispatcher";
|
2020-09-28 16:38:35 +00:00
|
|
|
import { IPipe } from "../types/nopeObservable.interface";
|
2020-08-25 10:33:33 +00:00
|
|
|
|
2020-09-11 07:36:14 +00:00
|
|
|
/**
|
2020-09-28 16:38:35 +00:00
|
|
|
* A Remote Observable. (Should be used inside of the GUI)
|
2020-09-11 07:36:14 +00:00
|
|
|
* This function acts a wrapper to access a Remote-Observable.
|
|
|
|
* The wrapper can be used to manipulate the item.
|
2020-09-28 16:38:35 +00:00
|
|
|
*
|
|
|
|
* It provides a full async API. It should ideally used to create
|
|
|
|
* interact with the UI.
|
2020-09-11 07:36:14 +00:00
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class nopeRemoteObservable
|
|
|
|
* @template T
|
|
|
|
* @template S
|
|
|
|
* @template G
|
|
|
|
*/
|
2020-09-08 14:58:50 +00:00
|
|
|
export class nopeRemoteObservable<T, S = T, G = T> {
|
2020-08-25 10:33:33 +00:00
|
|
|
protected _pathes: {
|
|
|
|
get: string,
|
|
|
|
set: string,
|
|
|
|
subscribe: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Remote Dispatcher.
|
|
|
|
* @param _dispatcher
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
protected _dispatcher: nopeDispatcher,
|
|
|
|
public options: {
|
|
|
|
path: string,
|
|
|
|
}
|
|
|
|
) {
|
2020-09-11 07:06:49 +00:00
|
|
|
// Assing the Pathes
|
|
|
|
this._pathes = {
|
|
|
|
get: this.options.path + '.get',
|
|
|
|
set: this.options.path + '.set',
|
|
|
|
subscribe: this.options.path + '.subscribe'
|
|
|
|
}
|
2020-08-25 10:33:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 07:36:14 +00:00
|
|
|
/**
|
|
|
|
* Currently used remote Value.
|
|
|
|
*
|
|
|
|
* @readonly
|
|
|
|
* @memberof nopeRemoteObservable
|
|
|
|
*/
|
2020-08-25 10:33:33 +00:00
|
|
|
public get currentRemoteValue() {
|
|
|
|
return this.getRemoteValue()
|
|
|
|
}
|
|
|
|
|
2020-09-11 07:36:14 +00:00
|
|
|
/**
|
|
|
|
* Manually Funciton for getting the Remote Value.
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
* @memberof nopeRemoteObservable
|
|
|
|
*/
|
2020-08-25 10:33:33 +00:00
|
|
|
public async getRemoteValue() {
|
2020-09-01 15:48:07 +00:00
|
|
|
return await this._dispatcher.performCall<G>(this._pathes.get, [])
|
2020-08-25 10:33:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 07:36:14 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-09-01 15:48:07 +00:00
|
|
|
public async setRemoteValue(value: S) {
|
2020-08-25 10:33:33 +00:00
|
|
|
// Perform an Update.
|
2020-09-01 15:48:07 +00:00
|
|
|
return await this._dispatcher.performCall<void>(this._pathes.set, [value]);
|
2020-08-25 10:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to Subscribe a NopeObservable on a Remote.
|
2020-09-11 07:36:14 +00:00
|
|
|
*
|
|
|
|
* @template K
|
|
|
|
* @param {(data: K) => void} next the callback, which will be called on new Data.
|
|
|
|
* @param {{
|
|
|
|
* scope?: { [index: string]: any },
|
2020-09-28 16:38:35 +00:00
|
|
|
* pipe?: IPipe<G, K>
|
2020-09-11 07:36:14 +00:00
|
|
|
* }} [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
|
2020-08-25 10:33:33 +00:00
|
|
|
*/
|
|
|
|
public async subscribe<K>(next: (data: K) => void, options: {
|
|
|
|
scope?: { [index: string]: any },
|
2020-09-28 16:38:35 +00:00
|
|
|
pipe?: IPipe<G, K>
|
2020-08-25 10:33:33 +00:00
|
|
|
} = {}) {
|
|
|
|
return await this._dispatcher.performCall<() => void>(this._pathes.subscribe, [next, options]);
|
|
|
|
}
|
|
|
|
}
|