Skip to content

Commit

Permalink
Add .subscribe- method to signals to add support for Svelte
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Sep 13, 2022
1 parent dbf905f commit 104feb9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-news-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals-core": minor
---

Add `.subscribe()`-method to signals to add support for natively using signals with Svelte
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Signals

Signals is a performant state management library with two primary goals:
Expand All @@ -17,21 +16,19 @@ npm install @preact/signals-core
npm install @preact/signals
# If you're using React
npm install @preact/signals-react
# If you're using Svelte
npm install @preact/signals-core
```

- [Guide / API](#guide--api)
- [`signal(initialValue)`](#signalinitialvalue)
- [`signal.peek()`](#signalpeek)
- [`computed(fn)`](#computedfn)
- [`effect(fn)`](#effectfn)
- [`batch(fn)`](#batchfn)
- [Preact Integration](./packages/preact/README.md#preact-integration)
- [Hooks](./packages/preact/README.md#hooks)
- [Rendering optimizations](./packages/preact/README.md#rendering-optimizations)
- [Attribute optimization (experimental)](./packages/preact/README.md#attribute-optimization-experimental)
- [React Integration](./packages/react/README.md#react-integration)
- [Hooks](./packages/react/README.md#hooks)
- [License](#license)
- [Signals](#signals)
- [Installation:](#installation)
- [Guide / API](#guide--api)
- [`signal(initialValue)`](#signalinitialvalue)
- [`signal.peek()`](#signalpeek)
- [`computed(fn)`](#computedfn)
- [`effect(fn)`](#effectfn)
- [`batch(fn)`](#batchfn)
- [License](#license)

## Guide / API

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class Signal<T = any> {
};
}

subscribe(fn: (value: T) => () => void) {
return effect(() => fn(this.value));
}

/**
* A custom update routine to run when this Signal's value changes.
* @internal
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/signal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ describe("signal", () => {
expect(b.peek()).to.equal(2);
});
});

describe(".subscribe()", () => {
it("should subscribe to a signal", () => {
const spy = sinon.spy();
const a = signal(1);

a.subscribe(spy);
expect(spy).to.be.calledWith(1);
});

it("should unsubscribe from a signal", () => {
const spy = sinon.spy();
const a = signal(1);

const dispose = a.subscribe(spy);
dispose();
spy.resetHistory();

a.value = 2;
expect(spy).not.to.be.called;
});
});
});

describe("effect()", () => {
Expand Down

0 comments on commit 104feb9

Please sign in to comment.