-
I'm doing kind of streaming log from the server where I poll every few seconds for new data via My code (based on https://github.com/vuejs/petite-vue/blob/main/examples/commits.html example): <script type="module">
import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'
const kMaxEvents = 256;
function len(a) { return a ? a.length : 0; }
let app = {
events: [],
nextFrom: 0,
fetchData() {
fetch(`/api/latestevents?nextFrom=${this.nextFrom}`)
.then((res) => res.json())
.then((data) => {
const n = len(data.events);
console.log(`fetchData(): got ${n} events`);
if (n == 0) {
return;
}
this.events.push(...data.events);
if (n > kMaxEvents) {
const toRemove = n - kMaxEvents;
this.events.splice(0, toRemove);
}
//this.nextFrom = data.nextFrom;
});
}
};
createApp(app).mount()
console.log("created and mounted the app");
const fetchData = app.fetchData.bind(app);
window.setInterval(fetchData, 3000);
</script>
</head>
<body>
<p>This is analytics for the poor, by the poor.</p>
<div v-scope v-effect="fetchData()">
<p>Latest events:</p>
<template v-for="event in events">
<div>event: {{ event }}</div>
</template>
</div>
</body> I verified in the debugger that Admittedly I don't know Vue much so I'm likely missing something obvious. I was just going from the docs in https://github.com/vuejs/petite-vue#readme Things I tried
I created Big questionHow can I make my Little questionWhat also puzzled me was: if I un-commented I don't understand why because Updated, stand-alone versionThis is updated, self-contained version that doesn't go to the network. Still, doesn't re-render even though <script type="module">
import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'
const kMaxEvents = 256;
function len(a) {
return a ? a.length : 0;
}
let app = {
events: [],
nextFrom: 0,
eventsCount: 0,
updateData(data) {
const n = len(data.events);
if (n == 0) { return; }
this.events.push(...data.events);
if (n > kMaxEvents) {
const toRemove = n - kMaxEvents;
this.events.splice(0, toRemove);
}
//this.nextFrom = data.nextFrom;
this.eventsCount = len(this.events);
console.log(`updateData(): got ${n} events, eventsCount: ${this.eventsCount}`);
},
fetchData() {
if (true) {
const data = {
events: ["foo"],
nextFrom: 0,
};
this.updateData(data);
return;
}
fetch(`/api/latestevents?nextFrom=${this.nextFrom}`)
.then((res) => res.json())
.then((data) => {
this.updateData(data);
});
}
};
createApp(app).mount()
app.fetchData();
const fetchData = app.fetchData.bind(app);
window.setInterval(fetchData, 3000);
</script>
</head>
<body>
<p>This is analytics for the poor, by the poor.</p>
<div v-scope>
<p>Latest {{ eventsCount }} events:</p>
<div v-for="event in events">
<div>event: {{ event }}</div>
</div>
<template v-for="event in events">
<div>event: {{ event }}</div>
</template>
</div>
</body> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
try to wrap app in reactive:
this seems working: |
Beta Was this translation helpful? Give feedback.
try to wrap app in reactive:
this seems working:
https://jsfiddle.net/9zuf2eo7/1/