@colyseus/schema 3.0.0-alpha.0 → 3.0.0-alpha.2

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.
@@ -39,8 +39,6 @@ export class Root {
39
39
  changes = new Map<ChangeTree, Map<number, OPERATION>>();
40
40
  filteredChanges = new Map<ChangeTree, Map<number, OPERATION>>();
41
41
 
42
- views: StateView[] = [];
43
-
44
42
  getNextUniqueId() {
45
43
  return this.nextUniqueId++;
46
44
  }
@@ -5,11 +5,10 @@ import { $changes, $encoder, $filter } from "../types/symbols";
5
5
  import * as encode from "../encoding/encode";
6
6
  import type { Iterator } from "../encoding/decode";
7
7
 
8
- import { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';
8
+ import { SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';
9
9
  import { Root } from "./ChangeTree";
10
10
  import { getNextPowerOf2 } from "../utils";
11
- import { StateView } from "./StateView";
12
- import { Metadata } from "../Metadata";
11
+ import type { StateView } from "./StateView";
13
12
 
14
13
  export class Encoder<T extends Schema = any> {
15
14
  static BUFFER_SIZE = 8 * 1024;// 8KB
@@ -18,7 +17,7 @@ export class Encoder<T extends Schema = any> {
18
17
  context: TypeContext;
19
18
  state: T;
20
19
 
21
- $root: Root;
20
+ root: Root;
22
21
 
23
22
  constructor(root: T) {
24
23
  this.setRoot(root);
@@ -36,20 +35,20 @@ export class Encoder<T extends Schema = any> {
36
35
  }
37
36
 
38
37
  protected setRoot(state: T) {
39
- this.$root = new Root();
38
+ this.root = new Root();
40
39
  this.state = state;
41
- state[$changes].setRoot(this.$root);
40
+ state[$changes].setRoot(this.root);
42
41
  }
43
42
 
44
43
  encode(
45
44
  it: Iterator = { offset: 0 },
46
45
  view?: StateView,
47
46
  bytes = this.sharedBuffer,
48
- changeTrees = this.$root.changes
47
+ changeTrees = this.root.changes
49
48
  ): Buffer {
50
49
  const initialOffset = it.offset; // cache current offset in case we need to resize the buffer
51
50
 
52
- const isEncodeAll = this.$root.allChanges === changeTrees;
51
+ const isEncodeAll = this.root.allChanges === changeTrees;
53
52
  const hasView = (view !== undefined);
54
53
  const rootChangeTree = this.state[$changes];
55
54
 
@@ -139,7 +138,7 @@ export class Encoder<T extends Schema = any> {
139
138
  // console.log("->", item[0].refId, item[0].ref.toJSON());
140
139
  // });
141
140
 
142
- return this.encode(it, undefined, this.sharedBuffer, this.$root.allChanges);
141
+ return this.encode(it, undefined, this.sharedBuffer, this.root.allChanges);
143
142
  }
144
143
 
145
144
  encodeAllView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {
@@ -149,7 +148,7 @@ export class Encoder<T extends Schema = any> {
149
148
  // this.debugAllFilteredChanges();
150
149
 
151
150
  // try to encode "filtered" changes
152
- this.encode(it, view, bytes, this.$root.allFilteredChanges);
151
+ this.encode(it, view, bytes, this.root.allFilteredChanges);
153
152
 
154
153
  return Buffer.concat([
155
154
  bytes.slice(0, sharedOffset),
@@ -173,7 +172,7 @@ export class Encoder<T extends Schema = any> {
173
172
  const viewOffset = it.offset;
174
173
 
175
174
  // try to encode "filtered" changes
176
- this.encode(it, view, bytes, this.$root.filteredChanges);
175
+ this.encode(it, view, bytes, this.root.filteredChanges);
177
176
 
178
177
  // encode visibility changes (add/remove for this view)
179
178
  const viewChangesIterator = view.changes.entries();
@@ -214,7 +213,7 @@ export class Encoder<T extends Schema = any> {
214
213
  ]);
215
214
  }
216
215
 
217
- onEndEncode(changeTrees = this.$root.changes) {
216
+ onEndEncode(changeTrees = this.root.changes) {
218
217
  const changeTreesIterator = changeTrees.entries();
219
218
  for (const [changeTree, _] of changeTreesIterator) {
220
219
  changeTree.endEncode();
@@ -223,14 +222,14 @@ export class Encoder<T extends Schema = any> {
223
222
 
224
223
  discardChanges() {
225
224
  // discard shared changes
226
- if (this.$root.changes.size > 0) {
227
- this.onEndEncode(this.$root.changes);
228
- this.$root.changes.clear();
225
+ if (this.root.changes.size > 0) {
226
+ this.onEndEncode(this.root.changes);
227
+ this.root.changes.clear();
229
228
  }
230
229
  // discard filtered changes
231
- if (this.$root.filteredChanges.size > 0) {
232
- this.onEndEncode(this.$root.filteredChanges);
233
- this.$root.filteredChanges.clear();
230
+ if (this.root.filteredChanges.size > 0) {
231
+ this.onEndEncode(this.root.filteredChanges);
232
+ this.root.filteredChanges.clear();
234
233
  }
235
234
  }
236
235
 
@@ -3,6 +3,10 @@ import { $changes } from "../types/symbols";
3
3
  import { DEFAULT_VIEW_TAG } from "../annotations";
4
4
  import { OPERATION } from "../encoding/spec";
5
5
  import { Metadata } from "../Metadata";
6
+ import type { Schema } from "../Schema";
7
+
8
+ export function createView(root: Schema) {
9
+ }
6
10
 
7
11
  export class StateView {
8
12
  /**
@@ -22,6 +22,9 @@
22
22
  */
23
23
 
24
24
  import type { TextEncoder } from "util";
25
+ import type { Iterator } from "./decode";
26
+
27
+ export type BufferLike = number[] | ArrayBufferLike;
25
28
 
26
29
  /**
27
30
  * msgpack implementation highly based on notepack.io
@@ -32,7 +35,7 @@ let textEncoder: TextEncoder;
32
35
  // @ts-ignore
33
36
  try { textEncoder = new TextEncoder(); } catch (e) { }
34
37
 
35
- function utf8Length(str) {
38
+ export function utf8Length(str) {
36
39
  var c = 0, length = 0;
37
40
  for (var i = 0, l = str.length; i < l; i++) {
38
41
  c = str.charCodeAt(i);
@@ -80,32 +83,32 @@ export function utf8Write(view, str, it) {
80
83
  }
81
84
  }
82
85
 
83
- export function int8(bytes, value, it) {
86
+ export function int8(bytes: BufferLike, value: number, it: Iterator) {
84
87
  bytes[it.offset++] = value & 255;
85
88
  };
86
89
 
87
- export function uint8(bytes, value, it) {
90
+ export function uint8(bytes: BufferLike, value: number, it: Iterator) {
88
91
  bytes[it.offset++] = value & 255;
89
92
  };
90
93
 
91
- export function int16(bytes, value, it) {
94
+ export function int16(bytes: BufferLike, value: number, it: Iterator) {
92
95
  bytes[it.offset++] = value & 255;
93
96
  bytes[it.offset++] = (value >> 8) & 255;
94
97
  };
95
98
 
96
- export function uint16(bytes, value, it) {
99
+ export function uint16(bytes: BufferLike, value: number, it: Iterator) {
97
100
  bytes[it.offset++] = value & 255;
98
101
  bytes[it.offset++] = (value >> 8) & 255;
99
102
  };
100
103
 
101
- export function int32(bytes, value, it) {
104
+ export function int32(bytes: BufferLike, value: number, it: Iterator) {
102
105
  bytes[it.offset++] = value & 255;
103
106
  bytes[it.offset++] = (value >> 8) & 255;
104
107
  bytes[it.offset++] = (value >> 16) & 255;
105
108
  bytes[it.offset++] = (value >> 24) & 255;
106
109
  };
107
110
 
108
- export function uint32(bytes, value, it) {
111
+ export function uint32(bytes: BufferLike, value: number, it: Iterator) {
109
112
  const b4 = value >> 24;
110
113
  const b3 = value >> 16;
111
114
  const b2 = value >> 8;
@@ -116,25 +119,25 @@ export function uint32(bytes, value, it) {
116
119
  bytes[it.offset++] = b4 & 255;
117
120
  };
118
121
 
119
- export function int64(bytes, value, it) {
122
+ export function int64(bytes: BufferLike, value: number, it: Iterator) {
120
123
  const high = Math.floor(value / Math.pow(2, 32));
121
124
  const low = value >>> 0;
122
125
  uint32(bytes, low, it);
123
126
  uint32(bytes, high, it);
124
127
  };
125
128
 
126
- export function uint64(bytes, value, it) {
129
+ export function uint64(bytes: BufferLike, value: number, it: Iterator) {
127
130
  const high = (value / Math.pow(2, 32)) >> 0;
128
131
  const low = value >>> 0;
129
132
  uint32(bytes, low, it);
130
133
  uint32(bytes, high, it);
131
134
  };
132
135
 
133
- export function float32(bytes, value, it) {
136
+ export function float32(bytes: BufferLike, value: number, it: Iterator) {
134
137
  writeFloat32(bytes, value, it);
135
138
  }
136
139
 
137
- export function float64(bytes, value, it) {
140
+ export function float64(bytes: BufferLike, value: number, it: Iterator) {
138
141
  writeFloat64(bytes, value, it);
139
142
  }
140
143
 
@@ -144,22 +147,22 @@ const _int32 = new Int32Array(2);
144
147
  const _float32 = new Float32Array(_int32.buffer);
145
148
  const _float64 = new Float64Array(_int32.buffer);
146
149
 
147
- export function writeFloat32(bytes, value, it) {
150
+ export function writeFloat32(bytes: BufferLike, value: number, it: Iterator) {
148
151
  _float32[0] = value;
149
152
  int32(bytes, _int32[0], it);
150
153
  };
151
154
 
152
- export function writeFloat64(bytes, value, it) {
155
+ export function writeFloat64(bytes: BufferLike, value: number, it: Iterator) {
153
156
  _float64[0] = value;
154
157
  int32(bytes, _int32[_isLittleEndian ? 0 : 1], it);
155
158
  int32(bytes, _int32[_isLittleEndian ? 1 : 0], it);
156
159
  };
157
160
 
158
- export function boolean(bytes, value, it) {
161
+ export function boolean(bytes: BufferLike, value: number, it: Iterator) {
159
162
  bytes[it.offset++] = value ? 1 : 0; // uint8
160
163
  };
161
164
 
162
- export function string(bytes: Buffer, value, it) {
165
+ export function string(bytes: BufferLike, value: string, it: Iterator) {
163
166
  // encode `null` strings as empty.
164
167
  if (!value) { value = ""; }
165
168
 
@@ -198,7 +201,7 @@ export function string(bytes: Buffer, value, it) {
198
201
  return size + length;
199
202
  }
200
203
 
201
- export function number(bytes, value, it) {
204
+ export function number(bytes: BufferLike, value: number, it: Iterator) {
202
205
  if (isNaN(value)) {
203
206
  return number(bytes, 0, it);
204
207