Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deriving from RxJS observables #4300

Merged
merged 5 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Fix updating a `<slot>` inside an `{#if}` or other block ([#4292](https://github.com/sveltejs/svelte/issues/4292))
* Fix using RxJS observables in `derived` stores ([#4298](https://github.com/sveltejs/svelte/issues/4298))

## 3.17.2

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export function validate_store(store, name) {
}
}

export function subscribe(store, callback) {
const unsub = store.subscribe(callback);
export function subscribe(store, ...args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ...callbacks instead of ...args?

const unsub = store.subscribe(...args);
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}

Expand Down
5 changes: 3 additions & 2 deletions src/runtime/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { run_all, noop, safe_not_equal, is_function, get_store_value } from 'svelte/internal';
import { run_all, subscribe, noop, safe_not_equal, is_function, get_store_value } from 'svelte/internal';

/** Callback to inform of a value updates. */
type Subscriber<T> = (value: T) => void;
Expand Down Expand Up @@ -173,7 +173,8 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
}
};

const unsubscribers = stores_array.map((store, i) => store.subscribe(
const unsubscribers = stores_array.map((store, i) => subscribe(
store,
(value) => {
values[i] = value;
pending &= ~(1 << i);
Expand Down
25 changes: 15 additions & 10 deletions test/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ describe('store', () => {
});
});

const fake_observable = {
subscribe(fn) {
fn(42);
return {
unsubscribe: () => {}
};
}
};

describe('derived', () => {
it('maps a single store', () => {
const a = writable(1);
Expand Down Expand Up @@ -346,6 +355,11 @@ describe('store', () => {
b.set(2);
assert.deepEqual(get(c), 'two 2');
});

it('works with RxJS-style observables', () => {
const d = derived(fake_observable, _ => _);
assert.equal(get(d), 42);
});
});

describe('get', () => {
Expand All @@ -355,16 +369,7 @@ describe('store', () => {
});

it('works with RxJS-style observables', () => {
const observable = {
subscribe(fn) {
fn(42);
return {
unsubscribe: () => {}
};
}
};

assert.equal(get(observable), 42);
assert.equal(get(fake_observable), 42);
});
});
});