<commit_before>/**
 * Copyright 2015 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

self.IOWA = self.IOWA || {};

IOWA.Schedule = (function() {

  "use strict";

  var SCHEDULE_ENDPOINT = 'api/v1/schedule';
  var SCHEDULE_ENDPOINT_USERS = 'api/v1/user/schedule';
  var QUEUED_SESSION_UPDATES_DB_NAME = 'shed-offline-session-updates';

  var scheduleData_ = null;
  var userSavedSessions_ = [];

  /**
   * Fetches the I/O schedule data.
   * @return {Promise} Resolves with response schedule data.
   */
  function fetchSchedule() {
    if (scheduleData_) {
      return Promise.resolve(scheduleData_);
    }

    return IOWA.Request.xhrPromise('GET', SCHEDULE_ENDPOINT, false).then(function(resp) {
      scheduleData_ = resp;
      return scheduleData_;
    });
  }

  /**
   * Fetches the user's saved sessions.
   * If this is the first time it's been called, then uses the cache-then-network strategy to
   * first try to read the data stored in the Cache Storage API, and invokes the callback with that
   * response. It then tries to fetch a fresh copy of the data from the network, saves the response
   * locally in memory, and resolves the promise with that response.
   * @param {function} callback The callback to execute when the user schedule data is available.
   */
  function fetchUserSchedule(callback) {
    if (userSavedSessions_.length) {
      callback(userSavedSessions_);
    } else {
      var callbackWrapper = function(userSavedSessions) {
        userSavedSessions_ = userSavedSessions || [];
        callback(userSavedSessions_);
      };

      IOWA.Request.cacheThenNetwork(SCHEDULE_ENDPOINT_USERS, callback, callbackWrapper, true);
    }
  }

  /**
   * Adds/removes a session from the user's bookmarked sessions.
   * @param {string} sessionId The session to add/remove.
   * @param {Boolean} save True if the session should be added, false if it
   *     should be removed.
   * @return {Promise} Resolves with the server's response.
   */
  function saveSession(sessionId, save) {
    IOWA.Analytics.trackEvent('session', 'bookmark', save);

    return IOWA.Auth.waitForSignedIn('Sign in to add events to My Schedule').then(function() {
      IOWA.Elements.Template.scheduleFetchingUserData = true;
      var url = SCHEDULE_ENDPOINT_USERS + '/' + sessionId;
      return IOWA.Request.xhrPromise(save ? 'PUT' : 'DELETE', url, true)
        .then(clearCachedUserSchedule)
        .catch(function(error) {
          IOWA.Elements.Template.scheduleFetchingUserData = false;
          if ('serviceWorker' in navigator) {<commit_msg>Make sure the toast message properly reflects whether the request was cached.<commit_after>/**
 * Copyright 2015 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

self.IOWA = self.IOWA || {};

IOWA.Schedule = (function() {

  "use strict";

  var SCHEDULE_ENDPOINT = 'api/v1/schedule';
  var SCHEDULE_ENDPOINT_USERS = 'api/v1/user/schedule';
  var QUEUED_SESSION_UPDATES_DB_NAME = 'shed-offline-session-updates';

  var scheduleData_ = null;
  var userSavedSessions_ = [];

  /**
   * Fetches the I/O schedule data.
   * @return {Promise} Resolves with response schedule data.
   */
  function fetchSchedule() {
    if (scheduleData_) {
      return Promise.resolve(scheduleData_);
    }

    return IOWA.Request.xhrPromise('GET', SCHEDULE_ENDPOINT, false).then(function(resp) {
      scheduleData_ = resp;
      return scheduleData_;
    });
  }

  /**
   * Fetches the user's saved sessions.
   * If this is the first time it's been called, then uses the cache-then-network strategy to
   * first try to read the data stored in the Cache Storage API, and invokes the callback with that
   * response. It then tries to fetch a fresh copy of the data from the network, saves the response
   * locally in memory, and resolves the promise with that response.
   * @param {function} callback The callback to execute when the user schedule data is available.
   */
  function fetchUserSchedule(callback) {
    if (userSavedSessions_.length) {
      callback(userSavedSessions_);
    } else {
      var callbackWrapper = function(userSavedSessions) {
        userSavedSessions_ = userSavedSessions || [];
        callback(userSavedSessions_);
      };

      IOWA.Request.cacheThenNetwork(SCHEDULE_ENDPOINT_USERS, callback, callbackWrapper, true);
    }
  }

  /**
   * Adds/removes a session from the user's bookmarked sessions.
   * @param {string} sessionId The session to add/remove.
   * @param {Boolean} save True if the session should be added, false if it
   *     should be removed.
   * @return {Promise} Resolves with the server's response.
   */
  function saveSession(sessionId, save) {
    IOWA.Analytics.trackEvent('session', 'bookmark', save);

    return IOWA.Auth.waitForSignedIn('Sign in to add events to My Schedule').then(function() {
      IOWA.Elements.Template.scheduleFetchingUserData = true;
      var url = SCHEDULE_ENDPOINT_USERS + '/' + sessionId;
      return IOWA.Request.xhrPromise(save ? 'PUT' : 'DELETE', url, true)
        .then(clearCachedUserSchedule)
        .catch(function(error) {
          IOWA.Elements.Template.scheduleFetchingUserData = false;
          // error will be an XMLHttpRequestProgressEvent if the xhrPromise() was rejected due to
          // a network error. Otherwise, error will be a Error object.
          if ('serviceWorker' in navigator && XMLHttpRequestProgressEvent &&
              error instanceof XMLHttpRequestProgressEvent) {
{'fraction_non_alphanumeric': 0.0916310323762981, 'fraction_numerical': 0.0030543677458766036, 'mean_word_length': 3.3982538616521154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '792367', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1865, 'n_words': 639}