SHIFT + D

Broadcastable

Updated on August 22, 2024Source codeTests

Broadcastable is a class that enriches application state, allowing it to:

  • Broadcast itself
  • Store the BroadcastChannel instance used for broadcasting

Construct a Broadcastable instance

The Broadcastable constructor accepts two parameters:

Parameter
Type
Required
Description
state
any
yes

Passes the state that will be made broadcastable.

state can be any type supported by the structured clone algorithm.

options
Object
no
Options for the Broadcastable instance. See the Broadcastable constructor options section for more guidance.

Broadcastable constructor options

Option
Type
Default
Description
name
String
baleada
Sets the name of the BroadcastChannel instance that Broadcastable uses to send messages.

State and methods

Property
Type
Description
Parameters
Return value
state
Getter/Setter
See return value
N/A

The state passed to the Broadcastable constructor.

If you assign a value directly to state, a setter will pass the new value to setState.

status
Getter
See return value
N/A

The status of the Broadcastable instance.

One of 'ready', 'broadcasting', 'broadcasted', 'errored', or 'stopped'.

channel
Getter
See return value
N/A

The BroadcastChannel instance that Broadcastable uses to send messages.

error
Getter
See return value
N/A

The error that was thrown during the last broadcast operation, when applicable.

setState(newState)
Function
Sets the Broadcastable instance's state
The new state
The Broadcastable instance (this)
broadcast()
Function
Broadcasts the Broadcastable instance's state
N/A
The Broadcastable instance (this)
stop()
Function
Closes the BroadcastChannel instance that Broadcastable uses to send messages
N/A
The Broadcastable instance (this)

Using with Listenable

Broadcastable is designed to work seamlessly with Listenable.

To listen for messages:

  1. Construct your Listenable instance, passing message as the event type.
  2. Use the toMessageListenParams function exported from Baleada Logic to easily format your Broadcastable instance and your onMessage callback into parameters for Listenable's listen method.
import {
  Listenable,
  Broadcastable,
  toMessageListenParams,
} from '@baleada/logic'

const broadcastable = new Broadcastable('hello world'),
      message = new Listenable('message')

message.listen(...toMessageListenParams(
  broadcastable,
  event => {
    console.log(event)
  }
))

Using with TypeScript

The Broadcastable constructor accepts one generic type that you can use to enforce a type for state. By default, TypeScript will infer the type from the initial state you pass to the constructor, but you can specify the type manually if needed.

const withInferredTypes = new Broadcastable(0)
withInferredTypes.state = 'a' // Type error

const withManualTypes = new Broadcastable<string | number>(0)
withManualTypes.state = 'a' // No type error

API design compliance

Spec
Compliance status
Notes
Access functionality by constructing an instance
Constructor accepts two parameters: a piece of state, and an options object.
Constructor does not access the DOM
Takes the form of a JavaScript Object
State and methods are accessible through properties of the object
Methods always return the instance
Stores the constructor's state in a public getter named after the state's type
state
Has a public method you can use to set a new value for that public getter
setState
Has a setter for that getter so you can assign a new value directly
Any other public getters that should be set by you in some cases also have setters and set<Property> methods
none
Has at least one additional getter property that you can't (and shouldn't) set directly
status, channel, error
Has one or more public methods that expose core functionality
broadcast, stop
Either has no side effects or has side effects that can be cleaned up with a stop method
Uses the sentence template to decide what state type should be accepted by a constructor
"State can be broadcasted."
Constructor does not accept options that only customize the behavior of public methods, it allows those options to be passed to the method itself as a parameter.
Named after its core action, proper-cased and suffixed with able

AnimateableCompleteable

Edit doc on GitHub

ON THIS PAGE

BroadcastableConstruct a Broadcastable instanceBroadcastable constructor optionsState and methodsUsing with ListenableUsing with TypeScriptAPI design compliance