Skip to content

Commit

Permalink
trying to fix #147
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Mar 30, 2024
1 parent 5f6c840 commit f456d8b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/decoder/DecodeOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as decode from "../encoding/decode";
import { getType } from "../types/typeRegistry";
import { $childType, $deleteByIndex, $getByIndex } from "../types/symbols";
import { ArraySchema, CollectionSchema, MapSchema, SetSchema } from "..";
import { Callback } from "./ReferenceTracker";
// import { Callback } from "./ReferenceTracker";

export interface DataChange<T = any, F = string> {
ref: Ref,
Expand Down
7 changes: 6 additions & 1 deletion src/decoder/strategy/StateCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export function getStateCallbacks(decoder: Decoder) {
const $root = decoder.$root;
const callbacks = $root.callbacks;

let isTriggeringOnAdd = false;

decoder.triggerChanges = function (allChanges: DataChange[]) {
const uniqueRefIds = new Set<number>();

Expand Down Expand Up @@ -113,8 +115,11 @@ export function getStateCallbacks(decoder: Decoder) {

if (change.op === OPERATION.ADD && change.previousValue === undefined) {
// triger onAdd

isTriggeringOnAdd = true;
$callbacks[OPERATION.ADD]?.forEach(callback =>
callback(change.value, change.dynamicIndex ?? change.field));
isTriggeringOnAdd = false;

} else if (change.op === OPERATION.DELETE) {
//
Expand Down Expand Up @@ -241,7 +246,7 @@ export function getStateCallbacks(decoder: Decoder) {
// TODO: https://github.com/colyseus/schema/issues/147
// If parent instance has "onAdd" registered, avoid triggering immediate callback.
//
onAdd(context.instance, callback, immediate);
onAdd(context.instance, callback, immediate && !isTriggeringOnAdd);

} else if (context.onInstanceAvailable) {
// collection instance not received yet
Expand Down
7 changes: 4 additions & 3 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,9 @@ $(decoder.state).teams.onAdd((team, index) => { // delayed
$(team).entities.onAdd((entity, entityId) => {
console.log(`Entities.onAdd =>`, { teamIndex: index, entityId, refId: decoder.$root.refIds.get(entity) });

$(entity).onChange(() => {
console.log("Entity changed!");
});
// $(entity).onChange(() => {
// console.log("Entity changed!");
// });

$(entity).listen("position", (value, previousValue) => {
console.log("entity position ->", value.toJSON());
Expand All @@ -397,6 +397,7 @@ $(decoder.state).teams.onAdd((team, index) => { // delayed

// const frontendObj: any = {};
// $(entity).position.bindTo(frontendObj, ["x", "y", "z"]);

});

// $(team).entities.get("one").position.listen("x", (value, previousValue) => {
Expand Down

0 comments on commit f456d8b

Please sign in to comment.