Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added coffeelint rules and refactored code to them. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions coffeelint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"arrow_spacing": {
"name": "arrow_spacing",
"level": "warn"
},
"braces_spacing": {
"name": "braces_spacing",
"level": "warn",
"empty_object_spaces": 0,
"spaces": 0
},
"camel_case_classes": {
"name": "camel_case_classes",
"level": "error"
},
"coffeescript_error": {
"level": "error"
},
"colon_assignment_spacing": {
"name": "colon_assignment_spacing",
"level": "warn",
"spacing": {
"left": 0,
"right": 1
}
},
"cyclomatic_complexity": {
"name": "cyclomatic_complexity",
"level": "ignore",
"value": 10
},
"duplicate_key": {
"name": "duplicate_key",
"level": "error"
},
"empty_constructor_needs_parens": {
"name": "empty_constructor_needs_parens",
"level": "warn"
},
"ensure_comprehensions": {
"name": "ensure_comprehensions",
"level": "warn"
},
"eol_last": {
"name": "eol_last",
"level": "warn"
},
"indentation": {
"name": "indentation",
"level": "error",
"value": 2
},
"line_endings": {
"name": "line_endings",
"level": "warn",
"value": "unix"
},
"max_line_length": {
"name": "max_line_length",
"value": 120,
"level": "warn",
"limitComments": false
},
"missing_fat_arrows": {
"name": "missing_fat_arrows",
"level": "ignore"
},
"newlines_after_classes": {
"name": "newlines_after_classes",
"level": "warn",
"value": 2
},
"no_backticks": {
"name": "no_backticks",
"level": "error"
},
"no_debugger": {
"name": "no_debugger",
"level": "error"
},
"no_empty_functions": {
"name": "no_empty_functions",
"level": "warn"
},
"no_empty_param_list": {
"name": "no_empty_param_list",
"level": "warn"
},
"no_implicit_braces": {
"name": "no_implicit_braces",
"level": "ignore",
"strict": true
},
"no_implicit_parens": {
"name": "no_implicit_parens",
"level": "ignore"
},
"no_interpolation_in_single_quotes": {
"name": "no_interpolation_in_single_quotes",
"level": "error"
},
"no_plusplus": {
"name": "no_plusplus",
"level": "ignore"
},
"no_stand_alone_at": {
"name": "no_stand_alone_at",
"level": "warn"
},
"no_tabs": {
"name": "no_tabs",
"level": "error"
},
"no_this": {
"name": "no_this",
"level": "warn"
},
"no_throwing_strings": {
"name": "no_throwing_strings",
"level": "error"
},
"no_trailing_semicolons": {
"name": "no_trailing_semicolons",
"level": "error"
},
"no_trailing_whitespace": {
"name": "no_trailing_whitespace",
"level": "warn",
"allowed_in_comments": false,
"allowed_in_empty_lines": true
},
"no_unnecessary_double_quotes": {
"name": "no_unnecessary_double_quotes",
"level": "warn"
},
"no_unnecessary_fat_arrows": {
"name": "no_unnecessary_fat_arrows",
"level": "warn"
},
"non_empty_constructor_needs_parens": {
"name": "non_empty_constructor_needs_parens",
"level": "ignore"
},
"prefer_english_operator": {
"name": "prefer_english_operator",
"level": "warn"
},
"space_operators": {
"name": "space_operators",
"level": "warn"
},
"spacing_after_comma": {
"name": "spacing_after_comma",
"level": "warn"
},
"transform_messes_up_line_numbers": {
"name": "transform_messes_up_line_numbers",
"level": "ignore"
}
}
2 changes: 1 addition & 1 deletion gulpfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ gulp.task 'browserify', ->
extensions: ['.coffee']
debug: true
.transform 'coffeeify'
.bundle (error)->
.bundle (error) ->
if error
console.error error
b.end()
Expand Down
31 changes: 16 additions & 15 deletions lib/connection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Connection extends EventEmitter
allowReconnect: true
reconnectionInterval: 500

constructor: (config = {})->
constructor: (config = {}) ->
config = _.defaults config, defaultConfig
@host = config.host
@port = config.port
Expand All @@ -19,36 +19,37 @@ class Connection extends EventEmitter

connect: ->
unless @socket
connection = @
connection = this
@socket = new WebSocket "ws://#{@host}:#{@port}"
@socket.onopen = ()->
@socket.onopen = ->
console.info 'Socket open'
# nop
@socket.onclose = (data)->
@socket.onclose = (data) ->
handleClose.call connection, data.code, data.reason
@socket.onmessage = (message)->
if message.data == 'OK'
@socket.onmessage = (message) ->
if message.data is 'OK'
handleOpen.call connection
else
handleResponse.call connection, message.data
@socket.onerror = (error)->
@socket.onerror = (error) ->
console.log 'onerror', error
@
this

handleOpen = ->
@stopReconnection()
@connected = true
@emit 'connect'

handleClose = (code, reason)->
handleClose = (code, reason) ->
delete @socket
if @connected
@connected = false
@emit 'disconnect', code, reason
if code != WS_CLOSE_NORMAL && @allowReconnect
if code isnt WS_CLOSE_NORMAL and @allowReconnect
@reconnect @reconnectionInterval

handleResponse = (data)->
for json in data.split "\n"
handleResponse = (data) ->
for json in data.split '\n'
if json.length > 0
response = JSON.parse json
@emit response.category, response
Expand All @@ -57,16 +58,16 @@ class Connection extends EventEmitter
@stopReconnection()
@socket?.close WS_CLOSE_NORMAL

reconnect: (intervalMillis = @reconnectionInterval)->
connection = @
reconnect: (intervalMillis = @reconnectionInterval) ->
connection = this
@reconnecting = setTimeout ->
connection.connect()
, intervalMillis

stopReconnection: ->
@reconnecting = clearTimeout @reconnecting

send: (request)->
send: (request) ->
if @socket
if request instanceof Object
request = JSON.stringify request
Expand Down
5 changes: 3 additions & 2 deletions lib/gazedata.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Frame
@STATE_TRACKING_FAIL = 1 << 3
@STATE_TRACKING_LOST = 1 << 4

constructor: (@data)->
constructor: (@data) ->
@timestampString = data.timestamp
@timestamp = data.time
@state = data.state
Expand Down Expand Up @@ -40,7 +40,7 @@ class Eye
@LEFT = 0
@RIGHT = 1

constructor: (@type, @data)->
constructor: (@type, @data) ->
@raw = @rawCoordinates = new Point2D(data.raw)
@average = @smoothedCoordinates = new Point2D(data.avg)
@pupilCenter = @pupilCenterCoordinates = new Point2D(data.pcenter)
Expand All @@ -58,6 +58,7 @@ class Eye
' | pupilCenter:' + @pupilCenter.toString() +
' ]'


class GazeData extends Frame
@Eye = Eye

Expand Down
16 changes: 8 additions & 8 deletions lib/gazeutils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Point2D = require './point2d'

class GazeUtils
# Find average pupil center of two eyes.
@getEyesCenterNormalized: (leftEye, rightEye)->
@getEyesCenterNormalized: (leftEye, rightEye) ->
if leftEye instanceof GazeData
{leftEye, rightEye} = leftEye

if leftEye? && rightEye?
if leftEye? and rightEye?
leftEye.pupilCenter.add rightEye.pupilCenter
.divide 2
else if leftEye?
Expand All @@ -17,7 +17,7 @@ class GazeUtils
rightEye.pupilCenter

# Find average pupil center of two eyes.
@getEyesCenterPixels: (leftEye, rightEye, screenWidth, screenHeight)->
@getEyesCenterPixels: (leftEye, rightEye, screenWidth, screenHeight) ->
if leftEye instanceof GazeData
[leftEye, screenWidth, screenHeight] = arguments
center = @getEyesCenterNormalized leftEye, rightEye
Expand All @@ -29,7 +29,7 @@ class GazeUtils

# Calculates distance between pupil centers based on previously
# recorded min and max values.
@getEyesDistanceNormalized: (leftEye, rightEye)->
@getEyesDistanceNormalized: (leftEye, rightEye) ->
if leftEye instanceof GazeData
{leftEye, rightEye} = leftEye

Expand All @@ -43,21 +43,21 @@ class GazeUtils
dist / (maxinumEyesDistance - minimamEyesDistance)

# Calculates distance between two points.
@getDistancePoint2D: (p1, p2)->
@getDistancePoint2D: (p1, p2) ->
Math.sqrt Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)

# Converts a relative point to screen point in pixels.
@getRelativeToScreenSpace: (point, screenWidth, screenHeight)->
@getRelativeToScreenSpace: (point, screenWidth, screenHeight) ->
if point?
new Point2D Math.round(point.x * screenWidth), Math.round(point.y * screenHeight)

# Normalizes a point on screen in screen dims
@getNormalizedCoords: (point, screenWidth, screenHeight)->
@getNormalizedCoords: (point, screenWidth, screenHeight) ->
if point?
new Point2D point.x / screenWidth, point.y / screenHeight

# Maps eye position of gaze coords in pixels within normalized space [x: -1:1 , y: -1:1]
@getNormalizedMapping: (point, screenWidth, screenHeight)->
@getNormalizedMapping: (point, screenWidth, screenHeight) ->
point = @getNormalizedCoords point, screenHeight, screenHeight
if point?
new Point2D point.x * 2 - 1, point.y * 2 - 1
Expand Down
17 changes: 9 additions & 8 deletions lib/heartbeat.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Heartbeat
constructor: (@tracker)->
heartbeat = @
tracker.on 'heartbeatinterval', (intervalMillis)->
constructor: (@tracker) ->
heartbeat = this
tracker.on 'heartbeatinterval', (intervalMillis) ->
heartbeat.restart intervalMillis

start: (@intervalMillis)->
heartbeat = @
start: (@intervalMillis) ->
heartbeat = this
connection = @tracker.connection

if intervalMillis?
Expand All @@ -17,14 +17,15 @@ class Heartbeat
heartbeat.stop()
else
@tracker.get 'heartbeatinterval'
@
this

restart: (intervalMillis)->
if @intervalMillis != intervalMillis
restart: (intervalMillis) ->
if @intervalMillis isnt intervalMillis
@stop()
@start intervalMillis

stop: ->
@intervalId = clearInterval @intervalId


module.exports = Heartbeat
4 changes: 2 additions & 2 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class EyeTribe
@Point2D = require('./point2d')
@_ = require('underscore')

@loop = (config, callback)->
if typeof config == 'function'
@loop = (config, callback) ->
if typeof config is 'function'
[callback, config] = [config, {}]
@loopTracker = new @Tracker config
.loop callback
Expand Down
Loading