How does `fetch` work?
GodBleak opened this issue · 5 comments
When I implement fetch in an event the event only fires once. the only way to get it to fire again is to reload.
import {event, command, reload, fetch} from '@grakkit/stdlib-paper'
event('org.bukkit.event.entity.PlayerDeathEvent', event => {
fetch('https://raw.githubusercontent.com/LearnWebCode/json-example/master/animals-1.json').json(true)
.then(data => console.log(JSON.stringify(data)))
})
command({
name: 'jsReload',
execute: () => reload(),
})
Is there something I'm doing wrong?
Edit:
I've found a workaround, but if there's a better way, that would be appreciated.
Instead of implementing the fetch in the event, have the event dispatch a command that makes the request, how you'd get the resulting data back to the event, I haven't tried to figure out as I don't need it to, but I figure that's pretty niche
import {event, command, server, reload, fetch} from '@grakkit/stdlib-paper'
event('org.bukkit.event.entity.PlayerDeathEvent', event => {
server.dispatchCommand(server.getConsoleSender(), `getExternalCommand`)
})
command({
name: 'jsReload',
execute: () => reload(),
})
command({
name: 'getExternalResource',
execute: (sender, args) => {
let res = fetch('https://raw.githubusercontent.com/LearnWebCode/json-example/master/animals-1.json').json()
console.log(res)
},
})
Notice the fetch is no longer async, that's because for some reason when it is, it generates an exception while executing task 261148 java.lang.IllegalStateException: Multi threaded access requested by thread Thread[Craft Scheduler Thread - 1309 - grakkit,5,main] but is not allowed for language(s) js.
What about something like this?
const requests = [];
stdlib.event('org.bukkit.event.player.AsyncPlayerChatEvent', event => {
requests.push(() => {
stdlib.fetch('https://raw.githubusercontent.com/LearnWebCode/json-example/master/animals-1.json')
.json(true)
.then(data => console.log(JSON.stringify(data)));
})
});
stdlib.task.interval(() => {
for (const request of requests.splice(0, requests.length)) {
request();
}
});
Sorry, just got to trying this,
unfortunately, no cigar with that either, gives the following stacktrace:
[grakkit] Task #12 for grakkit v5.0.4 generated an exception
java.util.ConcurrentModificationException: null
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:970) ~[?:?]
at java.util.LinkedList$ListItr.next(LinkedList.java:892) ~[?:?]
at java.lang.Iterable.forEach(Iterable.java:74) ~[?:?]
at grakkit.Grakkit.tick(Grakkit.java:94) ~[grakkit-5.0.4.paper(1).jar:?]
at org.bukkit.craftbukkit.v1_18_R1.scheduler.CraftTask.run(CraftTask.java:101) ~[paper-1.18.1.jar:git-Paper-147]
at org.bukkit.craftbukkit.v1_18_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:483) ~[paper-1.18.1.jar:git-Paper-147]
at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:1547) ~[paper-1.18.1.jar:git-Paper-147]
at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:480) ~[paper-1.18.1.jar:git-Paper-147]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1470) ~[paper-1.18.1.jar:git-Paper-147]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1264) ~[paper-1.18.1.jar:git-Paper-147]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:317) ~[paper-1.18.1.jar:git-Paper-147]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
stdlib v1 is being replaced soon, the fetch method is just an HTTP get system with async. it's very basic and under-featured.
@Mythical-Forest-Collective I'm not sure, I decided to shift my approach altogether. I'd like to check, however, I'm currently racing to finish another project before I leave on vacation for a few weeks. If time is of the essence, Let me know and I will check within the next few days, otherwise, I'd love to check when I get back, and if needed I can close this until then.
Heya @GodBleak, is it possible to confirm that this is still an issue?