Skip to content

Commit f66c4fb

Browse files
committed
[mergeable] getMergeableValuesDelta improvements
1 parent 72bc248 commit f66c4fb

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/mergeable-store.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Time,
1111
createMergeableStore as createMergeableStoreDecl,
1212
} from './types/mergeable-store';
13-
import {IdMap, mapEnsure, mapGet} from './common/map';
13+
import {IdMap, mapEnsure, mapGet, mapToObj} from './common/map';
1414
import {
1515
IdObj,
1616
objEnsure,
@@ -26,6 +26,7 @@ import {
2626
hashStampMapToObj,
2727
hashStampNewMap,
2828
hashStampNewThing,
29+
hashStampToStamp,
2930
stampNew,
3031
stampNewObj,
3132
updateHashStamp,
@@ -306,18 +307,18 @@ export const createMergeableStore = ((id: Id): MergeableStore => {
306307
const getMergeableValuesDelta = (
307308
valuesHsm: HashStamp<IdMap<HashStamp<ValueOrUndefined>>>,
308309
relativeTo: HashStamp<IdObj<HashStamp<ValueOrUndefined>>>,
309-
): Stamp<IdObj<Stamp<ValueOrUndefined>>> => {
310-
if (valuesHsm[2] == relativeTo?.[2]) {
311-
return stampNewObj();
312-
}
313-
const valuesDelta = {} as IdObj<Stamp<ValueOrUndefined>>;
314-
collForEach(valuesHsm[1], (valueHsm, valueId) =>
315-
valueHsm[2] === relativeTo?.[1]?.[valueId]?.[2]
316-
? 0
317-
: (valuesDelta[valueId] = [valueHsm[0], valueHsm[1]]),
318-
);
319-
return [valuesHsm[0], valuesDelta];
320-
};
310+
): Stamp<IdObj<Stamp<ValueOrUndefined>>> =>
311+
valuesHsm[2] == relativeTo?.[2]
312+
? stampNewObj()
313+
: [
314+
valuesHsm[0],
315+
mapToObj(
316+
valuesHsm[1],
317+
hashStampToStamp,
318+
(valueHsm, valueId) =>
319+
valueHsm[2] === relativeTo?.[1]?.[valueId]?.[2],
320+
),
321+
];
321322

322323
const setMergeableContent = (
323324
mergeableContent: MergeableContent,

src/mergeable-store/stamps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import {EMPTY_STRING} from '../common/strings';
55
import {Id} from '../types/common';
66
import {getHash} from './hash';
77

8+
export const hashStampToStamp = <Value>([
9+
time,
10+
value,
11+
]: HashStamp<Value>): Stamp<Value> => [time, value];
12+
813
const cloneHashStamp = <Value>(
914
[time, value, hash]: HashStamp<Value>,
1015
_id: Id = EMPTY_STRING,

0 commit comments

Comments
 (0)