Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BitFieldEmitter<T>

Type parameters

  • T

    The type of data all events will recive. For more details on the topic of type-safety you should read the guide about usage with unions

Hierarchy

  • BitFieldEmitter

Constructors

Properties

Methods

Constructors

constructor

  • Parameters

    • Default value maxBits: number = 32

      THe max number of bits to check againts.

      The default is 32 bits:

      const emitter = new BitFieldEmitter<void>()
      
      emitter.on(1 << 31, () => console.log('fired'))
      emitter.emit(1 << 31) // fired

      Passing less than the default results in some bits beeing ignored:

      const emitter = new BitFieldEmitter<void>(31)
      
      emitter.on(1 << 32, () => console.log('fired'))
      emitter.emit(1 << 31) // nothing

    Returns BitFieldEmitter

Properties

Private handlers

handlers: Set<BitFieldEventHandler<T>>[]

Array of event handler sets. Initialized in the constructors.

maxBits

maxBits: number

THe max number of bits to check againts.

The default is 32 bits:

const emitter = new BitFieldEmitter<void>()

emitter.on(1 << 31, () => console.log('fired'))
emitter.emit(1 << 31) // fired

Passing less than the default results in some bits beeing ignored:

const emitter = new BitFieldEmitter<void>(31)

emitter.on(1 << 32, () => console.log('fired'))
emitter.emit(1 << 31) // nothing

Methods

emit

  • emit(code: number, data: T, maxBitsHint?: number): this
  • Calls all handlers which have at least 1 bit in common with the given event code.

    const emitter = new BitFieldEmitter<void>()
    
    const handler00 = () => console.log('00')
    const handler01 = () => console.log('01')
    const handler10 = () => console.log('10')
    const handler11 = () => console.log('11')
    
    emitter
     .on(0b00, handler00)
     .on(0b10, handler10)
     .on(0b01, handler01)
     .on(0b11, handler11)
    
    emitter
     .emit(0b00) // nothing gets called
     .emit(0b10) // 10, 11
     .emit(0b01) // 01, 11
     .emit(0b11) // 01, 10, 11

    Parameters

    • code: number

      The code to trigger the listeners with.

    • data: T

      The data to pass to the listeners.

    • Default value maxBitsHint: number = this.maxBits

    Returns this

    The BitFieldEventHandler instance.

on

  • Adds an event listener. The listener will be called every time an event which overlaps with the passed code for at least 1 bit is fired.

    How it works:

    This methods iterates over all the bits of the event code. When a bit of index n equal to 1 is found, this adds it to the nth set of the handlers property.

    const emitter = new BitFieldEmitter<void>()
    
    emitter.on(0b011, () => console.log('fired'))
    
    emitter.emit(0b001) // fired, last bit is the same
    emitter.emit(0b100) // nothing, there's no overlap
    emitter.emit(0b101) // fired, last bit is the same
    emitter.emit(0b110) // fired, second bit is the same

    Parameters

    • code: number

      Bitfield containing encoding all the events to listen to.

    • handler: BitFieldEventHandler<T>

      The function to call when an event fires.

    • Default value maxBitsHint: number = this.maxBits

    Returns this

    The BitFieldEventHandler instance.

once

  • The same as on but calls remove after the first time the event is triggered.

    const emitter = new BitFieldEmitter<void>()
    const handler = () => console.log('fired')
    
    emitter
     .on(1, handler)
     .emit(1) // fired
     .emit(1) // nothing

    Parameters

    • code: number

      The bits to listen for.

    • handler: BitFieldEventHandler<T>

      The function to call when an event fires.

    • Default value maxBitsHint: number = this.maxBits

    Returns this

    The BitFieldEventHandler instance.

remove

  • Removes an event listener from the emitter.

    emitter.on(0, handler)
    emitter.emit(0, ...) // fires
    
    emitter.remove(handler)
    emitter.emit(0,...) // doesn't fire

    Parameters

    • handler: BitFieldEventHandler<T>

      The event listener to remove.

    • Default value maxBitsHint: number = this.maxBits

    Returns this

    The BitFieldEventHandler instance.

removeGroup

  • Takes an iterable and removes the containing handlers from the emitter.

    Nothing happens if the iterable contains duplicates but usually it's a good idea to use a set to prevent trying to remove the same element more than once :

    const group = new Set([handler1, handler2])
    
    emitter.removeGroup(group)

    Parameters

    • handlers: Iterable<BitFieldEventHandler<T>>

      Set with handlers to remove.

    • Default value maxBitsHint: number = this.maxBits

    Returns this

    The BitFieldEventHandler instance.

Generated using TypeDoc