T
TyphonJSNekro Darkmoon

Creating a derived reducer for a DynMapReducer

Do you have an example of how to make a derived reducer for a reducer that's not part of a TJSDcoument?
const reducer = getContext("reducer");
let derived = [];

if (enableGrouping) {
const filters = filterBy[compendiumType];
const property = `system.${groupBy[compendiumType]}`;

for (const filterValue of filters) {
const dr = $reducer.derived.create(`${filterValue}`);
console.log(dr);

dr.filters.add((doc) => doc[property] === filterValue);
derived.push(dr);
}

derived = derived;
}
const reducer = getContext("reducer");
let derived = [];

if (enableGrouping) {
const filters = filterBy[compendiumType];
const property = `system.${groupBy[compendiumType]}`;

for (const filterValue of filters) {
const dr = $reducer.derived.create(`${filterValue}`);
console.log(dr);

dr.filters.add((doc) => doc[property] === filterValue);
derived.push(dr);
}

derived = derived;
}
ND
Nekro Darkmoon150d ago
derived[0] is an instance of DynMapReducerDerived and the data does exist on it but doing [...derived[0]] or [...reducer.derived.get("somename")] spits out an empty array
TM
TyphonJS (Michael)150d ago
Just as an aside to start you can create a subclass of DynMapReducerDerived and this will make the code a bit nicer. I gather you might also be in control of the main DynMapReducer from the context reducer. Here is an example from the tests of DynMapReducer showing how to create a custom reducer and class based derived reducers. https://github.com/typhonjs-svelte/dynamic-reducer/blob/main/test/src/runner/tests/map/DerivedTests.js#L161-L252 This will only make the code a bit tidier, but we'll have to take a look at any of the specific aspects that you do or do not see from there. An interesting note is that a custom derived reducer is associated by passing in the class and not an instance. It appears you are setting up dynamic filters based on some criteria const filters = filterBy[compendiumType]; That may make things a bit harder to isolate in a custom class for a derived reducer. Just out of general interest. I think it's possible that dr.filters.add((doc) => doc[property] === filterValue); is not triggering an update for the index used behind the scenes. After you add the filter try:
dr.filters.add((doc) => doc[property] === filterValue);
dr.index.update(true);
derived.push(dr);
dr.filters.add((doc) => doc[property] === filterValue);
dr.index.update(true);
derived.push(dr);
Do report back if [...derived[0]] has the proper values you expect.
ND
Nekro Darkmoon150d ago
Will try that in a bit Yeah I'm still doing rough drafts trying to get it to work Will tidy it up into classes once it works ^^
TM
TyphonJS (Michael)150d ago
It should automatically perform an index update after adding a filter, but there is a chance that it isn't. The explicit dr.index.update(true) forces an update. Possibly it fixes the issue, but a little investigation warranted to verify what is going on.
ND
Nekro Darkmoon149d ago
that did infact work 😄
TM
TyphonJS (Michael)149d ago
Hmm... I'll have to review the dynamic reducer code to suss out if that behavior is intended. This line should trigger an index update automatically: https://github.com/typhonjs-svelte/dynamic-reducer/blob/main/src/common/adapter/AdapterFilters.ts#L175 subscribeCount should be 0 and filters.length should be 1 in the add invocation. Perhaps you can drop a console.log into node_modules\@typhonjs-fvtt\runtime\_dist\svelte\store\reducer\index.js. Search for the add function, but where you need to insert the log statement is line 382. Do a production build to ensure that the temporary log statement is picked up. Perhaps, you can also provide a link to the code that sets up the reducer. It's a bit suspect executing the code initially posted in the OP as this is presumably in a Svelte component. I know you are just trying to get something to work, but it would be great to get more knowledge on what you are doing exactly.
ND
Nekro Darkmoon146d ago
sorry for the late reply The code isn't on a public repo but I can post the reducer bit here
function getDocuments(docList) {
// Sort the documents into alphabetical order
docList.sort((a, b) => a.name.localeCompare(b.name));

const validDocs = new Map();
docList.forEach((doc) => validDocs.set(doc._id, doc));
return validDocs;
}

let reducer = new DynMapReducer();
reducer.setData(getDocuments([...document.index]), true);
function getDocuments(docList) {
// Sort the documents into alphabetical order
docList.sort((a, b) => a.name.localeCompare(b.name));

const validDocs = new Map();
docList.forEach((doc) => validDocs.set(doc._id, doc));
return validDocs;
}

let reducer = new DynMapReducer();
reducer.setData(getDocuments([...document.index]), true);
TM
TyphonJS (Michael)146d ago
Yeah... Go for it... Chances are when adding / removing a filter.. A force update should occur and that is an easy fix. Might just need to add true here: if (subscribeCount < filters.length) { this.#indexUpdate(true); }
ND
Nekro Darkmoon146d ago
This is how the reducer is being set up I was initally making a reducer of TJSDocuments but that wasn't needed since the documents won't be edited while viewing them for context the documents here are compendium documents so there's nothing in TRL to support that which is why the reducer approach
TM
TyphonJS (Michael)146d ago
I'll definitely take a look before shipping the next TRL and verify things in the dynamic reducer tests.
ND
Nekro Darkmoon146d ago
👌 ty yeah we're kind of going real hard on the svelte side of things with converting anything we can to svelte 😄
TM
TyphonJS (Michael)146d ago
And indeed it's a good idea to be mindful of bulk TJSDocument creation because it will add a hard link / callback to the underlying document. If things aren't properly destroyed that will become a dangling listener.
ND
Nekro Darkmoon146d ago
ND
Nekro Darkmoon146d ago
Also for context this is how all this is being used to replace compendium sheets
TM
TyphonJS (Michael)146d ago
And yeah.. I'll have to take a look at some of the additional Foundry integration points you've come up with and see about getting things into TRL proper to make life just a bit easier for everyone. If you get a chance though and can modify line 385 in node_modules\@typhonjs-fvtt\runtime\_dist\svelte\store\reducer\index.js from this.#indexUpdate(); to this.#indexUpdate(true); and let me know if that solves not manually having to do an index update that gives me some real world data to evaluate. Should be easy to do / make a production build then you can back it out, etc.
ND
Nekro Darkmoon146d ago
👌
Want results from more Discord servers?
Add your server
More Posts
Quest Log objective counts not displayingHi there. the Quest Log objectives (done/total) don't seem to change when I mark quests as completeItem Piles: AuctioneerA few people in the League & some that I know personally have commissioned an Item Piles powered aucSvelecte AlternativesI'm looking to implement a searchable list of sorts, and I found Svelecte: <https://mskocik.github.iSetting up TRL / contributing to existing module (Item Piles)Hi @Paith. I have started this forum post where we can continue to discuss setting up TRL and gettinReactivty and prepareDerivedDataFrom @Wasp > So I'm using prepareDerivedData to, well, derive some data for my items; some of them aLocal Development w/ NPM LinkFrom @Gerark: > Hello! this is probably a base webdev question but I can't find a good reference forIssues Configuring TyphonJS in TSI'm receiving this message in my TS Files: `Cannot find module '#runtime/svelte/application' or its TRL / Svelte based game systemsThis post is a list of repos for game systems built with TRL / Svelte for Foundry. Please DM me if yList of TjsdocumentsLink to original response: https://discord.com/channels/737953117999726592/1067337319041998869/11562Is there a way to export the quest log?I'm trying to setup a world for the PF2E Beginner Box but right now I working in a beta testing worFabricate@MisterPotts. Just starting a forum post to keep track of the conversation. > Fabricate doesn't maSvelte 5 TJSDocument PrototypeGreets @FVTT ▹ Developer. As some of you might have seen there is a bit of paradigm shift that is Funky interaction with fokus managementTJS's Focus Management is creating fun issues for me again. focusKeep = true causes my drag and dropHow to get rid of funky Prosemirror overlapHi. I'm getting some overlap with Prosemirror (see image) any ideas how to avoid that? ``` "@tyFQL / TextEditor enrichment issueFrom @ooblekie: > can anyone give me a hand as to why the text keeps coming up as object promise inTJSDocument not working properly with Module Sub-Types (Data Models)In Foundry, modules can define a [Sub-Types](<https://foundryvtt.com/article/module-sub-types/>) usiTRL `0.1.2` - Fine Tuning releaseGreets @FVTT ▹ Developer! I have just released a fine tuning release that brings a few more featureEmbed a DropDown/MenuList made with SvelteHello again! 😄 I've been pondering creating a second module for Foundry and a few questions poppedSlide Animations for SvelteApplicationI'd like my window ( which is an EmptyApplicationShell ) to play a slide-in animation when open andBug: Multiple ProseMirror editors on same svelte component do not save properlySimple reproduction in a foundry world with at least one actor defined: ``` <svelte:options accessor