<commit_before>﻿import { RecentItemViewModel } from './recent-item-view-model';
import { ContextService } from '../context.service';
import { ViewModelFactoryService } from '../view-model-factory.service';
import map from 'lodash/map';

import { UrlManagerService } from '../url-manager.service';
import { Pane } from '../route-data';

export class RecentItemsViewModel {

    constructor(
        viewModelFactory: ViewModelFactoryService,
        private readonly context: ContextService,
        private readonly urlManager: UrlManagerService,
        private readonly onPaneId: Pane
    ) {
        const items = map(this.context.getRecentlyViewed(), (o, i) => ({ obj: o, link: o.updateSelfLinkWithTitle(), index: i }));
        this.items = map(items, i => viewModelFactory.recentItemViewModel(i.obj, i.link!, onPaneId, false, i.index));
    }

    items: RecentItemViewModel[];

    readonly clear = () => {
        this.context.clearRecentlyViewed();
        this.items = [];
        this.urlManager.triggerPageReloadByFlippingReloadFlagInUrl(this.onPaneId);
    }
}<commit_msg>make recent items update dynamically<commit_after>﻿import { RecentItemViewModel } from './recent-item-view-model';
import { ContextService } from '../context.service';
import { ViewModelFactoryService } from '../view-model-factory.service';
import map from 'lodash/map';
import every from 'lodash/map';
import * as Models from '../models';
import { UrlManagerService } from '../url-manager.service';
import { Pane } from '../route-data';

export class RecentItemsViewModel {

    constructor(
        private readonly viewModelFactory: ViewModelFactoryService,
        private readonly context: ContextService,
        private readonly urlManager: UrlManagerService,
        private readonly onPaneId: Pane
    ) {
        this.recentlyViewed = this.context.getRecentlyViewed();
        this.refreshItems();
    }

    private recentlyViewed : Models.DomainObjectRepresentation[];

    private refreshItems() {       
        const items = map(this.recentlyViewed, (o, i) => ({ obj: o, link: o.updateSelfLinkWithTitle(), index: i }));
        this._items = map(items, i => this.viewModelFactory.recentItemViewModel(i.obj, i.link!, this.onPaneId, false, i.index));
    }

    private itemsHaveChanged() {
        const currentRecentlyViewed = this.context.getRecentlyViewed();

        const same = this.recentlyViewed.length === currentRecentlyViewed.length && 
                     every(this.recentlyViewed, (v, i) => v.id() === currentRecentlyViewed[i].id());

        if (!same) {
            this.recentlyViewed = currentRecentlyViewed;
        }

        return !same;
    }

    private _items : RecentItemViewModel[];

    get items() : RecentItemViewModel[] {
        if (this.itemsHaveChanged()) {
            this.refreshItems();
        }
        return this._items;
    }

    readonly clear = () => {
        this.context.clearRecentlyViewed();
        this._items = [];
        this.urlManager.triggerPageReloadByFlippingReloadFlagInUrl(this.onPaneId);
    }
}
{'fraction_non_alphanumeric': 0.12549019607843137, 'fraction_numerical': 0.0, 'mean_word_length': 3.170299727520436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '1218961', 'n_tokens_mistral': 879, 'n_tokens_neox': 834, 'n_words': 188}