This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
182 lines (139 loc) · 5.03 KB
/
app.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// <reference path="node-geogebra.d.ts" />
import { GGBPlotter } from 'node-geogebra';
import { EventEmitter } from 'events';
import { ActiveSession } from './types';
import { GGBConnectDatabase } from './database';
import * as SocketIo from 'socket.io';
export class GGBConnectApp {
private sessions: Map<string, ActiveSession>;
constructor(private db: GGBConnectDatabase, private io: SocketIo.Server) {
this.sessions = new Map<string, ActiveSession>();
}
public getSession(sessionId: string): ActiveSession | undefined {
return this.sessions.get(sessionId);
}
public async handshake(sessionId: string, version: string) {
/* Create session */
const sess = await this.db.getSession(sessionId, version);
/* Add session to store */
const plotter = new GGBPlotter({ ggb: 'local' });
this.sessions.set(sessionId, {
plotter,
id: sessionId,
});
/* Set perspective and add event listeners */
const page = await plotter.pagePromise;
page.exposeFunction('addListener', (...args: any[]) => {
this.io.to(sessionId).emit('add', ...args);
});
page.exposeFunction('removeListener', (...args: any[]) => {
this.io.to(sessionId).emit('remove', ...args);
});
page.exposeFunction('updateListener', async (objName: string) => {
const obj = await page.evaluate(
(objName) => {
return window.ggbApplet.getValueString(objName);
},
objName,
);
this.io.to(sessionId).emit('update', obj);
});
page.exposeFunction('renameListener', (...args: any[]) => {
this.io.to(sessionId).emit('rename', ...args);
});
const window: any = {};
page.evaluate(() => {
window.ggbApplet.setPerspective('T');
window.ggbApplet.registerAddListener('addListener');
window.ggbApplet.registerRemoveListener('removeListener');
window.ggbApplet.registerUpdateListener('updateListener');
window.ggbApplet.registerRenameListener('renameListener');
});
/* Done */
return {
sessionId,
};
}
public async command(sessionId: string, command: string): Promise<boolean> {
/* Get plotter or return false if not found */
const session = this.getSession(sessionId);
if (session === undefined) return false;
if (session.plotter === undefined) return false;
/* Eval command */
await session.plotter.evalGGBScript([command]);
return true;
}
public async exec(sessionId: string, prop: string, args: string[]): Promise<boolean> {
/* Get plotter or return false if not found */
const session = this.getSession(sessionId);
if (session === undefined || session.plotter === undefined) {
return false;
}
/* Eval command */
const page = await session.plotter.pagePromise;
const window: { [name: string]: any } = {};
const result = await page.evaluate(
(prop, args) => {
const func = window.ggbApplet[prop];
return (typeof func === 'function') ? func.apply(window.ggbApplet, args) : null;
},
prop,
args,
);
return true;
}
public async getBase64(sessionId: string): Promise<string | null> {
/* Get plotter */
const session = this.getSession(sessionId);
if (session === undefined) return null;
if (session.plotter === undefined) return null;
/* Export base64 in ggb format */
return session.plotter.exportGGB64();
}
public async getXml(sessionId: string): Promise<string | null> {
/* Get plotter */
const session = this.getSession(sessionId);
if (session === undefined) return null;
if (session.plotter === undefined) return null;
/* Export in XML format */
const page = await session.plotter.pagePromise;
const window: any = {};
return page.evaluate(() => {
return window.ggbApplet.getXML();
});
}
public async getPNG(sessionId: string): Promise<Buffer | null> {
/* Get plotter */
const session = this.getSession(sessionId);
if (session === undefined) return null;
if (session.plotter === undefined) return null;
/* Export base64 in ggb format */
return session.plotter.exportPNG(true, 90);
}
public async saveBase64(sessionId: string): Promise<string | null> {
/* Get base64 document */
const doc = await this.getBase64(sessionId);
if (doc === null) return null;
/* Write to database and return */
await this.db.updateDoc(sessionId, doc);
return doc;
}
public async saveXml(sessionId: string): Promise<string | null> {
/* Get base64 document */
const doc = await this.getXml(sessionId);
if (doc === null) return null;
/* Write to database and return */
await this.db.updateDoc(sessionId, doc);
return doc;
}
public async release(sessionId: string): Promise<boolean> {
/* Get plotter */
const session = this.getSession(sessionId);
if (session === undefined) return false;
if (session.plotter === undefined) return false;
/* Release plotter and remove session */
await session.plotter.release();
this.sessions.delete(sessionId);
return true;
}
}