Skip to content

Commit

Permalink
roll back. #147 not fixed yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Mar 29, 2024
1 parent c614384 commit bbd8b86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
36 changes: 17 additions & 19 deletions src/decoder/strategy/StateCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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; }

//
Expand Down Expand Up @@ -150,7 +148,6 @@ export function getStateCallbacks(decoder: Decoder) {

uniqueRefIds.add(refId);
}

};

function getProxy(metadataOrType: Metadata | DefinitionType, context: CallContext) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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);
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,19 @@ $(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 });
// });

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

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

0 comments on commit bbd8b86

Please sign in to comment.