Module

Yoga.Om.WorkerBees.SharedState

Package
purescript-yoga-om-workerbees
Repository
rowtype-yoga/purescript-yoga-om-workerbees

#SharedState Source

newtype SharedState a

Buffer layout:

  • Int32 slot 0 (bytes 0..3): mutex lock (0 = unlocked, 1 = locked)
  • Int32 slot 1 (bytes 4..7): data length in bytes
  • Bytes 8..N: UTF-8 encoded JSON string A shared mutable state that can be accessed from multiple worker threads. Values are JSON-serialized into a SharedArrayBuffer with a spinlock mutex.

#new Source

new :: forall a. WriteForeign a => Int -> a -> Effect (SharedState a)

Create a new SharedState with an initial value. maxBytes is the maximum size for the serialized JSON data.

#read Source

read :: forall a. ReadForeign a => SharedState a -> Effect a

Atomically read the current state.

#write Source

write :: forall a. WriteForeign a => SharedState a -> a -> Effect Unit

Atomically write a new state value.

#modify Source

modify :: forall a. ReadForeign a => WriteForeign a => SharedState a -> (a -> a) -> Effect a

Atomically modify the state. Returns the new value.

#toSendable Source

toSendable :: forall a. SharedState a -> SendWrapper (SharedState a)

Wrap for worker transfer. The underlying SharedArrayBuffer is shared.

#fromSendable Source

fromSendable :: forall a. SendWrapper (SharedState a) -> Effect (SharedState a)

Unwrap on the worker side. Reconstructs Int32Array views from the shared buffer.