<commit_before>'use strict';

var ajax = require('./ajax'),
ko = require('knockout'),
session;

function isConnected() {
  return !!session;
}

var isConnectedObs = ko.observable(isConnected());

function trash() {
  session = null;
  isConnectedObs(isConnected());
}

function login(username, password) {
  return ajax({ url: '/login', method: 'POST', data: {
    username: username,
    password: password
  }}).then(function (data) {
    session = data;
    isConnectedObs(isConnected());
    return session;
  });
}

function logout() {
  return ajax({ url: '/logout', method: 'GET' }, true).then(function () {
    trash();
    return null;
  }, function (error) {
    throw new Error(error.responseText);
  });
}

function get() {
  return session;
}

function refresh() {
  return ajax({ url: '/account/info', method: 'GET'}).then(function (data) {
    session = data;
    isConnectedObs(isConnected());
    return session;
  });
}

module.exports = {
  isConnected: isConnected,
  isConnectedObs: isConnectedObs,
  get: get,
  trash: trash,
  login: login,
  logout: logout,
  refresh: refresh
};
<commit_msg>Start rewriting session as a mithril component<commit_after>var m = require('mithril');
var utils = require('./utils');

var session = {};

session.controller = function() {

  this.session = null;

  this.isConnected = function() {
    return !!this.session;
  };

  this.login = function(username, password) {
    return m.request({
      url: window.apiEndPoint + '/login',
      method: 'POST',
      config: utils.xhrConfig,
      data: {
        username: username,
        password: password
      }
    }).then(function(data) {
      this.session = data;
    });
  };

  this.logout = function() {
    return m.request({
      url: window.apiEndPoint + '/logout',
      method: 'GET',
      config: utils.xhrConfig
    }).then(function() {
      this.session = null;
    }, function(error) {
      throw new Error(error.responseText);
    });
  };

  this.refresh = function() {
    return m.request({
      url: window.apiEndPoint + 'account/info',
      method: 'GET',
      config: utils.xhrConfig
    }).then(function(data) {
      this.session = data;
    });
  };
};

module.exports = session;

{'fraction_non_alphanumeric': 0.15737410071942445, 'fraction_numerical': 0.0, 'mean_word_length': 3.0825688073394497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6093276', 'n_tokens_mistral': 723, 'n_tokens_neox': 711, 'n_words': 167}