@kizahasi/flocon-core
Advanced tools
Comparing version 0.6.3 to 0.6.4
@@ -9,3 +9,3 @@ import { CustomResult } from '@kizahasi/result'; | ||
export declare type ProtectedTransformParameters<TServerState, TFirstOperation, TSecondOperation> = DualKeyRecordOperation.ProtectedTransformParameters<TServerState, TFirstOperation, TSecondOperation>; | ||
export declare const restore: <TState, TDownOperation, TTwoWayOperation, TCustomError = string>({ nextState, downOperation, innerRestore, }: { | ||
export declare const restore: <TState, TDownOperation, TTwoWayOperation, TCustomError = string>({ nextState: unsafeNextState, downOperation: unsafeDownOperation, innerRestore, }: { | ||
nextState: StringKeyRecord<TState>; | ||
@@ -19,3 +19,3 @@ downOperation?: StringKeyRecord<TDownOperation> | undefined; | ||
}) => CustomResult<RestoreResult<StringKeyRecord<TState>, StringKeyRecord<TTwoWayOperation>>, string | TCustomError>; | ||
export declare const apply: <TState, TUpOperation, TCustomError = string>({ prevState, operation, innerApply, defaultState, }: { | ||
export declare const apply: <TState, TUpOperation, TCustomError = string>({ prevState: unsafePrevState, operation, innerApply, defaultState, }: { | ||
prevState: StringKeyRecord<TState>; | ||
@@ -30,3 +30,3 @@ operation?: StringKeyRecord<TUpOperation> | undefined; | ||
}) => CustomResult<StringKeyRecord<TState>, string | TCustomError>; | ||
export declare const applyBack: <TState, TDownOperation, TCustomError = string>({ nextState, operation, innerApplyBack, defaultState, }: { | ||
export declare const applyBack: <TState, TDownOperation, TCustomError = string>({ nextState: unsafeNextState, operation, innerApplyBack, defaultState, }: { | ||
nextState: StringKeyRecord<TState>; | ||
@@ -50,3 +50,3 @@ operation?: StringKeyRecord<TDownOperation> | undefined; | ||
}) => CustomResult<StringKeyRecord<TOperation> | undefined, string | TCustomError>; | ||
export declare const serverTransform: <TServerState, TFirstOperation, TSecondOperation, TCustomError = string>({ first, second, prevState, nextState, innerTransform, defaultState, }: { | ||
export declare const serverTransform: <TServerState, TFirstOperation, TSecondOperation, TCustomError = string>({ first: unsafeFirst, second: unsafeSecond, prevState: unsafePrevState, nextState: unsafeNextState, innerTransform, defaultState, }: { | ||
prevState: StringKeyRecord<TServerState>; | ||
@@ -53,0 +53,0 @@ nextState: StringKeyRecord<TServerState>; |
{ | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -29,4 +29,4 @@ import { CustomResult, Result } from '@kizahasi/result'; | ||
export const restore = <TState, TDownOperation, TTwoWayOperation, TCustomError = string>({ | ||
nextState, | ||
downOperation, | ||
nextState: unsafeNextState, | ||
downOperation: unsafeDownOperation, | ||
innerRestore, | ||
@@ -45,5 +45,7 @@ }: { | ||
> => { | ||
if (downOperation == null) { | ||
const nextState = recordToMap(unsafeNextState); | ||
if (unsafeDownOperation == null) { | ||
return Result.ok({ | ||
prevState: nextState, | ||
prevState: mapToRecord(nextState), | ||
twoWayOperation: undefined, | ||
@@ -53,7 +55,7 @@ }); | ||
const prevState = { ...nextState }; | ||
const twoWayOperation: StringKeyRecord<TTwoWayOperation> = {}; | ||
const prevState = new Map(nextState); | ||
const twoWayOperation = new Map<string, TTwoWayOperation>(); | ||
for (const [key, value] of recordToMap(downOperation)) { | ||
const nextStateElement = nextState[key]; | ||
for (const [key, value] of recordToMap(unsafeDownOperation)) { | ||
const nextStateElement = nextState.get(key); | ||
if (nextStateElement === undefined) { | ||
@@ -73,5 +75,5 @@ return Result.error(`tried to update "${key}", but nextState does not have such a key`); | ||
} | ||
prevState[key] = restored.value.prevState; | ||
prevState.set(key, restored.value.prevState); | ||
if (restored.value.twoWayOperation !== undefined) { | ||
twoWayOperation[key] = restored.value.twoWayOperation; | ||
twoWayOperation.set(key, restored.value.twoWayOperation); | ||
} | ||
@@ -81,7 +83,10 @@ break; | ||
return Result.ok({ prevState, twoWayOperation }); | ||
return Result.ok({ | ||
prevState: mapToRecord(prevState), | ||
twoWayOperation: mapToRecord(twoWayOperation), | ||
}); | ||
}; | ||
export const apply = <TState, TUpOperation, TCustomError = string>({ | ||
prevState, | ||
prevState: unsafePrevState, | ||
operation, | ||
@@ -101,9 +106,10 @@ innerApply, | ||
if (operation == null) { | ||
return Result.ok(prevState); | ||
return Result.ok(unsafePrevState); | ||
} | ||
const nextState = { ...prevState }; | ||
const prevState = recordToMap(unsafePrevState); | ||
const nextState = new Map(prevState); | ||
for (const [key, value] of recordToMap(operation)) { | ||
const prevStateElement = prevState[key] ?? defaultState; | ||
const prevStateElement = prevState.get(key) ?? defaultState; | ||
const newValue = innerApply({ | ||
@@ -117,11 +123,11 @@ operation: value, | ||
} | ||
nextState[key] = newValue.value; | ||
nextState.set(key, newValue.value); | ||
break; | ||
} | ||
return Result.ok(nextState); | ||
return Result.ok(mapToRecord(nextState)); | ||
}; | ||
export const applyBack = <TState, TDownOperation, TCustomError = string>({ | ||
nextState, | ||
nextState: unsafeNextState, | ||
operation, | ||
@@ -141,9 +147,10 @@ innerApplyBack, | ||
if (operation == null) { | ||
return Result.ok(nextState); | ||
return Result.ok(unsafeNextState); | ||
} | ||
const prevState = { ...nextState }; | ||
const nextState = recordToMap(unsafeNextState); | ||
const prevState = new Map(nextState); | ||
for (const [key, value] of recordToMap(operation)) { | ||
const nextStateElement = nextState[key] ?? defaultState; | ||
const nextStateElement = nextState.get(key) ?? defaultState; | ||
const oldValue = innerApplyBack({ | ||
@@ -157,7 +164,7 @@ operation: value, | ||
} | ||
prevState[key] = oldValue.value; | ||
prevState.set(key, oldValue.value); | ||
break; | ||
} | ||
return Result.ok(prevState); | ||
return Result.ok(mapToRecord(prevState)); | ||
}; | ||
@@ -185,3 +192,3 @@ | ||
const result: StringKeyRecord<TOperation> = {}; | ||
const result = new Map<string, TOperation>(); | ||
@@ -191,6 +198,6 @@ for (const [key, groupJoined] of groupJoinMap(recordToMap(first), recordToMap(second))) { | ||
case left: | ||
result[key] = groupJoined.left; | ||
result.set(key, groupJoined.left); | ||
continue; | ||
case right: | ||
result[key] = groupJoined.right; | ||
result.set(key, groupJoined.right); | ||
continue; | ||
@@ -207,3 +214,3 @@ case both: { | ||
if (update.value !== undefined) { | ||
result[key] = update.value; | ||
result.set(key, update.value); | ||
} | ||
@@ -214,3 +221,3 @@ continue; | ||
} | ||
return Result.ok(result); | ||
return Result.ok(mapToRecord(result)); | ||
}; | ||
@@ -226,6 +233,6 @@ | ||
>({ | ||
first, | ||
second, | ||
prevState, | ||
nextState, | ||
first: unsafeFirst, | ||
second: unsafeSecond, | ||
prevState: unsafePrevState, | ||
nextState: unsafeNextState, | ||
innerTransform, | ||
@@ -245,12 +252,15 @@ defaultState, | ||
}): CustomResult<StringKeyRecord<TFirstOperation> | undefined, string | TCustomError> => { | ||
if (second === undefined) { | ||
if (unsafeSecond === undefined) { | ||
return Result.ok(undefined); | ||
} | ||
const result: StringKeyRecord<TFirstOperation> = {}; | ||
const result = new Map<string, TFirstOperation>(); | ||
const prevState = recordToMap(unsafePrevState); | ||
const nextState = recordToMap(unsafeNextState); | ||
const first = unsafeFirst == null ? undefined : recordToMap(unsafeFirst); | ||
for (const [key, operation] of recordToMap(second)) { | ||
const innerPrevState = prevState[key] ?? defaultState; | ||
const innerNextState = nextState[key] ?? defaultState; | ||
const innerFirst = first == null ? undefined : first[key]; | ||
for (const [key, operation] of recordToMap(unsafeSecond)) { | ||
const innerPrevState = prevState.get(key) ?? defaultState; | ||
const innerNextState = nextState.get(key) ?? defaultState; | ||
const innerFirst = first == null ? undefined : first.get(key); | ||
@@ -269,6 +279,6 @@ const transformed = innerTransform({ | ||
if (transformedUpdate !== undefined) { | ||
result[key] = transformedUpdate; | ||
result.set(key, transformedUpdate); | ||
} | ||
} | ||
return Result.ok(result); | ||
return Result.ok(mapToRecord(result)); | ||
}; | ||
@@ -364,3 +374,3 @@ | ||
}): StringKeyRecord<TOperation> => { | ||
const result: StringKeyRecord<TOperation> = {}; | ||
const result = new Map<string, TOperation>(); | ||
for (const [key, value] of groupJoinMap(recordToMap(prevState), recordToMap(nextState))) { | ||
@@ -388,6 +398,6 @@ let prevState: TState | undefined = undefined; | ||
} | ||
result[key] = diffResult; | ||
result.set(key, diffResult); | ||
continue; | ||
} | ||
return result; | ||
return mapToRecord(result); | ||
}; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
3200810
34929