transistor 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,7 +84,7 @@ var JSONP = function(){
84
84
  };
85
85
  };
86
86
 
87
- /*global JSONP, Faye */
87
+ /*global Faye */
88
88
  if (window.Transistor === undefined) {
89
89
  window.Transistor = {};
90
90
  }
@@ -93,60 +93,62 @@ if (window.Transistor === undefined) {
93
93
 
94
94
  var Radio = (function () {
95
95
 
96
- return function (station_finder, station_uid, token, cache) {
97
- var jsonp, init_url, bayeux, listeners, turnOn, error, tune, build_channel_path;
96
+ return function (aerial_url, station_uid, token, cache) {
97
+ var radio = this,
98
+ faye,
99
+ H,
100
+ Extension;
98
101
 
99
- jsonp = JSONP();
100
-
101
- init_url = station_finder + '/init/' + station_uid + '/' + token;
102
- listeners = [];
103
-
104
- turnOn = function () {
105
- var initial_value;
102
+ H = (function () {
103
+ var callbacks = {},
104
+ buildChannel;
106
105
 
107
- jsonp.get(init_url, {}, function (init) {
108
- if (init.error !== undefined) {
109
- error(init);
110
- }
106
+ buildChannel = function (channel_path) {
107
+ return ['', station_uid, token, channel_path].join('/');
108
+ };
111
109
 
112
- bayeux = new Faye.Client(init.aerial_url, { retry: 3 });
110
+ return {
111
+ registerCallback: function (channel_path, callback) {
112
+ var channel = buildChannel(channel_path),
113
+ listener = function (message) {
114
+ callback(message.event, message.args);
115
+ };
113
116
 
114
- for (var i = 0; i < listeners.length; i += 1) {
115
- initial_value = init.channels[listeners[i].channel_path];
117
+ callbacks[channel_path] = listener;
116
118
 
117
- if (initial_value !== undefined) {
118
- listeners[i].callback({
119
- event: 'init',
120
- args: initial_value
121
- });
122
- }
119
+ faye.subscribe(channel, listener);
120
+ },
121
+ triggerCallback: function (channel_path, message) {
122
+ callbacks[channel_path](message);
123
+ }
124
+ };
125
+ }());
123
126
 
124
- bayeux.subscribe(build_channel_path(listeners[i].channel_path), listeners[i].callback);
127
+ Extension = (function () {
128
+ var process = function (message, callback) {
129
+ if (message.ext && message.ext.init) {
130
+ var channel_path = message.subscription.split('/').splice(3).join('/');
131
+ H.triggerCallback(channel_path, {
132
+ event: 'init',
133
+ args: message.ext.init.collection
134
+ });
125
135
  }
126
- });
127
- };
136
+ callback(message);
137
+ };
128
138
 
129
- error = function (args) {
130
- throw "Init error: \"" + args.error + "\"";
131
- };
139
+ return function () {
140
+ this.incoming = process;
141
+ };
142
+ }());
132
143
 
133
- tune = function (channel_path, callback) {
134
- listeners.push({
135
- channel_path: channel_path,
136
- callback: function (event) {
137
- callback(event.event, event.args);
138
- }
139
- });
140
- };
144
+ faye = new Faye.Client(aerial_url);
145
+ faye.addExtension(new Extension());
141
146
 
142
- build_channel_path = function (channel_path) {
143
- return '/' + station_uid + '/' + token + '/' + channel_path;
147
+ radio.tune = function (channel_path, callback) {
148
+ H.registerCallback(channel_path, callback);
144
149
  };
145
150
 
146
- return {
147
- tune: tune,
148
- turnOn: turnOn
149
- };
151
+ return radio;
150
152
  };
151
153
  }());
152
154
 
@@ -333,12 +335,10 @@ if (window.Transistor === undefined) {
333
335
  }
334
336
  };
335
337
 
336
- return {
337
- set: set,
338
- insert: insert,
339
- update: update,
340
- remove: remove
341
- };
338
+ this.set = set;
339
+ this.insert = insert;
340
+ this.update = update;
341
+ this.remove = remove;
342
342
  };
343
343
  }());
344
344
 
@@ -19,7 +19,7 @@ describe("Transistor.Backbone.Collection", function () {
19
19
  return stub;
20
20
  }());
21
21
 
22
- collection = Transistor.Backbone.Collection({
22
+ collection = new Transistor.Backbone.Collection({
23
23
  channel: 'news',
24
24
  radio: radio
25
25
  })
@@ -48,7 +48,7 @@ describe("Transistor.Control", function () {
48
48
  return me;
49
49
  }());
50
50
 
51
- control = Transistor.Control("broadcaster_url", "station_uid", "token");
51
+ control = new Transistor.Control("broadcaster_url", "station_uid", "token");
52
52
  });
53
53
 
54
54
  describe("set", function () {
@@ -0,0 +1,112 @@
1
+ describe("Transistor.Radio", function () {
2
+ var faye = {},
3
+ servers = {
4
+ 'without_collection': 'http://localhost:12001/faye',
5
+ 'with_collection': 'http://localhost:12002/faye'
6
+ };
7
+
8
+ beforeEach(function () {
9
+ _.each(servers, function (url, key) {
10
+ faye[key] = new Faye.Client(url);
11
+ })
12
+ });
13
+
14
+ afterEach(function () {
15
+ });
16
+
17
+ describe('subscription to empty collection', function () {
18
+ var radio, calls;
19
+
20
+ it('triggers an init event', function () {
21
+ radio = new Transistor.Radio(servers['without_collection'], 'station_uid', 'token');
22
+
23
+ calls = [];
24
+
25
+ runs(function () {
26
+ radio.tune('test/channel', function (event, args) {
27
+ calls.push({
28
+ event: event,
29
+ args: args
30
+ });
31
+ });
32
+ });
33
+
34
+ waits(100);
35
+
36
+ runs(function () {
37
+ assertEqual(calls.length, 1);
38
+ assertEqual(calls[0].event, 'init');
39
+ assertEqual(calls[0].args, []);
40
+ });
41
+ });
42
+ });
43
+
44
+ describe('subscription to filled collection', function () {
45
+ var radio, calls;
46
+
47
+ it('triggers an init event', function () {
48
+ radio = new Transistor.Radio(servers['with_collection'], 'station_uid', 'token');
49
+
50
+ calls = [];
51
+
52
+ runs(function () {
53
+ radio.tune('test/channel', function (event, args) {
54
+ calls.push({
55
+ event: event,
56
+ args: args
57
+ });
58
+ });
59
+ });
60
+
61
+ waits(100);
62
+
63
+ runs(function () {
64
+ assertEqual(calls.length, 1);
65
+ assertEqual(calls[0].event, 'init');
66
+ assertEqual(calls[0].args, [{abcd: 1}, {efgh: 23}]);
67
+ });
68
+ });
69
+ });
70
+
71
+ describe('channel events', function () {
72
+ var radio, calls;
73
+
74
+ it('triggers the callback', function () {
75
+ radio = new Transistor.Radio(servers['with_collection'], 'station_uid', 'token');
76
+
77
+ calls = [];
78
+
79
+ runs(function () {
80
+ radio.tune('test/channel', function (event, args) {
81
+ calls.push({
82
+ event: event,
83
+ args: args
84
+ });
85
+ });
86
+ });
87
+
88
+ waits(100);
89
+
90
+ runs(function () {
91
+ channel_path = '/station_uid/token/test/channel'
92
+ message = {
93
+ event: 'insert',
94
+ args: {
95
+ entry: {}
96
+ }
97
+ }
98
+
99
+ faye['with_collection'].publish(channel_path, message)
100
+ });
101
+
102
+ waits(100);
103
+
104
+ runs(function () {
105
+ assertEqual(calls.length, 2);
106
+ assertEqual(calls[1].event, 'insert');
107
+ assertEqual(calls[1].args, {entry: {}});
108
+ });
109
+ });
110
+ });
111
+
112
+ });
data/spec/servers.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'faye'
2
+
3
+ class Extension
4
+ attr_reader :collection
5
+
6
+ def initialize(collection = [])
7
+ @collection = collection
8
+ end
9
+
10
+ def outgoing(message, callback)
11
+ if(message['channel'] == '/meta/subscribe')
12
+ (message['ext'] ||= {}).tap do |ext|
13
+ ext[:init] ||= {}
14
+ ext[:init][:collection] = collection
15
+ end
16
+ end
17
+ callback.call(message)
18
+ end
19
+ end
20
+
21
+ servers = []
22
+
23
+ servers << Thread.new do
24
+ Faye::RackAdapter.new({
25
+ :mount => '/faye',
26
+ :extensions => [
27
+ Extension.new
28
+ ]
29
+ }).listen(12001)
30
+ end
31
+
32
+ servers << Thread.new do
33
+ Faye::RackAdapter.new({
34
+ :mount => '/faye',
35
+ :extensions => [
36
+ Extension.new([{abcd: 1}, {efgh: 23}])
37
+ ]
38
+ }).listen(12002)
39
+ end
40
+
41
+ servers.each(&:join)
data/transistor.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "transistor"
5
- spec.version = "0.1.5"
5
+ spec.version = "0.1.6"
6
6
 
7
7
  spec.authors = ["Jakob Holderbaum"]
8
8
  spec.email = ["jakob@featurefabrik.de"]
@@ -20,4 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "jslint-v8"
21
21
  spec.add_development_dependency "jake"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "faye"
24
+ spec.add_development_dependency "thin"
23
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transistor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Holderbaum
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-17 00:00:00.000000000 Z
11
+ date: 2013-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faye
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: thin
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description:
84
112
  email:
85
113
  - jakob@featurefabrik.de
@@ -120,10 +148,12 @@ files:
120
148
  - spec/javascripts/TransistorBackboneCollectionSpec.js
121
149
  - spec/javascripts/TransistorBinderSpec.js
122
150
  - spec/javascripts/TransistorControlSpec.js
151
+ - spec/javascripts/TransistorRadioSpec.js
123
152
  - spec/javascripts/helpers/AssertHelpers.js
124
153
  - spec/javascripts/support/backbone.js
125
154
  - spec/javascripts/support/jasmine.yml
126
155
  - spec/javascripts/support/underscore.js
156
+ - spec/servers.rb
127
157
  - transistor.gemspec
128
158
  homepage: http://www.featurefabrik.de
129
159
  licenses:
@@ -153,7 +183,9 @@ test_files:
153
183
  - spec/javascripts/TransistorBackboneCollectionSpec.js
154
184
  - spec/javascripts/TransistorBinderSpec.js
155
185
  - spec/javascripts/TransistorControlSpec.js
186
+ - spec/javascripts/TransistorRadioSpec.js
156
187
  - spec/javascripts/helpers/AssertHelpers.js
157
188
  - spec/javascripts/support/backbone.js
158
189
  - spec/javascripts/support/jasmine.yml
159
190
  - spec/javascripts/support/underscore.js
191
+ - spec/servers.rb