UNPKG

@intuitionrobotics/db-api-generator

Version:
187 lines 6.96 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseDB_ApiGeneratorCaller = void 0; const index_1 = require("../index"); const frontend_1 = require("@intuitionrobotics/thunderstorm/frontend"); const ts_common_1 = require("@intuitionrobotics/ts-common"); class BaseDB_ApiGeneratorCaller extends ts_common_1.Module { // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected constructor(config, moduleName) { super(moduleName); this.errorHandler = (request, resError) => { if (this.onError(request, resError)) return; return frontend_1.XhrHttpModule.handleRequestFailure(request, resError); }; this.update = (toUpdate) => { return this.getUpdateRequest(toUpdate) .execute((response) => __awaiter(this, void 0, void 0, function* () { return this.onEntryUpdated(response); })); }; this.updateSync = (toUpdate) => { return this.getUpdateRequest(toUpdate) .executeSync(); }; this.query = (query) => { let _query = query; if (!_query) _query = {}; return this.getQueryBody(_query) .execute((response) => __awaiter(this, void 0, void 0, function* () { return this.onQueryReturned(response); })); }; this.querySync = (query) => { return this.getQueryBody(query) .executeSync(); }; this.unique = (_id) => { return this.getUniqueRequest(_id) .execute((response) => __awaiter(this, void 0, void 0, function* () { return this.onGotUnique(response); })); }; this.uniqueSync = (_id) => { return this.getUniqueRequest(_id).executeSync(); }; this.delete = (_id) => { return this.getDeleteRequest(_id) .execute((response) => __awaiter(this, void 0, void 0, function* () { return this.onEntryDeleted(response); })); }; this.deleteSync = (_id) => { return this.getDeleteRequest(_id) .executeSync(); }; this.ids = []; this.items = {}; this.dispatch = () => { var _a, _b; (_a = this.defaultDispatcher) === null || _a === void 0 ? void 0 : _a.dispatchUI(); (_b = this.defaultDispatcher) === null || _b === void 0 ? void 0 : _b.dispatchModule(); }; this.upsertId = (id) => { if (!this.ids.includes(id)) (0, ts_common_1.addItemToArray)(this.ids, id); }; this.setDefaultConfig(config); } setDefaultDispatcher(defaultDispatcher) { this.defaultDispatcher = defaultDispatcher; } createRequest(apiDef) { const request = frontend_1.XhrHttpModule .createRequest(apiDef.method, this.getRequestKey(apiDef)) .setRelativeUrl(`${this.config.relativeUrl}${apiDef.suffix ? "/" + apiDef.suffix : ""}`) .setOnError(this.errorHandler); const timeout = this.timeoutHandler(apiDef); if (timeout) request.setTimeout(timeout); return request; } getRequestKey(apiDef) { return `request-api--${this.config.key}-${apiDef.key}`; } timeoutHandler(apiDef) { } onError(request, resError) { return false; } getCreateRequest(toCreate) { return this .createRequest(index_1.DefaultApiDefs.Create) .setJsonBody(toCreate); } create(toCreate) { return this.getCreateRequest(toCreate) .execute((response) => __awaiter(this, void 0, void 0, function* () { return this.onEntryCreated(response); })); } createSync(toCreate) { return __awaiter(this, void 0, void 0, function* () { return this.getCreateRequest(toCreate) .executeSync(); }); } getUpdateRequest(toUpdate) { return this .createRequest(index_1.DefaultApiDefs.Update) .setJsonBody(toUpdate); } getQueryBody(query) { let _query = query; if (!_query) _query = {}; return this .createRequest(index_1.DefaultApiDefs.Query) .setJsonBody(_query); } getUniqueRequest(_id) { return this .createRequest(index_1.DefaultApiDefs.Unique) .setUrlParams({ _id }); } getDeleteRequest(_id) { return this .createRequest(index_1.DefaultApiDefs.Delete) .setUrlParams({ _id }); } getItems() { return this.ids.map(id => this.items[id]); } get(id) { return this.items[id]; } onEntryCreated(item) { return __awaiter(this, void 0, void 0, function* () { this.upsertId(item._id); this.items[item._id] = item; this.dispatch(); }); } onEntryDeleted(item) { return __awaiter(this, void 0, void 0, function* () { (0, ts_common_1.removeItemFromArray)(this.ids, item._id); delete this.items[item._id]; this.dispatch(); }); } onEntryUpdated(item) { return __awaiter(this, void 0, void 0, function* () { this.items[item._id] = item; this.dispatch(); }); } onGotUnique(item) { return __awaiter(this, void 0, void 0, function* () { if (!this.ids.includes(item._id)) (0, ts_common_1.addItemToArray)(this.ids, item._id); this.items[item._id] = item; this.dispatch(); }); } onQueryReturned(items) { return __awaiter(this, void 0, void 0, function* () { items.forEach(item => this.upsertId(item._id)); this.items = items.reduce((toRet, item) => { toRet[item._id] = item; return toRet; }, this.items); this.dispatch(); }); } } exports.BaseDB_ApiGeneratorCaller = BaseDB_ApiGeneratorCaller; //# sourceMappingURL=BaseDB_ApiGeneratorCaller.js.map