Skip to content

Events

Communicating between systems and modules can be achieved via Events.

There are two ways to use events: sending events with an EventWriter or receiving events with an EventReader.

Event payloads are defined by extending the Event class.

ts
import { Event } from '@vaguevoid/fiasco'

class MyEvent extends Event {
  foo!: string
}

All events must be serializable to raw bytes (ArrayBuffer) - the Event class defines some generic methods for serializing and deserializing the instance data to bytes. It uses a naive approach of JSON.stringify and then converting the string to bytes - to have a more optimized serialization and deserialization methods - you may override those methods.

Example Overriding Serialization

ts
import { Event } from '@vaguevoid/fiasco'

class MyEvent extends Event {
  ids!: [bigint, bigint]

  serialize(): ArrayBuffer {
    return new BigInt64Array(this.ids).buffer
  }

  static deserialize(bytes: ArrayBuffer) {
    const arr = new BigInt64Array(bytes)
    const ev = new MyEvent()
    ev.ids = [arr.at(0)!, arr.at(1)!]
    return ev
  }
}