{"version":3,"file":"ngxs-labs-dispatch.mjs","sources":["../../../src/lib/internals/static-injector.ts","../../../src/lib/internals/distribute-actions.ts","../../../src/lib/internals/action-completer.ts","../../../src/lib/internals/decorator-injector-adapter.ts","../../../src/lib/decorators/dispatch.ts","../../../src/lib/dispatch.module.ts","../../../src/ngxs-labs-dispatch.ts"],"sourcesContent":["import { Injector, NgZone } from '@angular/core';\nimport { Store } from '@ngxs/store';\n\nclass NgxsDispatchPluginModuleNotImported extends Error {\n  message = 'NgxsDispatchPluginModule is not imported';\n}\n\nlet _injector: Injector | null = null;\n\nexport function setInjector(injector: Injector | null): void {\n  _injector = injector;\n}\n\nexport function getStore(): never | Store {\n  if (_injector === null) {\n    throw new NgxsDispatchPluginModuleNotImported();\n  } else {\n    return _injector.get(Store);\n  }\n}\n\nexport function getNgZone(): never | NgZone {\n  if (_injector === null) {\n    throw new NgxsDispatchPluginModuleNotImported();\n  } else {\n    return _injector.get(NgZone);\n  }\n}\n","import { NgZone, ɵisPromise } from '@angular/core';\nimport { Store } from '@ngxs/store';\nimport { isObservable, Observable } from 'rxjs';\nimport { takeUntil } from 'rxjs/operators';\n\nimport { ActionCompleter } from './action-completer';\nimport { Wrapped, ActionOrActions } from './internals';\n\nfunction dispatch(store: Store, ngZone: NgZone, actionOrActions: ActionOrActions): void {\n  ngZone.run(() => store.dispatch(actionOrActions));\n}\n\nfunction unwrapObservable(\n  store: Store,\n  ngZone: NgZone,\n  wrapped: Observable<ActionOrActions>,\n  actionCompleter: ActionCompleter | null\n): Observable<ActionOrActions> {\n  if (actionCompleter !== null) {\n    wrapped = wrapped.pipe(takeUntil(actionCompleter.cancelUncompleted$));\n  }\n\n  wrapped.subscribe({\n    next: actionOrActions => dispatch(store, ngZone, actionOrActions)\n  });\n\n  return wrapped;\n}\n\nfunction unwrapPromise(\n  store: Store,\n  ngZone: NgZone,\n  wrapped: Promise<ActionOrActions>\n): Promise<ActionOrActions> {\n  return wrapped.then(actionOrActions => {\n    dispatch(store, ngZone, actionOrActions);\n    return actionOrActions;\n  });\n}\n\n/**\n * As dispatchers can have different return types, they can be either\n * synchronous or asynchronous, we have to determine its return type\n * and unwrap `Promise` or `Observable`\n */\nexport function distributeActions(\n  store: Store,\n  ngZone: NgZone,\n  wrapped: Wrapped,\n  actionCompleter: ActionCompleter | null\n) {\n  if (ɵisPromise(wrapped)) {\n    return unwrapPromise(store, ngZone, wrapped);\n  } else if (isObservable(wrapped)) {\n    return unwrapObservable(store, ngZone, wrapped, actionCompleter);\n  } else {\n    dispatch(store, ngZone, wrapped);\n    return wrapped;\n  }\n}\n","import { Subject } from 'rxjs';\n\nexport class ActionCompleter {\n  cancelUncompleted$ = new Subject<void>();\n\n  cancelPreviousAction(): void {\n    this.cancelUncompleted$.next();\n  }\n}\n\nexport function createActionCompleter(cancelUncompleted: boolean): ActionCompleter | null {\n  return cancelUncompleted ? new ActionCompleter() : null;\n}\n","import {\n  InjectionToken,\n  Injector,\n  INJECTOR,\n  Type,\n  ɵɵdirectiveInject,\n  ɵglobal\n} from '@angular/core';\n\n// Will be provided through Terser global definitions by Angular CLI\n// during the production build. This is how Angular does tree-shaking internally.\ndeclare const ngDevMode: boolean;\n\n// Angular doesn't export `NG_FACTORY_DEF`.\nconst NG_FACTORY_DEF = 'ɵfac';\n\n// A `Symbol` which is used to save the `Injector` onto the class instance.\nconst InjectorInstance: unique symbol = Symbol('InjectorInstance');\n\n// A `Symbol` which is used to determine if factory has been decorated previously or not.\nconst FactoryHasBeenDecorated: unique symbol = Symbol('FactoryHasBeenDecorated');\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function ensureLocalInjectorCaptured(target: Object): void {\n  if (FactoryHasBeenDecorated in target.constructor.prototype) {\n    return;\n  }\n\n  const constructor: ConstructorWithDefinitionAndFactory = target.constructor;\n  // Means we're in AOT mode.\n  if (typeof constructor[NG_FACTORY_DEF] === 'function') {\n    decorateFactory(constructor);\n  } else if (ngDevMode) {\n    // We're running in JIT mode and that means we're not able to get the compiled definition\n    // on the class inside the property decorator during the current message loop tick. We have\n    // to wait for the next message loop tick. Note that this is safe since this Promise will be\n    // resolved even before the `APP_INITIALIZER` is resolved.\n    // The below code also will be executed only in development mode, since it's never recommended\n    // to use the JIT compiler in production mode (by setting \"aot: false\").\n    decorateFactoryLater(constructor);\n  }\n\n  target.constructor.prototype[FactoryHasBeenDecorated] = true;\n}\n\nexport function localInject<T>(\n  instance: PrivateInstance,\n  token: InjectionToken<T> | Type<T>\n): T | null {\n  const injector: Injector | undefined = instance[InjectorInstance];\n  return injector ? injector.get(token) : null;\n}\n\nfunction decorateFactory(constructor: ConstructorWithDefinitionAndFactory): void {\n  const factory = constructor[NG_FACTORY_DEF];\n\n  if (typeof factory !== 'function') {\n    return;\n  }\n\n  // Let's try to get any definition.\n  // Caretaker note: this will be compatible only with Angular 9+, since Angular 9 is the first\n  // Ivy-stable version. Previously definition properties were named differently (e.g. `ngComponentDef`).\n  const def = constructor.ɵprov || constructor.ɵpipe || constructor.ɵcmp || constructor.ɵdir;\n\n  const decoratedFactory = () => {\n    const instance = factory();\n    // Caretaker note: `inject()` won't work here.\n    // We can use the `directiveInject` only during the component\n    // construction, since Angular captures the currently active injector.\n    // We're not able to use this function inside the getter (when the `selectorId` property is\n    // requested for the first time), since the currently active injector will be null.\n    instance[InjectorInstance] = ɵɵdirectiveInject(\n      // We're using `INJECTOR` token except of the `Injector` class since the compiler\n      // throws: `Cannot assign an abstract constructor type to a non-abstract constructor type.`.\n      // Caretaker note: that this is the same way of getting the injector.\n      INJECTOR\n    );\n    return instance;\n  };\n\n  if (def) {\n    def.factory = decoratedFactory;\n  }\n\n  Object.defineProperty(constructor, NG_FACTORY_DEF, {\n    get: () => decoratedFactory\n  });\n}\n\nfunction decorateFactoryLater(constructor: ConstructorWithDefinitionAndFactory): void {\n  // This function actually will be tree-shaken away when building for production since it's guarded with `ngDevMode`.\n  // We're having the `try-catch` here because of the `SyncTestZoneSpec`, which throws\n  // an error when micro or macrotask is used within a synchronous test. E.g. `Cannot call\n  // Promise.then from within a sync test`.\n  try {\n    Promise.resolve().then(() => {\n      decorateFactory(constructor);\n    });\n  } catch {\n    // This is kind of a \"hack\", but we try to be backwards-compatible,\n    // tho this `catch` block will only be executed when tests are run with Jasmine or Jest.\n    ɵglobal.process &&\n      ɵglobal.process.nextTick &&\n      ɵglobal.process.nextTick(() => {\n        decorateFactory(constructor);\n      });\n  }\n}\n\n// We could've used `ɵɵFactoryDef` but we try to be backwards-compatible,\n// since it's not exported in older Angular versions.\ntype Factory = () => PrivateInstance;\n\n// We could've used `ɵɵInjectableDef`, `ɵɵPipeDef`, etc. We try to be backwards-compatible\n// since they're not exported in older Angular versions.\ninterface Definition {\n  factory: Factory | null;\n}\n\ninterface ConstructorWithDefinitionAndFactory extends Function {\n  // Provider definition for the `@Injectable()` class.\n  ɵprov?: Definition;\n  // Pipe definition for the `@Pipe()` class.\n  ɵpipe?: Definition;\n  // Component definition for the `@Component()` class.\n  ɵcmp?: Definition;\n  // Directive definition for the `@Directive()` class.\n  ɵdir?: Definition;\n  [NG_FACTORY_DEF]?: Factory;\n}\n\ninterface PrivateInstance {\n  [InjectorInstance]?: Injector;\n}\n","import { NgZone, ɵivyEnabled } from '@angular/core';\nimport { Store } from '@ngxs/store';\n\nimport { DispatchOptions } from '../internals/internals';\nimport { getNgZone, getStore } from '../internals/static-injector';\nimport { distributeActions } from '../internals/distribute-actions';\nimport { createActionCompleter } from '../internals/action-completer';\nimport { ensureLocalInjectorCaptured, localInject } from '../internals/decorator-injector-adapter';\n\nconst defaultOptions: DispatchOptions = { cancelUncompleted: false };\n\nexport function Dispatch(options = defaultOptions): PropertyDecorator {\n  return (\n    target: Object,\n    propertyKey: string | symbol,\n    descriptor?: TypedPropertyDescriptor<Function>\n  ) => {\n    let originalValue: Function;\n\n    const actionCompleter = createActionCompleter(options.cancelUncompleted!);\n\n    function wrapped(this: ThisType<unknown>) {\n      // Every time the function is invoked we have to generate event\n      // to cancel previously uncompleted asynchronous job\n      if (actionCompleter !== null) {\n        actionCompleter.cancelPreviousAction();\n      }\n\n      const store = localInject(this, Store) || getStore();\n      const ngZone = localInject(this, NgZone) || getNgZone();\n      const wrapped = originalValue.apply(this, arguments);\n\n      return ngZone.runOutsideAngular(() =>\n        distributeActions(store, ngZone, wrapped, actionCompleter)\n      );\n    }\n\n    if (typeof descriptor?.value === 'function') {\n      originalValue = descriptor.value!;\n      descriptor.value = wrapped;\n    } else {\n      Object.defineProperty(target, propertyKey, {\n        set: value => (originalValue = value),\n        get: () => wrapped\n      });\n    }\n\n    if (ɵivyEnabled) {\n      ensureLocalInjectorCaptured(target);\n    }\n  };\n}\n","import { NgModule, ModuleWithProviders, NgModuleRef } from '@angular/core';\n\nimport { setInjector } from './internals/static-injector';\n\n@NgModule()\nexport class NgxsDispatchPluginModule {\n  constructor(ngModuleRef: NgModuleRef<unknown>) {\n    setInjector(ngModuleRef.injector);\n    ngModuleRef.onDestroy(() => {\n      setInjector(null);\n    });\n  }\n\n  static forRoot(): ModuleWithProviders<NgxsDispatchPluginModule> {\n    return {\n      ngModule: NgxsDispatchPluginModule\n    };\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAGA,MAAM,mCAAoC,SAAQ,KAAK;IAAvD;;QACE,YAAO,GAAG,0CAA0C,CAAC;KACtD;CAAA;AAED,IAAI,SAAS,GAAoB,IAAI,CAAC;SAEtB,WAAW,CAAC,QAAyB;IACnD,SAAS,GAAG,QAAQ,CAAC;AACvB,CAAC;SAEe,QAAQ;IACtB,IAAI,SAAS,KAAK,IAAI,EAAE;QACtB,MAAM,IAAI,mCAAmC,EAAE,CAAC;KACjD;SAAM;QACL,OAAO,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;KAC7B;AACH,CAAC;SAEe,SAAS;IACvB,IAAI,SAAS,KAAK,IAAI,EAAE;QACtB,MAAM,IAAI,mCAAmC,EAAE,CAAC;KACjD;SAAM;QACL,OAAO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