nope/lib/RpcManager/defaultSelectors.ts

148 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-12-23 10:48:06 +00:00
import { ValidSelectorFunction } from "../types/nope";
/**
* Helper, which will allways select the first item.
* @param options must contain the serviceName and rpcManager.
2021-12-23 10:49:15 +00:00
* @returns
2021-12-23 10:48:06 +00:00
*/
2021-12-23 10:49:15 +00:00
export const firstItemSelector: ValidSelectorFunction = ({
serviceName,
rpcManager,
}) => {
return rpcManager.services.originalData.get(serviceName)[0];
};
2021-12-23 10:48:06 +00:00
// export function _generateSelectorFunction(selector: ValidDefaultSelectors) {
// const _this = this;
// let helperData: any;
// switch (selector) {
// default:
// case "first":
// return async (dispatcher: string) => {
// return true;
// };
// case "dispatcher":
// // Our selector compares the dispatcher - id
// return async (externalDispatcher, options) => {
// return externalDispatcher == _this.id;
// };
// case "host":
// // Our selector compares the host-name:
// // 1. Get the current Host name of our dispatcher
// helperData = this._genAliveMessage().host.name;
// return async (externalDispatcher) => {
// const host = _this._externalDispatchers.get(externalDispatcher);
// return host?.host?.name == helperData;
// };
// case "cpu-usage":
// helperData = {
// arr: [],
// timeout: null,
// done: false,
// finish: () => {
// if (!helperData.done) {
// // Now we find the Min CPU usage:
// const bestOption = minOfArray(helperData.arr, "cpuUsage").index;
// for (const [index, { resolve }] of (
// helperData.arr as any[]
// ).entries()) {
// resolve(index === bestOption);
// }
// helperData.done = true;
// }
// if (helperData.timeout !== null) {
// clearTimeout(helperData.timeout);
// helperData.timeout = null;
// }
// },
// };
// return async (externalDispatcherId, options) => {
// if (options.initalTest) {
// return true;
// }
// let cpuUsage =
// _this._externalDispatchers.get(externalDispatcherId).host.cpu.usage;
// cpuUsage = cpuUsage === -1 ? Infinity : cpuUsage;
// // For every Call, we
// const promise = new Promise<boolean>((resolve) =>
// helperData.arr.push({ resolve, cpuUsage })
// );
// // If we are calling the Element at the firsttime,
// // we ensure, that we have callback, which will
// // be called after the specified amount of time to
// // assing the selection.
// if (options.first) {
// helperData.timeout = setTimeout(helperData.finish, options.timeout);
// }
// if (options.last) {
// helperData.finish();
// }
// return await promise;
// };
// case "free-ram":
// helperData = {
// arr: [],
// timeout: null,
// done: false,
// finish: () => {
// if (!helperData.done) {
// // Now we find the Min CPU usage:
// const bestOption = minOfArray(helperData.arr, "freeRam").index;
// for (const [index, { resolve }] of (
// helperData.arr as any[]
// ).entries()) {
// resolve(index === bestOption);
// }
// helperData.done = true;
// }
// if (helperData.timeout !== null) {
// clearTimeout(helperData.timeout);
// helperData.timeout = null;
// }
// },
// };
// return async (externalDispatcherId, options) => {
// if (options.initalTest) {
// return true;
// }
// let freeRam =
// _this._externalDispatchers.get(externalDispatcherId).host.ram.free;
// freeRam = freeRam === -1 ? Infinity : freeRam;
// // For every Call, we
// const promise = new Promise<boolean>((resolve) =>
// helperData.arr.push({ resolve, freeRam })
// );
// // If we are calling the Element at the firsttime,
// // we ensure, that we have callback, which will
// // be called after the specified amount of time to
// // assing the selection.
// if (options.first) {
// helperData.timeout = setTimeout(helperData.finish, options.timeout);
// }
// if (options.last) {
// helperData.finish();
// }
// return await promise;
// };
// }
2021-12-23 10:49:15 +00:00
// }