<commit_before>/*
 * Copyright (c) 2014-2018 by The Monix Project Developers.
 * See the project homepage at: https://monix.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package monix.reactive.internal.operators

import monix.eval.Task
import monix.execution.Ack
import monix.execution.Ack.Stop
import monix.execution.atomic.Atomic
import scala.util.control.NonFatal
import monix.reactive.Observable.Operator
import monix.reactive.observers.Subscriber

import scala.concurrent.Future
import scala.util.Success

private[reactive] final class EvalOnNextAckOperator[A](cb: (A, Ack) => Task[Unit])
  extends Operator[A,A] {

  def apply(out: Subscriber[A]): Subscriber[A] =
    new Subscriber[A] { self =>
      implicit val scheduler = out.scheduler
      private[this] val isActive = Atomic(true)

      def tryExecute(a: A, ack: Ack): Ack = {
        try { cb(a, ack); ack }
        catch { case ex if NonFatal(ex) => onError(ex); Stop }
      }

      def onNext(elem: A): Future[Ack] = {
        // We are calling out.onNext directly, meaning that in onComplete/onError
        // we don't have to do anything special to ensure that the last `onNext`
        // has been sent (like we are doing in mapTask); we only need to apply
        // back-pressure for the following onNext events
        val f = out.onNext(elem)
        val task = Task.fromFuture(f).flatMap { ack =>
          val r = try cb(elem,ack) catch { case ex if NonFatal(ex) => Task.raiseError(ex) }
          r.map(_ => ack).onErrorHandle { ex => onError(ex); Stop }
        }

        // Execution might be immediate
        val ack = task.runAsync
        ack.value match {
          case Some(Success(sync)) => sync
          case _ => ack
        }
      }

      def onComplete(): Unit = {
        if (isActive.getAndSet(false))
          out.onComplete()<commit_msg>Remove unused function from EvalOnNextAck (#709)<commit_after>/*
 * Copyright (c) 2014-2018 by The Monix Project Developers.
 * See the project homepage at: https://monix.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package monix.reactive.internal.operators

import monix.eval.Task
import monix.execution.Ack
import monix.execution.Ack.Stop
import monix.execution.atomic.Atomic
import scala.util.control.NonFatal
import monix.reactive.Observable.Operator
import monix.reactive.observers.Subscriber

import scala.concurrent.Future
import scala.util.Success

private[reactive] final class EvalOnNextAckOperator[A](cb: (A, Ack) => Task[Unit])
  extends Operator[A,A] {

  def apply(out: Subscriber[A]): Subscriber[A] =
    new Subscriber[A] { self =>
      implicit val scheduler = out.scheduler
      private[this] val isActive = Atomic(true)

      def onNext(elem: A): Future[Ack] = {
        // We are calling out.onNext directly, meaning that in onComplete/onError
        // we don't have to do anything special to ensure that the last `onNext`
        // has been sent (like we are doing in mapTask); we only need to apply
        // back-pressure for the following onNext events
        val f = out.onNext(elem)
        val task = Task.fromFuture(f).flatMap { ack =>
          val r = try cb(elem,ack) catch { case ex if NonFatal(ex) => Task.raiseError(ex) }
          r.map(_ => ack).onErrorHandle { ex => onError(ex); Stop }
        }

        // Execution might be immediate
        val ack = task.runAsync
        ack.value match {
          case Some(Success(sync)) => sync
          case _ => ack
        }
      }

      def onComplete(): Unit = {
        if (isActive.getAndSet(false))
          out.onComplete()
{'fraction_non_alphanumeric': 0.09832863034512698, 'fraction_numerical': 0.005860646841762535, 'mean_word_length': 2.9384615384615387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7155923', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1321, 'n_words': 471}