MCPcopy
hub / github.com/directus/directus / constructor

Method constructor

api/src/websocket/collab/room.ts:198–305  ·  view source on GitHub ↗
(
		uid: string,
		collection: string,
		item: PrimaryKey | null,
		version: string | null,
		initialChanges?: Item,
		messenger: Messenger = new Messenger(),
	)

Source from the content-addressed store, hash-verified

196 onDeleteHandler: (meta: Record<string, any>, context?: any) => Promise<void>;
197
198 constructor(
199 uid: string,
200 collection: string,
201 item: PrimaryKey | null,
202 version: string | null,
203 initialChanges?: Item,
204 messenger: Messenger = new Messenger(),
205 ) {
206 this.uid = uid;
207 this.collection = collection;
208 this.item = item;
209 this.version = version;
210 this.initialChanges = initialChanges;
211 this.messenger = messenger;
212 this.store = useStore<RoomData>(uid, roomDefaults);
213
214 this.onUpdateHandler = async (meta) => {
215 const { keys } = meta as { keys: PrimaryKey[] };
216
217 const target = this.version ?? this.item;
218
219 // Skip updates for different items (singletons have item=null)
220 if (target !== null && !keys.some((key) => String(key) === String(target))) return;
221
222 try {
223 const schema = await getSchema();
224
225 const result = await (async () => {
226 if (this.version) {
227 const service = getService('directus_versions', { schema });
228 const versionData = await service.readOne(this.version);
229 return versionData['delta'] ?? {};
230 }
231
232 const service = getService(collection, { schema });
233 return item ? await service.readOne(item) : await service.readSingleton({});
234 })();
235
236 const clients = await this.store(async (store) => {
237 let changes = await store.get('changes');
238
239 changes = Object.fromEntries(
240 Object.entries(changes).filter(([key, value]) => {
241 // Always clear relational fields after save to prevent duplicate creation
242 if (isDetailedUpdateSyntax(value)) return false;
243
244 // Partial delta for versions and full record for regular items
245 if (!(key in result)) return !!this.version;
246
247 // For primitives, only clear if saved value matches pending change
248 if (isEqual(value, result[key])) return false;
249
250 // Reconcile M2O objects with the PK in result
251 if (isObject(value)) {
252 const relation = schema.relations.find((r: any) => r.collection === collection && r.field === key);
253
254 if (relation) {
255 const pkField = schema.collections[relation.related_collection as string]?.primary;

Callers

nothing calls this directly

Calls 13

sendMethod · 0.95
sendAllMethod · 0.95
closeMethod · 0.95
isDetailedUpdateSyntaxFunction · 0.90
isObjectFunction · 0.90
getSchemaFunction · 0.85
getServiceFunction · 0.85
useLoggerFunction · 0.85
useStoreFunction · 0.70
readOneMethod · 0.65
readSingletonMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected