Skip to content

Commit

Permalink
Update README, package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Skaldebane committed Feb 16, 2024
1 parent 4332f3b commit 1465adb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
# Svelte Cleanable Store
# 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

```ts
const state = cleanable(0);

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

Console output:

```ts
state = 0

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

// state = 5
cleaning up 1...
state = 5
```

## Example

### Closing a WebSocket connection

```ts
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](./LICENSE.md). Copyright (c) 2024 Houssam Elbadissi.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-cleanable-store",
"version": "0.0.1",
"version": "0.1.0",
"description": "Custom Svelte store with built-in cleanup support on `subscribe` (like React's `useEffect`).",
"license": "MIT",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<script lang="ts">
import { cleanable } from "$lib/index.js";
let parent = cleanable(0);
const parent = cleanable(0);
parent.subscribe((value) => {
console.log("parent changed", value);
Expand Down

0 comments on commit 1465adb

Please sign in to comment.