#pragma once

#include <jsi/jsi.h>

#include <cstdint>
#include <utility>

namespace worklets {

/**
 * Identifies the owner of a job, so that a queue shared between several owners
 * can cancel the jobs of one of them without touching the jobs of the others.
 *
 * Worklet Runtimes send their RuntimeId as a token.
 */
using AbortToken = uint64_t;

inline constexpr AbortToken defaultToken{0};

class AsyncQueue : public facebook::jsi::NativeState {
 public:
  ~AsyncQueue() override = default;

  virtual void push(std::function<void()> &&job) = 0;

  /**
   * Pushes a job on behalf of the owner the token belongs to. The default
   * implementation discards the token.
   */
  virtual void push(std::function<void()> &&job, AbortToken /* abortToken */) {
    push(std::move(job));
  }

  /**
   * Drops the jobs that are still pending, destroying them on the calling
   * thread. A queue shared between several owners drops only the jobs pushed
   * with this token. The default implementation keeps every job.
   */
  virtual void abortPending(AbortToken /* abortToken */) {}

  /**
   * Drops the pending jobs of the aborted owner, as in `abortPending`, and
   * stops accepting new ones. A queue shared between several owners keeps
   * serving the others. The default implementation does nothing.
   */
  virtual void stop(AbortToken /* abortToken */) {}
};

} // namespace worklets
