client_job_queue 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ in your views.
10
10
 
11
11
  ## In your application.js (or wherever)
12
12
 
13
- new EventQueue("my_queue", {
13
+ new JobQueue("my_queue", {
14
14
  "console:log": function(options){
15
15
  console.log(options);
16
16
  }
@@ -1,3 +1,3 @@
1
1
  module ClientJobQueue
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,79 @@
1
+ class window.JobDispatcher
2
+
3
+ _events: {}
4
+
5
+ constructor: (callbacks={}) ->
6
+ @bindMultiple(callbacks)
7
+
8
+ bind: (name, callback) =>
9
+ (@_events[name] ||= []).push(callback)
10
+ @
11
+
12
+ bindMultiple: (events = {}) =>
13
+ @bind(name, callback) for name, callback of events
14
+ @
15
+
16
+ isBound: (name, callback) =>
17
+ !!(@_events[name] && (callback in @_events[name]))
18
+
19
+ unbind: (name, callback) =>
20
+ if @_events[name]
21
+ @_events[name] = @_events[name].filter (otherCallback) -> callback == otherCallback
22
+ @
23
+
24
+ trigger: (name, options) =>
25
+ callback?(options) for callback in (@_events[name] || [])
26
+ @
27
+
28
+
29
+
30
+ class window.JobQueue
31
+
32
+ constructor: (@name, handlers) ->
33
+ @dispatcher = new JobDispatcher(handlers)
34
+ queuedJobs = JobCookie.getJSON(@name)
35
+ JobCookie.delete(@name)
36
+ @enqueue(queuedJobs)
37
+
38
+ enqueue: (items) =>
39
+ return unless items
40
+ @work(job, options) for [job, options] in items
41
+ @
42
+
43
+
44
+ work: (job, options) =>
45
+ @dispatcher.trigger("before", [job, options])
46
+ @dispatcher.trigger("#{ job }:before", options)
47
+ @dispatcher.trigger(job, options)
48
+ @dispatcher.trigger("#{ job }:after", options)
49
+ @dispatcher.trigger("after", [job, options])
50
+ @
51
+
52
+
53
+ class window.JobCookie
54
+ @set: (name, value, days) ->
55
+ if days
56
+ date = new Date
57
+ date.setTime(date.getTime() + (days*24*60*60*1000))
58
+ expires = "; expires=" + date.toGMTString()
59
+ else
60
+ expires: ""
61
+ document.cookie = name + "=" + value + expires + "; path=/"
62
+
63
+ @get: (key) ->
64
+ key = key + "="
65
+ for c in document.cookie.split(';')
66
+ while c.charAt(0) is ' '
67
+ c = c.substring(1, c.length)
68
+ if c.indexOf(key) == 0
69
+ return c.substring(key.length, c.length)
70
+ return null
71
+
72
+ @getJSON: (key) ->
73
+ eval(decodeURIComponent(@get(key)) || "[]")
74
+
75
+ @delete: (name) ->
76
+ @set(name, "", -1)
77
+
78
+
79
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client_job_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -164,6 +164,7 @@ extra_rdoc_files: []
164
164
  files:
165
165
  - lib/client_job_queue/version.rb
166
166
  - lib/client_job_queue.rb
167
+ - vendor/assets/javascripts/client_job_queue.js.coffee
167
168
  - MIT-LICENSE
168
169
  - Rakefile
169
170
  - README.md