Skip to content

Commit 86e5f06

Browse files
committed
add debounce
1 parent 0a5e5f0 commit 86e5f06

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/Application.vala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ namespace jorts {
183183

184184
random_data.zoom = this.latest_zoom;
185185
note = new MainWindow(this, random_data);
186-
187-
188-
189186
}
190187
this.open_notes.add(note);
191188
this.save_to_stash ();
@@ -202,6 +199,7 @@ namespace jorts {
202199
jorts.Stash.check_if_stash ();
203200
string json_data = jorts.Jason.jsonify (open_notes);
204201
jorts.Stash.overwrite_stash (json_data, jorts.Constants.FILENAME_STASH);
202+
print("Saved " + open_notes.size.to_string() + "!\n");
205203
}
206204

207205

src/MainWindow.vala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ namespace jorts {
6060
public string content;
6161
public int zoom;
6262

63+
public uint debounce_timer_id;
64+
6365
public SimpleActionGroup actions { get; construct; }
6466

6567
public const string ACTION_PREFIX = "app.";
@@ -146,7 +148,7 @@ namespace jorts {
146148
// Save when title has changed. And ALSO set the WM title so multitasking has the new one
147149
notetitle.changed.connect (() => {
148150
this.set_title(notetitle.text);
149-
((Application)this.application).save_to_stash ();
151+
on_buffer_changed();
150152
});
151153

152154
headerbar.set_title_widget(notetitle);
@@ -158,7 +160,7 @@ namespace jorts {
158160
view = new jorts.StickyView (this.content);
159161

160162
view.buffer.changed.connect (() => {
161-
((Application)this.application).save_to_stash ();
163+
on_buffer_changed();
162164
});
163165

164166
scrolled.set_child (view);
@@ -309,6 +311,19 @@ namespace jorts {
309311
/* METHODS */
310312
/********************************************/
311313

314+
// Add a debounce so we aren't writing the entire buffer every character input
315+
public void on_buffer_changed() {
316+
if (debounce_timer_id != 0) {
317+
GLib.Source.remove (debounce_timer_id);
318+
}
319+
320+
debounce_timer_id = Timeout.add (jorts.Constants.DEBOUNCE, () => {
321+
debounce_timer_id = 0;
322+
((Application)this.application).save_to_stash ();
323+
return GLib.Source.REMOVE;
324+
});
325+
}
326+
312327
// Called when a change in settings is detected
313328
public void on_scribbly_changed() {
314329
if (Application.gsettings.get_boolean ("scribbly-mode-active")) {

src/Services/Constants.vala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ namespace jorts.Constants {
3939
const string FILENAME_STASH = "saved_state.json";
4040
const string FILENAME_BACKUP = "backup_state.json";
4141

42+
// in ms
43+
const int DEBOUNCE = 1000;
44+
4245

4346
// We need to say stop at some point
4447
const int ZOOM_MAX = 240;

src/Services/Stash.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ namespace jorts.Stash {
121121

122122

123123
}
124-
print(loaded_data.size.to_string());
124+
print("Loaded" + loaded_data.size.to_string() + "!\n");
125125

126126
// If we load nothing: Fallback to a random with blue theme as first
127127
if (loaded_data.size == 0 ) {

0 commit comments

Comments
 (0)