MCPcopy
hub / github.com/tinyplex/tinybase / mergeCellsOrValues

Function mergeCellsOrValues

src/mergeable-store/index.ts:271–324  ·  view source on GitHub ↗
(
    things: typeof isContent extends 1
      ? Stamp<IdObj<Stamp<Thing, true>>, true>
      : Stamp<IdObj<Stamp<Thing>>>,
    thingsStampMap: StampMap<Stamp<Thing, true>>,
    thingsChanges: {[thingId: Id]: Thing},
    isContent: 0 | 1,
  )

Source from the content-addressed store, hash-verified

269 };
270
271 const mergeCellsOrValues = <Thing extends CellOrUndefined | ValueOrUndefined>(
272 things: typeof isContent extends 1
273 ? Stamp<IdObj<Stamp<Thing, true>>, true>
274 : Stamp<IdObj<Stamp<Thing>>>,
275 thingsStampMap: StampMap<Stamp<Thing, true>>,
276 thingsChanges: {[thingId: Id]: Thing},
277 isContent: 0 | 1,
278 ): [thingsHlc: Hlc, oldThingsHash: number, newThingsHash: number] => {
279 const [
280 thingsObj,
281 incomingThingsHlc = EMPTY_STRING,
282 incomingThingsHash = 0,
283 ] = things;
284 const [thingStampMaps, oldThingsHlc, oldThingsHash] = thingsStampMap;
285
286 let thingsHlc = incomingThingsHlc;
287 let thingsHash = isContent ? incomingThingsHash : oldThingsHash;
288
289 objForEach(
290 thingsObj,
291 ([thing, thingHlc = EMPTY_STRING, incomingThingHash = 0], thingId) => {
292 const thingStampMap = mapEnsure<Id, Stamp<Thing, true>>(
293 thingStampMaps,
294 thingId,
295 () => [undefined as any, EMPTY_STRING, 0],
296 );
297 const [, oldThingHlc, oldThingHash] = thingStampMap;
298
299 if (!oldThingHlc || thingHlc > oldThingHlc) {
300 stampUpdate(
301 thingStampMap,
302 thingHlc,
303 isContent ? incomingThingHash : getValueHash(thing, thingHlc),
304 );
305 thingStampMap[0] = thing;
306 thingsChanges[thingId] = thing;
307 thingsHash ^= isContent
308 ? 0
309 : addOrRemoveHash(
310 getValueInValuesHash(thingId, oldThingHash),
311 getValueInValuesHash(thingId, thingStampMap[2]),
312 );
313 thingsHlc = getLatestHlc(thingsHlc, thingHlc);
314 }
315 },
316 );
317
318 thingsHash ^= isContent
319 ? 0
320 : replaceHlcHash(oldThingsHlc, incomingThingsHlc);
321 stampUpdate(thingsStampMap, incomingThingsHlc, thingsHash);
322
323 return [thingsHlc, oldThingsHash, thingsStampMap[2]];
324 };
325
326 const preStartTransaction = noop;
327

Callers 1

mergeContentOrChangesFunction · 0.70

Calls 8

objForEachFunction · 0.90
mapEnsureFunction · 0.90
stampUpdateFunction · 0.90
getValueHashFunction · 0.90
addOrRemoveHashFunction · 0.90
getValueInValuesHashFunction · 0.90
getLatestHlcFunction · 0.90
replaceHlcHashFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…