<commit_before>_ = require "underscore"
HasParent = require "../../common/has_parent"
PlotWidget = require "../../common/plot_widget"
properties = require "../../common/properties"
ImagePool = require "./image_pool"
{logger} = require "../../common/logging"

class DynamicImageView extends PlotWidget

  get_extent: () ->
    return [@x_range.get('start'), @y_range.get('start'), @x_range.get('end'), @y_range.get('end')]

  _set_data: () ->
    @map_plot = @plot_view.model
    @map_canvas = @plot_view.canvas_view.ctx
    @map_frame = @plot_view.frame
    @x_range = @map_plot.get('x_range')
    @x_mapper = this.map_frame.get('x_mappers')['default']
    @y_range = @map_plot.get('y_range')
    @y_mapper = this.map_frame.get('y_mappers')['default']
    @lastImage = undefined
    @extent = @get_extent()

  _map_data: () ->
    @initial_extent = @get_extent()

  _on_tile_load: (e) =>
    image_data = e.target.image_data
    image_data.img = e.target
    @mget('image_source').add_image(image_data)
    @lastImage = image_data

    if @get_extent().join(':') == image_data.cache_key
      @request_render()

  _on_tile_error: (e) =>
    return ''

  _create_image: (bounds) ->
    image = new Image()
    image.onload = @_on_tile_load
    image.onerror = @_on_tile_error
    image.alt = ''
    image.image_data =
      bounds : bounds
      cache_key : bounds.join(':')

    image.src = @mget('image_source').get_image_url(bounds[0], bounds[1], bounds[2], bounds[3], Math.ceil(@map_frame.get('height')), Math.ceil(@map_frame.get('width')))
    return image

  render: (ctx, indices, args) ->

    if not @map_initialized?
      @_set_data()
      @_map_data()
      @map_initialized = true

    extent = @get_extent()

    if @render_timer?
      clearTimeout(@render_timer)

    image_obj = @mget('image_source').images[extent.join(':')]
    if image_obj?
      @_draw_image(extent.join(':'))
    else
      if @lastImage?
        @_draw_image(@lastImage.cache_key)
      @render_timer = setTimeout((=> @_create_image(extent)), 65)

  _draw_image: (image_key) ->
    image_obj = @mget('image_source').images[image_key]
    if image_obj?
      @map_canvas.save()
      @_set_rect()
      @map_canvas.globalAlpha = @mget('alpha')<commit_msg>added handling for re-rendering during in-flight requests<commit_after>_ = require "underscore"
HasParent = require "../../common/has_parent"
PlotWidget = require "../../common/plot_widget"
properties = require "../../common/properties"
ImagePool = require "./image_pool"
{logger} = require "../../common/logging"

class DynamicImageView extends PlotWidget

  get_extent: () ->
    return [@x_range.get('start'), @y_range.get('start'), @x_range.get('end'), @y_range.get('end')]

  _set_data: () ->
    @map_plot = @plot_view.model
    @map_canvas = @plot_view.canvas_view.ctx
    @map_frame = @plot_view.frame
    @x_range = @map_plot.get('x_range')
    @x_mapper = this.map_frame.get('x_mappers')['default']
    @y_range = @map_plot.get('y_range')
    @y_mapper = this.map_frame.get('y_mappers')['default']
    @lastImage = undefined
    @extent = @get_extent()

  _map_data: () ->
    @initial_extent = @get_extent()

  _on_tile_load: (e) =>
    image_data = e.target.image_data
    image_data.img = e.target
    image_data.loaded = true
    @mget('image_source').add_image(image_data)
    @lastImage = image_data

    if @get_extent().join(':') == image_data.cache_key
      @request_render()

  _on_tile_error: (e) =>
    @mget('image_source').remove_image(image_data)

  _create_image: (bounds) ->
    image = new Image()
    image.onload = @_on_tile_load
    image.onerror = @_on_tile_error
    image.alt = ''
    image.image_data =
      bounds : bounds
      loaded : false
      cache_key : bounds.join(':')

    @mget('image_source').add_image(image_data)
    image.src = @mget('image_source').get_image_url(bounds[0], bounds[1], bounds[2], bounds[3], Math.ceil(@map_frame.get('height')), Math.ceil(@map_frame.get('width')))
    return image

  render: (ctx, indices, args) ->

    if not @map_initialized?
      @_set_data()
      @_map_data()
      @map_initialized = true

    extent = @get_extent()

    if @render_timer?
      clearTimeout(@render_timer)

    image_obj = @mget('image_source').images[extent.join(':')]
    if image_obj? and image_obj.image_data.loaded
      @_draw_image(extent.join(':'))
      return

    if @lastImage?
      @_draw_image(@lastImage.cache_key)

    else
      if @lastImage?
        @_draw_image(@lastImage.cache_key)
      @render_timer = setTimeout((=> @_create_image(extent)), 65)

  _draw_image: (image_key) ->
    image_obj = @mget('image_source').images[image_key]
    if image_obj?
      @map_canvas.save()
      @_set_rect()
      @map_canvas.globalAlpha = @mget('alpha')
{'fraction_non_alphanumeric': 0.16254713028906578, 'fraction_numerical': 0.0025136154168412233, 'mean_word_length': 3.551954242135367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3314282', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1696, 'n_words': 274}