1 | const pull = require('pull-stream');
|
2 | const get = require('lodash/get');
|
3 |
|
4 | const nameCache = {
|
5 |
|
6 | };
|
7 |
|
8 | let cacheClearerInitialised = false;
|
9 |
|
10 | module.exports = (sbot, getDisplayNameCb) => {
|
11 |
|
12 | function clearCacheEntriesOnNewName() {
|
13 | const aboutTypeStream = sbot.messagesByType({
|
14 | type: "about",
|
15 | live: true,
|
16 | gte: Date.now() - 60000
|
17 | })
|
18 |
|
19 | pull(aboutTypeStream, pull.drain(msg => {
|
20 | let about = get(msg, 'value.content.about');
|
21 |
|
22 | if (about && nameCache[about]) {
|
23 | delete nameCache[about];
|
24 | }
|
25 |
|
26 | }))
|
27 |
|
28 | }
|
29 |
|
30 | if (!cacheClearerInitialised) {
|
31 | cacheClearerInitialised = true;
|
32 | clearCacheEntriesOnNewName();
|
33 | }
|
34 |
|
35 | return {
|
36 | getDisplayName: (id, cb) => {
|
37 |
|
38 | const cacheHit = nameCache[id];
|
39 |
|
40 | if (cacheHit) {
|
41 | cb(null, cacheHit);
|
42 | } else {
|
43 | getDisplayNameCb(id, (err, result) => {
|
44 | nameCache[id] = result;
|
45 | cb(err, result);
|
46 | });
|
47 | }
|
48 |
|
49 | }
|
50 | }
|
51 | } |
\ | No newline at end of file |