Skip to content

Commit

Permalink
fix: allow DirectiveSuperclass.getInput$() to be used in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Jan 9, 2019
1 parent b00f390 commit 8e67212
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
30 changes: 17 additions & 13 deletions projects/s-ng-utils/src/lib/directive-superclass.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,25 @@ describe("DirectiveSuperclass", () => {

/////////

describe(".changedKeys$", () => {
describe(".lastChangedKeys$", () => {
it("emits the keys that change", fakeAsync(() => {
init();
const stub = jasmine.createSpy();
colorTextComponent.changedKeys$.subscribe(stub);
colorTextComponent.lastChangedKeys$.subscribe(stub);
expect(stub).toHaveBeenCalledTimes(1);
expect(stub.calls.argsFor(0)).toEqual([new Set(["prefix", "prefix2"])]);

click(darkButton());
expect(stub).toHaveBeenCalledTimes(1);
expect(stub).toHaveBeenCalledWith(new Set(["prefix"]));
expect(stub).toHaveBeenCalledTimes(2);
expect(stub.calls.argsFor(1)).toEqual([new Set(["prefix"])]);

click(slateButton());
expect(stub).toHaveBeenCalledTimes(2);
expect(stub).toHaveBeenCalledWith(new Set(["prefix2"]));
expect(stub).toHaveBeenCalledTimes(3);
expect(stub.calls.argsFor(2)).toEqual([new Set(["prefix2"])]);

click(bothButton());
expect(stub).toHaveBeenCalledTimes(3);
expect(stub).toHaveBeenCalledWith(new Set(["prefix", "prefix2"]));
expect(stub).toHaveBeenCalledTimes(4);
expect(stub.calls.argsFor(3)).toEqual([new Set(["prefix", "prefix2"])]);
}));
});

Expand All @@ -90,17 +92,19 @@ describe("DirectiveSuperclass", () => {
init();
const stub = jasmine.createSpy();
colorTextComponent.getInput$("prefix2").subscribe(stub);
expect(stub).toHaveBeenCalledTimes(1);
expect(stub.calls.argsFor(0)).toEqual([undefined]);

click(darkButton());
expect(stub).not.toHaveBeenCalled();
expect(stub).toHaveBeenCalledTimes(1);

click(slateButton());
expect(stub).toHaveBeenCalledTimes(1);
expect(stub).toHaveBeenCalledWith("Slate");
expect(stub).toHaveBeenCalledTimes(2);
expect(stub.calls.argsFor(1)).toEqual(["Slate"]);

click(bothButton());
expect(stub).toHaveBeenCalledTimes(2);
expect(stub).toHaveBeenCalledWith(undefined);
expect(stub).toHaveBeenCalledTimes(3);
expect(stub.calls.argsFor(2)).toEqual([undefined]);
}));
});

Expand Down
10 changes: 6 additions & 4 deletions projects/s-ng-utils/src/lib/directive-superclass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Injector,
ChangeDetectorRef,
} from "@angular/core";
import { Observable, Subject } from "rxjs";
import { keys } from "micro-dash/lib/object/keys";
import { BehaviorSubject, Observable, Subject } from "rxjs";
import { filter, map } from "rxjs/operators";
import { AutoDestroyable } from "./auto-destroyable";

Expand Down Expand Up @@ -48,7 +49,8 @@ import { AutoDestroyable } from "./auto-destroyable";
export abstract class DirectiveSuperclass extends AutoDestroyable
implements OnChanges {
/** Emits the set of `@Input()` property names that change during each call to `ngOnChanges()`. */
changedKeys$ = new Subject<Set<keyof this>>();
// lastChangedKeys$ = new Subject<Set<keyof this>>();
lastChangedKeys$ = new BehaviorSubject<Set<keyof this>>(new Set());

protected changeDetectorRef: ChangeDetectorRef;

Expand All @@ -58,14 +60,14 @@ export abstract class DirectiveSuperclass extends AutoDestroyable
}

ngOnChanges(changes: SimpleChanges) {
this.changedKeys$.next(
this.lastChangedKeys$.next(
new Set(Object.getOwnPropertyNames(changes) as Array<keyof this>),
);
}

/** @return an observable of the values for one of this directive's `@Input()` properties */
getInput$<K extends keyof this>(key: K): Observable<this[K]> {
return this.changedKeys$.pipe(
return this.lastChangedKeys$.pipe(
filter((keys) => keys.has(key)),
map(() => this[key]),
);
Expand Down

0 comments on commit 8e67212

Please sign in to comment.