-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
ActorAbstractMediaTypedFixed.ts
74 lines (65 loc) 路 2.75 KB
/
ActorAbstractMediaTypedFixed.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type { IActionContext } from '@comunica/types';
import type { IActorArgsMediaTyped } from './ActorAbstractMediaTyped';
import { ActorAbstractMediaTyped } from './ActorAbstractMediaTyped';
export abstract class ActorAbstractMediaTypedFixed<HI, HT, HO> extends ActorAbstractMediaTyped<HI, HT, HO> {
public readonly mediaTypePriorities: Record<string, number>;
public readonly mediaTypeFormats: Record<string, string>;
public readonly priorityScale: number;
public constructor(args: IActorArgsMediaTypedFixed<HI, HT, HO>) {
super(args);
const scale: number = this.priorityScale || this.priorityScale === 0 ? this.priorityScale : 1;
if (this.mediaTypePriorities) {
for (const [ _index, [ key, value ]] of Object.entries(this.mediaTypePriorities).entries()) {
this.mediaTypePriorities[key] = scale * value;
}
}
this.mediaTypePriorities = Object.freeze(this.mediaTypePriorities);
this.mediaTypeFormats = Object.freeze(this.mediaTypeFormats);
}
public async testHandle(action: HI, mediaType: string | undefined, context: IActionContext): Promise<HT> {
if (!mediaType || !(mediaType in this.mediaTypePriorities)) {
throw new Error(`Unrecognized media type: ${mediaType}`);
}
return await this.testHandleChecked(action, context);
}
/**
* Check to see if this actor can handle the given action.
* The media type has already been checked before this is called.
*
* @param {ActionContext} context An optional context.
* @param {HI} action The action to test.
*/
public abstract testHandleChecked(action: HI, context: IActionContext): Promise<HT>;
public async testMediaType(_context: IActionContext): Promise<boolean> {
return true;
}
public async getMediaTypes(_context: IActionContext): Promise<Record<string, number>> {
return this.mediaTypePriorities;
}
public async testMediaTypeFormats(_context: IActionContext): Promise<boolean> {
return true;
}
public async getMediaTypeFormats(_context: IActionContext): Promise<Record<string, string>> {
return this.mediaTypeFormats;
}
}
export interface IActorArgsMediaTypedFixed<HI, HT, HO> extends IActorArgsMediaTyped<HI, HT, HO> {
/**
* A record of media types, with media type name as key, and its priority as value.
* Priorities are numbers between [0, 1].
* @range {json}
*/
mediaTypePriorities: Record<string, number>;
/**
* A record of media types, with media type name as key, and its format IRI as value.
* @range {json}
*/
mediaTypeFormats: Record<string, string>;
/**
* A multiplier for media type priorities.
* This can be used for keeping the original media types in place,
* but scaling all of their scores with a certain value.
* @range {double}
*/
priorityScale?: number;
}