<commit_before>/*
 * Copyright (C) 2012-2017  Online-Go.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import * as React from "react";
import {_, pgettext, interpolate} from "translate";
import {post, get} from "requests";
import * as preferences from "preferences";
import {GameList} from "GameList";
import {comm_socket} from "sockets";

interface ObserveGamesProperties {
    // id?: any,
    // user?: any,
    // callback?: ()=>any,
}

export class ObserveGames extends React.PureComponent<ObserveGamesProperties, any> {
    last_refresh: number;
    next_refresh: any;
    auto_refresh: number;

    constructor(props) {
        super(props);
        this.state = {
            page: 1,
            num_pages: 1,
            page_size: preferences.get("observed-games-page-size"),
            viewing: preferences.get("observed-games-viewing"), /* live / correspondence */
            game_list: [],
            live_game_count: 0,
            corr_game_count: 0,
        };
    }

    doSubscribe = () => {{{
        comm_socket.send("gamelist/count/subscribe");
    }}}

    componentDidMount() {{{
        comm_socket.on("gamelist-count", this.updateCounts);
        comm_socket.on("connect", this.doSubscribe);
        if (comm_socket.connected) {
            this.doSubscribe();
        }
        this.refresh();
        //this.auto_refresh = setInterval(this.refresh, 500);
    }}}
    componentWillUnmount() {{{
        comm_socket.off("gamelist-count", this.updateCounts);
        comm_socket.off("connect", this.doSubscribe);
        if (comm_socket.connected) {
            comm_socket.send("gamelist/count/unsubscribe");
        }
        if (this.auto_refresh) {
            clearInterval(this.auto_refresh);
        }
    }}}
    updateCounts = (counts) => {{{
        console.log(counts);
        this.setState({
            live_game_count: counts.live,
            corr_game_count: counts.correspondence,
        });
    }}}
    setPageSize = (ev) => {{{
        let ct: number = parseInt(ev.target.value);
        preferences.set("observed-games-page-size", ct);
        this.setState({page_size: ct});
        setTimeout(this.refresh, 1);
    }}}
    refresh = () => {{{
        let now = Date.now();
        //if (this.last_refresh != null && (now - this.last_refresh < 1000.0)) {
        if (this.last_refresh != null && (now - this.last_refresh < 1.0)) {
            console.warn("Slow down");
            if (!this.next_refresh) {
                this.next_refresh = setTimeout(() => {
                    this.next_refresh = null;
                    this.refresh();
                }, 1000);
            }
            return;
        }
        this.last_refresh = now;

        comm_socket.send("gamelist/query", {
            list: this.state.viewing,
            sort_by: "rank",
            from: (this.state.page - 1) * this.state.page_size,
            limit: this.state.page_size
        },<commit_msg>observe games: changing page length resets page to 1<commit_after>/*
 * Copyright (C) 2012-2017  Online-Go.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import * as React from "react";
import {_, pgettext, interpolate} from "translate";
import {post, get} from "requests";
import * as preferences from "preferences";
import {GameList} from "GameList";
import {comm_socket} from "sockets";

interface ObserveGamesProperties {
    // id?: any,
    // user?: any,
    // callback?: ()=>any,
}

export class ObserveGames extends React.PureComponent<ObserveGamesProperties, any> {
    last_refresh: number;
    next_refresh: any;
    auto_refresh: number;

    constructor(props) {
        super(props);
        this.state = {
            page: 1,
            num_pages: 1,
            page_size: preferences.get("observed-games-page-size"),
            viewing: preferences.get("observed-games-viewing"), /* live / correspondence */
            game_list: [],
            live_game_count: 0,
            corr_game_count: 0,
        };
    }

    doSubscribe = () => {{{
        comm_socket.send("gamelist/count/subscribe");
    }}}

    componentDidMount() {{{
        comm_socket.on("gamelist-count", this.updateCounts);
        comm_socket.on("connect", this.doSubscribe);
        if (comm_socket.connected) {
            this.doSubscribe();
        }
        this.refresh();
        //this.auto_refresh = setInterval(this.refresh, 500);
    }}}
    componentWillUnmount() {{{
        comm_socket.off("gamelist-count", this.updateCounts);
        comm_socket.off("connect", this.doSubscribe);
        if (comm_socket.connected) {
            comm_socket.send("gamelist/count/unsubscribe");
        }
        if (this.auto_refresh) {
            clearInterval(this.auto_refresh);
        }
    }}}
    updateCounts = (counts) => {{{
        console.log(counts);
        this.setState({
            live_game_count: counts.live,
            corr_game_count: counts.correspondence,
        });
    }}}
    setPageSize = (ev) => {{{
        let ct: number = parseInt(ev.target.value);
        preferences.set("observed-games-page-size", ct);
        this.setState({page_size: ct});
        this.setPage(1);
        setTimeout(this.refresh, 1);
    }}}
    refresh = () => {{{
        let now = Date.now();
        //if (this.last_refresh != null && (now - this.last_refresh < 1000.0)) {
        if (this.last_refresh != null && (now - this.last_refresh < 1.0)) {
            console.warn("Slow down");
            if (!this.next_refresh) {
                this.next_refresh = setTimeout(() => {
                    this.next_refresh = null;
                    this.refresh();
                }, 1000);
            }
            return;
        }
        this.last_refresh = now;

        comm_socket.send("gamelist/query", {
            list: this.state.viewing,
            sort_by: "rank",
            from: (this.state.page - 1) * this.state.page_size,
            limit: this.state.page_size
        },
{'fraction_non_alphanumeric': 0.1253470294280955, 'fraction_numerical': 0.00832870627429206, 'mean_word_length': 2.2528216704288937, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '237278', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2038, 'n_words': 525}