Repository navigation
Custom objects implementing the EventTarget interface lose reactivity of their properties #4919
Alpine.js versionv3.17.4 Browser and operating systemFirefox 156 on macOS What do you need help with?Hi, I've noticed that a custom object loses reactivity when its class extends I created a quick and dirty CodePen here to demonstrate the issue, and you can find more detail below on what I want to achieve, and why I decided to design it like so. Code samples below are only fragments of the whole thing. Also please note that I'm more of a backend developer, so frontend is not my forte. I'm definitely interested in a better solution than what I've come up with if one exists, so do not hesitate to (gently) scold me and point me in the right direction if needed. Thanks! Background Basically, I need to implement a Phase counter, where each Phase is made of multiple Turns. The only thing a user can do is advance the Turn, and the Phase will automatically be updated after the correct amount of Turns. This is all handled by an instance of a simple class, but the main state (the one attached with <div x-data="appWithoutEvent">
<h1>Without event</h1>
<p x-data="turnCounter">Phase <span x-text="phase"></span>, Turn <span x-text="turn"></span></p>
<button @click="nextTurn">Next turn</button>
</div>class SimpleTurnCounter {
turn = 1;
phase = 1;
nextTurn() {
if (this.turn < TURN_PER_PHASE) {
this.turn += 1;
} else {
this.turn = 1;
this.phase += 1;
}
}
}
const appWithoutEvent = {
turnCounter: new SimpleTurnCounter(),
nextTurn() {
this.turnCounter.nextTurn();
if (this.turnCounter.turn == 1) {
console.log('new phase without event', this.turnCounter.phase);
}
},
}You can see that I check the value of class EventTurnCounter extends EventTarget {
turn = 1;
phase = 1;
constructor() {
super();
}
nextTurn() {
if (this.turn < TURN_PER_PHASE) {
this.turn += 1;
} else {
this.turn = 1;
this.phase += 1;
this.dispatchEvent(new CustomEvent('newphase', { detail: this.phase }));
}
}
}
const appWithEvent = {
turnCounter: new EventTurnCounter(),
init() {
this.turnCounter.addEventListener(
'newphase', (e) => console.log('new phase with event', e.detail)
);
},
nextTurn() {
this.turnCounter.nextTurn();
},
}Unfortunately, as soon as you extend The solution I ended up with I've tried many variations around which objects or properties to bind to the DOM, but none of them did the trick so in the end I came up with the idea of using an intermediary object implementing the class Messenger extends EventTarget {
constructor() {
super();
}
}Exporting an instance from its own module (or adding it to How do you expect it to work?I was expecting an instance of an object and its properties to keep being observable after extending Please confirm
|
Replies: 1 comment 1 reply
|
Yes, this is the Vue reactivity caveat you linked, rather than You can see the difference with your two classes: const plain = new SimpleTurnCounter()
const withEvents = new EventTurnCounter()
Object.prototype.toString.call(withEvents) // "[object EventTarget]"
Alpine.reactive(plain) === plain // false: a proxy was created
Alpine.reactive(withEvents) === withEvents // true: still the original objectThat explains why Your separate relay approach makes sense. If you don't need a shared event bus, you can also give the ordinary counter an Also, the empty |
Yes, this is the Vue reactivity caveat you linked, rather than
super()itself. Vue checks the object's type before creating a proxy. Your ordinary class is anObject, but the subclass is anEventTarget, which isn't one of the supported target types. Vue returns that instance unchanged, including its custom properties.You can see the difference with your two classes:
That explains why
x-texts…