How does `fetch` work?
See original GitHub issueWhen 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.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
@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.
stdlib v1 is being replaced soon, the fetch method is just an HTTP get system with async. it’s very basic and under-featured.