Skip to content

Commit

Permalink
debugging duplicate nested calls #147
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Mar 28, 2024
1 parent e1b6d41 commit c614384
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
60 changes: 34 additions & 26 deletions src/decoder/strategy/StateCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type OnInstanceAvailableCallback = (callback: (ref: Ref) => void) => void;

type CallContext = {
instance?: Ref,
nestedOnAdd?: boolean,
onParentInstanceAvailable?: OnInstanceAvailableCallback,
}

Expand Down Expand Up @@ -169,7 +170,6 @@ export function getStateCallbacks(decoder: Decoder) {
if (immediate && context.instance[prop] !== undefined) {
callback(context.instance[prop], undefined);
}

return $root.addCallback(
$root.refIds.get(context.instance),
prop,
Expand All @@ -186,15 +186,17 @@ export function getStateCallbacks(decoder: Decoder) {
}, {
get(target, prop: string) {
if (metadata[prop]) {

const instance = context.instance?.[prop];
const onParentInstanceAvailable: OnInstanceAvailableCallback = !instance && ((callback: (ref: Ref) => void) => {
// @ts-ignore
const dettach = $(context.instance).listen(prop, (value, previousValue) => {
dettach();
callback(value);
});
}) || undefined;
const onParentInstanceAvailable: OnInstanceAvailableCallback = (
!instance &&
((callback: (ref: Ref) => void) => {
// @ts-ignore
const dettach = $(context.instance).listen(prop, (value, previousValue) => {
dettach();
callback(value);
});
}) || undefined
);

return getProxy(metadata[prop].type, { instance, onParentInstanceAvailable });

Expand All @@ -203,49 +205,55 @@ export function getStateCallbacks(decoder: Decoder) {
return target[prop];
}
},
has(target, prop) { return metadata[prop] !== undefined; },
has(target, prop: string) { return metadata[prop] !== undefined; },
set(target, prop, value) { throw new Error("not allowed"); },
deleteProperty(target, p) { throw new Error("not allowed"); },
});

} else {
const onAdd = function (ref: Ref, callback: (value, key) => void, immediate: boolean = true) {
// collection instance is set
$root.addCallback(
const onAdd = function (
ref: Ref,
callback: (value: any, key: any) => void,
immediate: boolean
) {
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,
callback
);

if (immediate) {
(ref as ArraySchema).forEach((v, k) => callback(v, k));
}
}

/**
* Collection instances
*/
return new Proxy({
onAdd: function(callback: (value, key) => void, immediate: boolean = true) {
onAdd: function(callback: (value, key) => void, immediate: boolean = true, isNested: boolean = false) {
console.log(">> onAdd", { immediate, hasInstance: context.instance !== undefined });

if (context.instance) {
onAdd(context.instance, callback, immediate);
console.log("is nested??", isNested)
onAdd(context.instance, callback, immediate && !isNested);

} else if (context.onParentInstanceAvailable) {
console.log("onAdd, instance not available yet...");

// collection instance not received yet
context.onParentInstanceAvailable((ref: Ref) =>
onAdd(ref, callback, false));
context.onParentInstanceAvailable((ref: Ref) => {
console.log("PARENT INSTANCE AVAILABLE")
onAdd(ref, callback, false);
});
}
},
onRemove: function onRemove(callback) {
// $root.addCallback([...tree], OPERATION.DELETE, callback);
},
}, {
get(target, prop: string) {
if (!target[prop]) {
throw new Error(`Can't access '${prop}' through callback proxy. access the instance directly.`);
}
if (!target[prop]) { throw new Error(`Can't access '${prop}' through callback proxy. access the instance directly.`); }
return target[prop];
},
has(target, prop) { return target[prop] !== undefined; },
Expand Down
16 changes: 7 additions & 9 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,21 +373,15 @@ console.log("> register callbacks...");

const s: any = {};


console.log("> will decode...");

// decoder.decode(encoded);
const changes = decoder.decode(viewEncoded1);

$(decoder.state).listen("str", (value, previousValue) => {
console.log("'str' changed:", { value, previousValue });
});

$(decoder.state).teams.onAdd((team, index) => {
console.log("Teams.onAdd =>", { team, index });
console.log("Teams.onAdd =>", { index });

$(team).entities.onAdd((entity, entityId) => {
console.log(`Entities.onAdd =>`, { teamIndex: index, entity, entityId });
console.log(`Entities.onAdd =>`, { teamIndex: index, entityId });

// $(entity as Player).cards.onAdd((card, cardIndex) => {
// console.log(entityId, "card added =>", { card, cardIndex });
Expand All @@ -414,7 +408,11 @@ $(decoder.state).teams.onAdd((team, index) => {
// });


// $.listen("")
console.log("> will decode...");

// decoder.decode(encoded);
const changes = decoder.decode(viewEncoded1);

console.log("Decoded =>", decoder.state.toJSON());

// decoder.decode(viewEncoded2);
Expand Down

0 comments on commit c614384

Please sign in to comment.