Update to ChatMessage outside of Svelte doesn't trigger reactivity on linked TJSDocument

I'm a bit confused about this... 1. When I render a ChatMessage via svelte, in the chat message svelte component I do:
let foundryChatMessageDocument = new TJSDocument(void 0, { delete: () => {} }),

onMount(async () => {
await foundryChatMessageDocument.set(await game.messages.get(messageId));
let foundryChatMessageDocument = new TJSDocument(void 0, { delete: () => {} }),

onMount(async () => {
await foundryChatMessageDocument.set(await game.messages.get(messageId));
2. At various points in the workflow I save updates to the chat message using:
await $foundryChatMessageDocument.update({
flags: {
surge: { data: { contest: contestsData } },
},
});
await $foundryChatMessageDocument.update({
flags: {
surge: { data: { contest: contestsData } },
},
});
Which works reactivly. 3. However, I then have a class, which is not a svelte component because it represents shared logic. In that class I also update the chat message like so:
const message = game.messages.get(messageId);
message.update({ flags: { surge: { data: { contest } } } })
const message = game.messages.get(messageId);
message.update({ flags: { surge: { data: { contest } } } })
These changes are not however picked up within foundryChatMessageDocument reactively. I assume that is expected. However, is there a way I can make the update to the chat message in point 3 be reactively picked up by the TJSDocument?
3 Replies
TyphonJS (Michael)
Do you have a reactive statement in your component? $: if ($foundryChatMessageDocument) { // do something. } https://svelte.dev/tutorial/reactive-statements Also do keep in mind that changing a document with the sample code that I provided is not going to work well w/ Svelte. The core ChatLog is going to improperly / not destroy the component correctly; re-render and attach a new one and such. Unless you modify ChatLog.updateMessage you are in for a world of confusion and pain.
geoidesic
geoidesicOP17mo ago
I have done that last bit, based on your comments in the Svelte server, thx and it did relieve a world of pain indeed! But in principle here, should I expect $foundryChatMessageDocument to react to all updates to the Chat Message, even if not done via $foundryChatMessageDocument.update() but rather directly through core foundry?
Do you have a reactive statement in your component?
I have put one in now and it is not triggering after the update from the class.
TyphonJS (Michael)
It should work, but unfortunately I can't debug your code if you happen to be doing something slightly incorrect. You can certainly step through the TJSDocument code yourself and verify that things are working or not. I gather you are on the latest TRL "alpha" release 0.0.23 I believe or 0.1.0 has the changes required to work on v11. Install essential-svelte-esm and make sure the document examples work there. If they do you are likely doing something wrong. RE: Your feature request: https://discord.com/channels/170995199584108546/517504137403826177/1128115256858775582 You are trying to fit a square peg in a circular opening w/ most of what you are attempting. Constantly updating a chat message for flag data is not recommended. You really should be investigating using sockets to update a chat message with dynamic / changing targeting data and then when it is locked in and executed then you only update a chat message document once if that is necessary at all. IE quite likely that final targeting data is being consumed by some other part of your system to make something else go.
Want results from more Discord servers?
Add your server