Hyperlinkv0.8.0-beta.28

PubSub

PubSub.publishUnsafeconsteffect/PubSub.ts:969
<A>(value: A): (self: PubSub<A>) => boolean
<A>(self: PubSub<A>, value: A): boolean

Attempts to publish a message synchronously without applying the PubSub strategy's effectful surplus handling.

When to use

Use when you need a non-blocking synchronous publish attempt where false is an acceptable result when the message cannot be accepted immediately.

Details

Returns false if the PubSub is shut down or the message cannot be accepted immediately, for example when a bounded PubSub is full. Prefer publish when backpressure or sliding behavior should be honored.

Example (Publishing without suspending)

import { PubSub } from "effect"

declare const pubsub: PubSub.PubSub<string>

// Unsafe synchronous publish (non-blocking)
const published = PubSub.publishUnsafe(pubsub, "Hello")
if (published) {
  console.log("Message published successfully")
} else {
  console.log("Message dropped (PubSub full or shutdown)")
}

// Useful for scenarios where you don't want to suspend
const messages = ["msg1", "msg2", "msg3"]
const publishedCount =
  messages.filter((msg) => PubSub.publishUnsafe(pubsub, msg)).length
console.log(`Published ${publishedCount} out of ${messages.length} messages`)
publishingpublish
Source effect/PubSub.ts:96911 lines
export const publishUnsafe: {
  <A>(value: A): (self: PubSub<A>) => boolean
  <A>(self: PubSub<A>, value: A): boolean
} = dual(2, <A>(self: PubSub<A>, value: A): boolean => {
  if (self.shutdownFlag.current) return false
  if (self.pubsub.publish(value)) {
    self.strategy.completeSubscribersUnsafe(self.pubsub, self.subscribers)
    return true
  }
  return false
})
Referenced by 1 symbols