<commit_before>// Copyright (c) <NAME> & <NAME>. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from './Log.js';
import { CheckSessionIFrame } from './CheckSessionIFrame.js';
import { Global } from './Global.js';

export class SessionMonitor {

    constructor(userManager, CheckSessionIFrameCtor = CheckSessionIFrame, timer = Global.timer) {
        if (!userManager) {
            Log.error("SessionMonitor.ctor: No user manager passed to SessionMonitor");
            throw new Error("userManager");
        }

        this._userManager = userManager;
        this._CheckSessionIFrameCtor = CheckSessionIFrameCtor;
        this._timer = timer;

        this._userManager.events.addUserLoaded(this._start.bind(this));
        this._userManager.events.addUserUnloaded(this._stop.bind(this));

        this._userManager.getUser().then(user => {
            // doing this manually here since calling getUser 
            // doesn't trigger load event.
            if (user) {
                this._start(user);
            }
            else if (this._settings.monitorAnonymousSession) {
                this._userManager.querySessionStatus().then(session => {
                    let tmpUser = {
                        session_state : session.session_state
                    };
                    if (session.sub && session.sid) {
                        tmpUser.profile = {
                            sub: session.sub,
                            sid: session.sid
                        };
                    }
                    this._start(tmpUser);
                })
                .catch(err => {
                    // catch to suppress errors since we're in a ctor
                    Log.error("SessionMonitor ctor: error from querySessionStatus:", err.message);
                });
            }
        }).catch(err => {
            // catch to suppress errors since we're in a ctor
            Log.error("SessionMonitor ctor: error from getUser:", err.message);
        });
    }

    get _settings() {
        return this._userManager.settings;
    }
    get _metadataService() {
        return this._userManager.metadataService;
    }
    get _client_id() {
        return this._settings.client_id;
    }
    get _checkSessionInterval() {
        return this._settings.checkSessionInterval;
    }
    get _stopCheckSessionOnError() {
        return this._settings.stopCheckSessionOnError;
    }

    _start(user) {
        let session_state = user.session_state;

        if (session_state) {
            if (user.profile) {
                this._sub = user.profile.sub;
                this._sid = user.profile.sid;
                Log.debug("SessionMonitor._start: session_state:", session_state, ", sub:", this._sub);
            }
            else {
                this._sub = undefined;
                this._sid = undefined;
                Log.debug("SessionMonitor._start: session_state:", session_state, ", anonymous user");<commit_msg>resolve leaked promise #726<commit_after>// Copyright (c) <NAME> & <NAME>. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from './Log.js';
import { CheckSessionIFrame } from './CheckSessionIFrame.js';
import { Global } from './Global.js';

export class SessionMonitor {

    constructor(userManager, CheckSessionIFrameCtor = CheckSessionIFrame, timer = Global.timer) {
        if (!userManager) {
            Log.error("SessionMonitor.ctor: No user manager passed to SessionMonitor");
            throw new Error("userManager");
        }

        this._userManager = userManager;
        this._CheckSessionIFrameCtor = CheckSessionIFrameCtor;
        this._timer = timer;

        this._userManager.events.addUserLoaded(this._start.bind(this));
        this._userManager.events.addUserUnloaded(this._stop.bind(this));

        Promise.resolve(this._userManager.getUser().then(user => {
            // doing this manually here since calling getUser 
            // doesn't trigger load event.
            if (user) {
                this._start(user);
            }
            else if (this._settings.monitorAnonymousSession) {
                this._userManager.querySessionStatus().then(session => {
                    let tmpUser = {
                        session_state : session.session_state
                    };
                    if (session.sub && session.sid) {
                        tmpUser.profile = {
                            sub: session.sub,
                            sid: session.sid
                        };
                    }
                    this._start(tmpUser);
                })
                .catch(err => {
                    // catch to suppress errors since we're in a ctor
                    Log.error("SessionMonitor ctor: error from querySessionStatus:", err.message);
                });
            }
        }).catch(err => {
            // catch to suppress errors since we're in a ctor
            Log.error("SessionMonitor ctor: error from getUser:", err.message);
        }));
    }

    get _settings() {
        return this._userManager.settings;
    }
    get _metadataService() {
        return this._userManager.metadataService;
    }
    get _client_id() {
        return this._settings.client_id;
    }
    get _checkSessionInterval() {
        return this._settings.checkSessionInterval;
    }
    get _stopCheckSessionOnError() {
        return this._settings.stopCheckSessionOnError;
    }

    _start(user) {
        let session_state = user.session_state;

        if (session_state) {
            if (user.profile) {
                this._sub = user.profile.sub;
                this._sid = user.profile.sid;
                Log.debug("SessionMonitor._start: session_state:", session_state, ", sub:", this._sub);
            }
            else {
                this._sub = undefined;
                this._sid = undefined;
                Log.debug("SessionMonitor._start: session_state:", session_state, ", anonymous user");
{'fraction_non_alphanumeric': 0.10121654501216545, 'fraction_numerical': 0.0011354420113544202, 'mean_word_length': 1.7198941332157036, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '2933404', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1499, 'n_words': 352}