@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 17.8 kB
Source Map (JSON)
{"version":3,"sources":["src/common/Promise.ts"],"names":[],"mappings":"AAKA,oBAAY,YAAY;IACpB,IAAI,IAAA;IACJ,QAAQ,IAAA;IACR,QAAQ,IAAA;CACX;AAED,MAAM,WAAW,QAAQ,CAAC,CAAC;IACvB,MAAM,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC;IAE3B,YAAY,CAAC,mBAAmB,EAC5B,oBAAoB,EAAE,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC,CAAC,KAAK,mBAAmB,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAEnH,mBAAmB,CAAC,mBAAmB,EACnC,oBAAoB,EAAE,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,mBAAmB,CAAC,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAE7H,qBAAqB,CAAC,mBAAmB,EACrC,oBAAoB,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,mBAAmB,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAE7F,4BAA4B,CAAC,mBAAmB,EAC5C,oBAAoB,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,QAAQ,CAAC,mBAAmB,CAAC,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAEvG,EAAE,CAAC,eAAe,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,EAAE,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE9F,OAAO,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;CAC9C;AAED,MAAM,WAAW,SAAS,CAAC,CAAC;IACxB,KAAK,IAAI,YAAY,CAAC;IAEtB,OAAO,IAAI,QAAQ,CAAC,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,QAAQ,CAAU;;IAOnB,KAAK,qBAEX;IAEM,OAAO,mBAEb;IAEM,OAAO,6BAGb;IAEM,MAAM,iCAGZ;CACJ;AAGD,qBAAa,IAAI,CAAC,CAAC;IACf,OAAO,CAAC,SAAS,CAAmC;IACpD,OAAO,CAAC,iBAAiB,CAA0B;IACnD,OAAO,CAAC,uBAAuB,CAAqC;IAEpE,OAAO,CAAC,mBAAmB,CAAoC;IAC/D,OAAO,CAAC,iBAAiB,CAAkC;;aAOhD,KAAK,EAAI,YAAY;aAIrB,MAAM,EAAI,aAAa,CAAC,CAAC,CAAC;IAI9B,OAAO,sBAab;IAEM,MAAM,0BAaZ;IAEM,EAAE,yFAoBR;IAED,OAAO,CAAC,sBAAsB,CAM7B;IAED,OAAO,CAAC,oBAAoB,CAU3B;IAED,OAAO,CAAC,cAAc,CAGrB;CACJ","file":"Promise.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport { ArgumentNullError } from \"./Error\";\n\nexport enum PromiseState {\n None,\n Resolved,\n Rejected,\n}\n\nexport interface IPromise<T> {\n result(): PromiseResult<T>;\n\n continueWith<TContinuationResult>(\n continuationCallback: (promiseResult: PromiseResult<T>) => TContinuationResult): IPromise<TContinuationResult>;\n\n continueWithPromise<TContinuationResult>(\n continuationCallback: (promiseResult: PromiseResult<T>) => IPromise<TContinuationResult>): IPromise<TContinuationResult>;\n\n onSuccessContinueWith<TContinuationResult>(\n continuationCallback: (result: T) => TContinuationResult): IPromise<TContinuationResult>;\n\n onSuccessContinueWithPromise<TContinuationResult>(\n continuationCallback: (result: T) => IPromise<TContinuationResult>): IPromise<TContinuationResult>;\n\n on(successCallback: (result: T) => void, errorCallback: (error: string) => void): IPromise<T>;\n\n finally(callback: () => void): IPromise<T>;\n}\n\nexport interface IDeferred<T> {\n state(): PromiseState;\n\n promise(): IPromise<T>;\n\n resolve(result: T): IDeferred<T>;\n\n reject(error: string): IDeferred<T>;\n}\n\nexport class PromiseResult<T> {\n protected privIsCompleted: boolean;\n protected privIsError: boolean;\n protected privError: string;\n protected privResult: T;\n\n public constructor(promiseResultEventSource: PromiseResultEventSource<T>) {\n promiseResultEventSource.on((result: T) => {\n if (!this.privIsCompleted) {\n this.privIsCompleted = true;\n this.privIsError = false;\n this.privResult = result;\n }\n }, (error: string) => {\n if (!this.privIsCompleted) {\n this.privIsCompleted = true;\n this.privIsError = true;\n this.privError = error;\n }\n });\n }\n\n public get isCompleted(): boolean {\n return this.privIsCompleted;\n }\n\n public get isError(): boolean {\n return this.privIsError;\n }\n\n public get error(): string {\n return this.privError;\n }\n\n public get result(): T {\n return this.privResult;\n }\n\n public throwIfError = (): void => {\n if (this.isError) {\n throw this.error;\n }\n }\n}\n\n// tslint:disable-next-line:max-classes-per-file\nexport class PromiseResultEventSource<T> {\n\n private privOnSetResult: (result: T) => void;\n private privOnSetError: (error: string) => void;\n\n public setResult = (result: T): void => {\n this.privOnSetResult(result);\n }\n\n public setError = (error: string): void => {\n this.privOnSetError(error);\n }\n\n public on = (onSetResult: (result: T) => void, onSetError: (error: string) => void): void => {\n this.privOnSetResult = onSetResult;\n this.privOnSetError = onSetError;\n }\n}\n\n// tslint:disable-next-line:max-classes-per-file\nexport class PromiseHelper {\n public static whenAll = (promises: Array<Promise<any>>): Promise<boolean> => {\n if (!promises || promises.length === 0) {\n throw new ArgumentNullError(\"promises\");\n }\n\n const deferred = new Deferred<boolean>();\n const errors: string[] = [];\n let completedPromises: number = 0;\n\n const checkForCompletion = () => {\n completedPromises++;\n if (completedPromises === promises.length) {\n if (errors.length === 0) {\n deferred.resolve(true);\n } else {\n deferred.reject(errors.join(\", \"));\n }\n }\n };\n\n for (const promise of promises) {\n promise.on((r: any) => {\n checkForCompletion();\n }, (e: string) => {\n errors.push(e);\n checkForCompletion();\n });\n }\n\n return deferred.promise();\n }\n\n public static fromResult = <TResult>(result: TResult): Promise<TResult> => {\n const deferred = new Deferred<TResult>();\n deferred.resolve(result);\n return deferred.promise();\n }\n\n public static fromError = <TResult>(error: string): Promise<TResult> => {\n const deferred = new Deferred<TResult>();\n deferred.reject(error);\n return deferred.promise();\n }\n}\n\n// TODO: replace with ES6 promises\n// tslint:disable-next-line:max-classes-per-file\nexport class Promise<T> implements IPromise<T> {\n private privSink: Sink<T>;\n\n public constructor(sink: Sink<T>) {\n this.privSink = sink;\n }\n\n public result = (): PromiseResult<T> => {\n return this.privSink.result;\n }\n\n public continueWith = <TContinuationResult>(\n continuationCallback: (promiseResult: PromiseResult<T>) => TContinuationResult): Promise<TContinuationResult> => {\n\n if (!continuationCallback) {\n throw new ArgumentNullError(\"continuationCallback\");\n }\n\n const continuationDeferral = new Deferred<TContinuationResult>();\n\n this.privSink.on(\n (r: T) => {\n try {\n const continuationResult: TContinuationResult = continuationCallback(this.privSink.result);\n continuationDeferral.resolve(continuationResult);\n } catch (e) {\n continuationDeferral.reject(e);\n }\n },\n (error: string) => {\n try {\n const continuationResult: TContinuationResult = continuationCallback(this.privSink.result);\n continuationDeferral.resolve(continuationResult);\n } catch (e) {\n continuationDeferral.reject(`'Error handler for error ${error} threw error ${e}'`);\n }\n },\n );\n\n return continuationDeferral.promise();\n }\n\n public onSuccessContinueWith = <TContinuationResult>(\n continuationCallback: (result: T) => TContinuationResult): Promise<TContinuationResult> => {\n\n if (!continuationCallback) {\n throw new ArgumentNullError(\"continuationCallback\");\n }\n\n const continuationDeferral = new Deferred<TContinuationResult>();\n\n this.privSink.on(\n (r: T) => {\n try {\n const continuationResult: TContinuationResult = continuationCallback(r);\n continuationDeferral.resolve(continuationResult);\n } catch (e) {\n continuationDeferral.reject(e);\n }\n },\n (error: string) => {\n continuationDeferral.reject(error);\n },\n );\n\n return continuationDeferral.promise();\n }\n\n public continueWithPromise = <TContinuationResult>(\n continuationCallback: (promiseResult: PromiseResult<T>) => Promise<TContinuationResult>): Promise<TContinuationResult> => {\n\n if (!continuationCallback) {\n throw new ArgumentNullError(\"continuationCallback\");\n }\n\n const continuationDeferral = new Deferred<TContinuationResult>();\n\n this.privSink.on(\n (r: T) => {\n try {\n const continuationPromise: Promise<TContinuationResult> = continuationCallback(this.privSink.result);\n if (!continuationPromise) {\n throw new Error(\"'Continuation callback did not return promise'\");\n }\n continuationPromise.on((continuationResult: TContinuationResult) => {\n continuationDeferral.resolve(continuationResult);\n }, (e: string) => {\n continuationDeferral.reject(e);\n });\n } catch (e) {\n continuationDeferral.reject(e);\n }\n },\n (error: string) => {\n try {\n const continuationPromise: Promise<TContinuationResult> = continuationCallback(this.privSink.result);\n if (!continuationPromise) {\n throw new Error(\"Continuation callback did not return promise\");\n }\n continuationPromise.on((continuationResult: TContinuationResult) => {\n continuationDeferral.resolve(continuationResult);\n }, (e: string) => {\n continuationDeferral.reject(e);\n });\n } catch (e) {\n continuationDeferral.reject(`'Error handler for error ${error} threw error ${e}'`);\n }\n },\n );\n\n return continuationDeferral.promise();\n }\n\n public onSuccessContinueWithPromise = <TContinuationResult>(\n continuationCallback: (result: T) => Promise<TContinuationResult>): Promise<TContinuationResult> => {\n\n if (!continuationCallback) {\n throw new ArgumentNullError(\"continuationCallback\");\n }\n\n const continuationDeferral = new Deferred<TContinuationResult>();\n\n this.privSink.on(\n (r: T) => {\n try {\n const continuationPromise: Promise<TContinuationResult> = continuationCallback(r);\n if (!continuationPromise) {\n throw new Error(\"Continuation callback did not return promise\");\n }\n continuationPromise.on((continuationResult: TContinuationResult) => {\n continuationDeferral.resolve(continuationResult);\n }, (e: string) => {\n continuationDeferral.reject(e);\n });\n } catch (e) {\n continuationDeferral.reject(e);\n }\n },\n (error: string) => {\n continuationDeferral.reject(error);\n },\n );\n\n return continuationDeferral.promise();\n }\n\n public on = (\n successCallback: (result: T) => void,\n errorCallback: (error: string) => void): Promise<T> => {\n if (!successCallback) {\n throw new ArgumentNullError(\"successCallback\");\n }\n\n if (!errorCallback) {\n throw new ArgumentNullError(\"errorCallback\");\n }\n\n this.privSink.on(successCallback, errorCallback);\n return this;\n }\n\n public finally = (callback: () => void): Promise<T> => {\n if (!callback) {\n throw new ArgumentNullError(\"callback\");\n }\n\n const callbackWrapper = (_: any) => {\n callback();\n };\n\n return this.on(callbackWrapper, callbackWrapper);\n }\n}\n\n// tslint:disable-next-line:max-classes-per-file\nexport class Deferred<T> implements IDeferred<T> {\n private privPromise: Promise<T>;\n private privSink: Sink<T>;\n\n public constructor() {\n this.privSink = new Sink<T>();\n this.privPromise = new Promise<T>(this.privSink);\n }\n\n public state = (): PromiseState => {\n return this.privSink.state;\n }\n\n public promise = (): Promise<T> => {\n return this.privPromise;\n }\n\n public resolve = (result: T): Deferred<T> => {\n this.privSink.resolve(result);\n return this;\n }\n\n public reject = (error: string): Deferred<T> => {\n this.privSink.reject(error);\n return this;\n }\n}\n\n// tslint:disable-next-line:max-classes-per-file\nexport class Sink<T> {\n private privState: PromiseState = PromiseState.None;\n private privPromiseResult: PromiseResult<T> = null;\n private privPromiseResultEvents: PromiseResultEventSource<T> = null;\n\n private privSuccessHandlers: Array<((result: T) => void)> = [];\n private privErrorHandlers: Array<(e: string) => void> = [];\n\n public constructor() {\n this.privPromiseResultEvents = new PromiseResultEventSource();\n this.privPromiseResult = new PromiseResult(this.privPromiseResultEvents);\n }\n\n public get state(): PromiseState {\n return this.privState;\n }\n\n public get result(): PromiseResult<T> {\n return this.privPromiseResult;\n }\n\n public resolve = (result: T): void => {\n if (this.privState !== PromiseState.None) {\n throw new Error(\"'Cannot resolve a completed promise'\");\n }\n\n this.privState = PromiseState.Resolved;\n this.privPromiseResultEvents.setResult(result);\n\n for (let i = 0; i < this.privSuccessHandlers.length; i++) {\n this.executeSuccessCallback(result, this.privSuccessHandlers[i], this.privErrorHandlers[i]);\n }\n\n this.detachHandlers();\n }\n\n public reject = (error: string): void => {\n if (this.privState !== PromiseState.None) {\n throw new Error(\"'Cannot reject a completed promise'\");\n }\n\n this.privState = PromiseState.Rejected;\n this.privPromiseResultEvents.setError(error);\n\n for (const errorHandler of this.privErrorHandlers) {\n this.executeErrorCallback(error, errorHandler);\n }\n\n this.detachHandlers();\n }\n\n public on = (\n successCallback: (result: T) => void,\n errorCallback: (error: string) => void): void => {\n\n if (successCallback == null) {\n successCallback = (r: T) => { return; };\n }\n\n if (this.privState === PromiseState.None) {\n this.privSuccessHandlers.push(successCallback);\n this.privErrorHandlers.push(errorCallback);\n } else {\n if (this.privState === PromiseState.Resolved) {\n this.executeSuccessCallback(this.privPromiseResult.result, successCallback, errorCallback);\n } else if (this.privState === PromiseState.Rejected) {\n this.executeErrorCallback(this.privPromiseResult.error, errorCallback);\n }\n\n this.detachHandlers();\n }\n }\n\n private executeSuccessCallback = (result: T, successCallback: (result: T) => void, errorCallback: (error: string) => void): void => {\n try {\n successCallback(result);\n } catch (e) {\n this.executeErrorCallback(`'Unhandled callback error: ${e}'`, errorCallback);\n }\n }\n\n private executeErrorCallback = (error: string, errorCallback: (error: string) => void): void => {\n if (errorCallback) {\n try {\n errorCallback(error);\n } catch (e) {\n throw new Error(`'Unhandled callback error: ${e}. InnerError: ${error}'`);\n }\n } else {\n throw new Error(`'Unhandled error: ${error}'`);\n }\n }\n\n private detachHandlers = (): void => {\n this.privErrorHandlers = [];\n this.privSuccessHandlers = [];\n }\n}\n"]}