<commit_before>import EventEmitter from 'wolfy87-eventemitter'

const standardPreset = Object.freeze({
    names: [
        'Artist',
        'Album',
        'Track No',
        'Title',
        'Duration'
    ],
    expressions: [
        '%artist%',
        '%album%',
        '%track%',
        '%title%',
        '%length%'
    ]
});

const compactPreset = Object.freeze({
    names: [
        'Artist',
        'Title'
    ],
    expressions: [
        '%artist%',
        '%title%'
    ]
});

export default class PlaylistModel extends EventEmitter
{
    constructor(client)
    {
        super();

        this.client = client;
        this.playlists = [];
        this.playlistItems = [];
        this.currentPlaylistId = null;
        this.itemsWatch = null;
        this.columns = standardPreset;

        this.defineEvent('playlistsChange');
        this.defineEvent('itemsChange');

        this.handleItemsUpdate = this.handleItemsUpdate.bind(this);
    }

    setPlaylists(playlists)
    {
        this.playlists = playlists;

        var currentPlaylist = playlists.find(p => p.id == this.currentPlaylistId);

        if (!currentPlaylist)
        {
            currentPlaylist = playlists.find(p => p.isCurrent);

            this.currentPlaylistId = currentPlaylist ? currentPlaylist.id : null;
            this.watchPlaylistItems();
        }

        this.currentPlaylist = currentPlaylist;
        this.emit('playlistsChange');
    }

    setCurrentPlaylistId(id)
    {
        if (id == this.currentPlaylistId)
            return;

        this.currentPlaylistId = id;
        this.currentPlaylist = this.playlists.find(p => p.id == id);
        this.emit('playlistsChange');

        this.watchPlaylistItems();
    }

    setPlaylistItems(items)
    {
        this.playlistItems = items;
        this.emit('itemsChange');
    }

    watchPlaylistItems()
    {
        if (this.itemsWatch)
        {
            this.itemsWatch.close();
            this.itemsWatch = null;
        }

        if (!this.currentPlaylistId)
            return;

        var request = {
            playlistItems: true,
            plref: this.currentPlaylistId,
            plrange: '0:1000',
            plcolumns: this.columns.expressions,
        };

        this.itemsWatch = this.client.subscribe(request, this.handleItemsUpdate);
    }

    handleItemsUpdate(result)
    {
        if (result)
            this.setPlaylistItems(result.playlistItems);
    }

    setCompactMode(enabled)
    {
        var newColumns = enabled ? compactPreset : standardPreset;

        if (this.columns === newColumns)
            return;

        this.columns = newColumns;
        this.watchPlaylistItems();
    }

    addItems(items)
    {
        this.client.addPlaylistItems(this.currentPlaylistId, items);
    }

    activateItem(index)
    {<commit_msg>webui: fix regression caused by event stream api change (closes #26)<commit_after>import EventEmitter from 'wolfy87-eventemitter'

const standardPreset = Object.freeze({
    names: [
        'Artist',
        'Album',
        'Track No',
        'Title',
        'Duration'
    ],
    expressions: [
        '%artist%',
        '%album%',
        '%track%',
        '%title%',
        '%length%'
    ]
});

const compactPreset = Object.freeze({
    names: [
        'Artist',
        'Title'
    ],
    expressions: [
        '%artist%',
        '%title%'
    ]
});

export default class PlaylistModel extends EventEmitter
{
    constructor(client)
    {
        super();

        this.client = client;
        this.playlists = [];
        this.playlistItems = [];
        this.currentPlaylistId = null;
        this.itemsWatch = null;
        this.columns = standardPreset;

        this.defineEvent('playlistsChange');
        this.defineEvent('itemsChange');

        this.handleItemsUpdate = this.handleItemsUpdate.bind(this);
    }

    setPlaylists(playlists)
    {
        this.playlists = playlists;

        var currentPlaylist = playlists.find(p => p.id == this.currentPlaylistId);

        if (!currentPlaylist)
        {
            currentPlaylist = playlists.find(p => p.isCurrent);

            this.currentPlaylistId = currentPlaylist ? currentPlaylist.id : null;
            this.watchPlaylistItems();
        }

        this.currentPlaylist = currentPlaylist;
        this.emit('playlistsChange');
    }

    setCurrentPlaylistId(id)
    {
        if (id == this.currentPlaylistId)
            return;

        this.currentPlaylistId = id;
        this.currentPlaylist = this.playlists.find(p => p.id == id);
        this.emit('playlistsChange');

        this.watchPlaylistItems();
    }

    setPlaylistItems(items)
    {
        this.playlistItems = items;
        this.emit('itemsChange');
    }

    watchPlaylistItems()
    {
        if (this.itemsWatch)
        {
            this.itemsWatch.close();
            this.itemsWatch = null;
        }

        if (!this.currentPlaylistId)
            return;

        var request = {
            playlistItems: true,
            plref: this.currentPlaylistId,
            plrange: '0:1000',
            plcolumns: this.columns.expressions,
        };

        this.itemsWatch = this.client.subscribe(request, this.handleItemsUpdate);
    }

    handleItemsUpdate(result)
    {
        if (result && result.playlistItems)
            this.setPlaylistItems(result.playlistItems);
    }

    setCompactMode(enabled)
    {
        var newColumns = enabled ? compactPreset : standardPreset;

        if (this.columns === newColumns)
            return;

        this.columns = newColumns;
        this.watchPlaylistItems();
    }

    addItems(items)
    {
        this.client.addPlaylistItems(this.currentPlaylistId, items);
    }

    activateItem(index)
    {
{'fraction_non_alphanumeric': 0.11511164964514455, 'fraction_numerical': 0.0027696036004846805, 'mean_word_length': 1.9270516717325228, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2105862', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1671, 'n_words': 275}