-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (68 loc) · 2.18 KB
/
index.js
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
import EventEmitter from 'node:events';
import RSS from './handlers/RSS.js';
import Worldstate from './handlers/Worldstate.js';
import Twitter from './handlers/Twitter.js';
import { logger } from './utilities/index.js';
export default class WorldstateEmitter extends EventEmitter {
#locale;
#worldstate;
#twitter;
#rss;
static async make({ locale } = { locale: undefined }) {
const emitter = new WorldstateEmitter({ locale });
await emitter.#init();
return emitter;
}
/**
* Pull in and instantiate emitters
* @param {string} locale language to restrict events to
*/
constructor({ locale } = { locale: undefined }) {
super();
this.#locale = locale;
}
async #init() {
this.#rss = new RSS(this);
this.#worldstate = new Worldstate(this, this.#locale);
await this.#worldstate.init();
this.#twitter = new Twitter(this);
logger.silly('hey look, i started up...');
this.setupLogging();
}
/**
* Set up internal logging
* @private
*/
setupLogging() {
this.on('error', logger.error);
this.on('rss', (body) => logger.silly(`emitted: ${body.id}`));
this.on('ws:update:raw', (body) => logger.silly(`emitted raw: ${body.platform}`));
this.on('ws:update:parsed', (body) => logger.silly(`emitted parsed: ${body.platform} in ${body.language}`));
this.on('ws:update:event', (body) =>
logger.silly(`emitted event: ${body.id} ${body.platform} in ${body.language}`)
);
this.on('tweet', (body) => logger.silly(`emitted: ${body.id}`));
}
/**
* Get current rss feed items
* @returns {Object} [description]
*/
getRss() {
return this.#rss.feeder.list.map((i) => ({ url: i.url, items: i.items }));
}
/**
* Get a specific worldstate, defaulting to 'pc' for the platform and 'en' for the language
* @param {string} [language='en'] locale/languate to fetch
* @returns {Object} Requested worldstate
*/
getWorldstate(language = 'en') {
return this.#worldstate?.get(language);
}
/**
* Get Twitter data
* @returns {Promise} promised twitter data
*/
async getTwitter() {
return this.#twitter?.clientInfoValid ? this.twitter.getData() : undefined;
}
}