{"ast":null,"code":"import { Subscription } from '../Subscription';\nimport { subscribeToResult } from '../util/subscribeToResult';\nimport { OuterSubscriber } from '../OuterSubscriber';\nexport function bufferToggle(openings, closingSelector) {\n  return function bufferToggleOperatorFunction(source) {\n    return source.lift(new BufferToggleOperator(openings, closingSelector));\n  };\n}\n\nclass BufferToggleOperator {\n  constructor(openings, closingSelector) {\n    this.openings = openings;\n    this.closingSelector = closingSelector;\n  }\n\n  call(subscriber, source) {\n    return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));\n  }\n\n}\n\nclass BufferToggleSubscriber extends OuterSubscriber {\n  constructor(destination, openings, closingSelector) {\n    super(destination);\n    this.openings = openings;\n    this.closingSelector = closingSelector;\n    this.contexts = [];\n    this.add(subscribeToResult(this, openings));\n  }\n\n  _next(value) {\n    const contexts = this.contexts;\n    const len = contexts.length;\n\n    for (let i = 0; i < len; i++) {\n      contexts[i].buffer.push(value);\n    }\n  }\n\n  _error(err) {\n    const contexts = this.contexts;\n\n    while (contexts.length > 0) {\n      const context = contexts.shift();\n      context.subscription.unsubscribe();\n      context.buffer = null;\n      context.subscription = null;\n    }\n\n    this.contexts = null;\n\n    super._error(err);\n  }\n\n  _complete() {\n    const contexts = this.contexts;\n\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\n    this.contexts = null;\n\n    super._complete();\n  }\n\n  notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {\n    outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);\n  }\n\n  notifyComplete(innerSub) {\n    this.closeBuffer(innerSub.context);\n  }\n\n  openBuffer(value) {\n    try {\n      const closingSelector = this.closingSelector;\n      const closingNotifier = closingSelector.call(this, value);\n\n      if (closingNotifier) {\n        this.trySubscribe(closingNotifier);\n      }\n    } catch (err) {\n      this._error(err);\n    }\n  }\n\n  closeBuffer(context) {\n    const contexts = this.contexts;\n\n    if (contexts && context) {\n      const {\n        buffer,\n        subscription\n      } = context;\n      this.destination.next(buffer);\n      contexts.splice(contexts.indexOf(context), 1);\n      this.remove(subscription);\n      subscription.unsubscribe();\n    }\n  }\n\n  trySubscribe(closingNotifier) {\n    const contexts = this.contexts;\n    const buffer = [];\n    const subscription = new Subscription();\n    const context = {\n      buffer,\n      subscription\n    };\n    contexts.push(context);\n    const innerSubscription = subscribeToResult(this, closingNotifier, context);\n\n    if (!innerSubscription || innerSubscription.closed) {\n      this.closeBuffer(context);\n    } else {\n      innerSubscription.context = context;\n      this.add(innerSubscription);\n      subscription.add(innerSubscription);\n    }\n  }\n\n} //# sourceMappingURL=bufferToggle.js.map","map":null,"metadata":{},"sourceType":"module"}