Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | 122x 122x 122x 122x 122x 122x 122x 122x 122x 222x 222x 222x 222x 222x 222x 222x 5x 65x 65x 65x 65x 65x 65x 4x 4x 4x 4x 64x 64x 64x 64x 58x 6x 4x 1x 4x 64x 64x 64x 64x 58x 6x 64x 64x 64x 64x 4x 4x 1x 3x 11x 12x 73x 73x 1x 72x 1x 1x 1x 1x 71x 71x 71x 71x 71x 5x 71x 71x 71x 7x 7x 7x 7x 5x 2x | import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { delay } from 'lodash-es';
import { catchError, concat, map, Observable, of, shareReplay, Subject, switchMap, toArray } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Ext, mapExt, writeExt } from '../../model/ext';
import { mapPage, Page } from '../../model/page';
import { latest, TagPageArgs, TagQueryArgs } from '../../model/tag';
import { Store } from '../../store/store';
import { params } from '../../util/http';
import { OpPatch } from '../../util/json-patch';
import { hasPrefix, isQuery, localTag, protectedTag, removePrefix, tagOrigin } from '../../util/tag';
import { ConfigService } from '../config.service';
import { LoginService } from '../login.service';
export const EXT_CACHE_MS = 15 * 60 * 1000;
export const EXT_BATCH_THROTTLE_MS = 50;
export const EXT_BATCH_SIZE = 50;
@Injectable({
providedIn: 'root',
})
export class ExtService {
private _cache = new Map<string, Observable<Ext>>();
private _batchQueue: Array<{ key: string; tag: string; origin?: string; subject: Subject<Ext> }> = [];
private _batchTimer?: any;
constructor(
private http: HttpClient,
private config: ConfigService,
private login: LoginService,
private store: Store,
) { }
private get base() {
return this.config.api + '/api/v1/ext';
}
get init$() {
return this.loadExts$().pipe(
tap(() => this.store.local.loadExt([...this._cache.keys()])),
catchError(() => of(null)),
);
}
private loadExts$(page = 0): Observable<null> {
const alreadyLoaded = page * this.config.fetchBatch;
if (alreadyLoaded >= this.config.maxExts) {
console.error(`Too many exts to prefetch, only loaded ${alreadyLoaded}. Increase maxExts to load more.`)
return of(null);
}
const prefetch = this.store.local.extPrefetch.slice(page * this.config.fetchBatch, (page + 1) * this.config.fetchBatch);
const setOrigin = (t: string) => {
const [tag, origin] = t.split(':');
if (tag.includes('@')) return tag;
if ((origin || '') !== this.store.account.origin) {
return tag + (origin || '') + '|' + tag + this.store.account.origin;
}
return tag + this.store.account.origin;
};
return this.page({ query: prefetch.map(setOrigin).join('|'), size: 1000 }).pipe(
tap(batch => {
for (const key of prefetch) {
const [tag, defaultOrigin] = key.split(':');
if (!this._cache.has(key)) {
if (tag.includes('@')) {
this._cache.set(key, of(
batch.content.find(x => x.tag === localTag(tag) && x.origin === tagOrigin(tag))
|| this.defaultExt(tag)));
} else if (defaultOrigin) {
this._cache.set(key, of(
batch.content.find(x => x.tag === tag && x.origin === this.store.account.origin)
|| batch.content.find(x => x.tag === tag && x.origin === defaultOrigin)
|| latest(batch.content).find(x => x.tag === tag)
|| this.defaultExt(tag)));
} else {
this._cache.set(key, of(
batch.content.find(x => x.tag === tag && x.origin === this.store.account.origin)
|| latest(batch.content).find(x => x.tag === tag)
|| this.defaultExt(tag)));
}
}
}
}),
switchMap(batch => (page + 1) * this.config.fetchBatch >= this.store.local.extPrefetch.length ? of(null) : this.loadExts$(page + 1)),
);
}
create(ext: Ext): Observable<string> {
return this.http.post<string>(this.base, writeExt(ext)).pipe(
tap(() => this.clearCache(ext.tag, ext)),
catchError(err => this.login.handleHttpError(err)),
);
}
get(tag: string): Observable<Ext> {
return this.http.get(this.base, {
params: params({ tag }),
}).pipe(
map(mapExt),
tap(ext => {
this.prefillCache(ext);
this.store.local.loadExt([...this._cache.keys()]);
}),
catchError(err => this.login.handleHttpError(err)),
);
}
prefillCache(ext: Ext) {
const key1 = ext.tag + ext.origin + ':';
const key2 = ext.tag + ':' + ext.origin;
const value = of(ext);
this._cache.set(key1, value);
this._cache.set(key2, value);
delay(() => {
if (this._cache.get(key1) === value) this._cache.delete(key1);
if (this._cache.get(key2) === value) this._cache.delete(key2);
}, EXT_CACHE_MS);
}
private processBatch() {
Iif (this._batchQueue.length === 0) return;
// Take up to EXT_BATCH_SIZE items from the queue
const batch = this._batchQueue.splice(0, EXT_BATCH_SIZE);
// Build query string for all tags in the batch
const queries: string[] = [];
for (const item of batch) {
const tag = localTag(item.tag);
const origin = tagOrigin(item.tag);
Iif (origin) {
queries.push(tag + origin);
} else if (item.origin !== undefined) {
queries.push(tag + this.store.account.origin + '|' + tag + item.origin);
} else {
queries.push(tag + this.store.account.origin);
}
}
// Fetch all exts in a single page request
this.page({ query: queries.join('|'), size: EXT_BATCH_SIZE }).pipe(
catchError(() => of(Page.of([])))
).subscribe(result => {
// Process results for each item in the batch
for (const item of batch) {
const tag = localTag(item.tag);
const origin = tagOrigin(item.tag) || item.origin;
let ext: Ext | undefined;
Iif (tagOrigin(item.tag)) {
ext = result.content.find(x => x.tag === tag && x.origin === tagOrigin(item.tag));
} else if (item.origin !== undefined) {
ext = result.content.find(x => x.tag === tag && x.origin === this.store.account.origin)
|| result.content.find(x => x.tag === tag && x.origin === item.origin)
|| latest(result.content).find(x => x.tag === tag);
} else {
ext = result.content.find(x => x.tag === tag && x.origin === this.store.account.origin)
|| latest(result.content).find(x => x.tag === tag);
}
const resolvedExt = ext || this.defaultExt(item.tag, origin);
// Update cache with the resolved ext using the same logic as prefillCache()
this.prefillCache(resolvedExt);
// Emit the result to the subject
item.subject.next(resolvedExt);
item.subject.complete();
}
this.store.local.loadExt([...this._cache.keys()]);
});
// If there are more items in the queue, schedule another batch
if (this._batchQueue.length > 0) {
this._batchTimer = setTimeout(() => this.processBatch(), EXT_BATCH_THROTTLE_MS);
} else {
this._batchTimer = undefined;
}
}
getCachedExts(tags: string[], origin?: string): Observable<Ext[]> {
Iif (!tags) return of([]);
return concat(...tags.map(t => this.getCachedExt(t, origin))).pipe(toArray());
}
getCachedExt(tag: string, origin?: string) {
const key = tag + ':' + (origin || '');
// Return immediately if cached
if (this._cache.has(key)) {
return this._cache.get(key)!;
}
// For queries or empty tags, return default immediately
if (!tag || isQuery(tag)) {
const value = of(this.defaultExt(tag, origin));
this._cache.set(key, value);
this.store.local.loadExt([...this._cache.keys()]);
return value;
}
// For uncached exts, use batching mechanism
const subject = new Subject<Ext>();
const value = subject.asObservable().pipe(
shareReplay(1),
);
this._cache.set(key, value);
// Add to batch queue
this._batchQueue.push({ key, tag, origin, subject });
// Schedule batch processing if not already scheduled
if (!this._batchTimer) {
this._batchTimer = setTimeout(() => this.processBatch(), EXT_BATCH_THROTTLE_MS);
}
// Set cache expiration
delay(() => {
if (this._cache.get(key) === value) this._cache.delete(key);
}, EXT_CACHE_MS);
this.store.local.loadExt([...this._cache.keys()]);
return value;
}
defaultExt(tag: string, defaultOrigin = '', name = ''): Ext {
const origin = tagOrigin(tag) || defaultOrigin || '';
tag = localTag(tag);
name = name || hasPrefix(tag, 'user') ? removePrefix(protectedTag(tag) ? tag.substring(1) : tag) : name;
return { name, tag, origin };
}
clearCache(tag: string, ext?: Ext) {
for (const key of this._cache.keys()) {
if (key.startsWith(tag + ':') || key.startsWith(tag + '@')) this._cache.delete(key);
}
if (ext) this.prefillCache(ext);
}
page(args?: TagPageArgs): Observable<Page<Ext>> {
return this.http.get(`${this.base}/page`, {
params: params(args),
}).pipe(
map(mapPage(mapExt)),
catchError(err => this.login.handleHttpError(err)),
);
}
count(args?: TagQueryArgs): Observable<number> {
return this.http.get(`${this.base}/count`, {
responseType: 'text',
params: params(args),
}).pipe(
map(parseInt),
catchError(err => this.login.handleHttpError(err)),
);
}
update(ext: Ext): Observable<string> {
return this.http.put<string>(this.base, writeExt(ext)).pipe(
tap(() => this.clearCache(ext.tag, ext)),
catchError(err => this.login.handleHttpError(err)),
);
}
patch(tag: string, cursor: string, patch: OpPatch[]): Observable<string> {
return this.http.patch<string>(this.base, patch, {
headers: { 'Content-Type': 'application/json-patch+json' },
params: params({ tag, cursor }),
}).pipe(
tap(() => this.clearCache(localTag(tag))),
catchError(err => this.login.handleHttpError(err)),
);
}
merge(tag: string, cursor: string, patch: Partial<Ext>): Observable<string> {
return this.http.patch<string>(this.base, patch, {
headers: { 'Content-Type': 'application/merge-patch+json' },
params: params({ tag, cursor }),
}).pipe(
tap(() => this.clearCache(localTag(tag))),
catchError(err => this.login.handleHttpError(err)),
);
}
delete(tag: string): Observable<void> {
return this.http.delete<void>(this.base, {
params: params({ tag }),
}).pipe(
tap(() => this.clearCache(localTag(tag))),
catchError(err => this.login.handleHttpError(err)),
);
}
}
|