Skip to content

Commit 2a789a1

Browse files
committed
fixes .onChange() + .listen(). bump version.
1 parent a0ec61d commit 2a789a1

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "2.0.17",
3+
"version": "2.0.18",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen"

src/Schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ export abstract class Schema {
141141
protected $callbacks: { [op: number]: Array<Function> };
142142

143143
public onChange(callback: () => void): () => void {
144-
return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.REPLACE, callback);
144+
return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.REPLACE, callback);
145145
}
146146
public onRemove(callback: () => void): () => void {
147-
return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.DELETE, callback);
147+
return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.DELETE, callback);
148148
}
149149

150150
// allow inherited classes to have a constructor

src/types/ArraySchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
8686
public $callbacks: { [operation: number]: Array<(item: V, key: number) => void> };
8787
public onAdd(callback: (item: V, key: number) => void, triggerAll: boolean = true) {
8888
return addCallback(
89-
(this.$callbacks || (this.$callbacks = [])),
89+
(this.$callbacks || (this.$callbacks = {})),
9090
OPERATION.ADD,
9191
callback,
9292
(triggerAll)
9393
? this.$items
9494
: undefined
9595
);
9696
}
97-
public onRemove(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
98-
public onChange(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
97+
public onRemove(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.DELETE, callback); }
98+
public onChange(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.REPLACE, callback); }
9999

100100
static is(type: any) {
101101
return (

src/types/MapSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, S
6161
public $callbacks: { [operation: number]: Array<(item: V, key: string) => void> };
6262
public onAdd(callback: (item: V, key: string) => void, triggerAll: boolean = true) {
6363
return addCallback(
64-
(this.$callbacks || (this.$callbacks = [])),
64+
(this.$callbacks || (this.$callbacks = {})),
6565
OPERATION.ADD,
6666
callback,
6767
(triggerAll)
6868
? this.$items
6969
: undefined
7070
);
7171
}
72-
public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
73-
public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
72+
public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.DELETE, callback); }
73+
public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.REPLACE, callback); }
7474

7575
static is(type: any) {
7676
return type['map'] !== undefined;

test/ChangeAPI.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,25 @@ describe("Change API", () => {
11331133
});
11341134

11351135
describe(".listen()", () => {
1136+
it("should allow .onChange() + listen on same object", () => {
1137+
let callCount: number = 0;
1138+
1139+
class State extends Schema {
1140+
@type("string") map: string;
1141+
}
1142+
1143+
const state = new State();
1144+
state.map = "Hello world!";
1145+
1146+
const decodedState = new State();
1147+
decodedState.onChange(() => callCount++);
1148+
decodedState.listen("map", () => callCount++);
1149+
1150+
decodedState.decode(state.encode());
1151+
1152+
assert.strictEqual(2, callCount);
1153+
});
1154+
11361155
it("should trigger immediatelly", () => {
11371156
let listenCallCount = 0;
11381157

0 commit comments

Comments
 (0)