<commit_before>logger     = require './support/logger'
watcher    = require './watcher'
renderer   = require './renderer'
helpers    = require './support/helpers'
_          = require 'underscore'


class Project
  constructor: (@options={}) ->
    @path           = @options.path
    @name           = @options.name
    @scratchPath    = @options.scratchPath
    @editorManager  = @options.editorManager
    @browserManager = @options.browserManager

    if !@path || !@name || !@scratchPath || !@browserManager || !@editorManager
      throw('Project not instantiated with required options')

    @logger      = @options.logger || logger.silentLogger()
    @folder      = new watcher.Folder(
      path        : @path
      scratchPath : @scratchPath
      extensions  : ['scss', 'css']
    ) 

    @folder.on 'updated', @handleFolderUpdate.bind(@)

    @editorManager.on 'buffer:update', (data) =>
      return unless data.path.indexOf(@path) == 0
      
      @logger.debug 'processing buffer:update', data.path
      @folder.bufferUpdate(data.path, data.buffer)

    @editorManager.on 'buffer:reset', =>
      return unless data.path.indexOf(@path) == 0
      
      @logger.debug 'processing buffer:reset', data.path
      @folder.bufferClear(data.path)

    @browserManager.on 'stylesheet:resolve', (data, callback) =>
      return unless data.project_name == @name

      if match = helpers.pickBestFileForHref(data.href, _.keys(@folder.files))
        @logger.debug 'stylesheet', data.href, 'matched to', match
        callback(match) 
      else
        @logger.warn "coulnd't find a matching file for", data.href

    @browserManager.on 'stylesheet:listen', (data) =>
      return unless data.project_name == @name

      @logger.debug 'processing stylesheet:listen', data.id
      @handleFolderUpdate()

  handleFolderUpdate: ->
    @logger.error 'processing folder update event'

    watchedStylesheets = @browserManager.watchedStylesheetsForProject(@name)
    watchedStylesheets.forEach (path) =>
      file = @folder.getFile(path)
      if file
        @logger.debug 'rendering ', file.scratchPath
        renderer.for(file.scratchPath).render {file: file.scratchPath}, (error, body) =>
          if !error
            @browserManager.stylesheetRendered(@name, file.path, '/path/tp/some.css')
          else
            @logger.warn 'error rendering', file.scratchPath, ':', error
      else
        @logger.warn "couldn't find a file for watched stylesheet", path


  start: (callback) ->
    @logger.debug 'starting'
    @folder.start ->
      callback?()

  stop: ->
    @folder.stop()

module.exports = Project

<commit_msg>add body cache to project<commit_after>logger     = require './support/logger'
watcher    = require './watcher'
renderer   = require './renderer'
helpers    = require './support/helpers'
_          = require 'underscore'


class Project
  constructor: (@options={}) ->
    @path           = @options.path
    @name           = @options.name
    @scratchPath    = @options.scratchPath
    @editorManager  = @options.editorManager
    @browserManager = @options.browserManager

    if !@path || !@name || !@scratchPath || !@browserManager || !@editorManager
      throw('Project not instantiated with required options')

    @logger      = @options.logger || logger.silentLogger()
    @folder      = new watcher.Folder(
      path        : @path
      scratchPath : @scratchPath
      extensions  : ['scss', 'css']
    ) 

    @folder.on 'updated', @handleFolderUpdate.bind(@)

    @editorManager.on 'buffer:update', (data) =>
      return unless data.path.indexOf(@path) == 0
      
      @logger.debug 'processing buffer:update', data.path
      @folder.bufferUpdate(data.path, data.buffer)

    @editorManager.on 'buffer:reset', =>
      return unless data.path.indexOf(@path) == 0
      
      @logger.debug 'processing buffer:reset', data.path
      @folder.bufferClear(data.path)

    @browserManager.on 'stylesheet:resolve', (data, callback) =>
      return unless data.project_name == @name

      if match = helpers.pickBestFileForHref(data.href, _.keys(@folder.files))
        @logger.debug 'stylesheet', data.href, 'matched to', match
        callback(match) 
      else
        @logger.warn "coulnd't find a matching file for", data.href

    @browserManager.on 'stylesheet:listen', (data) =>
      return unless data.project_name == @name

      @logger.debug 'processing stylesheet:listen', data.id
      @handleFolderUpdate()

  handleFolderUpdate: ->
    @logger.error 'processing folder update event'
    @bodyCache = []

    watchedStylesheets = @browserManager.watchedStylesheetsForProject(@name)
    watchedStylesheets.forEach (path) =>
      file = @folder.getFile(path)
      if file
        @logger.debug 'rendering ', file.scratchPath
        renderer.for(file.scratchPath).render {file: file.scratchPath}, (error, body) =>
          if !error
            @bodyCache[file] = body
            @browserManager.stylesheetRendered(@name, file.path, '/path/tp/some.css')
          else
            @logger.warn 'error rendering', file.scratchPath, ':', error
      else
        @logger.warn "couldn't find a file for watched stylesheet", path

  getBodyForStylesheet: (id) ->
    @bodyCache[id]

  start: (callback) ->
    @logger.debug 'starting'
    @folder.start ->
      callback?()

  stop: ->
    @folder.stop()

module.exports = Project


{'fraction_non_alphanumeric': 0.12987974098057353, 'fraction_numerical': 0.0007400555041628122, 'mean_word_length': 2.8124118476727786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '66990', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1551, 'n_words': 356}