UNPKG

@casual-simulation/aux-common

Version:
170 lines 6.14 kB
import { Subject, filter, first, merge } from 'rxjs'; /** * Defines an interface that is able to provide events for when authentication information is requested or provided to partitions. */ export class PartitionAuthSource { /** * Gets an observable for when a partition requests or provides authentication information. */ get onAuthMessage() { return merge(this._onAuthRequest, this._onAuthResponse, this._onAuthPermissionRequest, this._onAuthPermissionResult, this._onAuthExternalPermissionRequest, this._onAuthExternalPermissionResult); } /** * Gets an observable for when a partition requests permission. */ get onAuthPermissionRequest() { return this._onAuthPermissionRequest; } /** * Gets an observable for when a partition provides permission result. */ get onAuthPermissionResult() { return this._onAuthPermissionResult; } /** * Gets an observable for when an external permissions request is recieved. */ get onAuthExternalPermissionRequest() { return this._onAuthExternalPermissionRequest; } /** * Gets an observable for when an external permissions result is recieved. */ get onAuthExternalPermissionResult() { return this._onAuthExternalPermissionResult; } /** * Gets an observable for when a partition requests authentication information. */ get onAuthRequest() { return this._onAuthRequest; } /** * Gets an observable for when auth information should be provided to a partition. */ get onAuthResponse() { return this._onAuthResponse; } constructor(initialIndicators) { this._onAuthRequest = new Subject(); this._onAuthResponse = new Subject(); this._onAuthPermissionRequest = new Subject(); this._onAuthPermissionResult = new Subject(); this._onAuthExternalPermissionRequest = new Subject(); this._onAuthExternalPermissionResult = new Subject(); this._indicators = new Map(); this._promises = new Map(); if (initialIndicators) { this._indicators = new Map(initialIndicators); } this._onAuthResponse.subscribe((response) => { if (response.success) { this._indicators.set(response.origin, response.indicator); } }); } /** * Gets the connection indicator that is currently stored for the given origin. * @param origin The origin. * @returns Returns the connection indicator or null if none is stored. */ getConnectionIndicatorForOrigin(origin) { var _a; return (_a = this._indicators.get(origin)) !== null && _a !== void 0 ? _a : null; } /** * Gets an observable that resolves when there is an auth response for the given HTTP origin. * @param origin The origin. */ onAuthResponseForOrigin(origin) { return this._onAuthResponse.pipe(filter((response) => response.origin === origin)); } /** * Sends a request for authentication information. Returns an observable that resolves with the first response for the origin. * @param request The request that should be sent. */ sendAuthRequest(request) { var _a, _b; const key = `${request.origin}:${request.kind}:${request.errorCode}:${(_a = request.resource) === null || _a === void 0 ? void 0 : _a.recordName}:${(_b = request.resource) === null || _b === void 0 ? void 0 : _b.inst}}`; if (this._promises.has(key)) { return this._promises.get(key); } const promise = new Promise((resolve, reject) => { this._onAuthResponse .pipe(first((r) => r.origin === request.origin)) .subscribe({ next: (r) => { this._promises.delete(key); resolve(r); }, error: (err) => { this._promises.delete(key); reject(err); }, }); this._onAuthRequest.next(request); }); this._promises.set(key, promise); return promise; } /** * Sends a response for authentication information. * @param response The response that should be sent. */ sendAuthResponse(response) { this._onAuthResponse.next(response); } /** * Sends the given request for permission. * @param request The request. */ sendAuthPermissionRequest(request) { this._onAuthPermissionRequest.next(request); } /** * Sends the given permission result. * @param result The result. */ sendAuthPermissionResult(result) { this._onAuthPermissionResult.next(result); } /** * Sends the given request for permission. * @param request The request. */ sendAuthExternalPermissionRequest(request) { this._onAuthExternalPermissionRequest.next(request); } /** * Sends the given permission result. * @param result The result. */ sendAuthExternalPermissionResult(result) { this._onAuthExternalPermissionResult.next(result); } /** * Sends the given auth message. * @param message The message that should be sent. */ sendAuthMessage(message) { if (message.type === 'response') { this.sendAuthResponse(message); } else if (message.type === 'request') { this.sendAuthRequest(message); } else if (message.type === 'permission_request') { this.sendAuthPermissionRequest(message); } else if (message.type === 'permission_result') { this.sendAuthPermissionResult(message); } else if (message.type === 'external_permission_request') { this.sendAuthExternalPermissionRequest(message); } else if (message.type === 'external_permission_result') { this.sendAuthExternalPermissionResult(message); } } } //# sourceMappingURL=PartitionAuthSource.js.map