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

feat(connectTo): pass target instance to selector #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { Store } from "./store";

export interface ConnectToSettings<T, R = T | any> {
onChanged?: string;
selector: ((store: Store<T>) => Observable<R>) | MultipleSelector<T, R>;
selector: ((store: Store<T>, targetInstance: any) => Observable<R>) | MultipleSelector<T, R>;
setup?: string;
target?: string;
teardown?: string;
}

export interface MultipleSelector<T, R = T | any> {
[key: string]: ((store: Store<T>) => Observable<R>);
[key: string]: ((store: Store<T>, targetInstance: any) => Observable<R>);
}

const defaultSelector = <T>(store: Store<T>) => store.state;

export function connectTo<T, R = any>(settings?: ((store: Store<T>) => Observable<R>) | ConnectToSettings<T, R>) {
export function connectTo<T, R = any>(settings?: ((store: Store<T>, targetInstance: any) => Observable<R>) | ConnectToSettings<T, R>) {
let $store: Store<T>;

// const store = Container.instance.get(Store) as Store<T>;
Expand All @@ -27,14 +27,14 @@ export function connectTo<T, R = any>(settings?: ((store: Store<T>) => Observabl
...settings
};

function getSource(selector: (((store: Store<T>) => Observable<R>))): Observable<any> {
function getSource(selector: (((store: Store<T>, targetInstance: any) => Observable<R>)), targetInstance: any): Observable<any> {
// if for some reason getSource is invoked before setup (bind lifecycle, typically)
// then we have no choice but to get the store instance from global container instance
// otherwise, assume that $store variable in the closure would be already assigned the right
// value from created callback
// Could also be in situation where it doesn't come from custom element, or some exotic setups/scenarios
const store = $store || ($store = Container.instance.get(Store));
const source = selector(store);
const source = selector(store, targetInstance);

if (source instanceof Observable) {
return source;
Expand Down Expand Up @@ -94,7 +94,7 @@ export function connectTo<T, R = any>(settings?: ((store: Store<T>) => Observabl
throw new Error("Provided onChanged handler does not exist on target VM");
}

this._stateSubscriptions = createSelectors().map(s => getSource(s.selector).subscribe((state: any) => {
this._stateSubscriptions = createSelectors().map(s => getSource(s.selector, this).subscribe((state: any) => {
const lastTargetIdx = s.targets.length - 1;
const oldState = s.targets.reduce((accu = {}, curr) => accu[curr], this);

Expand Down
19 changes: 18 additions & 1 deletion test/unit/decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container } from "aurelia-framework";
import { Subscription } from "rxjs";
import { pluck, distinctUntilChanged } from "rxjs/operators";
import { pluck, distinctUntilChanged, map } from "rxjs/operators";

import { Store } from "../../src/store";
import { connectTo } from "../../src/decorator";
Expand Down Expand Up @@ -62,6 +62,23 @@ describe("using decorators", () => {
expect(sut.state).toEqual(initialState.bar);
});

it("should pass the target instance to a state selector", () => {
const { initialState } = arrange();

@connectTo<DemoState>((store, targetInstance: DemoStoreConsumer) => store.state.pipe(map(state => state[targetInstance.baz])))
class DemoStoreConsumer {
baz: keyof DemoState = "foo";
state: DemoState;
}

const sut = new DemoStoreConsumer();
expect(sut.state).toEqual(undefined);

(sut as any).bind();

expect(sut.state).toEqual(initialState.foo);
});

describe("with a complex settings object", () => {
it("should be possible to provide a selector", () => {
const { initialState } = arrange();
Expand Down