-
Notifications
You must be signed in to change notification settings - Fork 121
/
MonitoringStore.ts
55 lines (47 loc) · 2.21 KB
/
MonitoringStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { EventEmitter } from 'events';
import type { Patch } from '../ldp/http/Patch';
import type { Representation } from '../ldp/representation/Representation';
import type { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import type { Conditions } from './Conditions';
import type { ResourceStore } from './ResourceStore';
/**
* Store that notifies listeners of changes to its source
* by emitting a `changed` event.
*/
export class MonitoringStore<T extends ResourceStore = ResourceStore>
extends EventEmitter implements ResourceStore {
private readonly source: T;
public constructor(source: T) {
super();
this.source = source;
}
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
conditions?: Conditions): Promise<Representation> {
return this.source.getRepresentation(identifier, preferences, conditions);
}
public async addResource(container: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ResourceIdentifier> {
const identifier = await this.source.addResource(container, representation, conditions);
this.emitChanged([ container, identifier ]);
return identifier;
}
public async deleteResource(identifier: ResourceIdentifier,
conditions?: Conditions): Promise<ResourceIdentifier[]> {
return this.emitChanged(await this.source.deleteResource(identifier, conditions));
}
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ResourceIdentifier[]> {
return this.emitChanged(await this.source.setRepresentation(identifier, representation, conditions));
}
public async modifyResource(identifier: ResourceIdentifier, patch: Patch,
conditions?: Conditions): Promise<ResourceIdentifier[]> {
return this.emitChanged(await this.source.modifyResource(identifier, patch, conditions));
}
private emitChanged(identifiers: ResourceIdentifier[]): typeof identifiers {
for (const identifier of identifiers) {
this.emit('changed', identifier);
}
return identifiers;
}
}