Skip to content

Commit bc9c79e

Browse files
committed
feat(data-objectstack): answer bundled by-name metadata reads from the app bundle
loadAppBundle(appId) reads GET /meta/app/{appId}/bundle once per app entry and revalidates with If-None-Match; an object, view, page, dashboard or app the active bundle holds is answered from it and never asked by name. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibzFu8iUogiZhxL9tAo
1 parent 69ce01d commit bc9c79e

3 files changed

Lines changed: 598 additions & 26 deletions

File tree

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* The app metadata bundle (objectstack#27627): one read, when the console
11+
* enters an app, answers the by-name metadata reads of everything that app
12+
* reaches — the app, the objects its navigation and their inbound relations
13+
* reach, and their views, pages and dashboards.
14+
*
15+
* The bundle is the caller's own: the server builds every part through the
16+
* same per-caller read gate as the by-name read it replaces, and an item the
17+
* caller cannot read is simply absent from it. So:
18+
*
19+
* - an item the bundle HOLDS is answered from it, and never by a by-name
20+
* read (no dual path for a bundled item);
21+
* - an item it does NOT hold is the by-name route's to answer, refusal
22+
* included (Studio, objects outside the app, an item withheld from the
23+
* caller);
24+
* - re-entering an app, or reading past the adapter's cache lifetime, asks
25+
* again with `If-None-Match`: a `304` keeps what is held;
26+
* - a metadata write the adapter sees, or an invalidation a host asks for,
27+
* marks the bundles stale, and the next bundled read asks again first.
28+
*
29+
* Nothing here knows the transport: the adapter hands in one function that
30+
* asks the server and reports what it answered.
31+
*/
32+
33+
/** One bundled item, as the by-name route's envelope would carry it. */
34+
export interface AppBundleEntry {
35+
/** The item's document. */
36+
item: unknown;
37+
/** The envelope keys the by-name route serves beside `item` (an object's `sortability`, `disabledActions`). */
38+
envelope: Record<string, unknown>;
39+
}
40+
41+
/** The bundle's items, by metadata type then name. */
42+
export type AppBundleItems = Map<string, Map<string, AppBundleEntry>>;
43+
44+
/** What one bundle request obtained. */
45+
export type AppBundleAnswer =
46+
/** A `200`: the bundle, and its validator when the server sent one. */
47+
| { status: 'fresh'; etag?: string; items: AppBundleItems }
48+
/** A `304`: what is held is current. */
49+
| { status: 'unchanged' }
50+
/** No bundle for this app: refused, unknown, or no such route on this server. */
51+
| { status: 'absent' };
52+
53+
export type AppBundleRequest = (appId: string, etag: string | undefined) => Promise<AppBundleAnswer>;
54+
55+
interface AppBundleState {
56+
appId: string;
57+
etag?: string;
58+
items: AppBundleItems;
59+
/** When the server last answered for this bundle (a `200` or a `304`). */
60+
checkedAt: number;
61+
stale: boolean;
62+
inflight: Promise<void> | null;
63+
}
64+
65+
/**
66+
* The bundles one adapter has loaded, one per app entered this session; the
67+
* last app entered is the one bundled reads are answered from. The others are
68+
* kept so re-entering one revalidates instead of downloading it again.
69+
*/
70+
export class AppBundleStore {
71+
private readonly bundles = new Map<string, AppBundleState>();
72+
private active: string | null = null;
73+
74+
constructor(
75+
private readonly request: AppBundleRequest,
76+
/** How long an answer stands before a bundled read asks again (the adapter cache's lifetime). */
77+
private readonly ttlMs: number,
78+
) {}
79+
80+
/**
81+
* Enter `appId`: load its bundle, or revalidate the one held. Joins a load
82+
* already in flight. Never rejects — a bundle that could not be had leaves
83+
* every read to its by-name route.
84+
*/
85+
enter(appId: string): Promise<void> {
86+
if (!appId) return Promise.resolve();
87+
this.active = appId;
88+
const state = this.bundles.get(appId);
89+
if (state?.inflight) return state.inflight;
90+
return this.ask(appId);
91+
}
92+
93+
/**
94+
* The bundled answer for `type` / `name`, or `undefined` when the active
95+
* bundle does not hold it (or no app was entered). Waits for a load in
96+
* flight, and asks again first when the bundle is stale or past its lifetime.
97+
*/
98+
async read(type: string, name: string): Promise<AppBundleEntry | undefined> {
99+
const appId = this.active;
100+
if (!appId) return undefined;
101+
let state = this.bundles.get(appId);
102+
if (!state) return undefined;
103+
if (state.inflight) await state.inflight;
104+
else if (state.stale || Date.now() - state.checkedAt >= this.ttlMs) await this.ask(appId);
105+
state = this.bundles.get(appId);
106+
return state?.items.get(type)?.get(name);
107+
}
108+
109+
/** Whether any bundle held this session holds `type` / `name`. */
110+
holds(type: string, name: string): boolean {
111+
for (const state of this.bundles.values()) {
112+
if (state.items.get(type)?.has(name)) return true;
113+
}
114+
return false;
115+
}
116+
117+
/** Whether any bundle held this session holds an item of `type`. */
118+
holdsType(type: string): boolean {
119+
for (const state of this.bundles.values()) {
120+
if ((state.items.get(type)?.size ?? 0) > 0) return true;
121+
}
122+
return false;
123+
}
124+
125+
/** Every held bundle asks again before it answers its next read. */
126+
markStale(): void {
127+
for (const state of this.bundles.values()) state.stale = true;
128+
}
129+
130+
/**
131+
* Forget every bundle's items and validator, keeping the apps entered: the
132+
* world they were read in is gone (an organization, session or locale
133+
* switch), so the next bundled read downloads the active bundle again.
134+
*/
135+
clear(): void {
136+
for (const appId of Array.from(this.bundles.keys())) {
137+
this.bundles.set(appId, { appId, items: new Map(), checkedAt: 0, stale: true, inflight: null });
138+
}
139+
}
140+
141+
private ask(appId: string): Promise<void> {
142+
const held = this.bundles.get(appId);
143+
const state: AppBundleState = held ?? { appId, items: new Map(), checkedAt: 0, stale: true, inflight: null };
144+
this.bundles.set(appId, state);
145+
const inflight = this.request(appId, state.items.size > 0 ? state.etag : undefined)
146+
.catch((): AppBundleAnswer => ({ status: 'absent' }))
147+
.then((answer) => {
148+
// A `clear()` while this was out replaced the state: this answer
149+
// belongs to the world it left, so it is dropped.
150+
if (this.bundles.get(appId) !== state) return;
151+
state.inflight = null;
152+
state.checkedAt = Date.now();
153+
state.stale = false;
154+
if (answer.status === 'fresh') {
155+
state.items = answer.items;
156+
state.etag = answer.etag;
157+
} else if (answer.status === 'absent') {
158+
state.items = new Map();
159+
state.etag = undefined;
160+
}
161+
});
162+
state.inflight = inflight;
163+
return inflight;
164+
}
165+
}
166+
167+
/** Where a bundle answer lists each metadata type. */
168+
const BUNDLE_LISTS: ReadonlyArray<readonly [key: string, type: string]> = [
169+
['objects', 'object'],
170+
['views', 'view'],
171+
['pages', 'page'],
172+
['dashboards', 'dashboard'],
173+
];
174+
175+
/**
176+
* Index a bundle answer's data by type and name. Each listed part is either the
177+
* by-name route's envelope (`{ item, … }`) or the bare document; the app rides
178+
* under `app`.
179+
*/
180+
export function indexAppBundle(data: unknown): AppBundleItems {
181+
const items: AppBundleItems = new Map();
182+
if (!data || typeof data !== 'object') return items;
183+
const bag = data as Record<string, unknown>;
184+
const put = (type: string, part: unknown) => {
185+
const entry = toEntry(part);
186+
if (!entry) return;
187+
const name = (entry.item as { name?: unknown }).name;
188+
if (typeof name !== 'string' || !name) return;
189+
let byName = items.get(type);
190+
if (!byName) items.set(type, (byName = new Map()));
191+
byName.set(name, entry);
192+
};
193+
if (bag.app !== undefined) put('app', bag.app);
194+
for (const [key, type] of BUNDLE_LISTS) {
195+
const list = bag[key];
196+
if (Array.isArray(list)) for (const part of list) put(type, part);
197+
}
198+
return items;
199+
}
200+
201+
function toEntry(part: unknown): AppBundleEntry | undefined {
202+
if (!part || typeof part !== 'object') return undefined;
203+
const p = part as Record<string, unknown>;
204+
if ('item' in p) {
205+
if (!p.item || typeof p.item !== 'object') return undefined;
206+
return { item: p.item, envelope: p };
207+
}
208+
return { item: p, envelope: { item: p } };
209+
}

0 commit comments

Comments
 (0)