<commit_before>noflo = require 'noflo'

class Draw extends noflo.Component
  description: 'Draws received drawing commands'
  icon: 'pencil'
  constructor: ->
    @every = false
    @clearevery = false
    @canvas = null
    @context = null
    @commands = []
    
    @inPorts =
      tick: new noflo.Port 'bang'
      every: new noflo.Port 'boolean'
      clearevery: new noflo.Port 'boolean'
      canvas: new noflo.Port 'object'
      commands: new noflo.ArrayPort 'object'

    @outPorts =
      canvas: new noflo.Port 'object'

    @inPorts.tick.on 'data', (tick) =>
      if @context
        @parse @commands
        return
      @sendLog
        loglevel: 'error'
        message: 'Received commands but there is not 2d context attached.'

    @inPorts.every.on 'data', (data) =>
      @every = data

    @inPorts.clearevery.on 'data', (data) =>
      @clearevery = data

    @inPorts.canvas.on 'data', (canvas) =>
      @canvas = canvas
      @context = canvas.getContext('2d')
      
    @inPorts.commands.on 'data', (commands, i) =>
      @commands[i] = commands
      if @every
        @parse @commands


    # @inPorts.commands.on 'detach', (i) =>
    #   @commands.splice i, 1
    #   if @every
    #     @parse @commands


  parse: (commands) =>
    return unless @context
    if @clearevery
      @context.clearRect 0, 0, @canvas.width, @canvas.height
    for thing in commands
      continue unless thing? and thing.type? and @[thing.type]?
      @[thing.type].call @, thing
    if @outPorts.canvas.isAttached()
      @outPorts.fill.send @canvas

  clear: (clear) =>
    @context.clearRect.apply @context, clear.rectangle

  strokeStyle: (data) =>
    @context.strokeStyle = data.value

  lineWidth: (data) =>
    @context.lineWidth = data.value

  fillStyle: (data) =>
    @context.fillStyle = data.value<commit_msg>rename every input to drawevery<commit_after>noflo = require 'noflo'

class Draw extends noflo.Component
  description: 'Draws received drawing commands'
  icon: 'pencil'
  constructor: ->
    @drawevery = false
    @clearevery = false
    @canvas = null
    @context = null
    @commands = []
    
    @inPorts =
      tick: new noflo.Port 'bang'
      drawevery: new noflo.Port 'boolean'
      clearevery: new noflo.Port 'boolean'
      canvas: new noflo.Port 'object'
      commands: new noflo.ArrayPort 'object'

    @outPorts =
      canvas: new noflo.Port 'object'

    @inPorts.tick.on 'data', (tick) =>
      if @context
        @parse @commands
        return
      @sendLog
        loglevel: 'error'
        message: 'Received commands but there is not 2d context attached.'

    @inPorts.drawevery.on 'data', (data) =>
      @drawevery = data

    @inPorts.clearevery.on 'data', (data) =>
      @clearevery = data

    @inPorts.canvas.on 'data', (canvas) =>
      @canvas = canvas
      @context = canvas.getContext('2d')
      
    @inPorts.commands.on 'data', (commands, i) =>
      @commands[i] = commands
      if @drawevery
        @parse @commands


    # @inPorts.commands.on 'detach', (i) =>
    #   @commands.splice i, 1
    #   if @drawevery
    #     @parse @commands


  parse: (commands) =>
    return unless @context
    if @clearevery
      @context.clearRect 0, 0, @canvas.width, @canvas.height
    for thing in commands
      continue unless thing? and thing.type? and @[thing.type]?
      @[thing.type].call @, thing
    if @outPorts.canvas.isAttached()
      @outPorts.fill.send @canvas

  clear: (clear) =>
    @context.clearRect.apply @context, clear.rectangle

  strokeStyle: (data) =>
    @context.strokeStyle = data.value

  lineWidth: (data) =>
    @context.lineWidth = data.value

  fillStyle: (data) =>
    @context.fillStyle = data.value
{'fraction_non_alphanumeric': 0.12722371967654986, 'fraction_numerical': 0.0026954177897574125, 'mean_word_length': 2.5242165242165244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '4973507', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1211, 'n_words': 311}