Skip to content

About

Basic Signal Implementation | Event-based | TypeScript

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Signal (Event-based)

Very small, basically Fundamental Signal Implementation NOT compliant with tc39 Signals proposal since it uses events to notify subscribers rather than closures, but looks similar. The subscription is (somewhat) complaint with Observabe Proposal.

Explanation

This is a kind of signal that provides get method - retrieves current value, set method - updates current and invokes registered callbacks (sometimes said as "notifies subscribers"), subscribe method - registers a callback (that will be invoked when current value is changed).

Event-based Signal can be interpreted as Excplicit comparing to Closure-based Signal.

Example

console.log("1. Create")
const firstName = new EventSignal.State("John")
const lastName = new EventSignal.State("Smith")
const showFullName = new EventSignal.State(true)

const displayName = new EventSignal.Computed(
  () => showFullName.get() ? `${firstName.get()} ${lastName.get()}` : firstName.get(),
  [showFullName, firstName, lastName, firstName]
)

new EventSignal.Computed(() => console.log("My name is", displayName.get()), [displayName])

console.log("2. Set showFullName: false ")
showFullName.set(false)

console.log("3. Change lastName")
lastName.set("Legend")

console.log("4. Set showFullName: true")
showFullName.set(true)

console.log("5. Change lastName while showFullName: true")
lastName.set("Who")

lastName.subscribe(console.log) // This is what makes it explicit.

About

Basic Signal Implementation | Event-based | TypeScript

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages