forked from xivanalysis/xivanalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.ts
215 lines (185 loc) · 6.63 KB
/
Module.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {MessageDescriptor} from '@lingui/core'
import {Ability, AbilityEvent, Pet, FflogsEvent} from 'fflogs'
import {Event} from 'legacyEvent'
import {cloneDeep} from 'lodash'
import 'reflect-metadata'
import {ensureArray} from 'utilities'
import {Injectable, MappedDependency, dependency, executeBeforeDoNotUseOrYouWillBeFired} from './Injectable'
import {EventHook, EventHookCallback, Filter, FilterPartial, TimestampHook, TimestampHookCallback} from './LegacyDispatcher'
import Parser from './Parser'
export enum DISPLAY_ORDER {
TOP = 0,
DEFAULT = 50,
BOTTOM = 100,
}
export enum DISPLAY_MODE {
COLLAPSIBLE,
FULL,
/** Don't use this unless you know what you're doing, and you've run it past me. */
RAW,
}
// Re-exports from injectable for back compat
export {dependency, executeBeforeDoNotUseOrYouWillBeFired}
export type {MappedDependency}
type ModuleFilter<T extends Event> = Filter<T> & FilterPartial<{
abilityId: Ability['guid'],
to: 'player' | 'pet' | FflogsEvent['targetID'],
by: 'player' | 'pet' | FflogsEvent['sourceID'],
}>
export default class Module extends Injectable {
static displayOrder: number = DISPLAY_ORDER.DEFAULT
static displayMode: DISPLAY_MODE
// TODO: Refactor this var
static i18n_id?: string // tslint:disable-line
private static _title: string | MessageDescriptor
static get title() {
if (!this._title) {
this._title = this.handle.charAt(0).toUpperCase() + this.handle.slice(1)
}
return this._title
}
static set title(value) {
this._title = value
}
constructor(
protected readonly parser: Parser,
) {
super({container: parser.container})
}
/**
* Because JS construct order is jank and nobody can fix it. Don't call this.
* Please. I'm begging you.
* @todo refactor `init` to public so this shit isn't needed.
*/
doTheMagicInitDance() {
this.init()
}
// So TS peeps don't need to pass the parser down
protected init() { /* noop */ }
normalise(events: Event[]): Event[] | Promise<Event[]> {
return events
}
/**
* This method is called when an error occurs, either when running
* event hooks or calling {@link Module#output} in this module or
* a module that depends on this module.
* @param source Either 'event' or 'output'
* @param error The error that occurred
* @param event The event that was being processed when the error occurred, if source is 'event'
* @returns The data to attach to automatic error reports, or undefined to rely on primitive value detection
*/
getErrorContext(_source: 'event' | 'output', _error: Error, _event?: Event): unknown {
return
}
/**
* Deprecated pass-through for `addEventHook`, maintained for backwards compatibility.
* @deprecated
*/
// tslint:disable-next-line:member-ordering
protected readonly addHook = this.addEventHook
protected addEventHook<T extends Event>(
events: T['type'] | ReadonlyArray<T['type']>,
cb: EventHookCallback<T>,
): Array<EventHook<T>>
protected addEventHook<T extends Event>(
events: T['type'] | ReadonlyArray<T['type']>,
filter: ModuleFilter<T>,
cb: EventHookCallback<T>,
): Array<EventHook<T>>
protected addEventHook<T extends Event>(
events: T['type'] | ReadonlyArray<T['type']>,
filterArg: ModuleFilter<T> | EventHookCallback<T>,
cbArg?: EventHookCallback<T>,
): Array<EventHook<T>> {
// I'm currently handling hooks at the module level
// Should performance become a concern, this can be moved up to the Parser without breaking the API
const cb = typeof filterArg === 'function'? filterArg : cbArg
let filter: ModuleFilter<T> = typeof filterArg === 'function'? {} : filterArg
// If there's no callback just... stop
if (!cb) { return [] }
const eventTypes = ensureArray(events)
// QoL filter transforms
filter = this.mapFilterEntity(filter, 'to', 'targetID')
filter = this.mapFilterEntity(filter, 'by', 'sourceID')
if (filter.abilityId !== undefined) {
const abilityFilter = filter as ModuleFilter<AbilityEvent>
if (!abilityFilter.ability) {
abilityFilter.ability = {}
}
abilityFilter.ability.guid = abilityFilter.abilityId
delete abilityFilter.abilityId
}
// Fflogs actor IDs are numbers, generic report structure actor IDs
// are strings. While we're trying to deal with both, normalise
// filter actor IDs to numbers to patch over the difference.
// TODO: REMOVE
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fixTypeDiscrepancy = (base: any): any =>
ensureArray(base).map(val => parseInt(val, 10))
if (filter.sourceID != null) {
filter.sourceID = fixTypeDiscrepancy(filter.sourceID)
}
if (filter.targetID != null) {
filter.targetID = fixTypeDiscrepancy(filter.targetID)
}
const hooks = eventTypes.map(event => ({
event,
filter,
module: (this.constructor as typeof Module).handle,
callback: cb.bind(this),
}))
hooks.forEach(hook => this.parser.legacyDispatcher.addEventHook(hook))
return hooks
}
// We don't talk about the type casts here
private mapFilterEntity<T extends Event>(
filterArg: ModuleFilter<T>,
qol: keyof ModuleFilter<T>,
raw: keyof FflogsEvent,
): ModuleFilter<T> {
if (!filterArg[qol]) { return filterArg }
const filter = cloneDeep(filterArg) as Filter<FflogsEvent>
// Sorry not sorry for the `any`s. Ceebs working out this filter _again_.
/* eslint-disable @typescript-eslint/no-explicit-any */
switch (filterArg[qol]) {
case 'player':
filter[raw] = this.parser.player.id as any
break
case 'pet':
filter[raw] = this.parser.player.pets.map((pet: Pet) => pet.id) as any
break
default:
filter[raw] = filterArg[qol] as any
}
/* eslint-enable @typescript-eslint/no-explicit-any */
delete filter[qol as keyof typeof filter]
return filter as ModuleFilter<T>
}
/**
* Deprecated pass-through for `removeEventHook`, maintained for backwards compatibility.
* @deprecated
*/
// tslint:disable-next-line:member-ordering
protected readonly removeHook = this.removeEventHook
/** Remove a previously added event hook. */
protected removeEventHook(hooks: Array<EventHook<Event>>) {
hooks.forEach(hook => this.parser.legacyDispatcher.removeEventHook(hook))
}
/** Add a hook for a timestamp. The provided callback will be fired a single time when the parser reaches the specified timestamp. */
protected addTimestampHook(timestamp: number, cb: TimestampHookCallback) {
const hook = {
timestamp,
module: (this.constructor as typeof Module).handle,
callback: cb.bind(this),
}
this.parser.legacyDispatcher.addTimestampHook(hook)
return hook
}
/** Remove a previously added timestamp hook */
protected removeTimestampHook(hook: TimestampHook) {
this.parser.legacyDispatcher.removeTimestampHook(hook)
}
output(): React.ReactNode {
return false
}
}