diff --git a/src/decoder/DecodeOperation.ts b/src/decoder/DecodeOperation.ts index a5252665..6817ec66 100644 --- a/src/decoder/DecodeOperation.ts +++ b/src/decoder/DecodeOperation.ts @@ -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 { ref: Ref, diff --git a/src/decoder/strategy/StateCallbacks.ts b/src/decoder/strategy/StateCallbacks.ts index 59748b1a..dfe28876 100644 --- a/src/decoder/strategy/StateCallbacks.ts +++ b/src/decoder/strategy/StateCallbacks.ts @@ -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(); @@ -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) { // @@ -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 diff --git a/src/v3.ts b/src/v3.ts index 45eacc1b..afab9f00 100644 --- a/src/v3.ts +++ b/src/v3.ts @@ -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()); @@ -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) => {