-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathck-store.js
More file actions
308 lines (281 loc) · 12 KB
/
Copy pathck-store.js
File metadata and controls
308 lines (281 loc) · 12 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// ck-store.js — CK.Lib.Js L1: CKStore, the typed-instance cache (v1.5.0, dispatch-only).
//
// A Map of `@id`/URN → typed JSONB instance, fed ONLY by `ckp.dispatch` replies and granted-scope
// events. It is **explicitly not an RDF store**: no quads, no 6-way hex index, no DatasetCore, no
// SPARQL, no `toQuads`. It addresses URNs and typed instances — never triples, graphs, or storage
// layout (SPEC.CK-LIB-JS.v1.5.0 §0.1, §5 L1, §4.5).
//
// Projection rules carried from the former hex-store's *instance* projection (the *triple*
// projection is gone): replace-by-id, dedup-by-seq, recent ring.
//
// Exports: CKStore (default — the cache), CKSubject, CKView, ckBind.
const ID_KEYS = ['@id', 'id', 'urn'];
const TYPE_KEYS = ['@type', 'type'];
/** Extract the URN/@id that keys an instance in the cache. */
export function instanceUrn(inst) {
if (!inst || typeof inst !== 'object') return null;
for (const k of ID_KEYS) {
const v = inst[k];
if (typeof v === 'string' && v.length) return v;
}
return null;
}
/** Extract the concept type of an instance (`@type` preferred, `type` accepted). */
export function instanceType(inst) {
if (!inst || typeof inst !== 'object') return null;
for (const k of TYPE_KEYS) {
const v = inst[k];
if (typeof v === 'string' && v.length) return v;
}
return null;
}
/**
* The linked `@id` refs an instance carries — object refs are `{"@id": "..."}` id-nodes
* (SPEC §4.2). Scans top-level property values (and arrays of them) for id-nodes. Returns a
* de-duplicated array of referenced URNs the cache could traverse to. No triple/quad accessor.
*/
export function instanceEdges(inst) {
if (!inst || typeof inst !== 'object') return [];
const out = new Set();
const consider = (v) => {
if (v && typeof v === 'object' && typeof v['@id'] === 'string') out.add(v['@id']);
};
for (const [k, v] of Object.entries(inst)) {
if (ID_KEYS.includes(k) || TYPE_KEYS.includes(k)) continue;
if (Array.isArray(v)) v.forEach(consider);
else consider(v);
}
return [...out];
}
/**
* CKSubject — a synchronous, allocation-light view of ONE instance in the cache (pure read;
* no fetch). Carries no physical token. `.get()` returns the JSONB instance; `.edges()` returns
* the linked `@id` refs the cache has seen. There is no triple/quad accessor.
*/
export class CKSubject {
constructor(store, urn) {
this._store = store;
this.urn = urn;
}
exists() { return this._store._map.has(this.urn); }
get() { return this._store._map.get(this.urn) ?? null; }
edges() { return instanceEdges(this.get()); }
type() { return instanceType(this.get()); }
}
/**
* CKView — a reactive, typed-instance view over one URN. `.on('change', {added, removed})` fires
* microtask-batched (added/removed are the edge-ref delta); `.fetch()` dispatches `instance.get`
* through the store's dispatcher when one is wired; `.dispose()` tears the view down.
*/
export class CKView {
constructor(store, urn, opts = {}) {
this._store = store;
this.urn = urn;
this._opts = opts;
this._listeners = new Set();
this._lastEdges = new Set(instanceEdges(store._map.get(urn)));
store._attachView(this);
}
exists() { return this._store._map.has(this.urn); }
get() { return this._store._map.get(this.urn) ?? null; }
edges() { return instanceEdges(this.get()); }
on(event, cb) {
if (event === 'change' && typeof cb === 'function') this._listeners.add(cb);
return this;
}
off(event, cb) { this._listeners.delete(cb); return this; }
/** Dispatch `instance.get` through the store's wired dispatcher. */
async fetch() {
if (typeof this._store._dispatch !== 'function') {
throw new Error('CKView.fetch() requires a dispatcher wired into CKStore (set via the L2 handle)');
}
const reply = await this._store._dispatch('instance.get', { id: this.urn });
this._store.ingest(reply);
return this.get();
}
/** Internal: invoked by the store on a microtask flush when this URN changed. */
_notify() {
const next = new Set(instanceEdges(this.get()));
const added = [...next].filter((e) => !this._lastEdges.has(e));
const removed = [...this._lastEdges].filter((e) => !next.has(e));
this._lastEdges = next;
for (const cb of this._listeners) cb({ added, removed, urn: this.urn });
}
dispose() {
this._listeners.clear();
this._store._detachView(this);
}
}
/**
* ckBind — build a URN-pattern matcher used by `CKStore.bind`. Patterns:
* '*' → any instance
* 'ckp://Instance#<id>' → exact URN
* 'ckp://Kernel#<Name>' → instances whose URN/type names that kernel
* 'ckp://Edge#<predicate>' → instances carrying that predicate
* Returns `{ pattern, match(inst, urn) }`. Pure; carries no physical token.
*/
export function ckBind(pattern, opts = {}) {
if (pattern === '*' || pattern == null) {
return { pattern: '*', match: () => true, opts };
}
const hashIdx = pattern.indexOf('#');
const kind = hashIdx >= 0 ? pattern.slice(0, hashIdx) : '';
const tail = hashIdx >= 0 ? pattern.slice(hashIdx + 1) : pattern;
let match;
if (kind === 'ckp://Instance') {
match = (_inst, urn) => urn === pattern || urn === tail;
} else if (kind === 'ckp://Edge') {
match = (inst) => instanceEdges(inst).some((e) => e === tail || e.endsWith('#' + tail)) ||
Object.prototype.hasOwnProperty.call(inst || {}, tail);
} else if (kind === 'ckp://Kernel') {
match = (inst, urn) => (urn && urn.includes(tail)) || (instanceType(inst) || '').includes(tail);
} else {
// Bare string: treat as exact-URN or type match.
match = (inst, urn) => urn === pattern || instanceType(inst) === pattern;
}
return { pattern, match, opts };
}
/**
* CKStore — the typed-instance cache. Fed by dispatch replies + granted events via `ingest`.
* Reactive reads via `view` / `urn` / `bind`. Zero dependencies; no RDF surface.
*/
export class CKStore {
/**
* @param {object} [opts]
* @param {boolean} [opts.replaceById=true] replace an existing instance with the same @id
* @param {boolean} [opts.dedupBySeq=true] drop an incoming instance whose `seq` <= stored `seq`
* @param {number} [opts.recentCapacity=256] size of the recent-insert ring
* @param {function}[opts.dispatch] optional dispatcher wired by the L2 handle for `.fetch()`
*/
constructor(opts = {}) {
this.replaceById = opts.replaceById !== false;
this.dedupBySeq = opts.dedupBySeq !== false;
this.recentCapacity = Number.isInteger(opts.recentCapacity) ? opts.recentCapacity : 256;
this._dispatch = typeof opts.dispatch === 'function' ? opts.dispatch : null;
this._map = new Map(); // urn → instance
this._recent = []; // ring of recently-inserted urns (most recent last)
this._binds = new Set(); // { matcher, fn, once }
this._views = new Map(); // urn → Set<CKView>
this._pending = new Set(); // urns changed since last microtask flush
this._flushQueued = false;
}
// ── Ingest ─────────────────────────────────────────────────────────────────
/**
* Ingest a dispatch reply or a granted-scope event. Accepts:
* - a typed instance ({ '@id', ... })
* - a dispatch reply ({ ok, result, ... }) whose `result` is an instance or instance[]
* - an array of instances
* Returns the number of instances projected.
*/
ingest(payload) {
if (payload == null) return 0;
if (Array.isArray(payload)) {
let n = 0;
for (const p of payload) n += this.ingest(p);
return n;
}
if (typeof payload !== 'object') return 0;
// A dispatch reply envelope carries `ok`/`result`/`violations` and NO instance markers
// (`@id`/`@type`). Its top-level `id` is the affected instance's id, not an instance body —
// so we must detect the envelope *before* the looser id-key extraction.
const isEnvelope = !('@id' in payload) && !('@type' in payload) &&
('ok' in payload || 'result' in payload || 'violations' in payload);
if (isEnvelope) {
if (payload.ok === false) return 0;
return 'result' in payload ? this.ingest(payload.result) : 0;
}
return this._insert(payload);
}
_insert(inst) {
const urn = instanceUrn(inst);
if (!urn) return 0;
const prev = this._map.get(urn);
if (prev) {
if (this.dedupBySeq && Number.isFinite(inst.seq) && Number.isFinite(prev.seq) && inst.seq <= prev.seq) {
return 0; // stale — keep the newer sealed fact
}
this._map.set(urn, this.replaceById ? inst : { ...prev, ...inst });
} else {
this._map.set(urn, inst);
this._touchRecent(urn);
}
this._markChanged(urn);
return 1;
}
_touchRecent(urn) {
this._recent.push(urn);
if (this._recent.length > this.recentCapacity) {
const evicted = this._recent.shift();
// Recent ring bounds *iteration order memory*, not the cache itself; the map keeps the instance.
void evicted;
}
}
// ── Reads ──────────────────────────────────────────────────────────────────
get(urn) { return this._map.get(urn) ?? null; }
has(urn) { return this._map.has(urn); }
get size() { return this._map.size; }
all() { return [...this._map.values()]; }
/** Recently-ingested instances, newest last, bounded by recentCapacity. */
recent() { return this._recent.filter((u) => this._map.has(u)).map((u) => this._map.get(u)); }
/** Synchronous pure-cache subject for one URN (no fetch, no allocation of a reactive view). */
urn(urn) { return this._map.has(urn) ? new CKSubject(this, urn) : null; }
/** Reactive view over one URN. */
view(urn, opts = {}) { return new CKView(this, urn, opts); }
/**
* Bind a callback to instances matching a URN pattern, fired after ingest (microtask-batched).
* `pattern` is a string (see ckBind) or a prebuilt matcher from ckBind().
*/
bind(pattern, fn, opts = {}) {
const matcher = typeof pattern === 'object' && pattern.match ? pattern : ckBind(pattern, opts);
const entry = { matcher, fn, once: !!opts.once };
this._binds.add(entry);
return () => this._binds.delete(entry); // unbind
}
bindOnce(pattern, fn, opts = {}) { return this.bind(pattern, fn, { ...opts, once: true }); }
// ── Reactive plumbing (microtask-batched) ───────────────────────────────────
_attachView(view) {
if (!this._views.has(view.urn)) this._views.set(view.urn, new Set());
this._views.get(view.urn).add(view);
}
_detachView(view) {
const set = this._views.get(view.urn);
if (set) { set.delete(view); if (!set.size) this._views.delete(view.urn); }
}
_markChanged(urn) {
this._pending.add(urn);
if (!this._flushQueued) {
this._flushQueued = true;
queueMicrotask(() => this._flush());
}
}
_flush() {
const changed = [...this._pending];
this._pending.clear();
this._flushQueued = false;
for (const urn of changed) {
const inst = this._map.get(urn);
// Notify URN-pattern binds.
for (const entry of [...this._binds]) {
if (entry.matcher.match(inst, urn)) {
try { entry.fn(inst, urn); } finally { if (entry.once) this._binds.delete(entry); }
}
}
// Notify reactive views on this URN.
const views = this._views.get(urn);
if (views) for (const v of [...views]) v._notify();
}
}
// ── Lifecycle ───────────────────────────────────────────────────────────────
clear() {
this._map.clear();
this._recent = [];
this._pending.clear();
}
dispose() {
for (const set of this._views.values()) for (const v of [...set]) v.dispose();
this._binds.clear();
this._views.clear();
this.clear();
}
}
export default CKStore;