Building Web Applications with Signals at Grammarly

Rate this content
Bookmark

Signals have recently become a popular concept. Their ideas are based on the functional reactive programming (FRP) approach, which has been leveraged in the industry for a while. I would like to share how someone can build an architecture based on signals and React and how we have used FRP at Grammarly for quite some time. The talk aims to showcase the foundational principles of observables and how to structure applications based on FRP ideas.

This talk has been presented at React Day Berlin 2024, check out the latest edition of this React Conference.

FAQ

The main challenge with using signals is testing, particularly integration testing, to ensure proper application of functions like diffing and debouncing. Debugging streams can also be complex due to dynamic changes in state over time.

In Grammarly's web applications, signals are used as a concept of reactivity to manage changes in the application's state, updating the UI or executing business logic in response to changes in text input by the user.

Grammarly uses signals in its editor to handle text changes dynamically. Signals help manage features such as providing real-time suggestions, calculating text statistics, and supporting multi-device editing by tracking text changes and updating related features accordingly.

Grammarly uses signals to efficiently manage the text suggestions feature by calculating text diffs instead of sending the full text, using techniques like throttling to optimize performance and reduce payload size for long documents.

Marble tests help in testing signals by providing a way to define and control the execution time of events in a stream, allowing developers to simulate and verify the behavior of signals over time with readable syntax.

There are various resources available, including community proposals for standardizing signals across frameworks, which provide insights into the potential integration of signals into JavaScript's core. These resources help developers explore the concept and application of signals in web development.

Yes, Grammarly's signal-based system is framework agnostic and can be integrated with other frameworks like React, using hooks to work with changing values and providing flexibility in application development.

GrammarlyFocal is an open-source library developed by Grammarly that provides tools for reactive state management using signals. It wraps RxJS to offer a simplified interface for working with signals and streams.

The benefits of using signals in web applications include decoupling the state from the view layer, facilitating declarative programming, and enabling the use of functional reactive programming techniques to handle dynamic data changes efficiently.

Lesh Levzhinsky is a web developer at Grammarly, working with several Grammarly clients, primarily focusing on web applications. He is also a maintainer of Grammarly's open-source library for reactive state management, GrammarlyFocal.

Oleksii Levzhynskyi
Oleksii Levzhynskyi
29 min
13 Dec, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Hello, welcome, everyone. Today, I'm going to talk about building web applications with signals in Grammarly. Signals are like values that can change over time and are used in Grammarly's editor for real-time updates. Signals can be implemented with React and are leveraged for state management and business logic. They enable functional reactive and declarative programming, allowing specific actions to be defined. Testing signals can be done using classical unit tests or marble tests. Signals are a perfect fit for Grammarly's domain and can be easily integrated with various libraries and frameworks. Handling cleanup and unsubscribing to signals can be done through the dispose method and React lifecycle. The main challenges in signal-driven systems are testing and debugging streams. Marble tests provide control over signal emission and combined states can be calculated using the view function. Grammarly is considering migrating to the signals proposal in the future. Thank you, Alexey!

1. Introduction to Signals in Grammarly

Short description:

Hello, welcome, everyone. Today, I'm going to talk about building web applications with signals in Grammarly. I joined Grammarly many years ago and now work with Grammarly clients, primarily web. I also maintain our Grammarly open source library for reactive state management. At Grammarly, we use signals to build our applications, and I encourage everyone to consider using similar approaches. Grammarly is a writing assistant that provides refinements and suggestions. We have a Grammarly editor with various features, including statistics, multi-device editing, and Grammarly checks. These features update and recalculate in real-time as the text changes.

Hello, welcome, everyone. My name is Lesh Levzhinsky, and today I'm going to talk about building web applications with signals in Grammarly. I was already introduced, but anyway, I joined Grammarly many years ago. I joined like a web developer, and now I'm working with a couple of Grammarly clients, primarily web. Also, I'm a maintainer for our Grammarly open source library that's about reactive state management. It's Grammarly for Kal, and also I'm a member of FW Days conference. It's the biggest conference about JavaScript in Ukraine.

Well, about my talk. Today, I would like to share our experience at Grammarly, how we are using signals to build our applications, as well as I would like to maybe encourage everyone to think about using similar approaches, if you are not yet.

Well, and before we dig into details, I have a couple of questions. First of all, have you ever used it as signals? Could you raise a hand? Oh, nice. What about FRP, functional reactive programming? Maybe something? Oh, it's going to be easy talk. Nice. But anyway, I'm not going to start with the technical definition. I would like to start with the use case.

Grammarly is a writing assistant. At Grammarly, user may type a text and we will provide a refinement for this text. We'll provide suggestions so you can go ahead, update it, rephrase, and so on. On this slide, there are a couple of typos, and red underlines it's something from Grammarly. Well, also, we have a Grammarly editor. It's like a typical editor where users can go ahead and create documents, as well as to get the full power of Grammarly to refine, do a revision of your text. Well, in this editor, we have a couple of features. First of all, yeah, maybe not first of all, but also move to the primary function like writing text. We would like to show some statistics. Also, we definitely need to send it somewhere to the backend for permanent storage. We would like our users to be able to edit it from different devices, maybe. And for sure, we need to provide a Grammarly check, so we need to send this text to maybe another service. As well as there could be multiple of different features that needs to be updated, recalculated as soon as text changes. Well, it sounds quite a lot for just one key press.

2. Understanding Signals and Their Usage

Short description:

Signals are like values that can change over time. They are a concept of reactivity and can be used to update UI or execute business logic. Many libraries and frameworks, including Angular and ArigGS, incorporate signals. Grammarly has its own library called GrammarlyFocal. Signals are leveraged in the Grammarly editor, and we can implement them with React.

Well, it sounds quite a lot for just one key press. And here signals may be useful. So, in this case, what might be a signal? The signal might be actually text. It's like a value, static values, that we can work with at any time, as well as the fact that it changes over time. And maybe a bit of what it is about.

It's a classical diagram. Nothing special here. On the left side, we have just a value. Okay, we can, what is the value, we can access it at any time synchronously. On the opposite side, we have a sort of observable, just a stream of events, like without the sort of static value, but rather you need to listen for it. And the signal is something in between, first of all. Usually, here it's like a bucket. There's a value that you can access at any time, as well as you can subscribe to it, so it behaves in both ways.

Well, this is another presentation. So, signals in general, it's a concept of reactivity. I'm not going to dig into reactivity, but, yeah, in general, we need to react on some change in state and update in a reactive way our UI, or maybe execute some business logic. Well, here is just an observable part of our signal, like we're applying to the stream of events, we're applying the bonds function in this case, and getting another stream. Well, what about the usage? There are quite a lot of libraries, frameworks that contain this concept of signals. For sure, there are signals in Angular, as well as Angular uses ArigGS.

Probably, the term signal become popular after solid.js introduced it. And for sure, there is a standalone library, ArigGS. It's not only about the signals, but there is a concept called behavior subjects that may contain the value that can be accessed synchronously. There is a standard proposal, for sure. At Grammarly, we have our very own library that was developed quite a long time ago. It provides a code called GrammarlyFocal, and it also basically provides or shares the same concept of signals, and there are a lot more examples.

Well, enough with sort of introduction. Let's look at how we are leveraging the idea of signals in our Grammarly editor. It's like the same screen, nothing fancy. I'm not going to dig into details, but let's look at how it might be, in theory, implemented with React. This is very simple, just for the sake of the demo.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Understanding React’s Fiber Architecture
React Advanced 2022React Advanced 2022
29 min
Understanding React’s Fiber Architecture
Top Content
This Talk explores React's internal jargon, specifically fiber, which is an internal unit of work for rendering and committing. Fibers facilitate efficient updates to elements and play a crucial role in the reconciliation process. The work loop, complete work, and commit phase are essential steps in the rendering process. Understanding React's internals can help with optimizing code and pull request reviews. React 18 introduces the work loop sync and async functions for concurrent features and prioritization. Fiber brings benefits like async rendering and the ability to discard work-in-progress trees, improving user experience.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
The Eternal Sunshine of the Zero Build Pipeline
React Finland 2021React Finland 2021
36 min
The Eternal Sunshine of the Zero Build Pipeline
For many years, we have migrated all our devtools to Node.js for the sake of simplicity: a common language (JS/TS), a large ecosystem (NPM), and a powerful engine. In the meantime, we moved a lot of computation tasks to the client-side thanks to PWA and JavaScript Hegemony.
So we made Webapps for years, developing with awesome reactive frameworks and bundling a lot of dependencies. We progressively moved from our simplicity to complex apps toolchains. We've become the new Java-like ecosystem. It sucks.
It's 2021, we've got a lot of new technologies to sustain our Users eXperience. It's time to have a break and rethink our tools rather than going faster and faster in the same direction. It's time to redesign the Developer eXperience. It's time for a bundle-free dev environment. It's time to embrace a new frontend building philosophy, still with our lovely JavaScript.
Introducing Snowpack, Vite, Astro, and other Bare Modules tools concepts!
Composition vs Configuration: How to Build Flexible, Resilient and Future-proof Components
React Summit 2022React Summit 2022
17 min
Composition vs Configuration: How to Build Flexible, Resilient and Future-proof Components
Top Content
Today's Talk discusses building flexible, resilient, and future-proof React components using composition and configuration approaches. The composition approach allows for flexibility without excessive conditional logic by using multiple components and passing props. The context API can be used for variant styling, allowing for appropriate styling and class specification. Adding variants and icons is made easy by consuming the variant context. The composition and configuration approaches can be combined for the best of both worlds.
Remix Architecture Patterns
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Remix Architecture Patterns
Top Content
This Talk introduces the Remix architecture patterns for web applications, with over 50% of participants using Remix professionally. The migration from single page applications to Remix involves step-by-step refactoring and offers flexibility in deployment options. Scalability can be achieved by distributing the database layer and implementing application caching. The backend for frontend pattern simplifies data fetching, and Remix provides real-time capabilities for collaborative features through WebSocket servers and Server-SendEvents.

Workshops on related topic

AI on Demand: Serverless AI
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Top Content
Featured WorkshopFree
Nathan Disidore
Nathan Disidore
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
High-performance Next.js
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Workshop
Michele Riva
Michele Riva
Next.js is a compelling framework that makes many tasks effortless by providing many out-of-the-box solutions. But as soon as our app needs to scale, it is essential to maintain high performance without compromising maintenance and server costs. In this workshop, we will see how to analyze Next.js performances, resources usage, how to scale it, and how to make the right decisions while writing the application architecture.