@intuitionrobotics/permissions
Version:
92 lines • 4.46 kB
JavaScript
;
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.PermissionsFE = exports.PermissionsModuleFE_Class = void 0;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const thunder_dispatcher_1 = require("@intuitionrobotics/thunderstorm/app-frontend/core/thunder-dispatcher");
const frontend_1 = require("@intuitionrobotics/thunderstorm/frontend");
const thunderstorm_1 = require("@intuitionrobotics/thunderstorm");
const dispatch_onPermissionsChanged = new thunder_dispatcher_1.ThunderDispatcher("__onPermissionsChanged");
const dispatch_onPermissionsFailed = new thunder_dispatcher_1.ThunderDispatcher("__onPermissionsFailed");
class PermissionsModuleFE_Class extends ts_common_1.Module {
constructor() {
super("PermissionsModuleFE");
this.loadingUrls = new Set();
this.userUrlsPermissions = {};
this.requestCustomField = {};
this.debounceTime = 100;
this.retryCounter = 0;
this.setPermissions = () => {
if (!this.config || !this.config.projectId)
throw new ts_common_1.ImplementationMissingException("need to set up a project id config");
this.debounce(() => {
const urls = {};
this.loadingUrls.forEach(url => {
urls[url] = false;
});
frontend_1.XhrHttpModule
.createRequest(thunderstorm_1.HttpMethod.POST, "user-urls-permissions")
.setRelativeUrl(`/v1/permissions/user-urls-permissions`)
// .setOnError(`Failed to get user urls permissions`)
.setLabel(`Getting user urls permissions`)
.setJsonBody({
projectId: this.config.projectId,
urls: urls,
requestCustomField: this.requestCustomField
})
.setOnError(() => {
this.logWarning(`Failed to get user urls permissions`);
if (this.retryCounter < 5) {
this.retryCounter++;
return (0, ts_common_1._setTimeout)(this.setPermissions, 5 * ts_common_1.Second);
}
dispatch_onPermissionsFailed.dispatchModule();
})
.execute((userUrlsPermissions) => __awaiter(this, void 0, void 0, function* () {
this.retryCounter = 0;
Object.keys(userUrlsPermissions).forEach(url => {
this.loadingUrls.delete(url);
this.userUrlsPermissions[url] = userUrlsPermissions[url];
});
dispatch_onPermissionsChanged.dispatchUI();
}));
}, "get-permissions", this.debounceTime);
};
}
setDebounceTime(time) {
this.debounceTime = time;
}
setCustomField(key, value) {
this.requestCustomField[key] = value;
this.setPermissions();
}
loadUrls(urls) {
urls.forEach(url => {
if (this.loadingUrls.has(url) || this.userUrlsPermissions[url] !== undefined)
return;
this.loadingUrls.add(url);
});
this.setPermissions();
}
doesUserHavePermissions(url) {
if (this.loadingUrls.has(url))
return undefined;
const permitted = this.userUrlsPermissions[url];
if (permitted !== undefined)
return permitted;
this.loadingUrls.add(url);
this.setPermissions();
return undefined;
}
}
exports.PermissionsModuleFE_Class = PermissionsModuleFE_Class;
exports.PermissionsFE = new PermissionsModuleFE_Class();
//# sourceMappingURL=PermissionsModuleFE.js.map