Skip to content

Commit

Permalink
Update to latest RxJS version (#235)
Browse files Browse the repository at this point in the history
* Update to latest RxJS version

* Update caniuse-lite to prevent warning logging

* Make parameter of fromObservable an Observable

Make parameter observable in fromObservable of type Observable instead of Subscribable as this causes issues with typing.

* Remove not needed typings

* Remove fromObservable typing from tutorial

* Undo changes as type is not correctly inferred

* Add RxJS version 7 to peerDepencies

* Use Subscription type instead of Subscribable

We claim compatibility with RxJS version 5 but in V5 the type Subscribable does not exist. This does not lead to an issue as the unsubscribe() method exists in Subscribable (V6+) and ISubscription (V5) but it is not very clean.

* Exclude dist folder from npm scripts
  • Loading branch information
guidoderooij authored Mar 25, 2024
1 parent 30b42ef commit 3d40b3e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"npm.exclude": "**/dist/**"
}
2 changes: 1 addition & 1 deletion extensions/sherlock-rxjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
},
"peerDependencies": {
"@politie/sherlock": "0.0.0-PLACEHOLDER",
"rxjs": "^5.4.0 || ^6.0.0"
"rxjs": "^5.4.0 || ^6.0.0 || ^7.0.0 "
}
}
27 changes: 21 additions & 6 deletions extensions/sherlock-rxjs/rxjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ describe('rxjs/rxjs', () => {
a$.setFinal('final value');
let value = '';
let complete = false;
toObservable(a$).subscribe(v => value = v, undefined, () => complete = true);
toObservable(a$).subscribe({
next: v => value = v,
complete: () => complete = true
});
expect(value).toBe('final value');
expect(complete).toBeTrue();
});

it('should complete the Observable when the derivable becomes final', () => {
let value = '';
let complete = false;
toObservable(a$).subscribe(v => value = v, undefined, () => complete = true);
toObservable(a$).subscribe({
next: v => value = v,
complete: () => complete = true
});
expect(value).toBe('a');
expect(complete).toBeFalse();

Expand All @@ -33,7 +39,10 @@ describe('rxjs/rxjs', () => {
it('should complete the Observable when until becomes true', () => {
let complete = false;
let value = '';
toObservable(a$, { until: d$ => d$.get().length > 2 }).subscribe(v => value = v, undefined, () => complete = true);
toObservable(a$, { until: d$ => d$.get().length > 2 }).subscribe({
next: v => value = v,
complete: () => complete = true
});
expect(complete).toBe(false);
expect(value).toBe('a');

Expand All @@ -49,7 +58,10 @@ describe('rxjs/rxjs', () => {
it('should complete the Observable after one value when once is true', () => {
let complete = false;
const values: string[] = [];
toObservable(a$, { once: true }).subscribe(v => values.push(v), undefined, () => complete = true);
toObservable(a$, { once: true }).subscribe({
next: v => values.push(v),
complete: () => complete = true
});
expect(complete).toBe(true);
expect(values).toEqual(['a']);

Expand All @@ -60,7 +72,10 @@ describe('rxjs/rxjs', () => {
it('should skip the first value if skipFirst is true', () => {
let complete = false;
const values: string[] = [];
toObservable(a$, { skipFirst: true, once: true }).subscribe(v => values.push(v), undefined, () => complete = true);
toObservable(a$, { skipFirst: true, once: true }).subscribe({
next: v => values.push(v),
complete: () => complete = true
});
expect(complete).toBe(false);
expect(Object.keys(values)).toHaveLength(0);

Expand Down Expand Up @@ -107,7 +122,7 @@ describe('rxjs/rxjs', () => {

it('should not complete on unsubscribe', () => {
let complete = false;
toObservable(a$).subscribe(undefined, undefined, () => complete = true).unsubscribe();
toObservable(a$).subscribe({ complete: () => complete = true}).unsubscribe();
expect(complete).toBe(false);
});
});
Expand Down
16 changes: 8 additions & 8 deletions extensions/sherlock-rxjs/rxjs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _internal, atom, Derivable, ErrorWrapper, ReactorOptions } from '@politie/sherlock';
import { Observable, Subscribable, Subscriber, Unsubscribable } from 'rxjs';
import { Observable, Subscriber, Subscription } from 'rxjs';

/**
* Creates an RxJS Observable from a Derivable. Optionally accepts a `ReactorOptions` that governs RxJS emissions
Expand All @@ -17,17 +17,17 @@ export function toObservable<V>(derivable: Derivable<V>, options?: Partial<React
});
}

export function fromObservable<V>(observable: Subscribable<V>): Derivable<V> {
export function fromObservable<V>(observable: Observable<V>): Derivable<V> {
const atom$ = atom.unresolved<V>();

let subscription: Unsubscribable | undefined;
let subscription: Subscription | undefined;
atom$.connected$.react(() => {
if (atom$.connected && !subscription) {
subscription = observable.subscribe(
value => atom$.set(value),
err => atom$.setFinal(new ErrorWrapper(err)),
() => atom$.setFinal(atom$.getState()),
);
subscription = observable.subscribe({
next: value => atom$.set(value),
error: err => atom$.setFinal(new ErrorWrapper(err)),
complete: () => atom$.setFinal(atom$.getState()),
});
}
// This is not chained with the previous as an `else` branch, because this can be true immediately after
// the subscription occurs. Observables can complete synchronously on subscription.
Expand Down
67 changes: 30 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"rollup": "^2.50.5",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-visualizer": "^5.5.0",
"rxjs": "^6.6.7",
"rxjs": "^7.8.1",
"shelljs": "^0.8.5",
"terser": "^5.14.2",
"ts-jest": "^27.0.2",
Expand Down

0 comments on commit 3d40b3e

Please sign in to comment.