Skip to content

Commit

Permalink
fixes .onChange() + .listen(). bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 9, 2023
1 parent a0ec61d commit 2a789a1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "2.0.17",
"version": "2.0.18",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "./bin/schema-codegen"
Expand Down
4 changes: 2 additions & 2 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ export abstract class Schema {
protected $callbacks: { [op: number]: Array<Function> };

public onChange(callback: () => void): () => void {
return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.REPLACE, callback);
return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.REPLACE, callback);
}
public onRemove(callback: () => void): () => void {
return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.DELETE, callback);
return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.DELETE, callback);
}

// allow inherited classes to have a constructor
Expand Down
6 changes: 3 additions & 3 deletions src/types/ArraySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
public $callbacks: { [operation: number]: Array<(item: V, key: number) => void> };
public onAdd(callback: (item: V, key: number) => void, triggerAll: boolean = true) {
return addCallback(
(this.$callbacks || (this.$callbacks = [])),
(this.$callbacks || (this.$callbacks = {})),
OPERATION.ADD,
callback,
(triggerAll)
? this.$items
: undefined
);
}
public onRemove(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
public onChange(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
public onRemove(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.DELETE, callback); }
public onChange(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.REPLACE, callback); }

static is(type: any) {
return (
Expand Down
6 changes: 3 additions & 3 deletions src/types/MapSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, S
public $callbacks: { [operation: number]: Array<(item: V, key: string) => void> };
public onAdd(callback: (item: V, key: string) => void, triggerAll: boolean = true) {
return addCallback(
(this.$callbacks || (this.$callbacks = [])),
(this.$callbacks || (this.$callbacks = {})),
OPERATION.ADD,
callback,
(triggerAll)
? this.$items
: undefined
);
}
public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.DELETE, callback); }
public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = {}), OPERATION.REPLACE, callback); }

static is(type: any) {
return type['map'] !== undefined;
Expand Down
19 changes: 19 additions & 0 deletions test/ChangeAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,25 @@ describe("Change API", () => {
});

describe(".listen()", () => {
it("should allow .onChange() + listen on same object", () => {
let callCount: number = 0;

class State extends Schema {
@type("string") map: string;
}

const state = new State();
state.map = "Hello world!";

const decodedState = new State();
decodedState.onChange(() => callCount++);
decodedState.listen("map", () => callCount++);

decodedState.decode(state.encode());

assert.strictEqual(2, callCount);
});

it("should trigger immediatelly", () => {
let listenCallCount = 0;

Expand Down

0 comments on commit 2a789a1

Please sign in to comment.