2020-09-01 15:48:07 +00:00
|
|
|
import { Observable } from 'rxjs';
|
2020-08-25 10:33:33 +00:00
|
|
|
import { nopeDispatcher } from "../dispatcher/nopeDispatcher";
|
|
|
|
|
2020-09-01 15:48:07 +00:00
|
|
|
export type pipe<T, K> = (scope: { [index: string]: any }, observable: Observable<T>) => Observable<K>
|
2020-08-25 10:33:33 +00:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public get currentRemoteValue() {
|
|
|
|
return this.getRemoteValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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.
|
|
|
|
* @param next the callback, which will be called on new Data.
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
public async subscribe<K>(next: (data: K) => void, options: {
|
|
|
|
scope?: { [index: string]: any },
|
2020-09-01 15:48:07 +00:00
|
|
|
pipe?: pipe<G, K>
|
2020-08-25 10:33:33 +00:00
|
|
|
} = {}) {
|
|
|
|
return await this._dispatcher.performCall<() => void>(this._pathes.subscribe, [next, options]);
|
|
|
|
}
|
|
|
|
}
|