Populate SharedList

To discuss development of addons / skins / customization of MediaMonkey.

Moderators: jiri, drakinite, Addon Administrators

heribertolugo
Posts: 9
Joined: Sun Sep 18, 2022 10:51 pm

Populate SharedList

Post by heribertolugo »

attempting to populate SharedList i get a fatal assertion exception which crashes MM and does not mention what the assertion actually was. i tried to populate the SharedList in 2 different ways, both of which resulted in the same crash (Assertion Failure n:\Delphi\MediaMonkeyBuildTrunk\HTML5Monkey\ceflib.pas, line 22394).

Code: Select all

let data = app.utils.createSharedList();
let arr = [
        {path: 'bobo', isSomething: true},
        {path: 'momo', isSomething: false},
        {path: 'dodo', isSomething: true},
        {path: 'foo', isSomething: true},
        {path: 'bar', isSomething: false},
    ];
    
    for(let i =0; i < arr.length; i++) {
            data.add(arr[i]);
    }
and also

Code: Select all

let data = app.utils.createSharedList();
let arr = [
        {path: 'bobo', isSomething: true},
        {path: 'momo', isSomething: false},
        {path: 'dodo', isSomething: true},
        {path: 'foo', isSomething: true},
        {path: 'bar', isSomething: false},
    ];
    
    for(let i =0; i < arr.length; i++) {
        data.modifyAsync(() => {
            data.add(arr[i]);
        });
    }

i've found multiple examples in the code which just uses the add method (not modifyAsync).

how do i populate a shared list?
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Populate SharedList

Post by drakinite »

Hi there,

SharedLists are native Delphi objects, and can only contain native Delphi objects themselves (e.g. Track, Album, etc.) I believe that the unhelpful assertion failure is because the expected data type does not match. I'll see if it's possible to make the assert provide a more useful error message.

As for using .add() outside of modifyAsync: I was not aware, but yeah, it turns out you don't need to be inside of a write lock to use .add().
Here's an example of adding data to a SharedList. Realistically, you'll want the data type of all items within your list to be the same. But due to the way object-oriented inheritance works, it technically won't yell at you if you do this, and this is just to illustrate an example.

Code: Select all

myList = app.utils.createSharedList();
myTrack = app.utils.createEmptyTrack();
myAlbum = app.utils.createEmptyAlbum();
myList.beginUpdate(); // Optional; beginUpdate() and endUpdate() prevent the list from emitting any events while you're modifying it.
myList.add(myTrack);
myList.add(myAlbum);
myList.endUpdate();
If you want to use your own custom data, for ListViews (like a GridView), then you can use an ArrayDataSource. That class implements the methods from SharedLists that are used in LVs, such as locked(), modifyAsync(), and setAutoSort(), but in pure JS.

Since documentation on using ListViews in addons for your own purposes is sparse to the point of not existing, I spent tonight writing up a sample script demonstrating how you can use an ArrayDataSource to show your own custom data in a GridView. It includes comments explaining how it works. It'll be available inside the SampleScripts folder of the first 5.1 beta build, but here's a copy you can download and install/view now: https://1drv.ms/u/s!AqHzUrf30uprsodXCSh9dPFwpP3CMw
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
Post Reply