Skip to content

Commit

Permalink
🐛 (createCanal) fix missing back subscription on instantiate
Browse files Browse the repository at this point in the history
  • Loading branch information
tpucci committed Jul 2, 2019
1 parent 8f24877 commit 88f7e28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/__tests__/createCanal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,17 @@ describe('createCanal', () => {
<Canal a />
</BackContext.Provider>
);
// @ts-ignore
testRenderer.root.children[0].instance.back$.subscribe(spy);
back$.next({ target: null });
expect(spy).toHaveBeenCalledWith({
target: 'a'
});
testRenderer.update(
<BackContext.Provider value={{ back$ }}>
<Canal a b />
</BackContext.Provider>
);
// @ts-ignore
testRenderer.root.children[0].instance.back$.subscribe(spy);
back$.next({ target: null });
expect(spy).toHaveBeenCalledWith({
target: 'b'
Expand Down
17 changes: 13 additions & 4 deletions src/createCanal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { ComponentType, Component as ReactComponent } from 'react';
import { ViewStyle, View, StyleSheet, StyleProp } from 'react-native';
import { Observer } from 'mobx-react/native';
import { fromStream } from 'mobx-utils';
import { Subject, Observable } from 'rxjs';
import { Subject, Observable, ConnectableObservable } from 'rxjs';
import {
map,
distinctUntilChanged,
withLatestFrom,
share
publish
} from 'rxjs/operators';
import { Navigation } from './Navigation';
import { last } from './utils/Array.last';
Expand Down Expand Up @@ -38,6 +38,8 @@ export const createCanal = <
implements ICanal {
constructor(props: WithBackContext<CanalComponentProps<Authorizations>>) {
super(props);

this.back$.connect();
const { style, backContext, ...nextAuthorizations } = props;

/**
Expand Down Expand Up @@ -108,7 +110,14 @@ export const createCanal = <
)
);

back$: Observable<IBackEvent> = this.props.backContext.back$.pipe(
/**
* @TODO Pipe operator cannot infer return type as ConnectableObservable.
* See https://github.com/ReactiveX/rxjs/issues/2972.
*/
// @ts-ignore
back$: ConnectableObservable<
IBackEvent
> = this.props.backContext.back$.pipe(
withLatestFrom(this.progress$),
map(([_, progress]) => {
const currentStop = last(progress);
Expand All @@ -117,7 +126,7 @@ export const createCanal = <
}
return { target: null };
}),
share()
publish()
);

shouldComponentUpdate({
Expand Down

0 comments on commit 88f7e28

Please sign in to comment.