UNPKG

@rx-angular/cdk

Version:

@rx-angular/cdk is a Component Development Kit for ergonomic and highly performant angular applications. It helps to to build Large scale applications, UI libs, state management, rendering systems and much more. Furthermore the unique way of mixing reacti

1 lines 11.9 kB
{"version":3,"file":"rx-angular-cdk-coalescing.mjs","sources":["../../../../libs/cdk/coalescing/src/lib/coalescingManager.ts","../../../../libs/cdk/coalescing/src/lib/coalesceWith.ts","../../../../libs/cdk/coalescing/src/rx-angular-cdk-coalescing.ts"],"sourcesContent":["interface CoalescingContextProps extends Record<string, unknown> {\n numCoalescingSubscribers: number;\n}\n\nexport interface CoalescingManager {\n remove: (scope: Record<string, unknown>) => void;\n add: (scope: Record<string, unknown>) => void;\n isCoalescing: (scope: Record<string, unknown>) => boolean;\n}\n\nexport const coalescingManager: CoalescingManager = createCoalesceManager();\ntype KeyOf<O> = keyof O;\ntype ValueOf<O> = O[keyof O];\nfunction hasKey<O>(ctx: O, property: KeyOf<O>): ctx is O {\n return ctx[property] != null;\n}\n/*\n * createPropertiesWeakMap\n *\n * @param getDefaults: (o: O) => P\n * Example:\n *\n * export interface Properties {\n * isCoalescing: boolean;\n * }\n *\n * const obj: object = {\n * foo: 'bar',\n * isCoalescing: 'weakMap version'\n * };\n *\n * const getDefaults = (ctx: object): Properties => ({isCoalescing: false});\n * const propsMap = createPropertiesWeakMap<object, Properties>(getDefaults);\n *\n * console.log('obj before:', obj);\n * // {foo: \"bar\", isCoalescing: \"weakMap version\"}\n * console.log('props before:', propsMap.getProps(obj));\n * // {isCoalescing: \"weakMap version\"}\n *\n * propsMap.setProps(obj, {isCoalescing: true});\n * console.log('obj after:', obj);\n * // {foo: \"bar\", isCoalescing: \"weakMap version\"}\n * console.log('props after:', propsMap.getProps(obj));\n * // {isCoalescing: \"true\"}\n * */\nfunction createPropertiesWeakMap<\n O extends Record<string, unknown>,\n P extends O\n>(getDefaults: (o: O) => P) {\n type K = KeyOf<P>;\n const propertyMap = new WeakMap<O, P>();\n\n return {\n getProps: getProperties,\n setProps: setProperties,\n };\n\n function getProperties(ctx: O): P {\n const defaults = getDefaults(ctx);\n const propertiesPresent: P | undefined = propertyMap.get(ctx);\n let properties: P;\n\n if (propertiesPresent !== undefined) {\n properties = propertiesPresent as P;\n } else {\n properties = {} as P;\n\n (Object.entries(defaults) as unknown[]).forEach(\n ([prop, value]: [KeyOf<P>, ValueOf<P>]): void => {\n if (hasKey(ctx as P, prop)) {\n properties[prop] = (ctx as P)[prop];\n } else {\n properties[prop] = value;\n }\n }\n );\n\n propertyMap.set(ctx, properties);\n }\n return properties;\n }\n\n function setProperties(ctx: O, props: Partial<P>): P {\n const properties: P = getProperties(ctx);\n (Object.entries(props) as [K, P[K]][]).forEach(([prop, value]) => {\n properties[prop] = value;\n });\n propertyMap.set(ctx, properties);\n return properties;\n }\n}\n\nconst coalescingContextPropertiesMap = createPropertiesWeakMap<\n Record<string, unknown>,\n CoalescingContextProps\n>((ctx) => ({\n numCoalescingSubscribers: 0,\n}));\n/**\n * @describe createCoalesceManager\n *\n * returns a\n * Maintains a weak map of component references ans flags\n * them if the coalescing process is already started for them.\n *\n * Used in render aware internally.\n */\nfunction createCoalesceManager(): CoalescingManager {\n return {\n remove: removeWork,\n add: addWork,\n isCoalescing,\n };\n\n // Increments the number of subscriptions in a scope e.g. a class instance\n function removeWork(scope: Record<string, unknown>): void {\n const numCoalescingSubscribers =\n coalescingContextPropertiesMap.getProps(scope).numCoalescingSubscribers -\n 1;\n coalescingContextPropertiesMap.setProps(scope, {\n numCoalescingSubscribers:\n numCoalescingSubscribers >= 0 ? numCoalescingSubscribers : 0,\n });\n }\n\n // Decrements the number of subscriptions in a scope e.g. a class instance\n function addWork(scope: Record<string, unknown>): void {\n const numCoalescingSubscribers =\n coalescingContextPropertiesMap.getProps(scope).numCoalescingSubscribers +\n 1;\n coalescingContextPropertiesMap.setProps(scope, {\n numCoalescingSubscribers,\n });\n }\n\n // Checks if anybody else is already coalescing atm\n function isCoalescing(scope: Record<string, unknown>): boolean {\n return (\n coalescingContextPropertiesMap.getProps(scope).numCoalescingSubscribers >\n 0\n );\n }\n}\n","import {\n MonoTypeOperatorFunction,\n Observable,\n Observer,\n Subscriber,\n Subscription,\n Unsubscribable,\n} from 'rxjs';\nimport { coalescingManager } from './coalescingManager';\n\n/**\n * @description\n * Limits the number of synchronous emitted a value from the source Observable to\n * one emitted value per\n * durationSelector e.g. [`AnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame), then repeats\n * this process for every tick of the browsers event loop.\n *\n * The coalesce operator is based on the [throttle](https://rxjs-dev.firebaseapp.com/api/operators/throttle) operator.\n * In addition to that is provides emitted values for the trailing end only, as well as maintaining a context to scope\n * coalescing.\n *\n * @param {function(value: T): Observable} durationSelector - A function\n * that receives a value from the source Observable, for computing the silencing\n * duration for each source value, returned as an Observable or a Promise.\n * It defaults to `requestAnimationFrame` as durationSelector.\n * @param scope\n * Defaults to `{ leading: false, trailing: true }`. The default scoping is per subscriber.\n * @return {Observable<T>} An Observable that performs the coalesce operation to\n * limit the rate of emissions from the source.\n *\n * @usageNotes\n * Emit clicks at a rate of at most one click per second\n * ```typescript\n * import { interval, fromEvent } from 'rxjs';\n * import { coalesceWith } from '@rx-angular/cdk/coalescing';\n *\n * const setTimeoutDurationSelector = interval(500);\n * const clicks = fromEvent(document, 'click');\n * const result = clicks.pipe(coalesceWith(setTimeoutDurationSelector));\n * result.subscribe(x => console.log(x));\n * ```\n */\nexport function coalesceWith<T>(\n durationSelector: Observable<unknown>,\n scope?: Record<string, unknown>\n): MonoTypeOperatorFunction<T> {\n const _scope = scope || {};\n\n return (source) => {\n return new Observable<T>((observer) => {\n const rootSubscription = new Subscription();\n rootSubscription.add(\n source.subscribe(createInnerObserver(observer, rootSubscription))\n );\n return rootSubscription;\n });\n\n function createInnerObserver(\n outerObserver: Subscriber<T>,\n rootSubscription: Subscription\n ): Observer<T> {\n let actionSubscription: Unsubscribable;\n let latestValue: T | undefined;\n\n const tryEmitLatestValue = () => {\n if (actionSubscription) {\n // We only decrement the number if it is greater than 0 (isCoalescing)\n coalescingManager.remove(_scope);\n if (!coalescingManager.isCoalescing(_scope)) {\n outerObserver.next(latestValue);\n }\n }\n };\n return {\n complete: () => {\n tryEmitLatestValue();\n outerObserver.complete();\n },\n error: (error) => outerObserver.error(error),\n next: (value) => {\n latestValue = value;\n if (!actionSubscription) {\n coalescingManager.add(_scope);\n actionSubscription = durationSelector.subscribe({\n error: (error) => outerObserver.error(error),\n next: () => {\n tryEmitLatestValue();\n actionSubscription?.unsubscribe();\n actionSubscription = undefined;\n },\n complete: () => {\n tryEmitLatestValue();\n actionSubscription = undefined;\n },\n });\n rootSubscription.add(\n new Subscription(() => {\n tryEmitLatestValue();\n actionSubscription?.unsubscribe();\n actionSubscription = undefined;\n })\n );\n }\n },\n };\n }\n };\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAUa,MAAA,iBAAiB,GAAsB,qBAAqB;AAGzE,SAAS,MAAM,CAAI,GAAM,EAAE,QAAkB,EAAA;AAC3C,IAAA,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,IAAI;AAC9B;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BK;AACL,SAAS,uBAAuB,CAG9B,WAAwB,EAAA;AAExB,IAAA,MAAM,WAAW,GAAG,IAAI,OAAO,EAAQ;IAEvC,OAAO;AACL,QAAA,QAAQ,EAAE,aAAa;AACvB,QAAA,QAAQ,EAAE,aAAa;KACxB;IAED,SAAS,aAAa,CAAC,GAAM,EAAA;AAC3B,QAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC;QACjC,MAAM,iBAAiB,GAAkB,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC;AAC7D,QAAA,IAAI,UAAa;AAEjB,QAAA,IAAI,iBAAiB,KAAK,SAAS,EAAE;YACnC,UAAU,GAAG,iBAAsB;;aAC9B;YACL,UAAU,GAAG,EAAO;AAEnB,YAAA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