Skip to content

Commit 3f79409

Browse files
feat(pos-switch): reactive render when forward/backward links are present
1 parent bfe927e commit 3f79409

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

elements/src/components/pos-switch/pos-switch.spec.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { newSpecPage } from '@stencil/core/testing';
22
import { PosSwitch } from './pos-switch';
33
import { when } from 'jest-when';
4-
import { RdfType, Thing } from '@pod-os/core';
4+
import { RdfType, Relation, Thing } from '@pod-os/core';
55
import { Subject } from 'rxjs';
66

77
describe('pos-switch', () => {
@@ -203,16 +203,15 @@ describe('pos-switch', () => {
203203
</pos-case>
204204
</pos-switch>`,
205205
});
206+
const observedRelations$ = new Subject<Relation[]>();
206207
const thing = {
207208
uri: 'https://pod.example/resource',
208-
relations: jest.fn(),
209+
observeRelations: () => observedRelations$,
209210
};
210-
when(thing.relations)
211-
.calledWith('https://schema.org/video')
212-
.mockReturnValue([{ predicate: 'https://schema.org/video', uris: ['https://video.test/video-1'] }]);
213-
when(thing.relations).calledWith('https://schema.org/description').mockReturnValue([]);
214-
215-
await page.rootInstance.receiveResource(thing);
211+
page.rootInstance.receiveResource(thing);
212+
observedRelations$.next([
213+
{ predicate: 'https://schema.org/video', label: 'video', uris: ['https://video.test/video-1'] },
214+
]);
216215
await page.waitForChanges();
217216
expect(page.root?.innerHTML).toEqualHtml(`
218217
<div>Resource has video</div>
@@ -236,16 +235,15 @@ describe('pos-switch', () => {
236235
</pos-case>
237236
</pos-switch>`,
238237
});
238+
const observedReverseRelations$ = new Subject<Relation[]>();
239239
const thing = {
240240
uri: 'https://pod.example/resource',
241-
reverseRelations: jest.fn(),
241+
observeReverseRelations: () => observedReverseRelations$,
242242
};
243-
when(thing.reverseRelations)
244-
.calledWith('https://schema.org/video')
245-
.mockReturnValue([{ predicate: 'https://schema.org/video', uris: ['https://video.test/video-1'] }]);
246-
when(thing.reverseRelations).calledWith('https://schema.org/subjectOf').mockReturnValue([]);
247-
248-
await page.rootInstance.receiveResource(thing);
243+
page.rootInstance.receiveResource(thing);
244+
observedReverseRelations$.next([
245+
{ predicate: 'https://schema.org/video', label: 'video', uris: ['https://video.test/video-1'] },
246+
]);
249247
await page.waitForChanges();
250248
expect(page.root?.innerHTML).toEqualHtml(`
251249
<div>Resource is video</div>

elements/src/components/pos-switch/pos-switch.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RdfType, Thing } from '@pod-os/core';
1+
import { RdfType, Relation, Thing } from '@pod-os/core';
22
import { Component, Element, Event, h, Host, State } from '@stencil/core';
33
import { ResourceAware, ResourceEventEmitter, subscribeResource } from '../events/ResourceAware';
44
import { firstValueFrom, Subject, takeUntil } from 'rxjs';
@@ -18,6 +18,8 @@ export class PosSwitch implements ResourceAware {
1818
@State() resource: Thing;
1919
@State() caseElements: HTMLPosCaseElement[];
2020
@State() types: RdfType[];
21+
@State() relations: Relation[];
22+
@State() reverseRelations: Relation[];
2123

2224
private readonly disconnected$ = new Subject<void>();
2325

@@ -40,12 +42,10 @@ export class PosSwitch implements ResourceAware {
4042
state = this.types.map(x => x.uri).includes(caseElement.getAttribute('if-typeof'));
4143
}
4244
if (caseElement.getAttribute('if-property') !== null) {
43-
const relations = this.resource.relations(caseElement.getAttribute('if-property'));
44-
state = relations.length > 0;
45+
state = this.relations.map(x => x.predicate).includes(caseElement.getAttribute('if-property'));
4546
}
4647
if (caseElement.getAttribute('if-rev') !== null) {
47-
const reverseRelations = this.resource.reverseRelations(caseElement.getAttribute('if-rev'));
48-
state = reverseRelations.length > 0;
48+
state = this.reverseRelations.map(x => x.predicate).includes(caseElement.getAttribute('if-rev'));
4949
}
5050
if (caseElement.getAttribute('not') != null) {
5151
state = !state;
@@ -61,6 +61,20 @@ export class PosSwitch implements ResourceAware {
6161
});
6262
await firstValueFrom(observeTypes);
6363
}
64+
if (this.caseElements.some(caseElement => caseElement.hasAttribute('if-property'))) {
65+
const observeRelations = resource.observeRelations().pipe(takeUntil(this.disconnected$));
66+
observeRelations.subscribe(relations => {
67+
this.relations = relations;
68+
});
69+
await firstValueFrom(observeRelations);
70+
}
71+
if (this.caseElements.some(caseElement => caseElement.hasAttribute('if-rev'))) {
72+
const observeReverseRelations = resource.observeReverseRelations().pipe(takeUntil(this.disconnected$));
73+
observeReverseRelations.subscribe(reverseRelations => {
74+
this.reverseRelations = reverseRelations;
75+
});
76+
await firstValueFrom(observeReverseRelations);
77+
}
6478
this.resource = resource;
6579
};
6680

0 commit comments

Comments
 (0)