Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
203: Building UIs with Laminar
Laminar is a bit of a departure from the way you might be used to thinking about the DOM. Instead of manually selecting elements and updating their inner text, we treat the UI as a direct reflection of state. I like to think of it as "wiring" the data to the view; once the connection is made, the data flows automatically.
To get a feel for this, let's build a simple "Focus Timer"—a basic countdown that lets you start and stop a timer. It sounds trivial, but it perfectly illustrates how Laminar handles state via Var and Signal.
Setting up the state
In Laminar, the source of truth is usually a Var. It's essentially a reactive variable. I'll start by defining the seconds remaining and whether the timer is currently running. I'm using a Var for both because these are values we intend to change based on user interaction.
import laminar._ import org.scalajs.dom val secondsVar = Var(25 * 60) // 25 minutes val isRunningVar = Var(false)Sketching the timer interface
Now for the UI. Laminar uses a DSL that looks very similar to HTML. One thing I love here is that we can embed
Signalsdirectly into the tags. SincesecondsVaris aVar, calling.signalon it gives us a stream of values that the UI can listen to.val timerApp = div( h1("Focus Timer"), div( span( // We map the total seconds into a readable MM:SS format secondsVar.signal.map { s => val mins = s / 60 val secs = s % 60 f"${mins}%02d:${secs}%02d" } ), button( // This is a simple observer that toggles our boolean Var onClick --> { isRunningVar.update(!isRunningVar.now) }, isRunningVar.signal.map(running => if (running) "Stop" else "Start") ) ) )The "Static UI" trap
Here is where I almost tripped up when I first started with Laminar. I tried to implement the actual countdown logic like this:
// WRONG: This only runs once when the app starts! if (isRunningVar.now) { dom.window.setInterval(() => { secondsVar.update(secondsVar.now - 1) }, 1000) }The problem?
isRunningVar.nowis just a snapshot of the value at the moment that line of code executes. It isn't "watching" the variable. My timer never started because theifstatement executed once during initialization and then disappeared into the void. To fix this, I need to react to the change of the state, not the current value.Wiring the clock tick
To actually make the timer move, I'll use a
Signalto trigger the side effect of starting and stopping the interval. I'll store the interval ID so I can clear it when the user hits "Stop".var intervalId: Int = 0 // We observe the isRunningVar signal isRunningVar.signal.foreach { running => if (running) { intervalId = dom.window.setInterval(() => { if (secondsVar.now > 0) { secondsVar.update(secondsVar.now - 1) } }, 1000) } else { dom.window.clearInterval(intervalId) } } // Finally, mount it to the body dom.body.appendChild(timerApp.toDomNode)Notice how the UI code remains completely separate from the timing logic. The button updates
isRunningVar, the observer handles thesetInterval, and thespansimply reflects whatever is insidesecondsVar. It's a clean, unidirectional loop.
📋 Practical Task
Exercise: Adding a "Reset" Feature to the Focus Timer
Currently, our Focus Timer can start and stop, but if the timer reaches zero or if the user wants to start over, they have to refresh the page. Your task is to extend the existing implementation.
- Add a "Reset" button to the UI.
- When the Reset button is clicked, it should:
- Set
isRunningVartofalse(stopping the timer). - Set
secondsVarback to its original value of 1500 (25 minutes).
- Set
- Ensure the "Start/Stop" button text updates correctly when the Reset button is pressed.
There are no comments for now.