make ajax configurable

This commit is contained in:
Géry Debongnie
2019-02-06 16:17:32 +01:00
parent e2fad78eeb
commit 82d8ca9da8
3 changed files with 21 additions and 10 deletions
+7 -1
View File
@@ -31,7 +31,7 @@ export const init = memoize(async function(): Promise<InitializedData> {
// services // services
const qweb = new QWeb(); const qweb = new QWeb();
const router = new Router(); const router = new Router();
const ajax = new Ajax(); const ajax = new Ajax(ajaxFetch);
const actionManager = new ActionManager(actionRegistry, ajax); const actionManager = new ActionManager(actionRegistry, ajax);
const notifications = new NotificationManager(); const notifications = new NotificationManager();
@@ -65,6 +65,12 @@ export const init = memoize(async function(): Promise<InitializedData> {
// Adapters // Adapters
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
function ajaxFetch(route: string, params: any): Promise<any> {
console.log("RPC", route, params);
const delay = Math.random() * 150;
return new Promise(resolve => setTimeout(resolve, delay));
}
/** /**
* Load xml templates as a string. * Load xml templates as a string.
*/ */
+9 -3
View File
@@ -26,16 +26,22 @@ interface RequestParameters {
params: { [key: string]: any }; params: { [key: string]: any };
} }
export type FetchMethod = (route: string, params: any) => Promise<any>;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Ajax // Ajax
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
export class Ajax implements IAjax { export class Ajax implements IAjax {
fetch: FetchMethod;
constructor(fetch: FetchMethod) {
this.fetch = fetch;
}
rpc(rpc: RPCQuery): Promise<any> { rpc(rpc: RPCQuery): Promise<any> {
const request = this.prepareRequest(rpc); const request = this.prepareRequest(rpc);
console.log("RPC", request.route, request.params); return this.fetch(request.route, request.params);
const delay = Math.random() * 150;
return new Promise(resolve => setTimeout(resolve, delay));
} }
private prepareRequest(query: RPCQuery): RequestParameters { private prepareRequest(query: RPCQuery): RequestParameters {
+5 -6
View File
@@ -1,5 +1,5 @@
import { readFile } from "fs"; import { readFile } from "fs";
import { IAjax, RPCQuery } from "../src/ts/services/ajax"; import { Ajax, RPCQuery } from "../src/ts/services/ajax";
import { WEnv } from "../src/ts/core/component"; import { WEnv } from "../src/ts/core/component";
import { Callback } from "../src/ts/core/event_bus"; import { Callback } from "../src/ts/core/event_bus";
import { NotificationManager } from "../src/ts/core/notifications"; import { NotificationManager } from "../src/ts/core/notifications";
@@ -30,7 +30,7 @@ export interface MockEnv extends Env {
} }
export function makeTestEnv(): MockEnv { export function makeTestEnv(): MockEnv {
const ajax = new MockAjax(); const ajax = new MockAjax(mockFetch);
const actionManager = new ActionManager(actionRegistry, ajax); const actionManager = new ActionManager(actionRegistry, ajax);
const router = new MockRouter(); const router = new MockRouter();
const notifications = new NotificationManager(); const notifications = new NotificationManager();
@@ -49,11 +49,10 @@ export function makeTestEnv(): MockEnv {
}; };
} }
class MockAjax implements IAjax { function mockFetch(route: string, params: any): Promise<any> {
async rpc(rpc: RPCQuery) { return Promise.resolve(true);
return true;
}
} }
class MockAjax extends Ajax {}
class MockRouter implements IRouter { class MockRouter implements IRouter {
currentQuery: Query = {}; currentQuery: Query = {};