topazcube 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topazcube",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "description": "TopazCube is a real-time collaborative document editing, and multiplayer game library.",
5
5
  "author": "László Matuska @BitOfGold",
6
6
  "license": "Apache-2.0",
package/src/server.ts CHANGED
@@ -97,10 +97,12 @@ export default class TopazCubeServer {
97
97
  documents: Record<string, any> = {}
98
98
  isLoading: Record<string, boolean> = {}
99
99
  _documentChanges: Record<string, any[]> = {}
100
+ _documentChanged: Record<string, boolean> = {}
100
101
  _documentState: Record<string, any> = {}
101
102
 
102
103
  update = 0
103
104
  lastUpdate = 0
105
+ _saveiv: any = null
104
106
  _loopiv: any = null
105
107
  _statsiv: any = null
106
108
  _stillUpdating = false
@@ -286,6 +288,7 @@ export default class TopazCubeServer {
286
288
  )
287
289
  if (!this._documentChanges[name]) {
288
290
  this._documentChanges[name] = []
291
+ this._documentChanged[name] = false
289
292
  }
290
293
  }
291
294
 
@@ -350,6 +353,9 @@ export default class TopazCubeServer {
350
353
  this._statsiv = setInterval(() => {
351
354
  this._doStats()
352
355
  }, 1000)
356
+ this._saveiv = setInterval(() => {
357
+ this._saveChanges()
358
+ }, 60000)
353
359
  }
354
360
 
355
361
  _loop(): void {
@@ -463,12 +469,14 @@ export default class TopazCubeServer {
463
469
  let name = message.n
464
470
  if (!this._documentChanges[name]) {
465
471
  this._documentChanges[name] = []
472
+ this._documentChanged[name] = false
466
473
  }
467
474
  for (let op of message.p) {
468
475
  if (!this.canSync(client, name, op)) {
469
476
  continue
470
477
  }
471
478
  this._documentChanges[name].push(op)
479
+ this._documentChanged[name] = true
472
480
  let dop = msgop(op)
473
481
  applyOperation(this.documents[name], dop)
474
482
  }
@@ -788,6 +796,7 @@ export default class TopazCubeServer {
788
796
 
789
797
  _onDocumentChange(name: string, op: any, target: any, path: any, value: any): void {
790
798
  this._documentChanges[name]?.push(opmsg(op, target, path, value))
799
+ this._documentChanged[name] = true
791
800
  }
792
801
 
793
802
  propertyChange(name: string, id: string | number, property: string): void {
@@ -1154,6 +1163,18 @@ export default class TopazCubeServer {
1154
1163
  }
1155
1164
  }
1156
1165
 
1166
+ async _saveChanges(): Promise<void> {
1167
+ if (!this.allowSave) {
1168
+ return
1169
+ }
1170
+ for (let name in this._documentChanged) {
1171
+ if (this._documentChanged[name]) {
1172
+ await this._saveDocument(name)
1173
+ this._documentChanged[name] = false
1174
+ }
1175
+ }
1176
+ }
1177
+
1157
1178
  _initServerDocument(): void {
1158
1179
  this.documents['_server'] = {
1159
1180
  nextUID: 100,
@@ -1167,6 +1188,8 @@ export default class TopazCubeServer {
1167
1188
  this.log('\nEXIT: Caught interrupt signal ' + signal)
1168
1189
  this._exited = true
1169
1190
  clearInterval(this._loopiv)
1191
+ clearInterval(this._statsiv)
1192
+ clearInterval(this._saveiv)
1170
1193
  this.onBeforeExit()
1171
1194
  this.broadcast({ server: 'Going down' })
1172
1195
  this._saveAllDocuments()