Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"packages/treecrdt-postgres-rs",
"packages/treecrdt-riblt-wasm",
"packages/treecrdt-sqlite-ext",
"packages/treecrdt-test-support",
"packages/treecrdt-wasm",
]
resolver = "2"
Expand Down
14 changes: 2 additions & 12 deletions examples/playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ export default function App() {
persistSyncSettings(syncServerUrl, syncTransportMode);
}, [syncServerUrl, syncTransportMode]);

const counterRef = useRef(0);
const lamportRef = useRef(0);
const initEpochRef = useRef(0);
const disposedRef = useRef(false);
Expand Down Expand Up @@ -483,18 +482,14 @@ export default function App() {
const active = nextClient ?? clientRef.current ?? client;
if (!active) return;
try {
const [lamport, counter] = await Promise.all([
active.meta.headLamport(),
replica ? active.meta.replicaMaxCounter(replica) : Promise.resolve(0),
]);
const lamport = await active.meta.headLamport();
lamportRef.current = Math.max(lamportRef.current, lamport);
setHeadLamport(lamportRef.current);
counterRef.current = Math.max(counterRef.current, counter);
} catch (err) {
console.error("Failed to refresh meta", err);
}
},
[client, replica]
[client]
);

const refreshParentsScheduledRef = useRef(false);
Expand Down Expand Up @@ -831,7 +826,6 @@ export default function App() {
setPayloadVersion((v) => v + 1);
knownOpsRef.current = new Set();
setCollapse({ defaultCollapsed: true, overrides: new Set([ROOT_ID]) });
counterRef.current = 0;
lamportRef.current = 0;
setHeadLamport(0);
setTotalNodes(null);
Expand Down Expand Up @@ -887,7 +881,6 @@ export default function App() {
await verifyLocalOps([op]);

lamportRef.current = Math.max(lamportRef.current, op.meta.lamport);
counterRef.current = Math.max(counterRef.current, op.meta.id.counter);
setHeadLamport(lamportRef.current);

notifyLocalUpdate([op]);
Expand Down Expand Up @@ -916,7 +909,6 @@ export default function App() {
scheduleRefreshParents(parentsAffectedByOps(stateBefore, [op]));
scheduleRefreshNodeCount();
lamportRef.current = Math.max(lamportRef.current, op.meta.lamport);
counterRef.current = Math.max(counterRef.current, op.meta.id.counter);
setHeadLamport(lamportRef.current);
} catch (err) {
console.error("Failed to append move op", err);
Expand Down Expand Up @@ -1014,7 +1006,6 @@ export default function App() {

for (const op of ops) {
lamportRef.current = Math.max(lamportRef.current, op.meta.lamport);
counterRef.current = Math.max(counterRef.current, op.meta.id.counter);
}
setHeadLamport(lamportRef.current);

Expand Down Expand Up @@ -1067,7 +1058,6 @@ export default function App() {
await ensureChildrenLoaded(parentId, { force: true });
}
lamportRef.current = Math.max(lamportRef.current, op.meta.lamport);
counterRef.current = Math.max(counterRef.current, op.meta.id.counter);
setHeadLamport(lamportRef.current);
setCollapse((prev) => {
const overrides = new Set(prev.overrides);
Expand Down
34 changes: 17 additions & 17 deletions examples/playground/src/playground/hooks/usePlaygroundSync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import type { Operation } from '@treecrdt/interface';
import { bytesToHex } from '@treecrdt/interface/ids';
import { type TreecrdtIdentityChainV1 } from '@treecrdt/auth';
Expand Down Expand Up @@ -285,6 +285,17 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
setPeers(merged);
};

const refreshLoadedSyncState = useCallback(
async (extraParentIds: Iterable<string> = []) => {
await refreshMeta();
const parentIds = new Set(Object.keys(treeStateRef.current.childrenByParent));
for (const parentId of extraParentIds) parentIds.add(parentId);
await refreshParents(Array.from(parentIds));
await refreshNodeCount();
},
[refreshMeta, refreshNodeCount, refreshParents, treeStateRef]
);

const isRemotePeerId = (peerId: string) => peerId.startsWith('remote:');
const syncOnceOptionsForPeer = (peerId: string, localCodewordsPerMessage: number) => ({
maxCodewords: PLAYGROUND_SYNC_MAX_CODEWORDS,
Expand Down Expand Up @@ -629,9 +640,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
if (lastErr) throw lastErr;
throw new Error('No peers responded to sync.');
}
await refreshMeta();
await refreshParents(Object.keys(treeStateRef.current.childrenByParent));
await refreshNodeCount();
await refreshLoadedSyncState();
} catch (err) {
console.error('Sync failed', err);
setSyncError(formatSyncError(err));
Expand Down Expand Up @@ -702,9 +711,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
if (lastErr) throw lastErr;
throw new Error('No peers responded to sync.');
}
await refreshMeta();
await refreshParents(Object.keys(treeStateRef.current.childrenByParent));
await refreshNodeCount();
await refreshLoadedSyncState();
} catch (err) {
console.error('Scoped sync failed', err);
setSyncError(formatSyncError(err));
Expand Down Expand Up @@ -787,11 +794,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
);
}

await refreshMeta();
const parentIds = new Set(Object.keys(treeStateRef.current.childrenByParent));
parentIds.add(viewRootId);
await refreshParents(Array.from(parentIds));
await refreshNodeCount();
await refreshLoadedSyncState([viewRootId]);

autoSyncDoneRef.current = true;
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -819,9 +822,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
authMaterial.localTokensB64.length,
autoSyncJoinTick,
joinMode,
refreshMeta,
refreshNodeCount,
refreshParents,
refreshLoadedSyncState,
syncBusy,
viewRootId,
]);
Expand Down Expand Up @@ -981,8 +982,7 @@ export function usePlaygroundSync(opts: UsePlaygroundSyncOptions): PlaygroundSyn
if (debugSync && ops.length > 0) {
console.debug(`[sync:${selfPeerId}] applyOps(${ops.length})`);
}
const affected =
ops.length > 0 ? ((await client.ops.appendMany(ops)) as unknown as string[]) : [];
const affected = ops.length > 0 ? await client.ops.appendMany(ops) : [];
await onRemoteOpsApplied(ops, affected);
},
};
Expand Down
13 changes: 9 additions & 4 deletions packages/treecrdt-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ pub use ids::{Lamport, NodeId, OperationId, ReplicaId};
pub use materialization::{
apply_incremental_ops_with_delta, apply_persisted_remote_ops_with_delta,
catch_up_materialized_state, materialize_persisted_remote_ops_with_delta,
IncrementalApplyResult, MaterializationCursor, MaterializationFrontier, MaterializationHead,
MaterializationKey, MaterializationState, PersistedRemoteApplyResult, PersistedRemoteStores,
orchestrate_persisted_remote_append, try_direct_rewind_catch_up_materialized_state,
try_shortcut_out_of_order_payload_noops, CatchUpResult, FrontierRewindStorage,
IncrementalApplyResult, MaterializationCursor, MaterializationFrontier,
MaterializationFrontierRef, MaterializationHead, MaterializationKey, MaterializationState,
MaterializationStateRef, PayloadNoopShortcut, PersistedRemoteApplyResult,
PersistedRemoteStores,
};
pub use ops::{cmp_op_key, cmp_ops, Operation, OperationKind, OperationMetadata};
pub use traits::{
Clock, IndexProvider, LamportClock, MemoryNodeStore, MemoryPayloadStore, MemoryStorage,
NodeStore, NoopParentOpIndex, NoopStorage, ParentOpIndex, PayloadStore, Storage,
Clock, ExactNodeStore, ExactPayloadStore, IndexProvider, LamportClock, MemoryNodeStore,
MemoryPayloadStore, MemoryStorage, NodeStore, NoopParentOpIndex, NoopStorage, ParentOpIndex,
PayloadStore, Storage, TruncatingParentOpIndex,
};
pub use tree::{
ApplyDelta, LocalFinalizePlan, LocalPlacement, NodeExport, NodeSnapshotExport, TreeCrdt,
Expand Down
Loading
Loading