Skip to content

PubSub

15 exports Added in v2.0.0 Source

Constructors

bounded

Added in v2.0.0 Source

Creates a bounded PubSub with the back pressure strategy. The PubSub will retain messages until they have been taken by all subscribers, applying back pressure to publishers if the PubSub is at capacity.

For best performance use capacities that are powers of two.

Signature

declare const bounded: <A>(
  capacity:
    | number
    | {
        readonly capacity: number;
        readonly replay?: number;
      },
) => Effect.Effect<PubSub<A>>;

dropping

Added in v2.0.0 Source

Creates a bounded PubSub with the dropping strategy. The PubSub will drop new messages if the PubSub is at capacity.

For best performance use capacities that are powers of two.

Signature

declare const dropping: <A>(
  capacity:
    | number
    | {
        readonly capacity: number;
        readonly replay?: number;
      },
) => Effect.Effect<PubSub<A>>;

sliding

Added in v2.0.0 Source

Creates a bounded PubSub with the sliding strategy. The PubSub will add new messages and drop old messages if the PubSub is at capacity.

For best performance use capacities that are powers of two.

Signature

declare const sliding: <A>(
  capacity:
    | number
    | {
        readonly capacity: number;
        readonly replay?: number;
      },
) => Effect.Effect<PubSub<A>>;

unbounded

Added in v2.0.0 Source

Creates an unbounded PubSub.

Signature

declare const unbounded: <A>(options?: { readonly replay?: number }) => Effect.Effect<PubSub<A>>;

Getters

capacity

Added in v2.0.0 Source

Returns the number of elements the queue can hold.

Signature

declare const capacity: <A>(self: PubSub<A>) => number;

isEmpty

Added in v2.0.0 Source

Returns true if the Queue contains zero elements, false otherwise.

Signature

declare const isEmpty: <A>(self: PubSub<A>) => Effect.Effect<boolean>;

isFull

Added in v2.0.0 Source

Returns true if the Queue contains at least one element, false otherwise.

Signature

declare const isFull: <A>(self: PubSub<A>) => Effect.Effect<boolean>;

isShutdown

Added in v2.0.0 Source

Returns true if shutdown has been called, otherwise returns false.

Signature

declare const isShutdown: <A>(self: PubSub<A>) => Effect.Effect<boolean>;

size

Added in v2.0.0 Source

Retrieves the size of the queue, which is equal to the number of elements in the queue. This may be negative if fibers are suspended waiting for elements to be added to the queue.

Signature

declare const size: <A>(self: PubSub<A>) => Effect.Effect<number>;

Models

PubSub interface

Added in v2.0.0 Source

A PubSub<A> is an asynchronous message hub into which publishers can publish messages of type A and subscribers can subscribe to take messages of type A.

Signature

interface PubSub<in out A> extends Enqueue<A>, Pipeable {
  readonly subscribe: Effect<Dequeue<A>, never, Scope>;
  publish(value: A): Effect<boolean>;
  publishAll(elements: Iterable<A>): Effect<boolean>;
}

Utils

Waits until the queue is shutdown. The Effect returned by this method will not resume until the queue has been shutdown. If the queue is already shutdown, the Effect will resume right away.

Signature

declare const awaitShutdown: <A>(self: PubSub<A>) => Effect.Effect<void>;

publish

Added in v2.0.0 Source

Publishes a message to the PubSub, returning whether the message was published to the PubSub.

Signature

declare const publish: {
  <A>(value: A): (self: PubSub<A>) => Effect<boolean>;
  <A>(self: PubSub<A>, value: A): Effect<boolean>;
};

publishAll

Added in v2.0.0 Source

Publishes all of the specified messages to the PubSub, returning whether they were published to the PubSub.

Signature

declare const publishAll: {
  <A>(elements: Iterable<A>): (self: PubSub<A>) => Effect<boolean>;
  <A>(self: PubSub<A>, elements: Iterable<A>): Effect<boolean>;
};

shutdown

Added in v2.0.0 Source

Interrupts any fibers that are suspended on offer or take. Future calls to offer* and take* will be interrupted immediately.

Signature

declare const shutdown: <A>(self: PubSub<A>) => Effect.Effect<void>;

subscribe

Added in v2.0.0 Source

Subscribes to receive messages from the PubSub. The resulting subscription can be evaluated multiple times within the scope to take a message from the PubSub each time.

Signature

declare const subscribe: <A>(
  self: PubSub<A>,
) => Effect.Effect<Queue.Dequeue<A>, never, Scope.Scope>;