refactor: 💡 Use a native class for the client daemon handler (#2026)

This commit is contained in:
Zhihe Li committed 2024-01-16 10:17:45 -05:00
1 parent 3f11019ef0
commit 7bc2ccb5ae
9 files changed
+40 -65

No files matched your search

@@ -1,6 +1,8 @@
import { inject as service } from '@ember/service';
import { getOwner, setOwner } from '@ember/application';
import ArrayProxy from '@ember/array/proxy';
import { pluralize } from 'ember-inflector';
import { generateMQLExpression } from '../utils/mqlQuery';
import ArrayProxy from '@ember/array/proxy';
/**
* Not all types are yet supported by the client daemon so we'll
@@ -13,11 +15,43 @@ const supportedTypes = {
session: ['id', 'type', 'status', 'endpoint', 'scope_id', 'target_id'],
};
/**
* Takes an array and the current page and pagesize to calculate the correct
* number of results to return to the caller.
*
* @param array
* @param page
* @param pageSize
* @returns {[*]}
*/
const paginateResults = (array, page, pageSize) => {
const length = array?.length;
if (!array || length === 0) {
return [];
}
if (!page || !pageSize) {
return array;
}
const offset = (page - 1) * pageSize;
const start = Math.min(length - 1, offset);
const end = Math.min(length, offset + pageSize);
return array.slice(start, end);
};
/**
* Handler to sit in front of the API request layer
* so we can request from the daemon first
*/
const ClientDaemonHandler = {
export default class ClientDaemonHandler {
@service session;
@service ipc;
constructor(context) {
setOwner(this, getOwner(context));
}
async request(context, next) {
switch (context.request.op) {
case 'query': {
@@ -92,32 +126,5 @@ const ClientDaemonHandler = {
default:
return next(context.request);
}
},
};
/**
* Takes an array and the current page and pagesize to calculate the correct
* number of results to return to the caller.
*
* @param array
* @param page
* @param pageSize
* @returns {[*]}
*/
const paginateResults = (array, page, pageSize) => {
const length = array?.length;
if (!array || length === 0) {
return [];
}
if (!page || !pageSize) {
return array;
}
const offset = (page - 1) * pageSize;
const start = Math.min(length - 1, offset);
const end = Math.min(length, offset + pageSize);
return array.slice(start, end);
};
export default ClientDaemonHandler;
}
+2 -6
View File
@@ -1,21 +1,17 @@
import Store, { CacheHandler } from '@ember-data/store';
import RequestManager from '@ember-data/request';
import { LegacyNetworkHandler } from '@ember-data/legacy-compat';
import { inject as service } from '@ember/service';
import ClientDaemonHandler from 'api/handlers/client-daemon-handler';
export default class extends Store {
@service ipc;
@service session;
requestManager = new RequestManager();
constructor(args) {
super(args);
ClientDaemonHandler.request = ClientDaemonHandler.request.bind(this);
const clientDaemonHandler = new ClientDaemonHandler(this);
this.requestManager.use([ClientDaemonHandler, LegacyNetworkHandler]);
this.requestManager.use([clientDaemonHandler, LegacyNetworkHandler]);
this.requestManager.useCache(CacheHandler);
}
}
@@ -22,7 +22,6 @@ import {
invalidateSession,
} from 'ember-simple-auth/test-support';
import WindowMockIPC from '../helpers/window-mock-ipc';
import Store from 'api/services/store';
module('Acceptance | authentication', function (hooks) {
setupApplicationTest(hooks);
@@ -129,9 +128,6 @@ module('Acceptance | authentication', function (hooks) {
// Mock the postMessage interface used by IPC.
this.owner.register('service:browser/window', WindowMockIPC);
setDefaultClusterUrl(this);
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
test('visiting index while unauthenticated redirects to global authenticate method', async function (assert) {
@@ -13,7 +13,6 @@ import { invalidateSession } from 'ember-simple-auth/test-support';
import { setupBrowserFakes } from 'ember-browser-services/test-support';
import WindowMockIPC from '../helpers/window-mock-ipc';
import config from '../../config/environment';
import Store from 'api/services/store';
module('Acceptance | clusterUrl', function (hooks) {
setupApplicationTest(hooks);
@@ -101,9 +100,6 @@ module('Acceptance | clusterUrl', function (hooks) {
urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`;
urls.projects = `${urls.scopes.global}/projects`;
urls.targets = `${urls.projects}/targets`;
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
hooks.afterEach(function () {
@@ -22,7 +22,6 @@ import {
STATUS_SESSION_CANCELING,
STATUS_SESSION_TERMINATED,
} from 'api/models/session';
import Store from 'api/services/store';
module('Acceptance | projects | sessions', function (hooks) {
setupApplicationTest(hooks);
@@ -123,9 +122,6 @@ module('Acceptance | projects | sessions', function (hooks) {
const ipcService = this.owner.lookup('service:ipc');
stubs.ipcService = sinon.stub(ipcService, 'invoke');
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
hooks.afterEach(function () {
@@ -15,7 +15,6 @@ import { authenticateSession } from 'ember-simple-auth/test-support';
import sinon from 'sinon';
import WindowMockIPC from '../../../helpers/window-mock-ipc';
import { STATUS_SESSION_ACTIVE } from 'api/models/session';
import Store from 'api/services/store';
module('Acceptance | projects | sessions | session', function (hooks) {
setupApplicationTest(hooks);
@@ -122,9 +121,6 @@ module('Acceptance | projects | sessions | session', function (hooks) {
const ipcService = this.owner.lookup('service:ipc');
stubs.ipcService = sinon.stub(ipcService, 'invoke');
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
hooks.afterEach(function () {
@@ -21,9 +21,8 @@ import {
invalidateSession,
currentSession,
} from 'ember-simple-auth/test-support';
import Store from 'api/services/store';
module('Acceptance | projects | targets', function (hooks) {
module('Acceptance | projects | targets | index', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
@@ -140,9 +139,6 @@ module('Acceptance | projects | targets', function (hooks) {
const ipcService = this.owner.lookup('service:ipc');
stubs.ipcService = sinon.stub(ipcService, 'invoke');
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
hooks.afterEach(function () {
@@ -10,7 +10,6 @@ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import sinon from 'sinon';
import { authenticateSession } from 'ember-simple-auth/test-support';
import WindowMockIPC from '../../../helpers/window-mock-ipc';
import Store from 'api/services/store';
module('Acceptance | projects | targets | target', function (hooks) {
setupApplicationTest(hooks);
@@ -132,9 +131,6 @@ module('Acceptance | projects | targets | target', function (hooks) {
const ipcService = this.owner.lookup('service:ipc');
stubs.ipcService = sinon.stub(ipcService, 'invoke');
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
hooks.afterEach(function () {
@@ -16,7 +16,6 @@ import {
invalidateSession,
} from 'ember-simple-auth/test-support';
import WindowMockIPC from '../helpers/window-mock-ipc';
import Store from 'api/services/store';
module('Acceptance | scopes', function (hooks) {
setupApplicationTest(hooks);
@@ -137,9 +136,6 @@ module('Acceptance | scopes', function (hooks) {
const ipcService = this.owner.lookup('service:ipc');
stubs.ipcService = sinon.stub(ipcService, 'invoke');
// Use the original store so we don't try and hit the client daemon
this.owner.register('service:store', Store);
});
hooks.afterEach(function () {