-
Notifications
You must be signed in to change notification settings - Fork 611
/
workspace_client_handlers.js
92 lines (85 loc) · 2.71 KB
/
workspace_client_handlers.js
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
/**
* @license
*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Websocket APIs for passing events between the client and the
* server.
* @author navil@google.com (Navil Perez)
*/
import * as Blockly from 'blockly/dist';
import io from 'socket.io-client';
const socket = io('http://localhost:3001');
/**
* A local representation of an entry in the database.
* @typedef {Object} LocalEntry
* @property {<!Array.<!Object>>} events An array of Blockly Events in JSON
* format.
* @property {string} entryId The id assigned to an event by the client.
*/
/**
* Query the database for entries since the given server id.
* @param {number} serverId serverId for the lower bound of the query.
* @return {!Promise} Promise object that represents the entries of events since
* the given serverId.
* @public
*/
export async function getEvents(serverId) {
return new Promise((resolve, reject) => {
socket.emit('getEvents', serverId, (entries) => {
entries.forEach((entry) => {
entry.events = entry.events.map((entry) => {
return Blockly.Events.fromJson(entry, Blockly.getMainWorkspace());
});
});
resolve(entries);
});
});
};
/**
* Add an entry to the database.
* @param {!LocalEntry} entry The entry to be added to the database.
* @return {!Promise} Promise object that represents the success of the write.
* @public
*/
export async function writeEvents(entry) {
const entryJson = {
entryId: entry.entryId,
events: entry.events.map((event) => event.toJson())
};
return new Promise((resolve, reject) => {
socket.emit('addEvents', entryJson, () => {
resolve();
});
});
};
/**
* Listen for events broadcast by the server.
* @param {!Function} callback The callback handler that passes the events to
* the workspace.
* @return {!Promise} Promise object that represents the success of the write.
* @public
*/
export function getBroadcast(callback) {
socket.on('broadcastEvents', (entries)=> {
entries.forEach((entry) => {
entry.events = entry.events.map((entry) => {
return Blockly.Events.fromJson(entry, Blockly.getMainWorkspace());
});
});
callback(entries);
});
};