Skip to content

#Enhancement:Cleaner State Management #447

Closed
SykoDaedalus wants to merge 1 commit intoarmanbilge:mainfrom
SykoDaedalus:test-branch
Closed

#Enhancement:Cleaner State Management #447
SykoDaedalus wants to merge 1 commit intoarmanbilge:mainfrom
SykoDaedalus:test-branch

Conversation

@SykoDaedalus
Copy link
Copy Markdown

@SykoDaedalus SykoDaedalus commented Mar 20, 2025

Summary

This PR introduces a state helper to simplify SignallingRef creation, reducing boilerplate and improving the ergonomics of Calico’s DSL.

Implementation Details

Added a new extension method in calico/syntax.scala:

extension (io: IO.type)
  def state[A](initial: A): Resource[IO, SignallingRef[IO, A]] =
    SignallingRef[IO].of[A](initial).toResource

Wraps the verbose SignallingRef[IO].of(initial).toResource pattern into a concise, reusable utility.
Integrated into the DSL to streamline state management for reactive components.

Example Usage
Before:

SignallingRef[IO].of("world").toResource.flatMap { name =>
   div(
      label("Your name: "),
      input.withSelf { self =>
        (
          placeholder := "Enter your name here",
          onInput --> (_.foreach(_ => self.value.get.flatMap(name.set)))
        )
      },
      span(" Hello, ", name.map(_.toUpperCase))
    )
}

After (with state):

IO.state("world").flatMap { name =>
    div(
      label("Your name: "),
      input.withSelf { self =>
        (
          placeholder := "Enter your name here",
          onInput --> (_.foreach(_ => self.value.get.flatMap(name.set)))
        )
      },
      span(" Hello, ", name.map(_.toUpperCase))
    )
}

Tested locally in a todoMvc app—builds and runs smoothly.

  • Benefits
  • Improved Readability: Cuts down repetitive code, making the DSL more concise and natural to write.
  • Enhanced Ergonomics: Simplifies state setup, especially useful for Web Components in Calico.

@SykoDaedalus
Copy link
Copy Markdown
Author

Hey @armanbilge @kubukoz please review the PR

@SykoDaedalus SykoDaedalus changed the title #Feature Idea:Cleaner State Management #Enhancement:Cleaner State Management Mar 20, 2025
@kubukoz
Copy link
Copy Markdown
Collaborator

kubukoz commented Mar 20, 2025

I'm not sure this is something we need, it's not that much harder to create a SignallingRef yourself. Conciseness is good, but I'd posit that creating refs isn't what's making Calico apps verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants