desktop: syncs on its own — at launch, soon after an edit, every few minutes and on focus
Android / Build, or is the channel already serving this? (push) Successful in 5s
CI & Build / Build now, or wait for Android? (push) Successful in 2s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Web typecheck and unit tests (push) Successful in 13s
CI & Build / Python tests (push) Successful in 14s
Desktop (Tauri) / Build, or is the channel already serving this? (push) Successful in 2s
CI & Build / integration (push) Successful in 47s
CI & Build / Build & push image (push) Skipped
Desktop (Tauri) / Web tests, clippy, Rust tests and rustfmt (push) Successful in 3m8s
Desktop (Tauri) / Windows installer (cross-compiled) (push) Successful in 2m53s
Desktop (Tauri) / Tauri desktop (Linux) (push) Successful in 4m47s
Desktop (Tauri) / Update manifest (push) Successful in 6s
Android / Kotlin + Rust (APK) (push) Successful in 11m34s
Android / Build, or is the channel already serving this? (push) Successful in 5s
CI & Build / Build now, or wait for Android? (push) Successful in 2s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Web typecheck and unit tests (push) Successful in 13s
CI & Build / Python tests (push) Successful in 14s
Desktop (Tauri) / Build, or is the channel already serving this? (push) Successful in 2s
CI & Build / integration (push) Successful in 47s
CI & Build / Build & push image (push) Skipped
Desktop (Tauri) / Web tests, clippy, Rust tests and rustfmt (push) Successful in 3m8s
Desktop (Tauri) / Windows installer (cross-compiled) (push) Successful in 2m53s
Desktop (Tauri) / Tauri desktop (Linux) (push) Successful in 4m47s
Desktop (Tauri) / Update manifest (push) Successful in 6s
Android / Kotlin + Rust (APK) (push) Successful in 11m34s
Until now the only caller of the sync engine was the "Sync now" button. A worker thread now owns every cycle (the button's included, so two never overlap): - launch: one cycle as the app opens; - edit: every 10s it reads a fingerprint of the pending set and sends when that moved. A fingerprint rather than "anything pending", because a rejected change stays pending and would otherwise be resent every tick forever; - timer: a pull every 5 minutes with nothing to send; - focus: at most once per 30s. Failed automatic cycles back off (doubling from 10s to 5 minutes). A panicking cycle counts as a failed one rather than ending the thread. Every cycle is emitted as inkwell://synced: the board reloads when the pull changed something, and the Sync screen shows the last automatic failure. No final push on quit; the launch cycle sends whatever was left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -157,6 +157,38 @@ export interface LinkInput {
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/** What started a sync cycle (autosync.rs). */
|
||||
export type SyncTrigger = "launch" | "edit" | "timer" | "focus" | "manual";
|
||||
|
||||
/**
|
||||
* The most recent cycle, automatic or not. The desktop syncs on its own (at launch,
|
||||
* soon after an edit, every few minutes and on focus), so most cycles have no
|
||||
* button press to report back to — this is how the screen hears about them.
|
||||
*/
|
||||
export interface LastCycle {
|
||||
at_ms: number;
|
||||
trigger: SyncTrigger;
|
||||
ok: boolean;
|
||||
error: string | null;
|
||||
/** Whether the pull changed anything this device shows. */
|
||||
changed: boolean;
|
||||
outcome: SyncOutcome | null;
|
||||
}
|
||||
|
||||
/** Fired at every window after each cycle, with a LastCycle as the payload. */
|
||||
const SYNCED_EVENT = "inkwell://synced";
|
||||
|
||||
/**
|
||||
* Run `handler` after every sync cycle. Returns an unlisten function, or a no-op
|
||||
* where there is no event API (the web build) — nothing syncs in the background
|
||||
* there, so there is nothing to hear.
|
||||
*/
|
||||
export async function onSynced(handler: (cycle: LastCycle) => void): Promise<() => void> {
|
||||
const events = window.__TAURI__?.event;
|
||||
if (!events) return () => {};
|
||||
return events.listen<LastCycle>(SYNCED_EVENT, (e) => handler(e.payload));
|
||||
}
|
||||
|
||||
export const sync = {
|
||||
/** Ask who's at an address without committing to anything. */
|
||||
probe: (url: string) => invoke<ProbeResult>("sync_probe", { url }),
|
||||
@@ -169,6 +201,8 @@ export const sync = {
|
||||
* without pushing first overwrites unsent local edits.
|
||||
*/
|
||||
now: () => invoke<SyncOutcome>("sync_now"),
|
||||
/** The last cycle, or null before the first one. */
|
||||
last: () => invoke<LastCycle | null>("sync_last"),
|
||||
hasPending: () => invoke<boolean>("sync_has_pending"),
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import EmptyState from "../components/EmptyState.vue";
|
||||
import FilterBar from "../components/FilterBar.vue";
|
||||
import NoteGrid from "../components/NoteGrid.vue";
|
||||
import NoteEditor from "../components/NoteEditor.vue";
|
||||
import { isDesktop, onCaptured, sync as syncBridge } from "../desktop/bridge";
|
||||
import { isDesktop, onCaptured, onSynced, sync as syncBridge } from "../desktop/bridge";
|
||||
|
||||
const notes = useNotesStore();
|
||||
const config = useConfigStore();
|
||||
@@ -233,15 +233,25 @@ onMounted(() => {
|
||||
// unregistered on the way out so a board that has been navigated away from does
|
||||
// not keep reloading itself.
|
||||
let stopCaptureListener: (() => void) | null = null;
|
||||
// The desktop syncs in the background (autosync.rs). A cycle that pulled something
|
||||
// changed SQLite under this board, so it reloads — only then, so the cycles that
|
||||
// change nothing (most of them) cost the board nothing.
|
||||
let stopSyncListener: (() => void) | null = null;
|
||||
onMounted(() => {
|
||||
void onCaptured(() => void reload()).then((stop) => {
|
||||
stopCaptureListener = stop;
|
||||
});
|
||||
void onSynced((cycle) => {
|
||||
if (cycle.changed) void reload();
|
||||
}).then((stop) => {
|
||||
stopSyncListener = stop;
|
||||
});
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener("keydown", onBoardKey);
|
||||
stopCaptureListener?.();
|
||||
stopSyncListener?.();
|
||||
ui.boardCardFocused = false;
|
||||
});
|
||||
watch([currentView, currentLabel, facetKey], reload);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { useUiStore } from "../stores/ui";
|
||||
import BaseButton from "../components/BaseButton.vue";
|
||||
import BaseInput from "../components/BaseInput.vue";
|
||||
@@ -7,12 +7,15 @@ import Icon from "../components/Icon.vue";
|
||||
import {
|
||||
SUGGESTED_CAPTURE_SHORTCUT,
|
||||
capture as captureBridge,
|
||||
onSynced,
|
||||
sync as syncBridge,
|
||||
updates as updateBridge,
|
||||
type CaptureShortcut,
|
||||
type Compatibility,
|
||||
type LastCycle,
|
||||
type ProbeResult,
|
||||
type RevokeOutcome,
|
||||
type SyncOutcome,
|
||||
type SyncStatus,
|
||||
type UpdateChannel,
|
||||
type UpdateStatus,
|
||||
@@ -48,6 +51,15 @@ const degraded = ref<string[]>([]);
|
||||
const syncing = ref(false);
|
||||
const syncError = ref("");
|
||||
const lastResult = ref("");
|
||||
// The most recent cycle, including the automatic ones nobody pressed a button for.
|
||||
const lastCycle = ref<LastCycle | null>(null);
|
||||
// Shown only for a failed AUTOMATIC cycle: a failed button press already says so in
|
||||
// `syncError`, and a success needs no more than the "Last synced" time moving.
|
||||
const autoError = computed(() =>
|
||||
lastCycle.value && !lastCycle.value.ok && lastCycle.value.trigger !== "manual"
|
||||
? lastCycle.value.error
|
||||
: null,
|
||||
);
|
||||
|
||||
/** Set only when unlinking left the token valid server-side (see revokeWarning). */
|
||||
const unlinkWarning = ref("");
|
||||
@@ -164,6 +176,7 @@ async function refresh() {
|
||||
try {
|
||||
status.value = await syncBridge.status();
|
||||
pending.value = await syncBridge.hasPending();
|
||||
lastCycle.value = await syncBridge.last();
|
||||
} catch {
|
||||
status.value = null;
|
||||
} finally {
|
||||
@@ -212,31 +225,37 @@ async function connect() {
|
||||
}
|
||||
}
|
||||
|
||||
/** Show what a cycle did. Used for the button's result and the background's alike. */
|
||||
function showOutcome(outcome: SyncOutcome) {
|
||||
status.value = outcome.status;
|
||||
const received = outcome.pull.notes_applied + outcome.pull.notes_deleted;
|
||||
const sent = outcome.push.created + outcome.push.applied;
|
||||
const blobs = outcome.pull.blobs_downloaded;
|
||||
const parts: string[] = [];
|
||||
if (sent > 0) parts.push(`sent ${sent}`);
|
||||
if (received > 0) parts.push(`received ${received}`);
|
||||
if (blobs > 0) parts.push(`${blobs} attachment${blobs === 1 ? "" : "s"}`);
|
||||
lastResult.value = parts.length ? `Synced — ${parts.join(", ")}.` : "Already up to date.";
|
||||
// Attachments that didn't arrive are retried next sync, so this is a note, not
|
||||
// an error — but saying nothing would leave a missing image unexplained.
|
||||
if (outcome.pull.blobs_failed > 0) {
|
||||
lastResult.value += ` ${outcome.pull.blobs_failed} attachment(s) didn't download — they'll retry on the next sync.`;
|
||||
}
|
||||
// Rejections are the server refusing a specific change — surfaced, never
|
||||
// swallowed, because only the person can resolve them. The background cycle does
|
||||
// not resend them on its own (autosync.rs), so this stays until they're fixed.
|
||||
syncError.value =
|
||||
outcome.push.rejected > 0
|
||||
? `${outcome.push.rejected} change(s) the server wouldn't accept: ${outcome.push.errors.join("; ")}`
|
||||
: "";
|
||||
}
|
||||
|
||||
async function syncNow() {
|
||||
syncing.value = true;
|
||||
syncError.value = "";
|
||||
try {
|
||||
const outcome = await syncBridge.now();
|
||||
status.value = outcome.status;
|
||||
showOutcome(await syncBridge.now());
|
||||
pending.value = await syncBridge.hasPending();
|
||||
const received = outcome.pull.notes_applied + outcome.pull.notes_deleted;
|
||||
const sent = outcome.push.created + outcome.push.applied;
|
||||
const blobs = outcome.pull.blobs_downloaded;
|
||||
const parts: string[] = [];
|
||||
if (sent > 0) parts.push(`sent ${sent}`);
|
||||
if (received > 0) parts.push(`received ${received}`);
|
||||
if (blobs > 0) parts.push(`${blobs} attachment${blobs === 1 ? "" : "s"}`);
|
||||
lastResult.value = parts.length ? `Synced — ${parts.join(", ")}.` : "Already up to date.";
|
||||
// Attachments that didn't arrive are retried next sync, so this is a note, not
|
||||
// an error — but saying nothing would leave a missing image unexplained.
|
||||
if (outcome.pull.blobs_failed > 0) {
|
||||
lastResult.value += ` ${outcome.pull.blobs_failed} attachment(s) didn't download — they'll retry on the next sync.`;
|
||||
}
|
||||
// Rejections are the server refusing a specific change — surfaced, never
|
||||
// swallowed, because only the person can resolve them.
|
||||
if (outcome.push.rejected > 0) {
|
||||
syncError.value = `${outcome.push.rejected} change(s) the server wouldn't accept: ${outcome.push.errors.join("; ")}`;
|
||||
}
|
||||
} catch (e) {
|
||||
syncError.value = String((e as Error)?.message ?? e);
|
||||
} finally {
|
||||
@@ -289,7 +308,23 @@ function fmt(iso: string | null): string {
|
||||
return new Date(iso).toLocaleString();
|
||||
}
|
||||
|
||||
onMounted(refresh);
|
||||
// A background cycle finished — redraw from it, so this screen is never staler
|
||||
// than the board behind it.
|
||||
let stopSyncListener: (() => void) | null = null;
|
||||
onMounted(() => {
|
||||
void refresh();
|
||||
void onSynced((cycle) => {
|
||||
lastCycle.value = cycle;
|
||||
if (cycle.outcome) showOutcome(cycle.outcome);
|
||||
void syncBridge
|
||||
.hasPending()
|
||||
.then((p) => (pending.value = p))
|
||||
.catch(() => {});
|
||||
}).then((stop) => {
|
||||
stopSyncListener = stop;
|
||||
});
|
||||
});
|
||||
onBeforeUnmount(() => stopSyncListener?.());
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -328,6 +363,10 @@ onMounted(refresh);
|
||||
Last synced {{ fmt(status?.last_sync_at ?? null) }}
|
||||
<span v-if="pending"> · unsent changes on this device</span>
|
||||
</p>
|
||||
<p class="mt-0.5 text-xs text-neutral-400">
|
||||
Syncs on its own while the app is open: at launch, soon after you edit, and every
|
||||
few minutes.
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex shrink-0 gap-2">
|
||||
<BaseButton :loading="syncing" @click="syncNow">Sync now</BaseButton>
|
||||
@@ -342,6 +381,10 @@ onMounted(refresh);
|
||||
This server doesn't support: {{ degraded.join(", ") }}. Everything else syncs normally.
|
||||
</p>
|
||||
<p v-if="syncError" class="mt-3 text-sm text-red-600 dark:text-red-400">{{ syncError }}</p>
|
||||
<p v-else-if="autoError" class="mt-3 text-xs text-amber-600 dark:text-amber-400">
|
||||
The last automatic sync ({{ new Date(lastCycle!.at_ms).toLocaleTimeString() }}) didn't
|
||||
go through: {{ autoError }} It will keep trying on its own.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<p class="text-xs text-neutral-400">
|
||||
|
||||
Reference in New Issue
Block a user