<commit_before>import * as cop  from "src/external/ContextJS/src/contextjs.js";

var resolveCounter = 0

window.dependendPromises = new Map()
window.dependendPromisesLogs = new Map()


window.currrentPromise = undefined
window.promiseRoots = []

function addDependend(oldPromise, newPromise) {
    if (!window.currrentPromise)  {
      window.promiseRoots.push(newPromise)
      return 
    }
    var dep = dependendPromises.get(oldPromise)
    if (!dep) {
      dep = []
      dependendPromises.set(oldPromise, dep)
    }
    dep.push(newPromise)  
}

function promisedLog(promise, args) {
    var logs = dependendPromisesLogs.get(promise)
    if (!logs) {
      logs = []
      dependendPromisesLogs.set(promise, logs)
    }
    logs.push(args)  
}

cop.layer(window, "LogPromisesLayer").refineClass(Promise, {
  then(onresolve, onerror) {
    // outside then
    this.__promise_debug_id__ =  resolveCounter++    
    
    addDependend(window.currrentPromise, this)    
    var oldPromise = this; 
    var newPromise = cop.proceed(
      onresolve ? (...args) => {
        // inside then
        cop.withoutLayers([LogPromisesLayer], () => {

          /// console.log("execute promise " + resolveCounter );
        })
        window.currrentPromise = oldPromise
        var result
        cop.withLayers([LogPromisesLayer], () => {
          result = onresolve(...args)        
        })
        window.currrentPromise = undefined
        return result
      } : undefined,
      onerror);
    addDependend(oldPromise, newPromise)
    
    return newPromise
  }
}).refineObject(console, {
  log(...args) {
    return cop.withoutLayers([LogPromisesLayer], () => {
      promisedLog(window.currrentPromise, args)
      return cop.proceed("PROMISED", ...args)
    })
  }
})<commit_msg>log promise experiments<commit_after>import * as cop  from "src/external/ContextJS/src/contextjs.js";

if (!window.OriginalPromise) {
  window.OriginalPromise = Promise;  
}
window.Promise = function Promise(...args) {
  if (this.constructorHook) {
    args = this.constructorHook(args)
  } 
  return new OriginalPromise(...args)
};
Promise.prototype = OriginalPromise.prototype;
Promise.prototype.constructorHook = function(args) {  return args }
Promise.__proto__ = OriginalPromise


export default class LogPromises {
  static resetLogs() {
    this.resolveCounter = 0
    this.dependendPromises = new Map()
    this.dependendPromisesLogs = new Map()
    this.currrentPromise = undefined
    this.promiseRoots = new Set()    
  }
  
  
  static addDependend(oldPromise, newPromise) {
    if (!LogPromises.currrentPromise)  {
      LogPromises.promiseRoots.add(newPromise)
      return 
    }
    var dep = this.dependendPromises.get(oldPromise)
    if (!dep) {
      dep = []
      this.dependendPromises.set(oldPromise, dep)
    }
    dep.push(newPromise)  
  }

  static promisedLog(promise, args) {
    var logs = this.dependendPromisesLogs.get(promise)
    if (!logs) {
      logs = []
      this.dependendPromisesLogs.set(promise, logs)
    }
    logs.push(args)  
  }
  
  static dependenciesTree(p) {
    return {
      name: "Promise " + p.__promise_debug_id__,
      logs: this.dependendPromisesLogs.get(p) ,
      children: (this.dependendPromises.get(p) || []).map(ea => this.dependenciesTree(ea)) 
    }  
  }

  static printPromisedLogs(p) {
    var logs = this.dependendPromisesLogs.get(p)
    if (logs) {
      cop.withoutLayers([LogPromisesLayer], () => {
        logs.forEach(ea => {
          console.log(...ea)          
        })
      });
      (this.dependendPromises.get(p) || []).forEach(ea => this.printPromisedLogs(ea)) 
    }
  } 
  
  static promisedAllDependendPromises(p, all=new Set()) {
    all.add(p)
    var deps = this.dependendPromises.get(p)
    if (deps) {
      for(let ea of deps) {
        if (!all.has(ea)) {
          this.promisedAllDependendPromises(ea, all)
        }
      }    
    }
    return all
  }
}

LogPromises.resetLogs()

cop.layer(window, "LogPromisesLayer").refineClass(Promise, {
  constructorHook(args) {
    var func = args[0]
    if (func) {
      args[0] = (...rest) => {
        LogPromises.addDependend(LogPromises.currrentPromise, this)    
        var oldPromise = this; 
        LogPromises.currrentPromise = oldPromise
        var result
        cop.withLayers([LogPromisesLayer], () => {
          result =  func(...rest)
        })
        LogPromises.currrentPromise = undefined
        return result
      }  
    }
    return cop.proceed(args)
  },
  
  then(onresolve, onerror) {
    // outside then
    this.__promise_debug_id__ =  LogPromises.resolveCounter++    
    
    LogPromises.addDependend(LogPromises.currrentPromise, this)    
    var oldPromise = this; 
    var newPromise = cop.proceed(
      onresolve ? (...args) => {
        // inside then
        cop.withoutLayers([LogPromisesLayer], () => {

          /// console.log("execute promise " + resolveCounter );
        })
        LogPromises.currrentPromise = oldPromise
        var result
        cop.withLayers([LogPromisesLayer], () => {
          result = onresolve(...args)        
        })
        LogPromises.currrentPromise = undefined
        return result
      } : undefined,
      onerror);
    LogPromises.addDependend(oldPromise, newPromise)
    
    return newPromise
  }
}).refineObject(console, {
  log(...args) {
    return cop.withoutLayers([LogPromisesLayer], () => {
      LogPromises.promisedLog(LogPromises.currrentPromise, args)
      return cop.proceed("PROMISED", ...args)
    })
  }
})
{'fraction_non_alphanumeric': 0.12369275153263613, 'fraction_numerical': 0.000721240533717995, 'mean_word_length': 2.551216389244558, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6061992', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1750, 'n_words': 293}