Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserSurfaceStore.ts

170 lines4.8 KB

1import { create } from "zustand";
2​
3export interface BrowserSurfaceRect {
4 readonly x: number;
5 readonly y: number;
6 readonly width: number;
7 readonly height: number;
8}
9​
10export interface BrowserSurfacePresentation {
11 readonly rect: BrowserSurfaceRect | null;
12 readonly visible: boolean;
13 readonly content: BrowserSurfaceContentPresentation | null;
14 readonly updatedAt: number;
15 readonly owner: symbol | null;
17​
18export interface BrowserSurfaceContentPresentation {
19 readonly x: number;
20 readonly y: number;
21 readonly width: number;
22 readonly height: number;
23 readonly scale: number;
24 readonly scrollLeft: number;
25 readonly scrollTop: number;
27​
28interface BrowserSurfaceStoreState {
29 readonly byTabId: Record<string, BrowserSurfacePresentation>;
30 readonly claim: (tabId: string, owner: symbol) => void;
31 readonly present: (
32 tabId: string,
33 owner: symbol,
34 rect: BrowserSurfaceRect,
35 visible: boolean,
36 ) => void;
37 readonly presentContent: (tabId: string, content: BrowserSurfaceContentPresentation) => void;
38 readonly release: (tabId: string, owner: symbol) => void;
40​
41export interface BrowserSurfaceLease {
42 readonly present: (rect: BrowserSurfaceRect, visible: boolean) => void;
43 readonly release: () => void;
45​
46export function resolveBrowserSurfacePanelRect(
47 byTabId: Readonly<Record<string, BrowserSurfacePresentation>>,
48 tabId: string,
49): BrowserSurfaceRect | null {
50 const current = byTabId[tabId];
51 if (current?.visible && current.rect) return current.rect;
52​
53 let latestVisible: BrowserSurfacePresentation | undefined;
54 for (const presentation of Object.values(byTabId)) {
55 if (
56 presentation.visible &&
57 presentation.rect &&
58 (!latestVisible || presentation.updatedAt > latestVisible.updatedAt)
59 ) {
60 latestVisible = presentation;
61 }
62 }
63 return latestVisible?.rect ?? current?.rect ?? null;
65​
66const rectEquals = (left: BrowserSurfaceRect | null, right: BrowserSurfaceRect): boolean =>
67 left !== null &&
68 left.x === right.x &&
69 left.y === right.y &&
70 left.width === right.width &&
71 left.height === right.height;
72​
73export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) => ({
74 byTabId: {},
75 claim: (tabId, owner) =>
76 set((state) => {
77 const current = state.byTabId[tabId];
78 if (current?.owner === owner) return state;
79 return {
80 byTabId: {
81 ...state.byTabId,
82 [tabId]: {
83 rect: current?.rect ?? null,
84 visible: false,
85 content: current?.content ?? null,
86 updatedAt: Date.now(),
87 owner,
88 },
89 },
90 };
91 }),
92 present: (tabId, owner, rect, visible) =>
93 set((state) => {
94 const current = state.byTabId[tabId];
95 if (current?.owner !== owner) return state;
96 if (current && current.visible === visible && rectEquals(current.rect, rect)) return state;
97 return {
98 byTabId: {
99 ...state.byTabId,
100 [tabId]: { ...current, rect, visible, updatedAt: Date.now() },
101 },
102 };
103 }),
104 presentContent: (tabId, content) =>
105 set((state) => {
106 const current = state.byTabId[tabId];
107 if (!current) {
108 return {
109 byTabId: {
110 ...state.byTabId,
111 [tabId]: {
112 rect: null,
113 visible: false,
114 content,
115 updatedAt: Date.now(),
116 owner: null,
117 },
118 },
119 };
120 }
121 const previous = current.content;
122 if (
123 previous &&
124 previous.x === content.x &&
125 previous.y === content.y &&
126 previous.width === content.width &&
127 previous.height === content.height &&
128 previous.scale === content.scale &&
129 previous.scrollLeft === content.scrollLeft &&
130 previous.scrollTop === content.scrollTop
131 ) {
132 return state;
133 }
134 return {
135 byTabId: {
136 ...state.byTabId,
137 [tabId]: { ...current, content, updatedAt: Date.now() },
138 },
139 };
140 }),
141 release: (tabId, owner) =>
142 set((state) => {
143 const current = state.byTabId[tabId];
144 if (current?.owner !== owner) return state;
145 return {
146 byTabId: {
147 ...state.byTabId,
148 [tabId]: { ...current, visible: false, updatedAt: Date.now(), owner: null },
149 },
150 };
151 }),
152}));
153​
154export function acquireBrowserSurface(tabId: string): BrowserSurfaceLease {
155 const owner = Symbol(`browser-surface:${tabId}`);
156 let released = false;
157 useBrowserSurfaceStore.getState().claim(tabId, owner);
158​
159 return {
160 present: (rect, visible) => {
161 if (released) return;
162 useBrowserSurfaceStore.getState().present(tabId, owner, rect, visible);
163 },
164 release: () => {
165 if (released) return;
166 released = true;
167 useBrowserSurfaceStore.getState().release(tabId, owner);
168 },
169 };