-
-
Notifications
You must be signed in to change notification settings - Fork 999
Expand file tree
/
Copy pathblock-model.ts
More file actions
51 lines (42 loc) · 1.49 KB
/
block-model.ts
File metadata and controls
51 lines (42 loc) · 1.49 KB
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
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { globalStore } from "@/app/store/jotaiStore";
import * as jotai from "jotai";
export interface BlockHighlightType {
blockId: string;
icon: string;
}
export class BlockModel {
private static instance: BlockModel | null = null;
private blockHighlightAtomCache = new Map<string, jotai.Atom<BlockHighlightType | null>>();
blockHighlightAtom: jotai.PrimitiveAtom<BlockHighlightType> = jotai.atom(null) as jotai.PrimitiveAtom<BlockHighlightType>;
private constructor() {
// Empty for now
}
getBlockHighlightAtom(blockId: string): jotai.Atom<BlockHighlightType | null> {
let atom = this.blockHighlightAtomCache.get(blockId);
if (!atom) {
atom = jotai.atom((get) => {
const highlight = get(this.blockHighlightAtom);
if (highlight?.blockId === blockId) {
return highlight;
}
return null;
});
this.blockHighlightAtomCache.set(blockId, atom);
}
return atom;
}
setBlockHighlight(highlight: BlockHighlightType | null) {
globalStore.set(this.blockHighlightAtom, highlight);
}
static getInstance(): BlockModel {
if (!BlockModel.instance) {
BlockModel.instance = new BlockModel();
}
return BlockModel.instance;
}
static resetInstance(): void {
BlockModel.instance = null;
}
}