|
|
@@ -1,43 +1,59 @@
|
|
1
|
1
|
import { Singleton, Initializable } from "depents";
|
|
2
|
2
|
import { RPCExporter } from "rpclibrary";
|
|
3
|
3
|
import { CanvasState, Point, Stroke } from "../../../client/model/canvas-state";
|
|
4
|
|
-import { ListenCallbackParam } from "../../../client/model/rpc-callbacks";
|
|
|
4
|
+import { OnStrokeCallback } from "../../../client/model/rpc-callbacks";
|
|
|
5
|
+import * as fs from "fs";
|
|
|
6
|
+import * as path from "path";
|
|
5
|
7
|
|
|
6
|
8
|
@Singleton()
|
|
7
|
9
|
export class StateService implements Initializable, RPCExporter {
|
|
8
|
10
|
name = 'StateService' as const
|
|
9
|
11
|
|
|
10
|
12
|
private canvasState: CanvasState = { strokes: [] }
|
|
11
|
|
- private clients: Array<(param: ListenCallbackParam) => void> = []
|
|
|
13
|
+ private onStrokeCallbacks: Array<OnStrokeCallback> = []
|
|
12
|
14
|
|
|
13
|
15
|
initialize() {
|
|
14
|
16
|
this.canvasState = { strokes: [] }
|
|
15
|
|
- this.clients = []
|
|
|
17
|
+ this.onStrokeCallbacks = []
|
|
16
|
18
|
};
|
|
17
|
19
|
|
|
18
|
20
|
beginStroke = async (stroke: Stroke) => {
|
|
19
|
21
|
const strokeId = this.canvasState.strokes.length
|
|
20
|
22
|
this.canvasState.strokes.push(stroke)
|
|
21
|
|
- this.updateclients(strokeId, stroke)
|
|
|
23
|
+ this.updateStrokeListeners(strokeId, stroke)
|
|
22
|
24
|
return strokeId
|
|
23
|
25
|
}
|
|
24
|
26
|
|
|
25
|
27
|
addPoint = async (strokeId: number, point: Point) => {
|
|
26
|
28
|
this.canvasState.strokes[strokeId].points.push(point)
|
|
27
|
|
- this.updateclients(strokeId, { ...this.canvasState.strokes[strokeId], points: [point] })
|
|
|
29
|
+ this.updateStrokeListeners(strokeId, { ...this.canvasState.strokes[strokeId], points: [point] })
|
|
28
|
30
|
}
|
|
29
|
31
|
|
|
30
|
|
- listen = async (callback: (state: any) => Promise<void>) => {
|
|
31
|
|
- this.clients = [...this.clients, callback]
|
|
32
|
|
- return this.canvasState
|
|
|
32
|
+ onStroke = async (callback: OnStrokeCallback) => {
|
|
|
33
|
+ this.onStrokeCallbacks = [...this.onStrokeCallbacks, callback]
|
|
|
34
|
+ return
|
|
33
|
35
|
}
|
|
34
|
36
|
|
|
35
|
37
|
getState = async (): Promise<CanvasState> => {
|
|
36
|
38
|
return this.canvasState
|
|
37
|
39
|
}
|
|
38
|
40
|
|
|
39
|
|
- private updateclients = (strokeId: number, stroke: Stroke) => {
|
|
40
|
|
- this.clients.forEach((client) => {
|
|
|
41
|
+ finalizePicture = () => {
|
|
|
42
|
+ const finalState = { ...this.canvasState }
|
|
|
43
|
+ this.canvasState = { strokes: [] }
|
|
|
44
|
+
|
|
|
45
|
+ const picturedir = path.join(__dirname, `../../../../circles-pictures/`);
|
|
|
46
|
+
|
|
|
47
|
+ if (!fs.existsSync(picturedir)) {
|
|
|
48
|
+ fs.mkdirSync(picturedir);
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ fs.writeFileSync(path.join(picturedir, `${Date.now()}.json`), JSON.stringify(finalState))
|
|
|
52
|
+ console.log(finalState)
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ private updateStrokeListeners = (strokeId: number, stroke: Stroke) => {
|
|
|
56
|
+ this.onStrokeCallbacks.forEach((client) => {
|
|
41
|
57
|
client({ strokeId, stroke })
|
|
42
|
58
|
})
|
|
43
|
59
|
}
|
|
|
@@ -47,8 +63,8 @@ export class StateService implements Initializable, RPCExporter {
|
|
47
|
63
|
this.addPoint,
|
|
48
|
64
|
this.getState,
|
|
49
|
65
|
{
|
|
50
|
|
- name: 'listen' as const,
|
|
51
|
|
- hook: (cb: any) => { this.listen(cb) }
|
|
|
66
|
+ name: 'onStroke' as const,
|
|
|
67
|
+ hook: this.onStroke,
|
|
52
|
68
|
}
|
|
53
|
69
|
]
|
|
54
|
70
|
|