import EventEmitter from "eventemitter3" ; import { forEachAsync } from "foreachasync" ; import globals from "global" ; import TextDatasource from "./TextDatasource" ; function markGlobalPresence ( ) { if ( typeof globals . _bcupAuthMgr !== "undefined" ) { console . warn ( "Detected multiple Buttercup authentication managers for datasources: Multiple copies of the same library may have been bundled together" ) ; } globals . _bcupAuthMgr = true ; } let __sharedManager ; interface AuthHandlerFunction { ( datasource ) : void ; } interface AuthHandlers { [ type ] : Array < AuthHandlerFunction > ; } augments EventEmitter memberof export default class DatasourceAuthManager extends EventEmitter { returns static memberof static getSharedManager ( ) { if ( ! __sharedManager ) { __sharedManager = new DatasourceAuthManager ( ) ; markGlobalPresence ( ) ; } return __sharedManager ; } _handlers : AuthHandlers ; /**
     * Constructor for the auth manager
     */ constructor ( ) { super ( ) ; this . _handlers = { } ; } param datasourceType param datasourceInst returns throws memberof executeAuthHandlers ( datasourceType , datasourceInst ) { const handlers = this . _handlers [ datasourceType ] ; if ( ! Array . isArray ( handlers ) ) { return Promise . reject ( new Error ( `Failed executing auth handlers: No handlers registered for datasource type: ${ datasourceType }` ) ) ; } return Promise . resolve ( ) . then ( ( ) => forEachAsync ( handlers , handler => handler ( datasourceInst ) ) ) . then ( ( ) => { /**
             * Event for when the handlers have been fired
             */ this . emit ( "authHandlersExecuted" , { datasource : datasourceInst , datasourceType } ) ; } ) ; } param datasourceType param handler example memberof throws registerHandler ( datasourceType , handler ) { if ( typeof handler !== "function" ) { throw new Error ( "Failed registering handler: Argument was not a function" ) ; } this . _handlers [ datasourceType ] = this . _handlers [ datasourceType ] || [ ] ; this . _handlers [ datasourceType ] . push ( handler ) ; } }