Skip to content

Commit 72ff720

Browse files
committed
fix: emit event inside NgZone
Refs: #1284
1 parent 8233cbc commit 72ff720

File tree

3 files changed

+161
-7
lines changed

3 files changed

+161
-7
lines changed

CHANGELOG.md

+137-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,140 @@
1-
# [6.1.4](https://github.com/k3nsei/ng-in-viewport/compare/v6.1.3...v6.1.4) (2020-10-03)
1+
# [15.0.0](https://github.com/k3nsei/ng-in-viewport/compare/v13.0.1...v15.0.0) (2023-01-30)
2+
3+
### Enhancements
4+
5+
- Library was rewritten from scratch with usage of new standalone APIs that comes in `Angular v14.0.0`
6+
- Unit test coverage is now 100%
7+
8+
### Bug Fixes
9+
10+
- Use `globalThis.btoa` for platform browser and server as `node.js` implemented it (#1274)(8b1fc918ac6e463afdd736324e19dd4a5c3a3e34)
11+
- Use `WeakMap` and generated unique id to reference `checkFn` (#1055)(8b1fc918ac6e463afdd736324e19dd4a5c3a3e34)
12+
13+
### Breaking Changes
14+
15+
With this release support for Angular lower than `v14.0.0` is dropped for those please use `ng-in-viewport` `v6.1.5` or `v13.0.1`
16+
17+
# [13.0.1](https://github.com/k3nsei/ng-in-viewport/compare/v13.0.0...v13.0.1) (2022-02-14)
18+
19+
**Note: This version is re-release of v13.0.0 with a corrected peer dependencies list. NX workspace added dependencies from unit tests, which was source of an issue.**
20+
21+
There were no changes in code, library was compiled with ivy in mind because Angular 13 is no longer supports view engine.
22+
23+
### Breaking Changes
24+
25+
With this version support for versions of angular lower than 12 is dropped for those please use ng-in-viewport v6.1.5
26+
27+
# [13.0.0](https://github.com/k3nsei/ng-in-viewport/compare/v6.1.5...v13.0.0) (2022-02-07)
28+
29+
There were no changes in code, library was compiled with ivy in mind because Angular 13 is no longer supports view engine.
30+
31+
### Breaking Changes
32+
33+
- With this version support for versions of angular lower than 12 is dropped for those please use **ng-in-viewport v6.1.5**
34+
35+
# [6.1.5](https://github.com/k3nsei/ng-in-viewport/compare/v6.1.4...v6.1.5) (2021-01-22)
36+
37+
### Enhancement
38+
39+
- Update readme
40+
41+
# [6.1.4](https://github.com/k3nsei/ng-in-viewport/compare/v6.1.3...v6.1.4) (2020-11-03)
42+
43+
### Bug Fixes
44+
45+
- Make options input public (#360)(42d460029dc88bceb1ff4d01e98691ccdb2706b0)
46+
47+
# [6.1.3](https://github.com/k3nsei/ng-in-viewport/compare/v6.1.1...v6.1.3) (2020-08-09)
48+
49+
### Bug Fixes
50+
51+
- Drop usage of Function constructor (#333)(2db638c0a0c767345984b21bf94da2e91cb8e3a8)
52+
53+
# [6.1.1](https://github.com/k3nsei/ng-in-viewport/compare/v6.1.0...v6.1.1) (2020-07-02)
54+
55+
### Bug Fixes
56+
57+
- Broken support of server platform in config (#316)(53ed86b6cb30fa60336f8560efc182e536f83ede)
58+
59+
# [6.1.0](https://github.com/k3nsei/ng-in-viewport/compare/v6.0.3...v6.1.0) (2020-04-14)
60+
61+
### Enhancement
62+
63+
- Support root with many options (#147)(22672d6c01322f190706093c24a184a39049256d)
64+
65+
# [6.0.3](https://github.com/k3nsei/ng-in-viewport/compare/v6.0.2...v6.0.3) (2018-11-05)
66+
67+
### Enhancement
68+
69+
- Use better predicate for completelyVisible (#101)(e5c6363ead40638619304d1ce6fdcf8caeda8cfa)
70+
71+
# [6.0.2](https://github.com/k3nsei/ng-in-viewport/compare/v6.0.1...v6.0.2) (2018-07-17)
72+
73+
### Enhancement
74+
75+
- Fix "similar-code" issue in projects/ng-in-viewport/src/lib/in-viewport.directive.ts (#53)(eeb4e27a8b77f027572e4161b6f06c400b729ca1)
76+
77+
# [6.0.1](https://github.com/k3nsei/ng-in-viewport/compare/v6.0.0...v6.0.1) (2018-06-30)
278

379
### Bug Fixes
480

5-
- Make options input public ([5918427](https://github.com/k3nsei/ng-in-viewport/commit/59184270d7423f8b7c8f186ff8cb7312e336b3d5))
81+
- Changed dependency semver ranges
82+
83+
# [6.0.0](https://github.com/k3nsei/ng-in-viewport/compare/v1.2.8...v6.0.0) (2018-06-28)
84+
85+
### Upgrade to Angular 6
86+
87+
- Optimization and refactoring
88+
- Change dependencies to require Angular 6 or newer
89+
- Change dependencies to require RxJS 6 or newer
90+
91+
### Breaking Changes
92+
93+
- (inViewportAction) output has changed. Now there is `visible` property instead of `value`.
94+
95+
# [1.2.8](https://github.com/k3nsei/ng-in-viewport/compare/v1.2.7...v1.2.8) (2018-06-08)
96+
97+
### Enhancement:
98+
99+
- Build with typescript v2.6.2 (#42)(d0008a2084029da59e4383fa50dcba70b8b31966)
100+
101+
### Bug Fixes:
102+
103+
- Fix platform-server support (#12)(d0008a2084029da59e4383fa50dcba70b8b31966)
104+
105+
# [1.2.7](https://github.com/k3nsei/ng-in-viewport/compare/v1.2.5...v1.2.7) (2018-05-25)
106+
107+
### Enhancement:
108+
109+
- Add platform-server support (#12)(5241efb2304af0b64eca6d610b3c1813bffb865d 69fb3c25815ee7991f32f7967325ac4d9bf4879e)
110+
111+
# [1.2.5](https://github.com/k3nsei/ng-in-viewport/compare/v1.2.0...v1.2.5) (2018-05-23)
112+
113+
### Bug Fixes:
114+
115+
- Run IntersectionObserver outside angular zone (#39)(a340fa4e862f374be5a10558f458279ab0f2aea5)
116+
117+
# [1.2.0](https://github.com/k3nsei/ng-in-viewport/compare/v1.1.3...v1.2.0) (2017-04-10)
118+
119+
- Use ES5 and ES2015 modules with AOT support
120+
- Use `intersection-observer` polyfill and drop strategies
121+
- Support for rootElement
122+
123+
# [1.1.3](https://github.com/k3nsei/ng-in-viewport/compare/v1.1.2...v1.1.3) (2017-03-28)
124+
125+
- Fire init check after view init
126+
127+
# [1.1.2](https://github.com/k3nsei/ng-in-viewport/compare/v1.1.1...v1.1.2) (2017-03-25)
128+
129+
- Do not modifies Observable prototype
130+
131+
# [1.1.1](https://github.com/k3nsei/ng-in-viewport/compare/v1.1.0...v1.1.1) (2017-03-25)
132+
133+
- Add docs and github-page
134+
135+
# [1.1.0](https://github.com/k3nsei/ng-in-viewport/compare/v1.0.0...v1.1.0) (2017-03-25)
136+
137+
# [1.0.0](https://github.com/k3nsei/ng-in-viewport/tree/v1.0.0) (2017-02-10)
138+
139+
- Optimize listening scroll and resize events
140+
- Create Module

projects/ng-in-viewport/src/lib/services/in-viewport.service.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { NgZone } from '@angular/core';
12
import { SpectatorService, createServiceFactory } from '@ngneat/spectator/jest';
23
import { uniqueId } from 'lodash';
4+
import { Subscription } from 'rxjs';
35

46
import { ObserverCache } from '../utils';
57
import { Config } from '../values';
@@ -51,13 +53,30 @@ describe('GIVEN InViewportService', () => {
5153
const node = createNode();
5254
const config = new Config();
5355

56+
let triggerCallback: (...args: any[]) => boolean;
57+
let triggerSubscription$: Subscription;
58+
5459
beforeEach(() => {
60+
triggerCallback = jest.fn().mockImplementation(() => NgZone.isInAngularZone());
61+
62+
triggerSubscription$ = service.trigger$.subscribe((...args) => triggerCallback(...args));
63+
5564
service.register(node, config);
5665
});
5766

67+
afterEach(() => triggerSubscription$.unsubscribe());
68+
5869
it('THEN `addNode` from cache should by called by service', () => {
5970
expect(mockAddNode).toHaveBeenCalledWith(node, config);
6071
});
72+
73+
it('THEN intersection event should be handled in NgZone', () => {
74+
expect(triggerCallback).toHaveReturnedWith(true);
75+
});
76+
77+
it('THEN `trigger$` should emit initial event', () => {
78+
expect(triggerCallback).toHaveBeenCalledWith({ target: node });
79+
});
6180
});
6281

6382
describe('AND `unregister` method was called', () => {

projects/ng-in-viewport/src/lib/services/in-viewport.service.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ export class InViewportService {
1414

1515
constructor(private readonly zone: NgZone) {
1616
this.zone.runOutsideAngular(() => {
17-
this.#cache = new ObserverCache((entries) => {
18-
if (Array.isArray(entries) && entries.length) {
19-
entries.forEach((entry) => this.#trigger$.next(entry));
20-
}
21-
});
17+
this.#cache = new ObserverCache((entries) => this.onIntersectionEvent(entries));
2218
});
2319
}
2420

@@ -33,4 +29,8 @@ export class InViewportService {
3329
this.#cache.deleteNode(node, config);
3430
});
3531
}
32+
33+
private onIntersectionEvent(entries: IntersectionObserverEntry[] = []): void {
34+
this.zone.run(() => entries.forEach((entry) => this.#trigger$.next(entry)));
35+
}
3636
}

0 commit comments

Comments
 (0)