Skip to content

Commit 60eb82f

Browse files
committed
Fix: use whole numbers for version, instead of numbers with fractional values.
1 parent fde9b8e commit 60eb82f

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

client/src/CodeChatEditor.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
UpdateMessageContents,
7171
CodeMirror,
7272
autosave_timeout_ms,
73+
rand,
7374
} from "./shared_types.mjs";
7475
import { show_toast } from "./show_toast.mjs";
7576

@@ -348,7 +349,7 @@ const save_lp = (is_dirty: boolean) => {
348349
update.contents = {
349350
metadata: current_metadata,
350351
source: code_mirror_diffable,
351-
version: Math.random(),
352+
version: rand(),
352353
};
353354
}
354355

client/src/shared_types.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
// =============================================
1919
// The time, in ms, to wait between the last user edit and sending updated data to the Server.
2020
export const autosave_timeout_ms = 300;
21-
//
21+
22+
// Produce a whole random number. Fractional numbers aren't consistently converted to the same number. Note that the mantissa of a JavaScript `Number` is 53 bits per the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#number_encoding). To be certain, also round the result.
23+
export const rand = () => Math.round(Math.random() * 2 ** 53);
24+
2225
// ### Message types
2326
//
2427
// These mirror the same definitions in the Rust webserver, so that the two can

extensions/VSCode/src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
autosave_timeout_ms,
4444
EditorMessage,
4545
MessageResult,
46+
rand,
4647
UpdateMessageContents,
4748
} from "../../../client/src/shared_types.mjs";
4849
import {
@@ -648,7 +649,7 @@ const send_update = (this_is_dirty: boolean) => {
648649
const file_path = ate.document.fileName;
649650
// Send contents only if necessary.
650651
const option_contents: null | [string, number] = is_dirty
651-
? [ate.document.getText(), (version = Math.random())]
652+
? [ate.document.getText(), (version = rand())]
652653
: null;
653654
is_dirty = false;
654655
console_log(

server/src/ide/filewatcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ async fn processing_task(
536536
doc: file_contents,
537537
doc_blocks: vec![],
538538
}),
539-
// The filewatcher doesn't store a version, since it only accepts plain (non-diff) results. Provide a version so the Client stays in sync with any diffs.
540-
version: random(),
539+
// The filewatcher doesn't store a version, since it only accepts plain (non-diff) results. Provide a version so the Client stays in sync with any diffs. Produce a whole number to avoid encoding difference with fractional values.
540+
version: random::<u64>() as f64,
541541
}),
542542
cursor_position: None,
543543
scroll_position: None,

server/src/translation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,8 @@ impl TranslationTask {
10461046
self.code_mirror_doc_blocks.as_ref().unwrap(),
10471047
)
10481048
{
1049-
cfw_version = random();
1049+
// Use a whole number to avoid encoding differences with fractional values.
1050+
cfw_version = random::<u64>() as f64;
10501051
// The Client needs an update.
10511052
let client_contents = self.diff_code_mirror(
10521053
cfw.metadata.clone(),

0 commit comments

Comments
 (0)