Skip to content

Commit

Permalink
callbacks: onChange()
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Mar 29, 2024
1 parent bbd8b86 commit 5f6c840
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
23 changes: 14 additions & 9 deletions src/decoder/strategy/StateCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ export function getStateCallbacks(decoder: Decoder) {
(change.op & OPERATION.DELETE) === OPERATION.DELETE &&
change.previousValue instanceof Schema
) {
change.previousValue['$callbacks']?.[OPERATION.DELETE]?.forEach(callback => callback());
callbacks[$root.refIds.get(change.previousValue)]?.[OPERATION.DELETE]?.forEach(callback =>
callback());
}

if (ref instanceof Schema) {
if (!uniqueRefIds.has(refId)) {
try {
// trigger onChange
$callbacks?.[OPERATION.REPLACE]?.forEach(callback =>
callback());
$callbacks?.[OPERATION.REPLACE]?.forEach(callback => callback());

} catch (e) {
console.error(e);
Expand Down Expand Up @@ -174,8 +174,12 @@ export function getStateCallbacks(decoder: Decoder) {
);
},
onChange: function onChange(callback: () => void) {
// TODO:
// $root.addCallback(tree, OPERATION.REPLACE, callback);
return $root.addCallback(
$root.refIds.get(context.instance),
OPERATION.REPLACE,
callback
);

},
bindTo: function bindTo(targetObject: any, properties?: Array<NonFunctionPropNames<T>>) {
console.log("bindTo", targetObject, properties);
Expand Down Expand Up @@ -241,17 +245,18 @@ export function getStateCallbacks(decoder: Decoder) {

} else if (context.onInstanceAvailable) {
// collection instance not received yet
context.onInstanceAvailable((ref: Ref) => {
onAdd(ref, callback, false);
});
context.onInstanceAvailable((ref: Ref) =>
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
20 changes: 13 additions & 7 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,21 @@ $(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 as Player).cards.onAdd((card, cardIndex) => {
// console.log(entityId, "card added =>", { card, cardIndex });
// });
$(entity).onChange(() => {
console.log("Entity changed!");
});

$(entity).listen("position", (value, previousValue) => {
console.log("entity position ->", value.toJSON());
});

$(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 All @@ -407,11 +415,9 @@ $(decoder.state).teams.onAdd((team, index) => { // delayed
// });
// });


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

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

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

Expand Down

0 comments on commit 5f6c840

Please sign in to comment.