ionian/packages/server/src/diff/content_list.ts
2024-10-08 02:43:00 +09:00

59 lines
1.2 KiB
TypeScript

import type { ContentFile } from "../content/mod.ts";
export class ContentList {
/** path map */
private cl: Map<string, ContentFile>;
/** hash map */
private hl: Map<string, ContentFile>;
constructor() {
this.cl = new Map();
this.hl = new Map();
}
hasByHash(s: string) {
return this.hl.has(s);
}
hasByPath(p: string) {
return this.cl.has(p);
}
getByHash(s: string) {
return this.hl.get(s);
}
getByPath(p: string) {
return this.cl.get(p);
}
async set(c: ContentFile) {
const path = c.path;
const hash = await c.getHash();
this.cl.set(path, c);
this.hl.set(hash, c);
}
/** delete content file */
async delete(c: ContentFile) {
const hash = await c.getHash();
let r = true;
r = this.cl.delete(c.path) && r;
r = this.hl.delete(hash) && r;
return r;
}
async deleteByPath(p: string) {
const o = this.getByPath(p);
if (o === undefined) return false;
return await this.delete(o);
}
deleteByHash(s: string) {
const o = this.getByHash(s);
if (o === undefined) return false;
let r = true;
r = this.cl.delete(o.path) && r;
r = this.hl.delete(s) && r;
return r;
}
clear() {
this.cl.clear();
this.hl.clear();
}
getAll() {
return [...this.cl.values()];
}
}