Skip to content

Commit 80dd89d

Browse files
committed
switch to signals
1 parent fa96c8c commit 80dd89d

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

src/app/thumbnail/thumbnail.component.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="thumbnail" [class.limit-width]="limitWidth">
2-
@if (isLoading$ | async) {
2+
@if (isLoading()) {
33
<div class="thumbnail-content outer">
44
<div class="inner">
55
<div class="centered">
@@ -9,11 +9,14 @@
99
</div>
1010
}
1111
<!-- don't use *ngIf="!isLoading" so the thumbnail can load in while the animation is playing -->
12-
@if ((src$ | async) !== null) {
13-
<img class="thumbnail-content img-fluid" [ngClass]="{'d-none': (isLoading$ | async)}"
14-
[src]="(src$ | async) | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()">
12+
@if (src() !== null) {
13+
<img class="thumbnail-content img-fluid" [ngClass]="{'d-none': isLoading()}"
14+
[src]="src() | dsSafeUrl"
15+
[alt]="alt | translate"
16+
(error)="errorHandler()"
17+
(load)="successHandler()">
1518
}
16-
@if ((src$ | async) === null && (isLoading$ | async) === false) {
19+
@if (src() === null && isLoading() === false) {
1720
<div class="thumbnail-content outer">
1821
<div class="inner">
1922
<div class="thumbnail-placeholder centered lead">

src/app/thumbnail/thumbnail.component.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,32 @@ describe('ThumbnailComponent', () => {
9999
});
100100

101101
describe('loading', () => {
102-
it('should start out with isLoading$ true', () => {
103-
expect(comp.isLoading$.getValue()).toBeTrue();
102+
it('should start out with isLoading true', () => {
103+
expect(comp.isLoading()).toBeTrue();
104104
});
105105

106106
it('should set isLoading$ to false once an image is successfully loaded', () => {
107107
comp.setSrc('http://bit.stream');
108108
fixture.debugElement.query(By.css('img.thumbnail-content')).triggerEventHandler('load', new Event('load'));
109-
expect(comp.isLoading$.getValue()).toBeFalse();
109+
expect(comp.isLoading()).toBeFalse();
110110
});
111111

112112
it('should set isLoading$ to false once the src is set to null', () => {
113113
comp.setSrc(null);
114-
expect(comp.isLoading$.getValue()).toBeFalse();
114+
expect(comp.isLoading()).toBeFalse();
115115
});
116116

117117
it('should show a loading animation while isLoading$ is true', () => {
118118
expect(de.query(By.css('ds-loading'))).toBeTruthy();
119119

120-
comp.isLoading$.next(false);
120+
comp.isLoading.set(false);
121121
fixture.detectChanges();
122122
expect(fixture.debugElement.query(By.css('ds-loading'))).toBeFalsy();
123123
});
124124

125125
describe('with a thumbnail image', () => {
126126
beforeEach(() => {
127-
comp.src$.next('https://bit.stream');
127+
comp.src.set('https://bit.stream');
128128
fixture.detectChanges();
129129
});
130130

@@ -133,7 +133,7 @@ describe('ThumbnailComponent', () => {
133133
expect(img).toBeTruthy();
134134
expect(img.classes['d-none']).toBeTrue();
135135

136-
comp.isLoading$.next(false);
136+
comp.isLoading.set(false);
137137
fixture.detectChanges();
138138
img = fixture.debugElement.query(By.css('img.thumbnail-content'));
139139
expect(img).toBeTruthy();
@@ -144,14 +144,14 @@ describe('ThumbnailComponent', () => {
144144

145145
describe('without a thumbnail image', () => {
146146
beforeEach(() => {
147-
comp.src$.next(null);
147+
comp.src.set(null);
148148
fixture.detectChanges();
149149
});
150150

151151
it('should only show the HTML placeholder once done loading', () => {
152152
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeFalsy();
153153

154-
comp.isLoading$.next(false);
154+
comp.isLoading.set(false);
155155
fixture.detectChanges();
156156
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeTruthy();
157157
});
@@ -247,14 +247,14 @@ describe('ThumbnailComponent', () => {
247247
describe('fallback', () => {
248248
describe('if there is a default image', () => {
249249
it('should display the default image', () => {
250-
comp.src$.next('http://bit.stream');
250+
comp.src.set('http://bit.stream');
251251
comp.defaultImage = 'http://default.img';
252252
comp.errorHandler();
253-
expect(comp.src$.getValue()).toBe(comp.defaultImage);
253+
expect(comp.src()).toBe(comp.defaultImage);
254254
});
255255

256256
it('should include the alt text', () => {
257-
comp.src$.next('http://bit.stream');
257+
comp.src.set('http://bit.stream');
258258
comp.defaultImage = 'http://default.img';
259259
comp.errorHandler();
260260

@@ -266,10 +266,10 @@ describe('ThumbnailComponent', () => {
266266

267267
describe('if there is no default image', () => {
268268
it('should display the HTML placeholder', () => {
269-
comp.src$.next('http://default.img');
269+
comp.src.set('http://default.img');
270270
comp.defaultImage = null;
271271
comp.errorHandler();
272-
expect(comp.src$.getValue()).toBe(null);
272+
expect(comp.src()).toBe(null);
273273

274274
fixture.detectChanges();
275275
const placeholder = fixture.debugElement.query(By.css('div.thumbnail-placeholder')).nativeElement;
@@ -363,7 +363,7 @@ describe('ThumbnailComponent', () => {
363363
it('should show the default image', () => {
364364
comp.defaultImage = 'default/image.jpg';
365365
comp.ngOnChanges({});
366-
expect(comp.src$.getValue()).toBe('default/image.jpg');
366+
expect(comp.src()).toBe('default/image.jpg');
367367
});
368368
});
369369
});
@@ -419,7 +419,7 @@ describe('ThumbnailComponent', () => {
419419
});
420420

421421
it('should start out with isLoading$ true', () => {
422-
expect(comp.isLoading$.getValue()).toBeTrue();
422+
expect(comp.isLoading()).toBeTrue();
423423
expect(de.query(By.css('ds-loading'))).toBeTruthy();
424424
});
425425

src/app/thumbnail/thumbnail.component.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import {
88
Input,
99
OnChanges,
1010
PLATFORM_ID,
11+
signal,
1112
SimpleChanges,
13+
WritableSignal,
1214
} from '@angular/core';
1315
import { TranslateModule } from '@ngx-translate/core';
14-
import { of as observableOf, BehaviorSubject } from 'rxjs';
16+
import { of as observableOf } from 'rxjs';
1517
import { switchMap } from 'rxjs/operators';
1618

1719
import { AuthService } from '../core/auth/auth.service';
@@ -54,7 +56,7 @@ export class ThumbnailComponent implements OnChanges {
5456
/**
5557
* The src attribute used in the template to render the image.
5658
*/
57-
src$: BehaviorSubject<string> = new BehaviorSubject<string>(undefined);
59+
src: WritableSignal<string> = signal(undefined);
5860

5961
retriedWithToken = false;
6062

@@ -77,7 +79,7 @@ export class ThumbnailComponent implements OnChanges {
7779
* Whether the thumbnail is currently loading
7880
* Start out as true to avoid flashing the alt text while a thumbnail is being loaded.
7981
*/
80-
isLoading$: BehaviorSubject<boolean> = new BehaviorSubject(true);
82+
isLoading: WritableSignal<boolean> = signal(true);
8183

8284
constructor(
8385
@Inject(PLATFORM_ID) private platformID: any,
@@ -96,8 +98,8 @@ export class ThumbnailComponent implements OnChanges {
9698
// every time the inputs change we need to start the loading animation again, as it's possible
9799
// that thumbnail is first set to null when the parent component initializes and then set to
98100
// the actual value
99-
if (this.isLoading$.getValue() === false) {
100-
this.isLoading$.next(true);
101+
if (!this.isLoading()) {
102+
this.isLoading.set(true);
101103
}
102104

103105
if (hasNoValue(this.thumbnail)) {
@@ -140,7 +142,7 @@ export class ThumbnailComponent implements OnChanges {
140142
* Otherwise, fall back to the default image or a HTML placeholder
141143
*/
142144
errorHandler() {
143-
const src = this.src$.getValue();
145+
const src = this.src();
144146
const thumbnail = this.bitstream;
145147
const thumbnailSrc = thumbnail?._links?.content?.href;
146148

@@ -192,16 +194,16 @@ export class ThumbnailComponent implements OnChanges {
192194
* @param src
193195
*/
194196
setSrc(src: string): void {
195-
this.src$.next(src);
197+
this.src.set(src);
196198
if (src === null) {
197-
this.isLoading$.next(false);
199+
this.isLoading.set(false);
198200
}
199201
}
200202

201203
/**
202204
* Stop the loading animation once the thumbnail is successfully loaded
203205
*/
204206
successHandler() {
205-
this.isLoading$.next(false);
207+
this.isLoading.set(false);
206208
}
207209
}

0 commit comments

Comments
 (0)