refactor character asset mapping and replicant sync persistence

This commit is contained in:
Pandipipas
2026-02-14 13:29:39 +01:00
parent f4eccf612f
commit 7c6086256d
2 changed files with 32 additions and 14 deletions
+10 -6
View File
@@ -43,6 +43,13 @@ export const syncStateWithReplicant = <T>(
storageKey?: string,
): void => {
const isApplyingReplicant = ref(false);
const persistSnapshot = (value: T): void => {
if (!storageKey) {
return;
}
writeStorageSnapshot(storageKey, value);
};
watch(
() => replicant?.data,
@@ -55,9 +62,7 @@ export const syncStateWithReplicant = <T>(
state.value = normalize(value);
isApplyingReplicant.value = false;
if (storageKey) {
writeStorageSnapshot(storageKey, state.value);
}
persistSnapshot(state.value);
},
{ deep: true, immediate: true },
);
@@ -65,14 +70,13 @@ export const syncStateWithReplicant = <T>(
watch(
state,
(value) => {
if (storageKey) {
writeStorageSnapshot(storageKey, value);
}
persistSnapshot(value);
if (isApplyingReplicant.value || !replicant) {
return;
}
// Replicants remain the source of truth for server/browser synchronization.
replicant.data = normalize(value);
replicant.save();
},