make ajax emit rpc events

This commit is contained in:
Géry Debongnie
2019-02-06 16:38:52 +01:00
parent 82d8ca9da8
commit 87facd993b
3 changed files with 58 additions and 5 deletions
+19 -3
View File
@@ -1,3 +1,5 @@
import { EventBus } from "../core/event_bus";
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
@@ -17,8 +19,11 @@ export interface RPCControllerQuery {
export type RPCQuery = RPCModelQuery | RPCControllerQuery;
type AjaxStatus = "loading" | "notloading";
export interface IAjax {
rpc(rpc: RPCQuery): Promise<any>;
on(event: "rpc_status", owner: any, callback: (status: AjaxStatus) => void);
}
interface RequestParameters {
@@ -32,16 +37,27 @@ export type FetchMethod = (route: string, params: any) => Promise<any>;
// Ajax
//------------------------------------------------------------------------------
export class Ajax implements IAjax {
export class Ajax extends EventBus implements IAjax {
fetch: FetchMethod;
counter: number = 0;
constructor(fetch: FetchMethod) {
super();
this.fetch = fetch;
}
rpc(rpc: RPCQuery): Promise<any> {
async rpc(rpc: RPCQuery): Promise<any> {
const request = this.prepareRequest(rpc);
return this.fetch(request.route, request.params);
if (this.counter === 0) {
this.trigger("rpc_status", "loading");
}
this.counter++;
const result = await this.fetch(request.route, request.params);
this.counter--;
if (this.counter === 0) {
this.trigger("rpc_status", "notloading");
}
return result;
}
private prepareRequest(query: RPCQuery): RequestParameters {