From bbd8b86443db62729a170868d6fd7be83d03d5ab Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Fri, 29 Mar 2024 10:50:36 -0300 Subject: [PATCH] roll back. #147 not fixed yet. --- src/decoder/strategy/StateCallbacks.ts | 36 ++++++++++++-------------- src/v3.ts | 8 +++--- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/decoder/strategy/StateCallbacks.ts b/src/decoder/strategy/StateCallbacks.ts index c0653fa3..e63ffddf 100644 --- a/src/decoder/strategy/StateCallbacks.ts +++ b/src/decoder/strategy/StateCallbacks.ts @@ -56,8 +56,8 @@ type OnInstanceAvailableCallback = (callback: (ref: Ref) => void) => void; type CallContext = { instance?: Ref, - nestedOnAdd?: boolean, - onParentInstanceAvailable?: OnInstanceAvailableCallback, + parentInstance?: Ref, + onInstanceAvailable?: OnInstanceAvailableCallback, } export function getStateCallbacks(decoder: Decoder) { @@ -73,8 +73,6 @@ export function getStateCallbacks(decoder: Decoder) { const ref = change.ref; const $callbacks = callbacks[refId]; - // console.log("change =>", { refId, field: change.field }); - if (!$callbacks) { continue; } // @@ -150,7 +148,6 @@ export function getStateCallbacks(decoder: Decoder) { uniqueRefIds.add(refId); } - }; function getProxy(metadataOrType: Metadata | DefinitionType, context: CallContext) { @@ -187,7 +184,7 @@ export function getStateCallbacks(decoder: Decoder) { get(target, prop: string) { if (metadata[prop]) { const instance = context.instance?.[prop]; - const onParentInstanceAvailable: OnInstanceAvailableCallback = ( + const onInstanceAvailable: OnInstanceAvailableCallback = ( !instance && ((callback: (ref: Ref) => void) => { // @ts-ignore @@ -197,8 +194,11 @@ export function getStateCallbacks(decoder: Decoder) { }); }) || undefined ); - - return getProxy(metadata[prop].type, { instance, onParentInstanceAvailable }); + return getProxy(metadata[prop].type, { + instance, + parentInstance: context.instance, + onInstanceAvailable, + }); } else { // accessing the function @@ -216,12 +216,10 @@ export function getStateCallbacks(decoder: Decoder) { callback: (value: any, key: any) => void, immediate: boolean ) { + // Trigger callback on existing items if (immediate) { - console.log("IMMEDIATE!"); - // trigger for existing items (ref as ArraySchema).forEach((v, k) => callback(v, k)); } - context.nestedOnAdd = true; return $root.addCallback( $root.refIds.get(ref), OPERATION.ADD, @@ -233,17 +231,17 @@ export function getStateCallbacks(decoder: Decoder) { * Collection instances */ return new Proxy({ - onAdd: function(callback: (value, key) => void, immediate: boolean = true, isNested: boolean = false) { - console.log(">> onAdd", { immediate, hasInstance: context.instance !== undefined }); - + onAdd: function(callback: (value, key) => void, immediate: boolean = true) { if (context.instance) { - console.log("is nested??", isNested) - onAdd(context.instance, callback, immediate && !isNested); + // + // TODO: https://github.com/colyseus/schema/issues/147 + // If parent instance has "onAdd" registered, avoid triggering immediate callback. + // + onAdd(context.instance, callback, immediate); - } else if (context.onParentInstanceAvailable) { + } else if (context.onInstanceAvailable) { // collection instance not received yet - context.onParentInstanceAvailable((ref: Ref) => { - console.log("PARENT INSTANCE AVAILABLE") + context.onInstanceAvailable((ref: Ref) => { onAdd(ref, callback, false); }); } diff --git a/src/v3.ts b/src/v3.ts index 231ee293..1dc4ebfc 100644 --- a/src/v3.ts +++ b/src/v3.ts @@ -377,11 +377,11 @@ $(decoder.state).listen("str", (value, previousValue) => { console.log("'str' changed:", { value, previousValue }); }); -$(decoder.state).teams.onAdd((team, index) => { - console.log("Teams.onAdd =>", { index }); +$(decoder.state).teams.onAdd((team, index) => { // delayed + console.log("Teams.onAdd =>", { index, refId: decoder.$root.refIds.get(team) }); $(team).entities.onAdd((entity, entityId) => { - console.log(`Entities.onAdd =>`, { teamIndex: index, entityId }); + console.log(`Entities.onAdd =>`, { teamIndex: index, entityId, refId: decoder.$root.refIds.get(entity) }); // $(entity as Player).cards.onAdd((card, cardIndex) => { // console.log(entityId, "card added =>", { card, cardIndex }); @@ -389,7 +389,7 @@ $(decoder.state).teams.onAdd((team, index) => { // const frontendObj: any = {}; // $(entity).position.bindTo(frontendObj, ["x", "y", "z"]); - }); + }, false); // $(team).entities.get("one").position.listen("x", (value, previousValue) => { // });