-
Notifications
You must be signed in to change notification settings - Fork 17
/
event-cache.ts
220 lines (208 loc) · 6.05 KB
/
event-cache.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
216
217
218
219
220
import {type Filter, Kind, type Event} from "nostr-tools";
export class EventCache {
eventsById: Map<string, Event> = new Map();
metadataByPubKey: Map<string, Event> = new Map();
contactsByPubKey: Map<string, Event> = new Map();
authorsKindsByPubKey: Map<string, Map<number, Event[]>> = new Map();
eventsByTags: Map<string, Event[]> = new Map();
#addEventToAuthorKindsByPubKey(event: Event) {
const kindsByPubKey = this.authorsKindsByPubKey.get(event.pubkey);
if (!kindsByPubKey) {
this.authorsKindsByPubKey.set(
event.pubkey,
new Map([[event.kind, [event]]])
);
} else {
const events = kindsByPubKey.get(event.kind);
if (!events) {
kindsByPubKey.set(event.kind, [event]);
} else {
if (event.kind === Kind.Metadata || event.kind === Kind.Contacts) {
if (event.created_at > events[0].created_at) {
events[0] = event;
}
} else {
events.push(event);
}
}
}
}
#addEventToEventsByTags(event: Event) {
for (const tag of event.tags) {
let tag2 = tag[0] + ":" + tag[1];
const events = this.eventsByTags.get(tag2);
if (events) {
events.push(event);
} else {
this.eventsByTags.set(tag2, [event]);
}
}
}
addEvent(event: Event) {
if (this.getEventById(event.id)) {
return;
}
this.eventsById.set(event.id, event);
if (event.kind === Kind.Metadata) {
this.metadataByPubKey.set(event.pubkey, event);
}
if (event.kind === Kind.Contacts) {
this.contactsByPubKey.set(event.pubkey, event);
}
this.#addEventToAuthorKindsByPubKey(event);
this.#addEventToEventsByTags(event);
}
getEventById(id: string): Event | undefined {
return this.eventsById.get(id);
}
hasEventById(id: string): boolean {
return this.eventsById.has(id);
}
#getCachedEventsByPubKeyWithUpdatedFilter(
filter: Filter & {
relay?: string;
noCache?: boolean;
}
): {filter: Filter & {relay?: string}; events: Set<Event>} | undefined {
if (
filter.noCache ||
!filter.authors ||
!filter.kinds ||
filter.kinds.find(
(kind) => kind !== Kind.Contacts && kind !== Kind.Metadata
) !== undefined
) {
return undefined;
}
const authors: string[] = [];
const events = new Set<Event>();
for (const author of filter.authors) {
let contactEvent;
if (filter.kinds.includes(Kind.Contacts)) {
contactEvent = this.contactsByPubKey.get(author);
if (!contactEvent) {
authors.push(author);
continue;
}
}
let metadataEvent;
if (filter.kinds.includes(Kind.Metadata)) {
metadataEvent = this.metadataByPubKey.get(author);
if (!metadataEvent) {
authors.push(author);
continue;
}
}
if (contactEvent) {
events.add(contactEvent);
}
if (metadataEvent) {
events.add(metadataEvent);
}
}
return {filter: {...filter, authors}, events};
}
#getCachedEventsByPubKeyWithUpdatedFilter2(
filter: Filter & {
relay?: string;
noCache?: boolean;
}
): {filter: Filter & {relay?: string}; events: Set<Event>} | undefined {
if (filter.noCache || !filter.authors) {
return undefined;
}
const events = new Set<Event>();
for (const author of filter.authors) {
if (filter.kinds) {
const kindsByPubKey = this.authorsKindsByPubKey.get(author);
if (kindsByPubKey) {
for (const kind of filter.kinds) {
const events2 = kindsByPubKey.get(kind);
if (events2) {
for (const event of events2) {
events.add(event);
}
}
}
}
} else {
const kindsByPubKey = this.authorsKindsByPubKey.get(author);
if (kindsByPubKey) {
for (const events2 of kindsByPubKey.values()) {
for (const event3 of events2) {
events.add(event3);
}
}
}
}
}
return {filter, events};
}
#getCachedEventsByTagsWithUpdatedFilter(
filter: Filter & {
relay?: string;
noCache?: boolean;
}
): {filter: Filter & {relay?: string}; events: Set<Event>} | undefined {
if (filter.noCache) {
return undefined;
}
const events = new Set<Event>();
for (const tag in filter) {
if (tag[0] !== "#") {
continue;
}
// @ts-ignore
let tag2 = tag.slice(1) + ":" + filter[tag][0];
const events2 = this.eventsByTags.get(tag2);
if (events2) {
for (const event of events2) {
events.add(event);
}
}
}
return {filter, events};
}
#getCachedEventsByIdWithUpdatedFilter(
filter: Filter & {relay?: string; noCache?: boolean}
): {filter: Filter & {relay?: string}; events: Set<Event>} | undefined {
if (!filter.ids) {
return undefined;
}
const events = new Set<Event>();
const ids: string[] = [];
for (const id of filter.ids) {
const event = this.getEventById(id);
if (event) {
events.add(event);
} else {
ids.push(id);
}
}
return {filter: {...filter, ids}, events};
}
getCachedEventsWithUpdatedFilters(
filters: (Filter & {relay?: string; noCache?: boolean})[],
relays: string[]
): {
filters: (Filter & {relay?: string})[];
events: Event[];
} {
const events: Set<Event> = new Set();
const new_filters: (Filter & {relay?: string})[] = [];
for (const filter of filters) {
const new_data = this.#getCachedEventsByIdWithUpdatedFilter(filter) ||
// this.#getCachedEventsByPubKeyWithUpdatedFilter(filter) ||
this.#getCachedEventsByPubKeyWithUpdatedFilter2(filter) ||
this.#getCachedEventsByTagsWithUpdatedFilter(filter) || {
filter,
events: [],
};
for (const event of new_data.events) {
events.add(event);
}
new_filters.push(new_data.filter);
}
return {filters: new_filters, events: [...events]};
}
}