8000 fix(cdk-render-strategies): don't send complete event to strategy-behavior by hoebbelsB · Pull Request #954 · rx-angular/rx-angular · GitHub
[go: up one dir, main page]

Skip to content

fix(cdk-render-strategies): don't send complete event to strategy-behavior #954

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

Merged
merged 2 commits into from
Sep 23, 2021
Merged
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
94 changes: 94 additions & 0 deletions libs/cdk/spec/render-strategies/onStrategy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { ɵglobal } from '@angular/core';
import { TestScheduler } from 'rxjs/testing';
import * as rxjs from 'rxjs';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { jestMatcher } from '@test-helpers';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { onStrategy, RX_NATIVE_STRATEGIES } from '@rx-angular/cdk';
import { animationFrameScheduler, observeOn, of } from 'rxjs';

// tslint:disable: no-duplicate-string
describe('onStrategy', () => {

let testScheduler: TestScheduler;
let handles = [];
let animationRuns = 0;
function _animate() {
handles.forEach(handle => handle());
}
let _animationFrameRestore;
let _cancelAnimationFrameRestore;
beforeEach(() => {
testScheduler = new TestScheduler(jestMatcher);
_animationFrameRestore = ɵglobal.requestAnimationFrame;
_cancelAnimationFrameRestore = ɵglobal.cancelAnimationFrame;
ɵglobal.requestAnimationFrame = (cb?) => {
handles[animationRuns] = cb;
animationRuns++;
return animationRuns;
}
ɵglobal.cancelAnimationFrame = (id: number) => {
handles = handles.splice(id, 1);
}
});


afterEach(() => {
ɵglobal.requestAnimationFrame = _animationFrameRestore;
ɵglobal.cancelAnimationFrame = _cancelAnimationFrameRestore;
animationRuns = 0;
handles = [];
});

it('should emit 42', () => {
testScheduler.run(({expectObservable}) => {
const work = jest.fn();
const values = { x: 42 };
const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.native, work);
const expected = '(x|)';
expectObservable(stream$).toBe(expected, values)
});
});

it('should throw error', () => {
testScheduler.run(({expectObservable}) => {
const error = new Error('error');
const work = () => {
throw error;
}
const value = 42;
const errorValues = [error, value];
const expected = '#';
const stream$ = onStrategy(value, RX_NATIVE_STRATEGIES.native, work);
expectObservable(stream$).toBe(expected, null, errorValues);
});
});

it('should run on animationFrame', () => {
testScheduler.run(({ expectObservable, animate }) => {
const work = jest.fn();
animate(' --------x');
const expected = '--------(x|)';
of(null).pipe(observeOn(animationFrameScheduler)).subscribe(() => {
_animate();
});
const values = { x: 42 };
const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.local, work);
expectObservable(stream$).toBe(expected, values);
});
});

it('it should call work once when async', () => {
const work = jest.fn();
onStrategy(42, RX_NATIVE_STRATEGIES.local, work).subscribe();
expect(work).not.toHaveBeenCalled();
_animate();
expect(work).toHaveBeenCalledTimes(1);
});

it('it should call work', () => {
const work = jest.fn();
onStrategy(42, RX_NATIVE_STRATEGIES.native, work).subscribe();
expect(work).toHaveBeenCalledTimes(1);
});
});
38 changes: 7 additions & 31 deletions libs/cdk/src/lib/utils/onStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import { RxCoalescingOptions } from '@rx-angular/cdk/coalescing';
import { switchMap } from 'rxjs/operators';
import { switchMap, take } from 'rxjs/operators';
import {
RxRenderWork,
RxStrategyCredentials,
} from '../model';
import { Observable, Observer, of, throwError } from 'rxjs';
import { Observable, of, throwError } from 'rxjs';
import {
RxRenderError,
RxRenderErrorFactory,
} from '../template-management/render-error';

/**
* @internal
*
* @param value
* @param strategy
* @param workFactory
* @param options
*/
/*export function onStrategy<T>(
value: T,
strategy: RxStrategyCredentials,
workFactory: (
value: T,
work: RxRenderWork,
options: RxCoalescingOptions
) => void,
options: RxCoalescingOptions = {}
): Observable<T> {
return of(value).pipe(
strategy.behavior(
() => workFactory(value, strategy.work, options),
options.scope || {}
)
);
}*/

/**
* @internal
*
Expand All @@ -57,7 +30,9 @@ export function onStrategy<T>(
errorFactory: RxRenderErrorFactory<T, any> = (e, v) => [e, v]
): Observable<T> {
let error: Error;
return of(value).pipe(
return new Observable<T>(subscriber => {
subscriber.next(value);
}).pipe(
strategy.behavior(() => {
try {
workFactory(value, strategy.work, options);
Expand All @@ -67,6 +42,7 @@ export function onStrategy<T>(
}, options.scope || {}),
switchMap(() =>
error ? throwError(errorFactory(error, value)) : of(value)
)
),
take(1)
);
}
0