Giter Site home page Giter Site logo

skaldebane / svelte-cleanable-store Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 57 KB

Custom Svelte store with cleanup function support.

Home Page: https://www.npmjs.com/package/svelte-cleanable-store

License: MIT License

JavaScript 30.70% HTML 11.24% TypeScript 38.11% Svelte 19.96%
effect javascript svelte svelte-store typescript

svelte-cleanable-store's Introduction

svelte-cleanable-store

Custom Svelte store with built-in cleanup support on subscribe. Inspired by React's useEffect cleanup mechanism.

While the core functionality is simple and complete, this package is still pre-1.0 until I get feedback on the API shape. (e.g. Is cleanable a confusing name? Do we need a readable equivalent as well?)

Usage

const state = cleanable(0);

state.subscribe((value) => {
    console.log(`state = ${value}`);
    return () => console.log(`cleaning up ${value}...`);
});

Console output:

'state = 0'

// $state++
'cleaning up 0...'
'state = 1'

// $state = 5
'cleaning up 1...'
'state = 5'

Example

Closing a WebSocket connection

const path = cleanable("https://example.com/stream");
path.subscribe((value) => {
    const webSocket = new WebSocket(value);
    webSocket.onmessage = (event) => console.log(event);

    return () => webSocket.close();
});

But what about Svelte 5's $effect?

It's absolutely great! In fact, it inspired me to create this package.

However, Svelte 5 is still in beta now, and while there are many workarounds for Svelte 4, this package provides a clean, store-based solution that works with Svelte 4 (and 5), can be used anywhere (in plain .js/.ts files!), and is compatible with auto-subscriptions in .svelte files (with the $ prefix), all without feeling like a hack.

License

MIT License. Copyright (c) 2024 Houssam Elbadissi.

svelte-cleanable-store's People

Contributors

skaldebane avatar

Stargazers

Charraj Zakariaa  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.