<commit_before>'use strict';

const request = require('superagent-promise')(require('superagent'), Promise);

const defaultSettings = {
  syncTimeTreshold: 60000, // default to syncing once per minute
  maxCachedHandles: 100  // default to keeping a maximum of 100 handles in-memory at a time
};

const ExtendedMindPublicItems = require('./extendedmind-data.js')

module.exports = (apiUrl, settings) => {

  let backendApi = apiUrl;
  let config = settings ? settings : {};
  config.apiUrl = apiUrl;

  for (var defaultSetting in defaultSettings){
    if (defaultSettings.hasOwnProperty(defaultSetting) && !config.hasOwnProperty(defaultSetting)){
      config[defaultSetting] = defaultSettings[defaultSetting];
    }
  }
  // Cache object for all public notes
  let handleCache = require("lru-cache")(
    { max: config.maxCachedHandles}
  );

  async function fetchPublicItems(handle){
    let publicOwnerUrl = backendApi + '/public/' + handle;
    let backendResponse = await request.get(publicOwnerUrl);
    if (backendResponse.status === 200){
      return new ExtendedMindPublicItems(backendResponse.body);
    }
  }

  async function refreshPublicItems(cachedItems){
    if (Date.now() - cachedItems.getLastSynced() > config.syncTimeTreshold){
      let publicOwnerModifiedUrl = backendApi + '/public/' + handle +
                                  '?modified=' + cachedItems.getLatestModified();
      let backendResponse = await request.get(publicOwnerModifiedUrl);
      if (backendResponse.status === 200){
        cachedItems.updateItems(backendResponse.body);
      }
    }
    return cachedItems;
  }

  async function getPublicItems(handle){
    const cachedItems = handleCache.get(handle);
    const items = cachedItems ? await refreshPublicItems(cachedItems) : await fetchPublicItems(handle);
    handleCache.set(handle, items);
    return items;
  }

  return {
    // Returns a list of public items for given handle
    getPublicItems: async function(handle) {
      return await getPublicItems(handle);
    }
  }
};

<commit_msg>Fixed bug with sync not working<commit_after>'use strict';

const request = require('superagent-promise')(require('superagent'), Promise);

const defaultSettings = {
  syncTimeTreshold: 60000, // default to syncing once per minute
  maxCachedHandles: 100  // default to keeping a maximum of 100 handles in-memory at a time
};

const ExtendedMindPublicItems = require('./extendedmind-data.js')

module.exports = (apiUrl, settings) => {

  let backendApi = apiUrl;
  let config = settings ? settings : {};
  config.apiUrl = apiUrl;

  for (var defaultSetting in defaultSettings){
    if (defaultSettings.hasOwnProperty(defaultSetting) && !config.hasOwnProperty(defaultSetting)){
      config[defaultSetting] = defaultSettings[defaultSetting];
    }
  }
  // Cache object for all public notes
  let handleCache = require("lru-cache")(
    { max: config.maxCachedHandles}
  );

  async function fetchPublicItems(handle){
    let publicOwnerUrl = backendApi + '/public/' + handle;
    let backendResponse = await request.get(publicOwnerUrl);
    if (backendResponse.status === 200){
      return new ExtendedMindPublicItems(backendResponse.body);
    }
  }

  async function refreshPublicItems(handle, cachedItems){
    if (Date.now() - cachedItems.getLastSynced() > config.syncTimeTreshold){
      let publicOwnerModifiedUrl = backendApi + '/public/' + handle +
                                  '?modified=' + cachedItems.getLatestModified();
      let backendResponse = await request.get(publicOwnerModifiedUrl);
      if (backendResponse.status === 200){
        cachedItems.updateItems(backendResponse.body);
      }
    }
    return cachedItems;
  }

  async function getPublicItems(handle){
    const cachedItems = handleCache.get(handle);
    const items = cachedItems ? await refreshPublicItems(handle, cachedItems) : await fetchPublicItems(handle);
    handleCache.set(handle, items);
    return items;
  }

  return {
    // Returns a list of public items for given handle
    getPublicItems: async function(handle) {
      return await getPublicItems(handle);
    }
  }
};


{'fraction_non_alphanumeric': 0.10562015503875968, 'fraction_numerical': 0.008236434108527133, 'mean_word_length': 3.7242562929061784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6094972', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1104, 'n_words': 289}