File size: 3,030 Bytes
4cadbaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

import {EventEmitter} from "events";
import sha1 from "sha1";
import * as diff from "diff";


export default class RemoteFile extends EventEmitter {
	autoReconnect: boolean;

	socket: WebSocket;
	connected: boolean = false;
	filePath: string;

	timestamp: number;
	_content: string;


	constructor ({autoReconnect = false} = {}) {
		super();

		this.autoReconnect = autoReconnect;
	}


	get hash (): string {
		return sha1(this._content) as string;
	}


	get content (): string {
		return this._content;
	}


	set content (value: string) {
		const timestamp = Date.now();

		const patch = diff.createPatch(this.filePath, this._content, value);

		this.socket.send(JSON.stringify({
			command: "increase",
			timestamp,
			fromHash: this.hash,
			toHash: sha1(value),
			patch,
		}));

		this.timestamp = timestamp;
		this._content = value;
	}


	connect (host: string, filePath: string) {
		if (this.socket)
			this.socket.close();

		this.socket = new WebSocket(host, "editor-frontend");

		this.socket.onopen = () => {
			console.debug("[RemoteFile]	socket open.");

			this.connected = true;
			this.emit("connected");

			this.socket.send(JSON.stringify({ command: "bindFile", filePath }));
		};
	
		this.socket.onclose = event => {
			console.debug("Synchronizer service socket closed:", event.code, event.reason);

			this.connected = false;
			this.emit("disconnected");

			if (this.autoReconnect && event.code === 1006) {
				console.log("[RemoteFile]	try to reconnect...");
				setTimeout(() => this.connect(host, filePath), 100);
			}
		};

		this.socket.onmessage = event => {
			const message = JSON.parse(event.data);
			switch (message.command) {
			case "failure":
				console.warn("service failure:", message.description);
				this.close();

				break;
			case "fullSync":
				this.timestamp = message.timestamp;
				this._content = message.content;
				console.assert(this.hash === message.hash, "[RemoteFile] verify failed:", this.hash, message.hash);

				this.emit("sync", {timestamp: this.timestamp});

				break;
			case "increase":
				//console.log("increase:", this.hash, message);

				// already consistent with remote, update timestemp only
				if (this.hash === message.toHash) {
					this.timestamp = Math.max(this.timestamp, message.timestamp);
					break;
				}

				if (this.hash !== message.fromHash) {
					if (message.timestamp < this.timestamp)
						break;

					console.warn("hash mismatched:", this.hash, message.fromHash);
					this.socket.send(JSON.stringify({ command: "requestFullSync", timestamp: this.timestamp}));
				}
				else {
					this.timestamp = message.timestamp;
					this._content = diff.applyPatch(this._content, message.patch);
					console.assert(this.hash === message.toHash, "[RemoteFile] verify failed:", this.hash, message.toHash);

					this.emit("sync", {timestamp: this.timestamp});
				}

				break;
			default:
				console.warn("[RemoteFile]	unexpected command:", message);
			}
		};

		this.filePath = filePath;
	}


	close () {
		this.socket.close();
	}
};