<commit_before>const moment               = require('moment');
const DigitTicker          = require('./digit_ticker');
const ViewPopupUI          = require('../user/view_popup/view_popup.ui');
const showLocalTimeOnHover = require('../../base/clock').showLocalTimeOnHover;
const BinarySocket         = require('../../base/socket');
const addComma             = require('../../../_common/base/currency_base').addComma;
const localize             = require('../../../_common/localize').localize;
const getPropertyValue     = require('../../../_common/utility').getPropertyValue;

const DigitDisplay = (() => {
    let $container,
        contract,
        tick_count,
        spot_times;

    const subscribe = (request) => {
        // Subscribe if contract is still ongoing/running.
        if (contract.current_spot_time < contract.date_expiry) {
            request.subscribe = 1;
            request.end       = 'latest';
        } else {
            request.end = contract.date_expiry;
        }
    };

    const init = (id_render, proposal_open_contract) => {
        tick_count = 1;
        contract   = proposal_open_contract;
        spot_times = [];

        $container = $(`#${id_render}`);
        $container
            .addClass('normal-font')
            .html($('<h5 />', { text: contract.display_name, class: 'center-text' }))
            .append($('<div />', { class: 'gr-8 gr-centered gr-12-m' })
                .append($('<div />', { class: 'gr-row', id: 'table_digits' })
                    .append($('<strong />', { class: 'gr-3', text: localize('Tick') }))
                    .append($('<strong />', { class: 'gr-3', text: localize('Spot') }))
                    .append($('<strong />', { class: 'gr-6', text: localize('Spot Time (GMT)') }))))
            .append($('<div />', { class: 'digit-ticker invisible', id: 'digit_ticker_container' }));

        DigitTicker.init(
            'digit_ticker_container',
            contract.contract_type,
            contract.shortcode,
            contract.tick_count,
            contract.status
        );
        const request = {
            ticks_history: contract.underlying,
            start        : contract.date_start,
        };

        subscribe(request);

        BinarySocket.send(request, { callback: update });
    };

    const updateTable = (spot, time) => {
        if (spot_times.some(item => item.spot === spot && item.time === time)) {
            return;
        }
        if (spot_times.filter(spot_time => spot_time.spot === spot && spot_time.time === time).length !== 0) {
            return;
        }

        spot_times.push({
            spot,
            time,
        });

        const csv_spot = addComma(spot);

        $container
            .find('#table_digits')
            .append($('<p />', { class: 'gr-3', text: tick_count }))
            .append($('<p />', { class: 'gr-3 gray', html: tick_count === contract.tick_count ? `${csv_spot.slice(0, csv_spot.length - 1)}<strong>${csv_spot.substr(-1)}</strong>` : csv_spot }))
            .append($('<p />', { class: 'gr-6 gray digit-spot-time no-underline', text: moment(+time * 1000).utc().format('YYYY-MM-DD HH:mm:ss') }));

        DigitTicker.update(
            tick_count,
            {
                quote: contract.status !== 'open' ? contract.exit_tick : spot,
                epoch: contract.status !== 'open' ? contract.exit_tick_time : contract.date_expiry,
            }
        );
    };

    const redrawFromHistory = (response) => {
        tick_count = 1;
        if (!$container.is(':visible') || !response || (!response.history)) {
            return;
        }
        $container.find('#table_digits').empty();
        $container.find('#table_digits')
            .append($('<strong />', { class: 'gr-3', text: localize('Tick') }))
            .append($('<strong />', { class: 'gr-3', text: localize('Spot') }))
            .append($('<strong />', { class: 'gr-6', text: localize('Spot Time (GMT)') }));

        response.history.times.some((time, idx) => {
            if (+time >= +contract.entry_tick_time && time <= +contract.exit_tick_time) {
                const spot = response.history.prices[idx];
                const csv_spot = addComma(spot);

                $container
                    .find('#table_digits')
                    .append($('<p />', { class: 'gr-3', text: tick_count }))
                    .append($('<p />', { class: 'gr-3 gray', html: tick_count === contract.tick_count ? `${csv_spot.slice(0, csv_spot.length - 1)}<strong>${csv_spot.substr(-1)}</strong>` : csv_spot }))
                    .append($('<p />', { class: 'gr-6 gray digit-spot-time no-underline', text: moment(+time * 1000).utc().format('YYYY-MM-DD HH:mm:ss') }));

                tick_count += 1;
            }
            return tick_count > contract.tick_count;
        });

        showLocalTimeOnHover('.digit-spot-time');

    };

    const update = (response) => {
        if (!$container.is(':visible') || !response || (!response.tick && !response.history)) {
            return;
        }

        if (getPropertyValue(response, ['tick', 'id']) && document.getElementById('sell_content_wrapper')) {
            ViewPopupUI.storeSubscriptionID(response.tick.id);
        }

        if (response.history) {
            response.history.times.some((time, idx) => {
                if (+time >= +contract.entry_tick_time) {
                    updateTable(response.history.prices[idx], time);
                    tick_count += 1;
                }
                return tick_count > contract.tick_count;
            });
        } else if (response.tick) {
            if (tick_count <= contract.tick_count &&
                +response.tick.epoch >= +contract.entry_tick_time) {
                updateTable(response.tick.quote, response.tick.epoch);
                tick_count += 1;
            }
        }
        showLocalTimeOnHover('.digit-spot-time');
    };

    const end = (proposal_open_contract) => {
        if (proposal_open_contract.status !== 'open') {
            DigitTicker.update(proposal_open_contract.tick_count, {
                quote: proposal_open_contract.exit_tick,
                epoch: proposal_open_contract.exit_tick_time,
            });

            const request = {
                ticks_history: contract.underlying,
                start        : contract.entry_tick_time,
                end          : contract.exit_tick_time,
            };

            // force rerender the table by sending the history
            BinarySocket.send(request, { callback: redrawFromHistory });
        }
        if (proposal_open_contract.status === 'won') {
            DigitTicker.markAsWon();
            DigitTicker.markDigitAsWon(proposal_open_contract.exit_tick.slice(-1));
        }
        if (proposal_open_contract.status === 'lost') {
            DigitTicker.markAsLost();
            DigitTicker.markDigitAsLost(proposal_open_contract.exit_tick.slice(-1));
        }
    };

    return {
        end,
        init,
        update,
    };
})();

module.exports = DigitDisplay;<commit_msg>Conditionally redraw, only on portfolio page when the contract is streaming.<commit_after>const moment               = require('moment');
const DigitTicker          = require('./digit_ticker');
const ViewPopupUI          = require('../user/view_popup/view_popup.ui');
const showLocalTimeOnHover = require('../../base/clock').showLocalTimeOnHover;
const BinarySocket         = require('../../base/socket');
const addComma             = require('../../../_common/base/currency_base').addComma;
const localize             = require('../../../_common/localize').localize;
const getPropertyValue     = require('../../../_common/utility').getPropertyValue;

const DigitDisplay = (() => {
    let $container,
        contract,
        tick_count,
        spot_times,
        is_redraw_possible;

    is_redraw_possible = false;

    const subscribe = (request) => {
        // Subscribe if contract is still ongoing/running.
        if (contract.current_spot_time < contract.date_expiry) {
            request.subscribe = 1;
            request.end       = 'latest';
        } else {
            request.end = contract.date_expiry;
        }
    };

    const init = (id_render, proposal_open_contract) => {
        tick_count = 1;
        contract   = proposal_open_contract;
        spot_times = [];

        $container = $(`#${id_render}`);
        $container
            .addClass('normal-font')
            .html($('<h5 />', { text: contract.display_name, class: 'center-text' }))
            .append($('<div />', { class: 'gr-8 gr-centered gr-12-m' })
                .append($('<div />', { class: 'gr-row', id: 'table_digits' })
                    .append($('<strong />', { class: 'gr-3', text: localize('Tick') }))
                    .append($('<strong />', { class: 'gr-3', text: localize('Spot') }))
                    .append($('<strong />', { class: 'gr-6', text: localize('Spot Time (GMT)') }))))
            .append($('<div />', { class: 'digit-ticker invisible', id: 'digit_ticker_container' }));

        DigitTicker.init(
            'digit_ticker_container',
            contract.contract_type,
            contract.shortcode,
            contract.tick_count,
            contract.status
        );
        const request = {
            ticks_history: contract.underlying,
            start        : contract.date_start,
        };

        subscribe(request);

        BinarySocket.send(request, { callback: update });
    };

    const updateTable = (spot, time) => {
        if (spot_times.some(item => item.spot === spot && item.time === time)) {
            return;
        }
        if (spot_times.filter(spot_time => spot_time.spot === spot && spot_time.time === time).length !== 0) {
            return;
        }

        spot_times.push({
            spot,
            time,
        });

        const csv_spot = addComma(spot);

        $container
            .find('#table_digits')
            .append($('<p />', { class: 'gr-3', text: tick_count }))
            .append($('<p />', { class: 'gr-3 gray', html: tick_count === contract.tick_count ? `${csv_spot.slice(0, csv_spot.length - 1)}<strong>${csv_spot.substr(-1)}</strong>` : csv_spot }))
            .append($('<p />', { class: 'gr-6 gray digit-spot-time no-underline', text: moment(+time * 1000).utc().format('YYYY-MM-DD HH:mm:ss') }));

        DigitTicker.update(
            tick_count,
            {
                quote: contract.status !== 'open' ? contract.exit_tick : spot,
                epoch: contract.status !== 'open' ? contract.exit_tick_time : contract.date_expiry,
            }
        );
    };

    const redrawFromHistory = (response) => {
        tick_count = 1;
        if (!$container.is(':visible') || !response || (!response.history)) {
            return;
        }
        $container.find('#table_digits').empty();
        $container.find('#table_digits')
            .append($('<strong />', { class: 'gr-3', text: localize('Tick') }))
            .append($('<strong />', { class: 'gr-3', text: localize('Spot') }))
            .append($('<strong />', { class: 'gr-6', text: localize('Spot Time (GMT)') }));

        response.history.times.some((time, idx) => {
            if (+time >= +contract.entry_tick_time && time <= +contract.exit_tick_time) {
                const spot = response.history.prices[idx];
                const csv_spot = addComma(spot);

                $container
                    .find('#table_digits')
                    .append($('<p />', { class: 'gr-3', text: tick_count }))
                    .append($('<p />', { class: 'gr-3 gray', html: tick_count === contract.tick_count ? `${csv_spot.slice(0, csv_spot.length - 1)}<strong>${csv_spot.substr(-1)}</strong>` : csv_spot }))
                    .append($('<p />', { class: 'gr-6 gray digit-spot-time no-underline', text: moment(+time * 1000).utc().format('YYYY-MM-DD HH:mm:ss') }));

                tick_count += 1;
            }
            return tick_count > contract.tick_count;
        });

        showLocalTimeOnHover('.digit-spot-time');

    };

    const update = (response) => {
        if (!$container.is(':visible') || !response || (!response.tick && !response.history)) {
            return;
        }

        if (getPropertyValue(response, ['tick', 'id']) && document.getElementById('sell_content_wrapper')) {
            ViewPopupUI.storeSubscriptionID(response.tick.id);
        }

        if (response.history) {
            response.history.times.some((time, idx) => {
                if (+time >= +contract.entry_tick_time) {
                    updateTable(response.history.prices[idx], time);
                    tick_count += 1;
                }
                return tick_count > contract.tick_count;
            });
        } else if (response.tick) {
            is_redraw_possible = true;
            if (tick_count <= contract.tick_count &&
                +response.tick.epoch >= +contract.entry_tick_time) {
                updateTable(response.tick.quote, response.tick.epoch);
                tick_count += 1;
            }
        }
        showLocalTimeOnHover('.digit-spot-time');
    };

    const end = (proposal_open_contract) => {
        if (proposal_open_contract.status !== 'open') {
            DigitTicker.update(proposal_open_contract.tick_count, {
                quote: proposal_open_contract.exit_tick,
                epoch: proposal_open_contract.exit_tick_time,
            });

            const request = {
                ticks_history: contract.underlying,
                start        : contract.entry_tick_time,
                end          : contract.exit_tick_time,
            };
            if (is_redraw_possible) {
                // force rerender the table by sending the history
                BinarySocket.send(request, { callback: redrawFromHistory });
            }
        }
        if (proposal_open_contract.status === 'won') {
            DigitTicker.markAsWon();
            DigitTicker.markDigitAsWon(proposal_open_contract.exit_tick.slice(-1));
        }
        if (proposal_open_contract.status === 'lost') {
            DigitTicker.markAsLost();
            DigitTicker.markDigitAsLost(proposal_open_contract.exit_tick.slice(-1));
        }
    };

    return {
        end,
        init,
        update,
    };
})();

module.exports = DigitDisplay;
{'fraction_non_alphanumeric': 0.1594875346260388, 'fraction_numerical': 0.005401662049861496, 'mean_word_length': 1.9085599194360523, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 75, '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': '5992042', 'n_tokens_mistral': 4284, 'n_tokens_neox': 4108, 'n_words': 708}