文件
mynode/packages/tools/fuzzer/model/ResourceRecord.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

76 行
1.7 KiB
TypeScript

import { ItemId, ResourceData } from './types';
import Serializable, { BaseSchema } from './Serializable';
interface InitializationOptions extends ResourceData {
referencedBy: ItemId[];
}
const schema = {
isResource: 'boolean',
id: 'id',
title: 'string',
mimeType: 'string',
referencedBy: 'id[]',
} satisfies BaseSchema;
export default class ResourceRecord extends Serializable<typeof schema> implements ResourceData {
public readonly parentId: undefined;
public readonly id: ItemId;
public readonly title: string;
public readonly mimeType: string;
public readonly referencedBy: readonly ItemId[] = [];
public constructor(options: InitializationOptions) {
super(schema);
this.id = options.id;
this.title = options.title;
this.mimeType = options.mimeType;
this.referencedBy = [...options.referencedBy];
}
public static fromSerialized(serialized: unknown) {
const data = ResourceRecord.deserialize(schema, serialized);
return new ResourceRecord(data);
}
public serialize() {
return {
isResource: true,
id: this.id,
title: this.title,
referencedBy: [...this.referencedBy],
mimeType: this.mimeType,
};
}
public get referenceCount() {
return this.referencedBy.length;
}
public withReference(noteId: ItemId) {
if (this.referencedBy.includes(noteId)) {
return this;
}
return new ResourceRecord({
id: this.id,
title: this.title,
mimeType: this.mimeType,
referencedBy: [...this.referencedBy, noteId],
});
}
public withoutReference(noteId: ItemId) {
if (this.referencedBy.includes(noteId)) {
return this;
}
return new ResourceRecord({
id: this.id,
title: this.title,
mimeType: this.mimeType,
referencedBy: this.referencedBy.filter(ref => ref !== noteId),
});
}
}