<commit_before>import { Store } from 'consus-core/flux';
import StudentStore from './student-store';
import { checkOutContents } from '../lib/api-client';

let contents = [];

let timer = null;

function clearTimer() {
    clearTimeout(timer);
    timer = null;
}

class CartStore extends Store {
    getContents() {
        return contents;
    }

    isOnTimeout(){
        return timer !== null;
    }
}

const store = new CartStore();

store.TIMEOUT_TIME = 60000;

store.registerHandler('STUDENT_FOUND', () => {
    if(store.isOnTimeout()){
        clearTimer();
    }
});

store.registerHandler('CHECKOUT_ITEM_FOUND', data => {
    if(store.isOnTimeout()){
        clearTimer();
    }
    let item = {
        address: data.address,
        status: data.status
    };
    contents.push(item);
    timer = setTimeout(() => {
        checkOutContents(StudentStore.getStudent().id, contents.map(content => content.address));
        timer = null;
    }, store.TIMEOUT_TIME);
    store.emitChange();
});

store.registerHandler('CHECKOUT_MODEL_FOUND', data => {
    if(store.isOnTimeout()){
        clearTimer();
    }
    if(data.inStock > 0){
        let model = {
            address: data.address,
            quantity: 1
        };
        contents.push(model);
    }
    timer = setTimeout(() => {
        checkOutContents(StudentStore.getStudent().id, contents.map(content => content.address));
        timer = null;
    }, store.TIMEOUT_TIME);
    store.emitChange();
});

store.registerHandler('CHECKOUT_DUPLICATE_MODEL', data => {
    if(store.isOnTimeout()){
        clearTimer();
    }

    let model = contents.find(content => {
        return content.address === data.address;
    });

    if(model.quantity < data.inStock){
        model.quantity++;
    }

    timer = setTimeout(() => {
        checkOutContents(StudentStore.getStudent().id, contents.map(content => content.address));
        timer = null;
    }, store.TIMEOUT_TIME);
    store.emitChange();
});

store.registerHandler('CHECKOUT_SUCCESS', () => {
    clearTimer();
    store.waitFor(StudentStore);
    contents = [];
    store.emitChange();
});

store.registerHandler('CLEAR_ALL_DATA', () => {
    contents = [];
    store.emitChange();
});

store.registerHandler('CLEAR_CONTENTS', () => {
    contents = [];
    store.emitChange();
});<commit_msg>Refactor cart timeout timer into function<commit_after>import { Store } from 'consus-core/flux';
import StudentStore from './student-store';
import { checkOutContents } from '../lib/api-client';

let contents = [];

let timer = null;

function startTimer(period) {
    timer = setTimeout(() => {
        checkOutContents(StudentStore.getStudent().id, contents.map(content => content.address));
        clearTimer();
    }, period);
}

function clearTimer() {
    clearTimeout(timer);
    timer = null;
}

class CartStore extends Store {
    getContents() {
        return contents;
    }

    isOnTimeout(){
        return timer !== null;
    }
}

const store = new CartStore();

store.TIMEOUT_TIME = 60000;

store.registerHandler('STUDENT_FOUND', () => {
    if(store.isOnTimeout()){
        clearTimer();
    }
});

store.registerHandler('CHECKOUT_ITEM_FOUND', data => {
    if(store.isOnTimeout()){
        clearTimer();
    }
    let item = {
        address: data.address,
        status: data.status
    };
    contents.push(item);
    startTimer(store.TIMEOUT_TIME);
    store.emitChange();
});

store.registerHandler('CHECKOUT_MODEL_FOUND', data => {
    if(store.isOnTimeout()){
        clearTimer();
    }
    if(data.inStock > 0){
        let model = {
            address: data.address,
            quantity: 1
        };
        contents.push(model);
    }
    startTimer(store.TIMEOUT_TIME);
    store.emitChange();
});

store.registerHandler('CHECKOUT_DUPLICATE_MODEL', data => {
    if(store.isOnTimeout()){
        clearTimer();
    }

    let model = contents.find(content => {
        return content.address === data.address;
    });

    if(model.quantity < data.inStock){
        model.quantity++;
    }

    startTimer(store.TIMEOUT_TIME);
    store.emitChange();
});

store.registerHandler('CHECKOUT_SUCCESS', () => {
    clearTimer();
    store.waitFor(StudentStore);
    contents = [];
    store.emitChange();
});

store.registerHandler('CLEAR_ALL_DATA', () => {
    contents = [];
    store.emitChange();
});

store.registerHandler('CLEAR_CONTENTS', () => {
    contents = [];
    store.emitChange();
});
{'fraction_non_alphanumeric': 0.15781214974221028, 'fraction_numerical': 0.003138309796009863, 'mean_word_length': 2.8399311531841653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '1646281', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1345, 'n_words': 227}