architect 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a8eaf51e9027f92886fc6f7a80a32962314d6b3
4
- data.tar.gz: c9e08da85647823e5191dcbf95d0efa5e82eef14
3
+ metadata.gz: daa8aa8ac05c622ddde6c3d2ede4dec0600a655c
4
+ data.tar.gz: aad21f214942aac852900fef7bf51ccbd740652a
5
5
  SHA512:
6
- metadata.gz: 5e01127c50ab864af2b781d6ee9a8d54fb2dbdc639a9857a8ef711ce06e6d86883e51a42e6c1b2f17e665941a819743c991ee3d6a3fcb910d6426b2a62ec1dd9
7
- data.tar.gz: 03257ade602761acedb3c484973aba2a436daeeefe761f65bfe60a9f041557d969690338947f451ac57c2c3e594b1f01045ec189e8edb9bb839419b1c4937049
6
+ metadata.gz: c77723dd253c39a2249168eff0a57a0672ff08c9edf50f319d89f1b8e2478fd64f4b55c99570c2ee1347c76f94f9a4829d95648c62226ad32fbe5f41b4c1c662
7
+ data.tar.gz: 367cb3d57759dd32c63f98960e1935abb6e0981c98545689de99bb4748d6b517220e1cfee2e68af8bd07de616931d076d48e6fe55b286751406fab402ce72222
data/README.md CHANGED
@@ -1,12 +1,22 @@
1
- **Architect** is a JavaScript library built on top of [Web Workers][].<br>
2
- He will manage them workers so you don’t have to.
1
+ <p align="center">
2
+ <a href="https://github.com/EtienneLem/architect">
3
+ <img src="https://f.cloud.github.com/assets/436043/856991/59ff07ce-f547-11e2-9a89-74501d0878c3.png">
4
+ </a>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <strong>Architect</strong> is a JavaScript library built on top of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html">Web Workers</a>.<br>
9
+ He will manage and polyfill them workers so you don’t have to.
10
+ </p>
11
+
12
+ ## Short-lived workers
13
+ These will be automatically terminated as soon as the work is done. It will spawn a new worker every time.
3
14
 
4
- ## Methods
5
15
  ### proxy
6
16
  Returns anything it receives in a background process. Useful when dealing with heavy DOM manipulation (i.e. Infinite scroll). It greatly improves initial page load speed, especially on mobiles.
7
17
 
8
18
  ```js
9
- images = ['foo.png', 'bar.png', 'twiz.png', 'foozle.png', 'barzle.png', 'twizle.png']
19
+ var images = ['foo.png', 'bar.png', 'twiz.png', 'foozle.png', 'barzle.png', 'twizle.png']
10
20
  Architect.proxy(images, function(data) {
11
21
  console.log(data)
12
22
  // => ['foo.png', 'bar.png', 'twiz.png', 'foozle.png', 'barzle.png', 'twizle.png']
@@ -20,6 +30,8 @@ Architect.proxy(images, function(data) {
20
30
  })
21
31
  ```
22
32
 
33
+ Alias for `Architect.work(data, 'proxy', callback)`.
34
+
23
35
  ### ajax
24
36
  Makes an Ajax request in a background process.
25
37
 
@@ -30,8 +42,10 @@ Architect.ajax('/users/1', function(data) {
30
42
  })
31
43
  ```
32
44
 
45
+ Alias for `Architect.work(url, 'ajax', callback)`.
46
+
33
47
  ### jsonp
34
- Makes a JSONP request in a background process. **Do not add `?callback=foo` to your URL**, Architect will handle JSONP callbacks himself.
48
+ Makes a JSONP request in a background process. **Do not add `?callback=foo` to your URL**, Architect will handle JSONP callbacks himself. See the [request Architect makes](https://api.github.com/users/etiennelem?callback=architect_jsonp).
35
49
 
36
50
  ```js
37
51
  Architect.jsonp('https://api.github.com/users/etiennelem', function(data) {
@@ -40,7 +54,72 @@ Architect.jsonp('https://api.github.com/users/etiennelem', function(data) {
40
54
  })
41
55
  ```
42
56
 
43
- See the [request Architect makes](https://api.github.com/users/etiennelem?callback=architect_jsonp).
57
+ Alias for `Architect.work(url, 'jsonp', callback)`.
58
+
59
+ ## Long-lived workers
60
+ These need to be manually terminated when the work is done. The same worker can therefore be reused many times with different data.
61
+
62
+ ### proxyOn
63
+ ```js
64
+ var images, jobName, imagesCount
65
+
66
+ images = ['foo.png', 'bar.png', 'twiz.png', 'foozle.png', 'barzle.png', 'twizle.png']
67
+ jobName = 'appendImages'
68
+ imagesCount = 0
69
+
70
+ images.forEach(function(image) {
71
+ Architect.proxyOn(jobName, image, function(data) {
72
+ imagesCount++
73
+
74
+ img = document.createElement('img')
75
+ img.src = data
76
+ document.body.appendChild(img)
77
+
78
+ if (imagesCount == images.length) { Architect.endJob(jobName) }
79
+ })
80
+ })
81
+ ```
82
+
83
+ Alias for `Architect.workOn(jobName, data, 'proxy', callback)`.
84
+
85
+ ### ajaxOn
86
+ ```js
87
+ var totalPages, jobName, queryApi
88
+
89
+ totalPages = 10
90
+ jobName = 'getUsers'
91
+
92
+ queryApi = function(page) {
93
+ Architect.ajaxOn(jobName, '/users?page=' + page, function(data) {
94
+ // [Add DOM elements, do your thing ;)]
95
+
96
+ if (page == totalPages) {
97
+ // Manually terminate the 'getUsers' ajax worker
98
+ Architect.endJob(jobName)
99
+ console.log('Done')
100
+ } else {
101
+ // Reuse the same worker
102
+ queryApi(page + 1)
103
+ }
104
+ })
105
+ }
106
+
107
+ queryApi(1)
108
+ ```
109
+
110
+ Alias for `Architect.workOn(jobName, url, 'ajax', callback)`.
111
+
112
+ ### jsonpOn
113
+ ```js
114
+ Architect.jsonpOn('profile', 'https://api.github.com/users/etiennelem', function(data) {
115
+ console.log(data);
116
+ // => { meta: { status: 200, … }, data: { login: 'EtienneLem', company: 'Heliom', … } }
117
+
118
+ Architect.endJob('profile')
119
+ })
120
+ ```
121
+
122
+ Alias for `Architect.workOn(jobName, url, 'jsonp', callback)`.
44
123
 
45
124
  ## Setup
46
125
  ### Rails
@@ -49,11 +128,8 @@ See the [request Architect makes](https://api.github.com/users/etiennelem?callba
49
128
  3. Restart your server and you'll have access to your very own Architect!
50
129
 
51
130
  ### Other
52
- You’ll need to serve the [worker files](/static/workers) at `/architect` (i.e. `http://foo.com/architect/proxy_worker.min.js`) and manually add [architect.min.js](/static/architect.min.js) to your HTML page.
131
+ You’ll need to serve the [worker files](/static/workers) at `/architect` (i.e. `http://foo.com/architect/proxy_worker.min.js`) and manually include [architect.min.js](/static/architect.min.js) to your HTML pages.
53
132
 
54
133
  ## Todo
55
- - A way to reuse the same worker (and stop it)
56
- - Support [Shared workers][]
57
-
58
- [Web Workers]: http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html
59
- [Shared workers]: http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html#shared-workers
134
+ - Tests
135
+ - Support Shared Workers [[See #3](https://github.com/EtienneLem/architect/issues/3)]
@@ -1,5 +1,2 @@
1
- Architect = {}
2
- Architect.VERSION = '<%= Architect::VERSION %>'
3
-
4
- # Global scope
5
- window.Architect = Architect
1
+ @Architect = {}
2
+ @Architect.VERSION = '<%= Architect::VERSION %>'
@@ -1,7 +1,7 @@
1
1
  #= require_self
2
2
  #= require_tree ./workers
3
3
 
4
- class Architect.Worker
4
+ class @Architect.Worker
5
5
 
6
6
  constructor: ->
7
7
  @callbacks = {}
@@ -24,6 +24,3 @@ class Architect.Worker
24
24
  this.dispatch('message', data)
25
25
 
26
26
  terminate: -> # This is meant to be empty
27
-
28
- # Global scope
29
- window.Architect.Worker = Architect.Worker
@@ -1,4 +1,4 @@
1
- class Architect.AjaxWorker extends Architect.Worker
1
+ class @Architect.AjaxWorker extends @Architect.Worker
2
2
 
3
3
  postMessage: (url) ->
4
4
  xhr = new XMLHttpRequest
@@ -9,6 +9,3 @@ class Architect.AjaxWorker extends Architect.Worker
9
9
  this.handleRequest(xhr.responseText)
10
10
 
11
11
  xhr.send()
12
-
13
- # Global scope
14
- window.Architect.AjaxWorker = Architect.AjaxWorker
@@ -1,4 +1,4 @@
1
- class Architect.JSONPWorker extends Architect.Worker
1
+ class @Architect.JSONPWorker extends @Architect.Worker
2
2
 
3
3
  constructor: ->
4
4
  super()
@@ -19,6 +19,3 @@ class Architect.JSONPWorker extends Architect.Worker
19
19
 
20
20
  appendQuery: (url, query) ->
21
21
  (url + '&' + query).replace(/[&?]{1,2}/, '?')
22
-
23
- # Global scope
24
- window.Architect.JSONPWorker = Architect.JSONPWorker
@@ -1,7 +1,4 @@
1
- class Architect.ProxyWorker extends Architect.Worker
1
+ class @Architect.ProxyWorker extends @Architect.Worker
2
2
 
3
3
  postMessage: (data) ->
4
4
  this.handleRequest(data)
5
-
6
- # Global scope
7
- window.Architect.ProxyWorker = Architect.ProxyWorker
@@ -1,37 +1,48 @@
1
1
  #= require ./architect/version
2
2
  #= require ./architect/worker
3
3
 
4
- class _Architect
5
-
6
- constructor: ->
7
- @VERSION = Architect.VERSION
8
-
9
- @SUPPORT_WORKER = !!window.Worker
10
- @WORKERS =
11
- proxy: { class: Architect.ProxyWorker, workerPath: '<%= Architect.worker_path("proxy") %>' }
12
- ajax: { class: Architect.AjaxWorker, workerPath: '<%= Architect.worker_path("ajax") %>' }
13
- jsonp: { class: Architect.JSONPWorker, workerPath: '<%= Architect.worker_path("jsonp") %>' }
14
-
15
- proxy: (data, callback) -> this.work(data, 'proxy', callback)
16
- ajax: (url, callback) -> this.work(url, 'ajax', callback)
17
- jsonp: (url, callback) -> this.work(url, 'jsonp', callback)
18
-
19
- work: (data, type, callback) ->
20
- if typeof type is 'function'
21
- callback = type
22
- type = undefined
23
-
24
- type = 'proxy' if type is undefined
25
-
26
- worker = this.spawnWorker(type)
27
- worker.postMessage(data)
28
- worker.addEventListener 'message', (e) =>
29
- worker.terminate()
30
- callback(e.data)
31
-
32
- spawnWorker: (type) ->
33
- return new @WORKERS[type].class unless @SUPPORT_WORKER
34
- worker = new Worker(@WORKERS[type].workerPath)
35
-
36
- # Singleton
37
- window.Architect = new _Architect
4
+ # Constants
5
+ @Architect.SUPPORT_WORKER = !!window.Worker
6
+ @Architect.WORKERS =
7
+ proxy: { polyfill: @Architect.ProxyWorker, workerPath: '<%= Architect.worker_path("proxy") %>' }
8
+ ajax: { polyfill: @Architect.AjaxWorker, workerPath: '<%= Architect.worker_path("ajax") %>' }
9
+ jsonp: { polyfill: @Architect.JSONPWorker, workerPath: '<%= Architect.worker_path("jsonp") %>' }
10
+
11
+ # Private methods
12
+ spawnWorker = (type) ->
13
+ return new @Architect.WORKERS[type].polyfill unless @Architect.SUPPORT_WORKER
14
+ new Worker(@Architect.WORKERS[type].workerPath)
15
+
16
+ # Short-lived workers
17
+ @Architect.work = (data, type, callback) ->
18
+ worker = spawnWorker(type)
19
+ worker.postMessage(data)
20
+ worker.addEventListener 'message', (e) ->
21
+ worker.terminate()
22
+ callback(e.data)
23
+
24
+ @Architect.proxy = (data, callback) => @Architect.work(data, 'proxy', callback)
25
+ @Architect.ajax = (url, callback) => @Architect.work(url, 'ajax', callback)
26
+ @Architect.jsonp = (url, callback) => @Architect.work(url, 'jsonp', callback)
27
+
28
+ # Long-lived workers
29
+ jobs = {}
30
+ @Architect.workOn = (jobName, data, type, callback) ->
31
+ alreadySpawned = !!jobs[jobName]
32
+
33
+ worker = jobs[jobName] ||= spawnWorker(type)
34
+ worker.postMessage(data)
35
+
36
+ return if alreadySpawned
37
+ worker.addEventListener 'message', (e) ->
38
+ callback(e.data)
39
+
40
+ @Architect.endJob = (jobName) ->
41
+ return unless worker = jobs[jobName]
42
+
43
+ worker.terminate()
44
+ delete jobs[jobName]
45
+
46
+ @Architect.proxyOn = (jobName, data, callback) => @Architect.workOn(jobName, data, 'proxy', callback)
47
+ @Architect.ajaxOn = (jobName, url, callback) => @Architect.workOn(jobName, url, 'ajax', callback)
48
+ @Architect.jsonpOn = (jobName, url, callback) => @Architect.workOn(jobName, url, 'jsonp', callback)
@@ -1,3 +1,3 @@
1
1
  module Architect
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: architect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Etienne Lemay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-24 00:00:00.000000000 Z
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coffee-rails