<commit_before>import config from '../config';

const INTERVAL = config.FLUSH_INTERVAL;

var state = {
    user: null,
    timeline: null,
    position: 1,
    events: []
};

function handleUser (type, e) {
    state.user = e;
}

function handlePost (type, e) {
    var post = Object.assign({
        impressionOrder: state.position++,
        visibility: type,
        visibilityInfo: e.visibilityInfo,
        type: 'impression',
        timelineId: state.timeline.id
    }, e.data);

    if (post.visibility === 'public') {
        post.html = e.element.html();
    }
    state.events.push(post);
}

function handleTimeline (type, e) {
    state.position = 1;
    state.timeline = {
        type: 'timeline',
        id: e.uuid,
        location: window.location.href
    };
    state.events.push(state.timeline);
}

function sync (hub) {
    if (state.events.length) {
        // Send timelines to the page handling the communication with the API.
        // This might be refactored using something compatible to the HUB architecture.
        chrome.runtime.sendMessage({ type: 'sync', payload: state.events, userId: state.user.id },
                                   (response) => hub.event('syncResponse', response));

        state.events = [];
    }
}

export function register (hub) {
    hub.register('user', handleUser);
    hub.register('newPost', handlePost);
    hub.register('newTimeline', handleTimeline);
    hub.register('windowUnload', sync.bind(null, hub));
    window.setInterval(sync.bind(null, hub), INTERVAL);
}
<commit_msg>Fix problem with timeline event<commit_after>import config from '../config';

const INTERVAL = config.FLUSH_INTERVAL;

var state = {
    user: null,
    timeline: null,
    position: 1,
    events: []
};

function handleUser (type, e) {
    state.user = e;
}

function handlePost (type, e) {
    var post = Object.assign({
        impressionOrder: state.position++,
        visibility: type,
        visibilityInfo: e.visibilityInfo,
        type: 'impression',
        timelineId: state.timeline.id
    }, e.data);

    if (post.visibility === 'public') {
        post.html = e.element.html();
    }
    state.events.push(post);
}

function handleTimeline (type, e) {
    state.position = 1;
    state.timeline = {
        type: 'timeline',
        id: e.uuid,
        startTime: e.startTime,
        location: window.location.href
    };
    state.events.push(state.timeline);
}

function sync (hub) {
    if (state.events.length) {
        // Send timelines to the page handling the communication with the API.
        // This might be refactored using something compatible to the HUB architecture.
        chrome.runtime.sendMessage({ type: 'sync', payload: state.events, userId: state.user.id },
                                   (response) => hub.event('syncResponse', response));

        state.events = [];
    }
}

export function register (hub) {
    hub.register('user', handleUser);
    hub.register('newPost', handlePost);
    hub.register('newTimeline', handleTimeline);
    hub.register('windowUnload', sync.bind(null, hub));
    window.setInterval(sync.bind(null, hub), INTERVAL);
}

{'fraction_non_alphanumeric': 0.13365079365079366, 'fraction_numerical': 0.0012698412698412698, 'mean_word_length': 2.62183908045977, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3196951', 'n_tokens_mistral': 952, 'n_tokens_neox': 932, 'n_words': 234}