utils.coffee | |
---|---|
Template | |
Private classes | |
| class Class |
Override the default | toString: -> @constructor.name |
Private variables | |
Mapping of all timers currently being managed. | timings = {} |
Utilities setup | utils = window.utils = new class Utils extends Class |
Public functions | |
Call a function asynchronously with the arguments provided and then pass
the returned value to | async: (fn, args..., callback) ->
if callback? and typeof callback isnt 'function'
args.push callback
callback = null
setTimeout ->
result = fn args...
callback? result
, 0 |
Generate a unique key based on the current time and using a randomly generated hexadecimal number of the specified length. | keyGen: (separator = '.', length = 5, prefix = '', upperCase = yes) ->
parts = [] |
Populate the segment(s) to attempt uniquity. | parts.push new Date().getTime()
if length > 0
min = @repeat '1', '0', if length is 1 then 1 else length - 1
max = @repeat 'f', 'f', if length is 1 then 1 else length - 1
min = parseInt min, 16
max = parseInt max, 16
parts.push @random min, max |
Convert segments to their hexadecimal (base 16) forms. | parts[i] = part.toString 16 for part, i in parts |
Join all segments using | key = prefix + parts.join separator
if upperCase then key.toUpperCase() else key.toLowerCase() |
Retrieve the first entity/all entities that pass the specified | query: (entities, singular, filter) ->
if singular
return entity for entity in entities when filter entity
else
entity for entity in entities when filter entity |
Generate a random number between the | random: (min, max) -> Math.floor(Math.random() * (max - min + 1)) + min |
Bind | ready: (context, handler) ->
unless handler?
handler = context
context = window
if context.jQuery?
context.jQuery handler
else
context.document.addEventListener 'DOMContentLoaded', handler |
Repeat the string provided the specified number of times. | repeat: (str = '', repeatStr = str, count = 1) ->
if count isnt 0 |
Repeat to the right if | str += repeatStr for i in [1..count] if count > 0 |
Repeat to the left if | str = repeatStr + str for i in [1..count*-1] if count < 0
str |
Start a new timer for the specified | time: (key) ->
if timings.hasOwnProperty key
new Date().getTime() - timings[key]
else
timings[key] = new Date().getTime() |
End the timer for the specified | timeEnd: (key) ->
if timings.hasOwnProperty key
start = timings[key]
delete timings[key]
new Date().getTime() - start
else
0 |
Convenient shorthand for | url: -> chrome.extension.getURL arguments... |
Public classes | |
Objects within the extension should extend this class wherever possible. | utils.Class = Class |
| class utils.Runner extends utils.Class |
Create a new instance of | constructor: -> @queue = [] |
Finalize the process by resetting this | finish: (args...) ->
@queue = []
@started = no
@onfinish? args... |
Remove the next task from the queue and call it. | next: (args...) ->
if @started
if @queue.length
ctx = fn = null
task = @queue.shift() |
Determine what context the function should be executed in. | switch typeof task.reference
when 'function' then fn = task.reference
when 'string'
ctx = task.context
fn = ctx[task.reference] |
Unpack the arguments where required. | if typeof task.args is 'function'
task.args = task.args.apply null
fn?.apply ctx, task.args
return yes
else
@finish args...
no |
Add a new task to the queue using the values provided. | push: (context, reference, args...) -> @queue.push
args: args
context: context
reference: reference |
Add a new task to the queue using the packed values provided. | pushPacked: (context, reference, packedArgs) -> @queue.push
args: packedArgs
context: context
reference: reference |
Start the process by calling the first task in the queue and register the
| run: (@onfinish) ->
@started = yes
@next() |
Remove the specified number of tasks from the front of the queue. | skip: (count = 1) -> @queue.splice 0, count
|