npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

svelte-reactive-classes

v1.3.0

Published

`svelte-reactive-classes` is a convenient and lightweight library that allows you to create reactive class instances with encapsulation in your Svelte applications. The library provides an easy way to convert your classes into Svelte stores on the fly, wh

Downloads

4

Readme

svelte-reactive-classes

svelte-reactive-classes is a convenient and lightweight library that allows you to create reactive class instances with encapsulation in your Svelte applications. The library provides an easy way to convert your classes into Svelte stores on the fly, while removing the update and set functions to improve encapsulation. It also supports the CQS ( Command Query Separation) pattern through the @command decorator.

Motivation

As a backend developer with a solid background in Object-Oriented Programming (OOP) and software architecture, I found the way state management is handled in many frontend frameworks to be quite annoying. In OOP, it is essential that only a class should change its state, without exposing its internal data outside. This principle ensures better encapsulation and maintainability.

Svelte provides a reactive store system that can be utilized to improve the state management in frontend applications. This inspired me to develop a solution that leverages Svelte's store values while maintaining the OOP principle of encapsulation.

The implementation presented here allows you to mark specific methods as commands that trigger reactivity. This approach aligns well with the Command Query Separation (CQS) principle, where commands are responsible for causing side effects, such as state changes, and should not return any data.

By combining the power of Svelte's reactive store system with the principles of OOP and CQS, this solution provides an elegant and maintainable approach to state management in frontend applications.

Installation

You can install svelte-reactive-classes using npm:

npm install svelte-reactive-classes

Usage

1. Create your class with methods decorated with @command

Create a TypeScript class and decorate the methods that will trigger reactivity with the @command decorator. These methods are considered "commands" in the CQS pattern.

// Counter.ts
import {command} from "svelte-reactive-classes";

export class Counter {
    count = 0;

    @command
    increment() {
        this.count++;
    }

    @command
    decrement() {
        this.count--;
    }
}

2. Use the reactiveInstanceOf function in your Svelte component

In your Svelte component, import your class and the reactiveInstanceOf function. Use the function to create a reactive instance of your class.

<!-- App.svelte -->
<script lang="ts">
  import { Counter } from "./Counter";
  import { reactiveInstanceOf } from "svelte-reactive-classes";

  const count = reactiveInstanceOf(Counter);
</script>

3. Interact with the reactive class instance in your Svelte markup

You can now use the reactive class instance in your Svelte component's markup. Access properties with the $ syntax, and call methods directly.

<!-- App.svelte -->
<main>
  <h1>{$count.count}</h1>
  <button on:click={() => count.increment()}>Increase</button>
  <button on:click={() => count.decrement()}>Decrease</button>
</main>

API

@command

A decorator to mark methods as "commands" in the CQS pattern. These methods will trigger reactivity when called.

reactiveInstanceOf(ClassConstructor)

Creates a reactive instance of the given class constructor. The instance will have reactive properties and can be used in Svelte components with the $ syntax.

  • ClassConstructor: The class constructor function.
  • Returns: A reactive instance of the class.

Keywords

  • Svelte
  • Reactive store
  • Object-Oriented Programming (OOP)
  • Encapsulation
  • Command Query Separation (CQS)
  • State management
  • Frontend architecture

License

MIT License

Contributing

Contributions are welcome! If you have any questions, suggestions, or issues, please feel free to open a GitHub issue or submit a pull request.