Skip to content

Commit

Permalink
[mergeable] getMergeableValuesDelta improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgpearce committed Mar 18, 2024
1 parent 72bc248 commit f66c4fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/mergeable-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Time,
createMergeableStore as createMergeableStoreDecl,
} from './types/mergeable-store';
import {IdMap, mapEnsure, mapGet} from './common/map';
import {IdMap, mapEnsure, mapGet, mapToObj} from './common/map';
import {
IdObj,
objEnsure,
Expand All @@ -26,6 +26,7 @@ import {
hashStampMapToObj,
hashStampNewMap,
hashStampNewThing,
hashStampToStamp,
stampNew,
stampNewObj,
updateHashStamp,
Expand Down Expand Up @@ -306,18 +307,18 @@ export const createMergeableStore = ((id: Id): MergeableStore => {
const getMergeableValuesDelta = (
valuesHsm: HashStamp<IdMap<HashStamp<ValueOrUndefined>>>,
relativeTo: HashStamp<IdObj<HashStamp<ValueOrUndefined>>>,
): Stamp<IdObj<Stamp<ValueOrUndefined>>> => {
if (valuesHsm[2] == relativeTo?.[2]) {
return stampNewObj();
}
const valuesDelta = {} as IdObj<Stamp<ValueOrUndefined>>;
collForEach(valuesHsm[1], (valueHsm, valueId) =>
valueHsm[2] === relativeTo?.[1]?.[valueId]?.[2]
? 0
: (valuesDelta[valueId] = [valueHsm[0], valueHsm[1]]),
);
return [valuesHsm[0], valuesDelta];
};
): Stamp<IdObj<Stamp<ValueOrUndefined>>> =>
valuesHsm[2] == relativeTo?.[2]
? stampNewObj()
: [
valuesHsm[0],
mapToObj(
valuesHsm[1],
hashStampToStamp,
(valueHsm, valueId) =>
valueHsm[2] === relativeTo?.[1]?.[valueId]?.[2],
),
];

const setMergeableContent = (
mergeableContent: MergeableContent,
Expand Down
5 changes: 5 additions & 0 deletions src/mergeable-store/stamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {EMPTY_STRING} from '../common/strings';
import {Id} from '../types/common';
import {getHash} from './hash';

export const hashStampToStamp = <Value>([
time,
value,
]: HashStamp<Value>): Stamp<Value> => [time, value];

const cloneHashStamp = <Value>(
[time, value, hash]: HashStamp<Value>,
_id: Id = EMPTY_STRING,
Expand Down

0 comments on commit f66c4fb

Please sign in to comment.