Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve concurrent editor input order

Daniel1e534539ecbd
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts
index 276bc03a76..8de4997c9c 100644
--- a/app/src/protyle/index.ts
+++ b/app/src/protyle/index.ts
@@ -336,6 +336,7 @@ export class Protyle {
this.protyle.preview.render(this.protyle);
return;
}
+ const hadContent = this.protyle.wysiwyg.element.childElementCount > 0;
let needCreateAction = "";
let hasDeleteOp = false;
data.data[0].doOperations.find((item: IOperation) => {
@@ -380,7 +381,8 @@ export class Protyle {
});
return;
}
- if (this.protyle.wysiwyg.element.childElementCount === 0 && this.protyle.block.parentID && needCreateAction) {
+ if (this.protyle.element.dataset.loading === "finished" && hadContent &&
+ this.protyle.wysiwyg.element.childElementCount === 0 && this.protyle.block.parentID && needCreateAction) {
if (needCreateAction === "delete" && this.protyle.block.showAll) {
if (this.protyle.options.handleEmptyContent) {
this.protyle.options.handleEmptyContent();
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 681394a642..273f771651 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -86,6 +86,8 @@ const syncFoldAttr = (element: Element, operation: IOperation) => {
});
};
+const transactionQueues = new WeakMap<IProtyle, Promise<void>>();
+
// 用于执行操作,外加处理当前编辑器中块引用、嵌入块的更新
const promiseTransaction = (options: {
protyle: IProtyle,
@@ -402,7 +404,8 @@ const promiseTransaction = (options: {
focusByWbr(emptyElement, range);
}
}
- fetchPost("/api/transactions", {
+ const previousTransaction = transactionQueues.get(protyle) || Promise.resolve();
+ const currentTransaction = previousTransaction.then(() => fetchPost("/api/transactions", {
session: protyle.id,
app: Constants.SIYUAN_APPID,
transactions: [{
@@ -431,7 +434,8 @@ const promiseTransaction = (options: {
}
});
options.callback?.();
- });
+ }));
+ transactionQueues.set(protyle, currentTransaction);
};
const containsOperationAnchor = (element: Element, operation: IOperation) => {
diff --git a/app/src/util/fetch.ts b/app/src/util/fetch.ts
index 9c452e6eb0..893032872f 100644
--- a/app/src/util/fetch.ts
+++ b/app/src/util/fetch.ts
@@ -42,7 +42,7 @@ export const fetchPost = (
init.signal = signal;
}
let isGetFile202 = false;
- fetch(url, init).then((response) => {
+ return fetch(url, init).then((response) => {
switch (response.status) {
case 403:
case 404:
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 1fab5cfaaa..4f2ecfcf5f 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -23,6 +23,7 @@ import (
"path"
"path/filepath"
"slices"
+ "sort"
"strings"
"sync"
"sync/atomic"
@@ -57,7 +58,7 @@ func IsMoveOutlineHeading(transactions *[]*Transaction) bool {
func FlushTxQueue() {
time.Sleep(time.Duration(50) * time.Millisecond)
- for 0 < len(txQueue) || isFlushing {
+ for 0 < txQueueSize() || isFlushing.Load() {
time.Sleep(10 * time.Millisecond)
}
}
@@ -72,9 +73,9 @@ func PerformTxSync(tx *Transaction) (err error) {
tx.m = &sync.Mutex{}
}
flushLock.Lock()
- isFlushing = true
+ isFlushing.Store(true)
defer func() {
- isFlushing = false
+ isFlushing.Store(false)
flushLock.Unlock()
}()
if txErr := performTx(tx); nil != txErr {
@@ -84,9 +85,10 @@ func PerformTxSync(tx *Transaction) (err error) {
}
var (
- txQueue = make(chan *Transaction, 7)
- flushLock = sync.Mutex{}
- isFlushing = false
+ txQueue []*Transaction
+ txQueueLock sync.Mutex
+ flushLock sync.Mutex
+ isFlushing atomic.Bool
)
func init() {
@@ -94,22 +96,22 @@ func init() {
}
func flushQueue() {
- for {
- select {
- case tx := <-txQueue:
+ ticker := time.NewTicker(50 * time.Millisecond)
+ defer ticker.Stop()
+ for range ticker.C {
+ flushLock.Lock()
+ isFlushing.Store(true)
+ transactions := takeQueuedTransactions()
+ for _, tx := range transactions {
flushTx(tx)
}
+ isFlushing.Store(false)
+ flushLock.Unlock()
}
}
func flushTx(tx *Transaction) {
defer logging.Recover()
- flushLock.Lock()
- isFlushing = true
- defer func() {
- isFlushing = false
- flushLock.Unlock()
- }()
start := time.Now()
if txErr := performTx(tx); nil != txErr {
@@ -150,10 +152,29 @@ func flushTx(tx *Transaction) {
}
func PerformTransactions(transactions *[]*Transaction) {
+ txQueueLock.Lock()
+ defer txQueueLock.Unlock()
for _, tx := range *transactions {
tx.m = &sync.Mutex{}
- txQueue <- tx
+ txQueue = append(txQueue, tx)
}
+ sort.SliceStable(txQueue, func(i, j int) bool {
+ return txQueue[i].Timestamp < txQueue[j].Timestamp
+ })
+}
+
+func takeQueuedTransactions() (ret []*Transaction) {
+ txQueueLock.Lock()
+ defer txQueueLock.Unlock()
+ ret = txQueue
+ txQueue = nil
+ return
+}
+
+func txQueueSize() int {
+ txQueueLock.Lock()
+ defer txQueueLock.Unlock()
+ return len(txQueue)
}
const (