transistor 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/index.html DELETED
@@ -1,40 +0,0 @@
1
- <!DOCTYPE HTML>
2
- <html>
3
- <head>
4
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
5
- <title>radiotower</title>
6
- <script type="text/javascript" src="vendor/faye-browser-min.js"></script>
7
- <script type="text/javascript" src="vendor/jsonp.js"></script>
8
- <script type="text/javascript" src="lib/transistor/radio.js"></script>
9
- <script type="text/javascript" src="lib/transistor/control.js"></script>
10
- <script type="text/javascript" src="lib/transistor/binder.js"></script>
11
- <script type="text/javascript">
12
-
13
- var control = Transistor.Control(
14
- 'http://localhost:9700', // broadcaster
15
- '6c6da56f84c148e4865fc7bc7a032ef2', // station uid
16
- '36062ea123173651fb3b18de21f8c5cb' // push token
17
- )
18
-
19
- var radio = Transistor.Radio(
20
- 'http://localhost:9703', // station_finder
21
- '6c6da56f84c148e4865fc7bc7a032ef2', // station uid
22
- '30a8765fab4f56ff351576202ae67645' // test/live token
23
- );
24
-
25
- // explicit listener function
26
- radio.tune("about", function(event, args) {
27
- console.log('about', event, args);
28
- });
29
-
30
- // bind channel to a local array
31
- var news = [];
32
- radio.tune("news", Transistor.Binder(news));
33
-
34
- radio.turnOn();
35
- </script>
36
- </head>
37
- <body>
38
-
39
- </body>
40
- </html>
@@ -1,109 +0,0 @@
1
- if (window.Transistor === undefined) {
2
- window.Transistor = {};
3
- }
4
-
5
- (function (transistor) {
6
- var Binder = (function () {
7
- return function (array) {
8
- var H, set, insert, update, remove;
9
-
10
- H = (function () {
11
- return {
12
- clear: function () {
13
- for (;array.length !== 0;) {
14
- array.pop();
15
- }
16
- },
17
-
18
- indexOfId: function (id) {
19
- for (var i = 0; i < array.length; i += 1) {
20
- if (array[i].id === id) {
21
- return i;
22
- }
23
- }
24
-
25
- throw new Error("unknown id: " + id);
26
- },
27
-
28
- expectCollection: function (args) {
29
- if (args.collection === undefined) {
30
- throw new Error("undefined collection");
31
- }
32
- },
33
-
34
- expectEntry: function (args) {
35
- if (args.entry === undefined) {
36
- throw new Error("undefined entry");
37
- }
38
- },
39
-
40
- expectId: function (args) {
41
- if (args.id === undefined) {
42
- throw new Error("undefined id");
43
- }
44
- }
45
- };
46
- }());
47
-
48
- set = function (collection) {
49
- H.clear();
50
-
51
- if (collection !== undefined) {
52
- for (var i = 0 ; i < collection.length; i += 1) {
53
- array[i] = collection[i];
54
- }
55
- }
56
- };
57
-
58
- insert = function (entry) {
59
- if (entry !== undefined) {
60
- array.push(entry);
61
- }
62
- };
63
-
64
- update = function (id, entry) {
65
- if (id === undefined) {
66
- throw new Error("undefined id");
67
- }
68
-
69
- array[H.indexOfId(id)] = entry;
70
- };
71
-
72
- remove = function (id) {
73
- array.splice(H.indexOfId(id), 1);
74
- };
75
-
76
- return function (event, args) {
77
- if (args === undefined) {
78
- throw new Error("undefined arguments");
79
- }
80
-
81
- switch (event) {
82
- case 'init':
83
- set(args);
84
- break;
85
- case 'set':
86
- H.expectCollection(args);
87
- set(args.collection);
88
- break;
89
- case 'insert':
90
- H.expectEntry(args);
91
- insert(args.entry);
92
- break;
93
- case 'update':
94
- H.expectId(args);
95
- H.expectEntry(args);
96
- update(args.id, args.entry);
97
- break;
98
- case 'remove':
99
- H.expectId(args);
100
- remove(args.id);
101
- break;
102
- }
103
- };
104
- };
105
- }());
106
-
107
- transistor.Binder = Binder;
108
-
109
- }(window.Transistor));
@@ -1,62 +0,0 @@
1
- /*global JSONP */
2
- if (window.Transistor === undefined) {
3
- window.Transistor = {};
4
- }
5
-
6
- (function (transistor) {
7
-
8
- var Control = (function () {
9
-
10
- return function (broadcaster, station_uid, token) {
11
- var set, insert, update, remove, command_url, send;
12
-
13
- set = function (channel, collection, cb) {
14
- send(command_url('set', channel), {collection: collection}, cb);
15
- };
16
-
17
- insert = function (channel, entry, cb) {
18
- send(command_url('insert', channel), {entry: entry}, cb);
19
- };
20
-
21
- update = function (channel, id, entry, cb) {
22
- send(command_url('update', channel), {id: id, entry: entry}, cb);
23
- };
24
-
25
- remove = function (channel, id, cb) {
26
- send(command_url('remove', channel), {id: id}, cb);
27
- };
28
-
29
- command_url = function (cmd, channel) {
30
- var base = broadcaster + '/command/' + cmd + '/' + station_uid + '/' + token;
31
-
32
- if (channel === undefined) {
33
- return base;
34
- } else {
35
- return base + '/' + channel;
36
- }
37
- };
38
-
39
- send = function (command_url, data, cb) {
40
- if (cb === undefined) {
41
- cb = function (result) {};
42
- }
43
-
44
- if (data === undefined) {
45
- JSONP.get(command_url, {}, cb);
46
- } else {
47
- JSONP.get(command_url, {data: JSON.stringify(data)}, cb);
48
- }
49
- };
50
-
51
- return {
52
- set: set,
53
- insert: insert,
54
- update: update,
55
- remove: remove
56
- };
57
- };
58
- }());
59
-
60
- transistor.Control = Control;
61
-
62
- }(window.Transistor));
@@ -1,67 +0,0 @@
1
- /*global JSONP, Faye */
2
- if (window.Transistor === undefined) {
3
- window.Transistor = {};
4
- }
5
-
6
- (function (transistor) {
7
-
8
- var Radio = (function () {
9
-
10
- return function (station_finder, station_uid, token, cache) {
11
- var init_url, bayeux, listeners, turnOn, error, tune, build_channel_path;
12
-
13
- init_url = station_finder + '/init/' + station_uid + '/' + token;
14
- listeners = [];
15
-
16
- turnOn = function () {
17
- var initial_value;
18
-
19
- JSONP.get(init_url, {}, function (init) {
20
- if (init.error !== undefined) {
21
- error(init);
22
- }
23
-
24
- bayeux = new Faye.Client(init.aerial_url, { retry: 3 });
25
-
26
- for (var i = 0; i < listeners.length; i += 1) {
27
- initial_value = init.channels[listeners[i].channel_path];
28
-
29
- if (initial_value !== undefined) {
30
- listeners[i].callback({
31
- event: 'init',
32
- args: initial_value
33
- });
34
- }
35
-
36
- bayeux.subscribe(build_channel_path(listeners[i].channel_path), listeners[i].callback);
37
- }
38
- });
39
- };
40
-
41
- error = function (args) {
42
- throw "Init error: \"" + args.error + "\"";
43
- };
44
-
45
- tune = function (channel_path, callback) {
46
- listeners.push({
47
- channel_path: channel_path,
48
- callback: function (event) {
49
- callback(event.event, event.args);
50
- }
51
- });
52
- };
53
-
54
- build_channel_path = function (channel_path) {
55
- return '/' + station_uid + '/' + token + '/' + channel_path;
56
- };
57
-
58
- return {
59
- tune: tune,
60
- turnOn: turnOn
61
- };
62
- };
63
- }());
64
-
65
- transistor.Radio = Radio;
66
-
67
- }(window.Transistor));