You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
9.5 KiB
1 lines
9.5 KiB
{"version":3,"file":"bufferToggle.js","sourceRoot":"","sources":["../../src/operators/bufferToggle.ts"],"names":[],"mappings":";;;;;;AAGA,6BAA6B,iBAAiB,CAAC,CAAA;AAC/C,kCAAkC,2BAA2B,CAAC,CAAA;AAC9D,gCAAgC,oBAAoB,CAAC,CAAA;AAIrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,sBACE,QAAkC,EAClC,eAAyD;IAEzD,MAAM,CAAC,sCAAsC,MAAqB;QAChE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,oBAAoB,CAAO,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC;IAChF,CAAC,CAAC;AACJ,CAAC;AAPe,oBAAY,eAO3B,CAAA;AAED;IAEE,8BAAoB,QAAkC,EAClC,eAAyD;QADzD,aAAQ,GAAR,QAAQ,CAA0B;QAClC,oBAAe,GAAf,eAAe,CAA0C;IAC7E,CAAC;IAED,mCAAI,GAAJ,UAAK,UAA2B,EAAE,MAAW;QAC3C,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,sBAAsB,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC;IACvG,CAAC;IACH,2BAAC;AAAD,CAAC,AATD,IASC;AAOD;;;;GAIG;AACH;IAA2C,0CAAqB;IAG9D,gCAAY,WAA4B,EACpB,QAAkC,EAClC,eAAgE;QAClF,kBAAM,WAAW,CAAC,CAAC;QAFD,aAAQ,GAAR,QAAQ,CAA0B;QAClC,oBAAe,GAAf,eAAe,CAAiD;QAJ5E,aAAQ,GAA4B,EAAE,CAAC;QAM7C,IAAI,CAAC,GAAG,CAAC,qCAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC9C,CAAC;IAES,sCAAK,GAAf,U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ACvC,IAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAE/B,IAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,IAAM,YAAY,GAAG,IAAI,2BAAY,EAAE,CAAC;QACxC,IAAM,OAAO,GAAG,EAAE,cAAM,EAAE,0BAAY,EAAE,CAAC;QACzC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAEvB,IAAM,iBAAiB,GAAG,qCAAiB,CAAC,IAAI,EAAE,eAAe,EAAO,OAAO,CAAC,CAAC;QAEjF,EAAE,CAAC,CAAC,CAAC,iBAAiB,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;YACnD,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAC5B,CAAC;QAAC,IAAI,CAAC,CAAC;YACC,iBAAkB,CAAC,OAAO,GAAG,OAAO,CAAC;YAE5C,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;YAC5B,YAAY,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IACH,6BAAC;AAAD,CAAC,AAhGD,CAA2C,iCAAe,GAgGzD","sourcesContent":["import { Operator } from '../Operator';\nimport { Subscriber } from '../Subscriber';\nimport { Observable, SubscribableOrPromise } from '../Observable';\nimport { Subscription } from '../Subscription';\nimport { subscribeToResult } from '../util/subscribeToResult';\nimport { OuterSubscriber } from '../OuterSubscriber';\nimport { InnerSubscriber } from '../InnerSubscriber';\nimport { OperatorFunction } from '../interfaces';\n\n/**\n * Buffers the source Observable values starting from an emission from\n * `openings` and ending when the output of `closingSelector` emits.\n *\n * <span class=\"informal\">Collects values from the past as an array. Starts\n * collecting only when `opening` emits, and calls the `closingSelector`\n * function to get an Observable that tells when to close the buffer.</span>\n *\n * <img src=\"./img/bufferToggle.png\" width=\"100%\">\n *\n * Buffers values from the source by opening the buffer via signals from an\n * Observable provided to `openings`, and closing and sending the buffers when\n * a Subscribable or Promise returned by the `closingSelector` function emits.\n *\n * @example <caption>Every other second, emit the click events from the next 500ms</caption>\n * var clicks = Rx.Observable.fromEvent(document, 'click');\n * var openings = Rx.Observable.interval(1000);\n * var buffered = clicks.bufferToggle(openings, i =>\n * i % 2 ? Rx.Observable.interval(500) : Rx.Observable.empty()\n * );\n * buffered.subscribe(x => console.log(x));\n *\n * @see {@link buffer}\n * @see {@link bufferCount}\n * @see {@link bufferTime}\n * @see {@link bufferWhen}\n * @see {@link windowToggle}\n *\n * @param {SubscribableOrPromise<O>} openings A Subscribable or Promise of notifications to start new\n * buffers.\n * @param {function(value: O): SubscribableOrPromise} closingSelector A function that takes\n * the value emitted by the `openings` observable and returns a Subscribable or Promise,\n * which, when it emits, signals that the associated buffer should be emitted\n * and cleared.\n * @return {Observable<T[]>} An observable of arrays of buffered values.\n * @method bufferToggle\n * @owner Observable\n */\nexport function bufferToggle<T, O>(\n openings: SubscribableOrPromise<O>,\n closingSelector: (value: O) => SubscribableOrPromise<any>\n): OperatorFunction<T, T[]> {\n return function bufferToggleOperatorFunction(source: Observable<T>) {\n return source.lift(new BufferToggleOperator<T, O>(openings, closingSelector));\n };\n}\n\nclass BufferToggleOperator<T, O> implements Operator<T, T[]> {\n\n constructor(private openings: SubscribableOrPromise<O>,\n private closingSelector: (value: O) => SubscribableOrPromise<any>) {\n }\n\n call(subscriber: Subscriber<T[]>, source: any): any {\n return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));\n }\n}\n\ninterface BufferContext<T> {\n buffer: T[];\n subscription: Subscription;\n}\n\n/**\n * We need this JSDoc comment for affecting ESDoc.\n * @ignore\n * @extends {Ignored}\n */\nclass BufferToggleSubscriber<T, O> extends OuterSubscriber<T, O> {\n private contexts: Array<BufferContext<T>> = [];\n\n constructor(destination: Subscriber<T[]>,\n private openings: SubscribableOrPromise<O>,\n private closingSelector: (value: O) => SubscribableOrPromise<any> | void) {\n super(destination);\n this.add(subscribeToResult(this, openings));\n }\n\n protected _next(value: T): void {\n const contexts = this.contexts;\n const len = contexts.length;\n for (let i = 0; i < len; i++) {\n contexts[i].buffer.push(value);\n }\n }\n\n protected _error(err: any): void {\n const contexts = this.contexts;\n while (contexts.length > 0) {\n const context = contexts.shift();\n context.subscription.unsubscribe();\n context.buffer = null;\n context.subscription = null;\n }\n this.contexts = null;\n super._error(err);\n }\n\n protected _complete(): void {\n const contexts = this.contexts;\n while (contexts.length > 0) {\n const context = contexts.shift();\n this.destination.next(context.buffer);\n context.subscription.unsubscribe();\n context.buffer = null;\n context.subscription = null;\n }\n this.contexts = null;\n super._complete();\n }\n\n notifyNext(outerValue: any, innerValue: O,\n outerIndex: number, innerIndex: number,\n innerSub: InnerSubscriber<T, O>): void {\n outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);\n }\n\n notifyComplete(innerSub: InnerSubscriber<T, O>): void {\n this.closeBuffer((<any> innerSub).context);\n }\n\n private openBuffer(value: O): void {\n try {\n const closingSelector = this.closingSelector;\n const closingNotifier = closingSelector.call(this, value);\n if (closingNotifier) {\n this.trySubscribe(closingNotifier);\n }\n } catch (err) {\n this._error(err);\n }\n }\n\n private closeBuffer(context: BufferContext<T>): void {\n const contexts = this.contexts;\n\n if (contexts && context) {\n const { buffer, subscription } = context;\n this.destination.next(buffer);\n contexts.splice(contexts.indexOf(context), 1);\n this.remove(subscription);\n subscription.unsubscribe();\n }\n }\n\n private trySubscribe(closingNotifier: any): void {\n const contexts = this.contexts;\n\n const buffer: Array<T> = [];\n const subscription = new Subscription();\n const context = { buffer, subscription };\n contexts.push(context);\n\n const innerSubscription = subscribeToResult(this, closingNotifier, <any>context);\n\n if (!innerSubscription || innerSubscription.closed) {\n this.closeBuffer(context);\n } else {\n (<any> innerSubscription).context = context;\n\n this.add(innerSubscription);\n subscription.add(innerSubscription);\n }\n }\n}\n"]} |