{"version":3,"file":"ngx-operators.mjs","sources":["../../../projects/ngx-operators/src/lib/prepare.ts","../../../projects/ngx-operators/src/lib/indicate.ts","../../../projects/ngx-operators/src/lib/throw-for-codes.ts","../../../projects/ngx-operators/src/lib/http.ts","../../../projects/ngx-operators/src/lib/download.ts","../../../projects/ngx-operators/src/lib/ignore-not-found.ts","../../../projects/ngx-operators/src/lib/run-zone.ts","../../../projects/ngx-operators/src/lib/upload.ts","../../../projects/ngx-operators/src/public-api.ts","../../../projects/ngx-operators/src/ngx-operators.ts"],"sourcesContent":["import { defer, Observable } from \"rxjs\";\n\n/**\n * Invokes a callback upon subscription.\n *\n * @param callback function to invoke upon subscription\n * @returns stream which will invoke callback\n */\nexport function prepare<T>(\n  callback: () => void\n): (source: Observable<T>) => Observable<T> {\n  return (source: Observable<T>): Observable<T> =>\n    defer(() => {\n      callback();\n      return source;\n    });\n}\n","import { prepare } from \"./prepare\";\nimport { Observable, Subject } from \"rxjs\";\nimport { finalize } from \"rxjs/operators\";\n\n/**\n * Indicates whether the observable is currently loading (meaning subscription is active and\n * it hasn't completed or errored).\n *\n * @param indicator subject as target for indication\n * @returns stream which will indicate loading through passed subject\n */\nexport function indicate<T>(\n  indicator: Subject<boolean>\n): (source: Observable<T>) => Observable<T> {\n  return (source: Observable<T>) =>\n    source.pipe(\n      prepare(() => indicator.next(true)),\n      finalize(() => indicator.next(false))\n    );\n}\n","import { Observable, throwError } from \"rxjs\";\nimport { catchError } from \"rxjs/operators\";\nimport { HttpErrorResponse } from \"@angular/common/http\";\n\n/**\n * Maps Angular HTTP status codes to more semantic errors.\n *\n * @param codeErrors mapping from status code to error\n * @returns stream which will map http error codes to passed errors\n */\nexport function throwForCodes<T>(codeErrors: {\n  [status: number]: () => Error;\n}): (source: Observable<T>) => Observable<T> {\n  return (source: Observable<T>) =>\n    source.pipe(\n      catchError(error => {\n        if (error instanceof HttpErrorResponse) {\n          return throwError(codeErrors[error.status]?.() ?? error);\n        }\n        return throwError(error);\n      })\n    );\n}\n","import { HttpEvent, HttpResponse, HttpEventType, HttpProgressEvent } from \"@angular/common/http\";\n\nexport function isHttpResponse<T>(\n  event: HttpEvent<T>\n): event is HttpResponse<T> {\n  return event.type === HttpEventType.Response;\n}\n\nexport function isHttpProgressEvent(\n  event: HttpEvent<unknown>\n): event is HttpProgressEvent {\n  return (\n    event.type === HttpEventType.DownloadProgress ||\n    event.type === HttpEventType.UploadProgress\n  );\n}\n","import { HttpEvent } from '@angular/common/http';\nimport { Observable } from 'rxjs';\nimport { distinctUntilChanged, scan } from 'rxjs/operators';\nimport { isHttpProgressEvent, isHttpResponse } from './http';\nexport interface Download {\n  content: Blob | null;\n  progress: number;\n  state: \"PENDING\" | \"IN_PROGRESS\" | \"DONE\";\n}\n\nexport function download(\n  saver?: (b: Blob) => void\n): (source: Observable<HttpEvent<Blob>>) => Observable<Download> {\n  return (source: Observable<HttpEvent<Blob>>) =>\n    source.pipe(\n      scan(\n        (last: Download, event): Download => {\n          if (isHttpProgressEvent(event)) {\n            return {\n              progress: event.total\n                ? Math.round((100 * event.loaded) / event.total)\n                : last.progress,\n              state: \"IN_PROGRESS\",\n              content: null\n            };\n          }\n          if (isHttpResponse(event)) {\n            if (saver && event.body) {\n              saver(event.body);\n            }\n            return {\n              progress: 100,\n              state: \"DONE\",\n              content: event.body\n            };\n          }\n          return last;\n        },\n        { state: \"PENDING\", progress: 0, content: null }\n      ),\n      distinctUntilChanged(\n        (a, b) =>\n          a.state === b.state &&\n          a.progress === b.progress &&\n          a.content === b.content\n      )\n    );\n}\n","import { HttpResponseBase } from \"@angular/common/http\";\nimport { EMPTY, Observable, throwError } from \"rxjs\";\nimport { catchError } from \"rxjs/operators\";\n\n/**\n * If the underlying request throws with a 404 error, it will swallow the error\n * and complete the observable instead. All other errors will be rethrown.\n *\n * You can then set a fallback value with `defaultIfEmpty`.\n *\n * @returns stream which will map 404 errors to `undefined`\n * @example\n * this.http.get<User>('/users/123').pipe(\n *   ignoreNotFound()\n * )\n */\nexport function ignoreNotFound<T>(): (source: Observable<T>) => Observable<T> {\n  return (source: Observable<T>): Observable<T> =>\n    source.pipe(\n      catchError(error => {\n        if (error instanceof HttpResponseBase && error.status === 404) {\n          return EMPTY;\n        }\n        return throwError(error);\n      })\n    );\n}\n","import { Observable, OperatorFunction } from \"rxjs\";\nimport { NgZone } from \"@angular/core\";\n\nexport const noZoneRunner = (zone: NgZone) => <T>(\n  source: Observable<T>\n): Observable<T> => {\n  return new Observable<T>(subscriber => {\n    return zone.runOutsideAngular(() => {\n      runInZone(zone)(source).subscribe(subscriber);\n    });\n  });\n};\n\nexport function runOutsideZone<T>(zone: NgZone): OperatorFunction<T, T> {\n  return source => {\n    return new Observable(subscriber => {\n      return source.subscribe(\n        (value: T) => zone.runOutsideAngular(() => subscriber.next(value)),\n        (e: any) => zone.runOutsideAngular(() => subscriber.error(e)),\n        () => zone.runOutsideAngular(() => subscriber.complete())\n      );\n    });\n  };\n}\n\nexport function runInZone<T>(zone: NgZone): OperatorFunction<T, T> {\n  return source => {\n    return new Observable(subscriber => {\n      return source.subscribe(\n        (value: T) => zone.run(() => subscriber.next(value)),\n        (e: any) => zone.run(() => subscriber.error(e)),\n        () => zone.run(() => subscriber.complete())\n      );\n    });\n  };\n}\n","import { HttpEvent, HttpResponse } from \"@angular/common/http\";\nimport { Observable } from \"rxjs\";\nimport { distinctUntilChanged, scan } from \"rxjs/operators\";\nimport { isHttpProgressEvent, isHttpResponse } from \"./http\";\n\nexport interface Upload<T> {\n  progress: number;\n  state: \"PENDING\" | \"IN_PROGRESS\" | \"DONE\";\n  body: T | null;\n}\n\nexport function upload<T = unknown>(): (\n  source: Observable<HttpEvent<T>>\n) => Observable<Upload<T>> {\n  const initialState: Upload<T> = { state: \"PENDING\", progress: 0, body: null };\n  const reduceState = (state: Upload<T>, event: HttpEvent<T>): Upload<T> => {\n    if (isHttpProgressEvent(event)) {\n      return {\n        progress: event.total\n          ? Math.round((100 * event.loaded) / event.total)\n          : state.progress,\n        state: \"IN_PROGRESS\",\n        body: null,\n      };\n    }\n    if (isHttpResponse<T>(event)) {\n      return {\n        progress: 100,\n        state: \"DONE\",\n        body: event.body,\n      };\n    }\n    return state;\n  };\n  return (source) =>\n    source.pipe(\n      scan(reduceState, initialState),\n      distinctUntilChanged(\n        (a, b) => a.state === b.state && a.progress === b.progress\n      )\n    );\n}\n","/*\n * Public API Surface of ngx-operators\n */\n\nexport * from \"./lib/prepare\";\nexport * from \"./lib/indicate\";\nexport * from \"./lib/throw-for-codes\";\nexport * from \"./lib/download\";\nexport * from \"./lib/ignore-not-found\";\nexport * from \"./lib/run-zone\";\nexport * from \"./lib/upload\";\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAEA;;;;;AAKG;AACG,SAAU,OAAO,CACrB,QAAoB,EAAA;IAEpB,OAAO,CAAC,MAAqB,KAC3B,KAAK,CAAC,MAAK;AACT,QAAA,QAAQ,EAAE;AACV,QAAA,OAAO,MAAM;AACf,KAAC,CAAC;AACN;;ACZA;;;;;;AAMG;AACG,SAAU,QAAQ,CACtB,SAA2B,EAAA;AAE3B,IAAA,OAAO,CAAC,MAAqB,KAC3B,MAAM,CAAC,IAAI,CACT,OAAO,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EACnC,QAAQ,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CACtC;AACL;;ACfA;;;;;AAKG;AACG,SAAU,aAAa,CAAI,UAEhC,EAAA;AACC,IAAA,OAAO,CAAC,MAAqB,KAC3B,MAAM,CAAC,IAAI,CACT,UAAU,CAAC,KAAK,IAAG;AACjB,QAAA,IAAI,KAAK,YAAY,iBAAiB,EAAE;AACtC,YAAA,OAAO,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC;;AAE1D,QAAA,OAAO,UAAU,CAAC,KAAK,CAAC;KACzB,CAAC,CAC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