NOT-OMEGA commited on
Commit
1c73348
·
verified ·
1 Parent(s): b36682c

Update document_store.hpp

Browse files
Files changed (1) hide show
  1. document_store.hpp +12 -15
document_store.hpp CHANGED
@@ -100,18 +100,11 @@ struct Document {
100
  {
101
  pending_broadcast.push_back(std::move(op_json));
102
 
103
- auto now = std::chrono::steady_clock::now();
104
- auto age_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
105
- now - last_flush).count();
106
-
107
- bool full = static_cast<int>(pending_broadcast.size()) >= BATCH_MAX_OPS;
108
- bool stale = age_ms >= BATCH_MAX_MS;
109
-
110
- if (full || stale) {
111
- flush_broadcast_locked(exclude_user);
112
- return true;
113
- }
114
- return false;
115
  }
116
 
117
  // Flush pending ops as a single "batch" message.
@@ -150,8 +143,12 @@ struct Document {
150
 
151
  // Call this AFTER releasing mu to fire any deferred broadcast callbacks.
152
  void fire_pending_broadcasts() {
153
- for (auto& cb : pending_after_unlock_) cb();
154
- pending_after_unlock_.clear();
 
 
 
 
155
  }
156
 
157
  nlohmann::json get_state_json() const;
@@ -203,4 +200,4 @@ private:
203
  // Global singleton
204
  inline DocumentStore g_store;
205
 
206
- } // namespace collab
 
100
  {
101
  pending_broadcast.push_back(std::move(op_json));
102
 
103
+ // Always flush immediately because we don't have a background timer thread.
104
+ // If we only flush when limits are reached, the last few operations
105
+ // will get stuck in the queue indefinitely.
106
+ flush_broadcast_locked(exclude_user);
107
+ return true;
 
 
 
 
 
 
 
108
  }
109
 
110
  // Flush pending ops as a single "batch" message.
 
143
 
144
  // Call this AFTER releasing mu to fire any deferred broadcast callbacks.
145
  void fire_pending_broadcasts() {
146
+ std::vector<std::function<void()>> cbs;
147
+ {
148
+ std::lock_guard<std::mutex> lk(mu);
149
+ cbs.swap(pending_after_unlock_);
150
+ }
151
+ for (auto& cb : cbs) cb();
152
  }
153
 
154
  nlohmann::json get_state_json() const;
 
200
  // Global singleton
201
  inline DocumentStore g_store;
202
 
203
+ } // namespace collab