An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Update mobile sync button on data changes https://github.com/siyuan-note/siyuan/issues/19648
Danieldea65664ef9c
diff --git a/app/src/mobile/util/onMessage.ts b/app/src/mobile/util/onMessage.tsindex 2c002c6c79..e2d2293e70 100644--- a/app/src/mobile/util/onMessage.ts+++ b/app/src/mobile/util/onMessage.ts@@ -29,6 +29,7 @@ import {handleMobileKernelExit} from "./kernelExit";import {sanitizeKernelHTML} from "../../util/hostCapabilities";import {applyEntryVisibility} from "../../config/entryVisibility/runtime";import {removeMobileBacklinkContent} from "./backlinkPanels";+import {isPaidUser, needSubscribe} from "../../util/needSubscribe";let statusTimeout: number;const statusElement = document.querySelector("#status") as HTMLElement;@@ -40,6 +41,12 @@ const dispatchMobileSidePanelConfigChange = () => {export const onMessage = (app: App, data: IWebSocketData) => {if (data) {switch (data.cmd) {+ case "syncPending":+ document.getElementById("toolbarSync").classList.toggle("fn__none", !(data.data === true &&+ ((0 !== window.siyuan.config.sync.provider && isPaidUser()) ||+ (0 === window.siyuan.config.sync.provider && !needSubscribe(""))) &&+ window.siyuan.config.repo.key && window.siyuan.config.sync.enabled));+ break;case "databaseIndexCommit":processBacklinkIndexCommit(data.data);break;@@ -187,9 +194,6 @@ export const onMessage = (app: App, data: IWebSocketData) => {break;case"syncing":processSync(data);- if (data.code === 1) {- document.getElementById("toolbarSync").classList.add("fn__none");- }break;case "openFileById":openMobileFileById(app, data.data.id);diff --git a/kernel/model/sync.go b/kernel/model/sync.goindex 4ece7fe31a..676dc54dd6 100644--- a/kernel/model/sync.go+++ b/kernel/model/sync.go@@ -83,6 +83,7 @@ func SyncDataDownload() (err error) {}func syncDataDownloadLocked() (err error) {+ revision := pendingSync.begin()util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)now := util.CurrentTimeMillis()@@ -94,6 +95,7 @@ func syncDataDownloadLocked() (err error) {code = 2}util.BroadcastByType("main", "syncing", code, Conf.Sync.Stat, nil)+ pendingSync.finish(revision, err == nil, notifySyncPending)if 1 == code {consumeShorthands()}@@ -151,6 +153,7 @@ func SyncDataUpload() (err error) {return}defer unlock()+ revision := pendingSync.begin()util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)now := util.CurrentTimeMillis()@@ -162,10 +165,12 @@ func SyncDataUpload() (err error) {code = 2}util.BroadcastByType("main", "syncing", code, Conf.Sync.Stat, nil)+ pendingSync.finish(revision, err == nil, notifySyncPending)return}var (+ pendingSync syncPendingStatesyncSameCount = atomic.Int32{}autoSyncErrCount = 0fixSyncInterval = 5 * time.Minute@@ -209,6 +214,7 @@ func BootSyncData() {now := util.CurrentTimeMillis()Conf.Sync.Synced = now+ revision := pendingSync.begin()util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)err := bootSyncRepoWithDNSRetry()code := 1@@ -216,6 +222,7 @@ func BootSyncData() {code = 2}util.BroadcastByType("main", "syncing", code, Conf.Sync.Stat, nil)+ pendingSync.finish(revision, err == nil, notifySyncPending)if 1 == code {// 启动同步成功后消费本地速记临时文件,避免移动端开启云同步时需手动触发同步才能刷新闪念速记consumeShorthands()@@ -305,6 +312,7 @@ func syncData(exit, byHand bool) {}func syncDataLocked(exit, byHand bool) error {+ revision := pendingSync.begin()util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)if exit {ExitSyncSucc = 0@@ -324,6 +332,7 @@ func syncDataLocked(exit, byHand bool) error {code = 2}util.BroadcastByType("main", "syncing", code, Conf.Sync.Stat, nil)+ pendingSync.finish(revision, err == nil, notifySyncPending)if !exit && 1 == code {consumeShorthands()@@ -1059,6 +1068,11 @@ func loadSyncIgnoreLines() (ret []string, err error) {func IncSync() {syncSameCount.Store(0)planSyncAfter(time.Duration(Conf.Sync.Interval) * time.Second)+ pendingSync.change(notifySyncPending)+}++func notifySyncPending(pending bool) {+ util.BroadcastByType("main", "syncPending", 0, "", pending)}func planSyncAfter(d time.Duration) {diff --git a/kernel/model/sync_pending.go b/kernel/model/sync_pending.gonew file mode 100644index 0000000000..a3fb01b44b--- /dev/null+++ b/kernel/model/sync_pending.go@@ -0,0 +1,49 @@+package model++import (+ "sync"+ "time"+)++// syncPendingState 合并数据变更通知,并保留同步期间产生的新变更。+type syncPendingState struct {+ mu sync.Mutex+ revision uint64+ pending bool+ timer *time.Timer+}++func (s *syncPendingState) change(notify func(bool)) {+ s.mu.Lock()+ defer s.mu.Unlock()+ s.revision+++ s.pending = true+ if s.timer != nil {+ return+ }+ s.timer = time.AfterFunc(100*time.Millisecond, func() {+ s.mu.Lock()+ defer s.mu.Unlock()+ s.timer = nil+ notify(s.pending)+ })+}++func (s *syncPendingState) begin() uint64 {+ s.mu.Lock()+ defer s.mu.Unlock()+ return s.revision+}++func (s *syncPendingState) finish(revision uint64, success bool, notify func(bool)) {+ s.mu.Lock()+ defer s.mu.Unlock()+ if !success {+ return+ }+ if s.revision == revision {+ s.pending = false+ }+ // 通知与状态修改持有同一把锁,避免旧状态覆盖随后发生的新变更。+ notify(s.pending)+}diff --git a/kernel/model/sync_pending_test.go b/kernel/model/sync_pending_test.gonew file mode 100644index 0000000000..22872228c1--- /dev/null+++ b/kernel/model/sync_pending_test.go@@ -0,0 +1,113 @@+package model++import (+ "reflect"+ "sync"+ "testing"+ "testing/synctest"+ "time"+)++func TestSyncPendingCoalescesChanges(t *testing.T) {+ synctest.Test(t, func(t *testing.T) {+ var state syncPendingState+ var notifications []bool+ notify := func(pending bool) { notifications = append(notifications, pending) }+ for range 100 {+ state.change(notify)+ }+ time.Sleep(101 * time.Millisecond)+ synctest.Wait()+ if !reflect.DeepEqual(notifications, []bool{true}) {+ t.Fatalf("notifications = %v", notifications)+ }+ state.change(notify)+ time.Sleep(101 * time.Millisecond)+ synctest.Wait()+ if !reflect.DeepEqual(notifications, []bool{true, true}) {+ t.Fatalf("subsequent changes must notify again: %v", notifications)+ }+ })+}++func TestSyncPendingCompletion(t *testing.T) {+ for _, test := range []struct {+ name string+ success bool+ changed bool+ pending bool+ }{+ {"success", true, false, false},+ {"failure", false, false, true},+ {"change during success", true, true, true},+ {"change during failure", false, true, true},+ } {+ t.Run(test.name, func(t *testing.T) {+ synctest.Test(t, func(t *testing.T) {+ var state syncPendingState+ var notifications []bool+ notify := func(pending bool) { notifications = append(notifications, pending) }+ state.change(notify)+ revision := state.begin()+ if test.changed {+ state.change(notify)+ }+ state.finish(revision, test.success, notify)+ time.Sleep(101 * time.Millisecond)+ synctest.Wait()+ for _, pending := range notifications {+ if pending != test.pending {+ t.Fatalf("completion or delayed notification = %v, want %v", notifications, test.pending)+ }+ }+ state.finish(state.begin(), true, notify)+ if notifications[len(notifications)-1] {+ t.Fatal("next successful sync must clear pending changes")+ }+ })+ })+ }+}++func TestSyncPendingConcurrentChanges(t *testing.T) {+ synctest.Test(t, func(t *testing.T) {+ var state syncPendingState+ notify := func(bool) {}+ revision := state.begin()+ var workers sync.WaitGroup+ for range 100 {+ workers.Go(func() { state.change(notify) })+ }+ workers.Wait()+ state.finish(revision, true, func(pending bool) {+ if !pending {+ t.Fatal("concurrent changes must remain pending")+ }+ })+ time.Sleep(101 * time.Millisecond)+ synctest.Wait()+ })+}++func TestSyncPendingFailurePreservesFrontendHint(t *testing.T) {+ var state syncPendingState+ state.finish(state.begin(), false, func(bool) {+ t.Fatal("failed sync must not overwrite the frontend hint")+ })+}++func TestSyncPendingChangeAfterCompletion(t *testing.T) {+ synctest.Test(t, func(t *testing.T) {+ var state syncPendingState+ var notifications []bool+ notify := func(pending bool) { notifications = append(notifications, pending) }+ state.change(notify)+ state.finish(state.begin(), true, notify)+ state.change(notify)+ time.Sleep(101 * time.Millisecond)+ synctest.Wait()+ if !reflect.DeepEqual(notifications, []bool{false, true}) {+ t.Fatalf("new changes after completion must restore the hint: %v", notifications)+ }+ })+}