vzcode 0.11.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vzcode",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "Multiplayer code editor system",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -18,6 +18,10 @@ dotenv.config({ path: '../../.env' });
18
18
  // The time in milliseconds by which auto-saving is debounced.
19
19
  const autoSaveDebounceTimeMS = 800;
20
20
 
21
+ // The time in milliseconds by which auto-saving is throttled
22
+ // when the user is interacting with the widgets in the editor.
23
+ const throttleTimeMS = 100;
24
+
21
25
  // Server port.
22
26
  const port = 3030;
23
27
 
@@ -69,7 +73,6 @@ let previousDocument = initialDocument;
69
73
 
70
74
  // Saves the files that changed.
71
75
  const save = () => {
72
- console.log('saving');
73
76
  const currentDocument = shareDBDoc.data;
74
77
 
75
78
  // Take a look at each file (key) previously and currently.
@@ -78,8 +81,8 @@ const save = () => {
78
81
  ...previousDocument.files,
79
82
  });
80
83
  for (const key of allKeys) {
81
- const previous = previousDocument[key];
82
- const current = currentDocument[key];
84
+ const previous = previousDocument.files[key];
85
+ const current = currentDocument.files[key];
83
86
 
84
87
  // If this file was neither created nor deleted...
85
88
  if (previous && current) {
@@ -119,31 +122,37 @@ const save = () => {
119
122
  // });
120
123
  // });
121
124
 
122
- const throttleTimeMS = 200;
123
-
124
125
  let lastExecutedTime = Date.now();
126
+ let lastChangeTimeout; // This timeout ensures the last change is saved
125
127
 
126
128
  // Function to throttle the saving
127
129
  function throttleSave() {
128
- console.log('throttling');
129
130
  const now = Date.now();
130
- if (now - lastExecutedTime > throttleTimeMS) {
131
+ const timeSinceLastExecution = now - lastExecutedTime;
132
+
133
+ if (timeSinceLastExecution > throttleTimeMS) {
131
134
  save();
132
135
  lastExecutedTime = now;
136
+ } else {
137
+ // Clear the previous timeout and set a new one to save the last change
138
+ clearTimeout(lastChangeTimeout);
139
+ lastChangeTimeout = setTimeout(() => {
140
+ save();
141
+ lastExecutedTime = now;
142
+ }, throttleTimeMS - timeSinceLastExecution);
133
143
  }
134
144
  }
135
145
 
136
146
  // Function to debounce the saving
137
147
  let debounceTimeout;
138
148
  function debounceSave() {
139
- console.log('debouncing');
140
149
  clearTimeout(debounceTimeout);
141
150
  debounceTimeout = setTimeout(save, autoSaveDebounceTimeMS);
142
151
  }
143
152
 
144
153
  // Subscribe to listen for modifications
145
154
  shareDBDoc.subscribe(() => {
146
- shareDBDoc.on('op', () => {
155
+ shareDBDoc.on('op', (op) => {
147
156
  if (shareDBDoc.data.isInteracting) {
148
157
  throttleSave();
149
158
  } else {