Skip to content

Commit

Permalink
refactor observers into subsub
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjennings committed Jun 19, 2015
1 parent da05570 commit d5d03b4
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 32 deletions.
Binary file modified .DS_Store
Binary file not shown.
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eon-chart",
"version": "0.2.14",
"version": "0.2.15",
"homepage": "https://github.com/pubnub/eon-chart",
"authors": [
"Ian Jennings <ian@meetjennings.com>"
Expand Down Expand Up @@ -30,7 +30,8 @@
"dependencies": {
"d3": "~3.5.5",
"c3": "~0.4.10",
"pubnub": "~3.7.8"
"pubnub": "~3.7.8",
"subsub": "*"
},
"resolutions": {
"d3": "<=3.5.0"
Expand Down
14 changes: 14 additions & 0 deletions bower_components/subsub/.bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "subsub",
"homepage": "https://github.com/pubnub/subsub",
"_release": "a54ddab1a2",
"_resolution": {
"type": "branch",
"branch": "master",
"commit": "a54ddab1a22a0a3551191be638d9fe0ce718b211"
},
"_source": "git://github.com/pubnub/subsub.git",
"_target": "*",
"_originalSource": "subsub",
"_direct": true
}
22 changes: 22 additions & 0 deletions bower_components/subsub/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 PubNub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16 changes: 16 additions & 0 deletions bower_components/subsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# subsub.js

Subsub allows you to create multiple callbacks for a single channel.

# Usage

```
subsub.subscribe(pubnub, channel, connect, callback);
```

Option | Explanation
-------|-----------
pubnub | Your PubNub javascript object.
channel | The string of the PubNub channel to subscribe to
connect | Optional callback to call when PubNub connects
callback | function to fire when a message is recieved. Works same way as ```pubnub.subscribe```
37 changes: 37 additions & 0 deletions bower_components/subsub/example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>
<body>
<h1>PubNub SubSub</h1>
<p>View source to see a subsub example.</p>
<script src="http://cdn.pubnub.com/pubnub-3.7.10.min.js"></script>
<script src="../subsub.js"></script>
<script type="text/javascript">

var pubnub = PUBNUB({
publish_key: 'demo',
subscribe_key: 'demo'
});

subsub.subscribe(pubnub, 'subsub_chan', null, function(message, env, channel) {
console.log('subsub cb 1', message);
});

subsub.subscribe(pubnub, 'subsub_chan', null, function(message, env, channel) {
console.log('subsub cb 2', message);
});

subsub.subscribe(pubnub, 'subsub_chan', null, function(message, env, channel) {
console.log('subsub cb 3', message);
});

setInterval(function(){

pubnub.publish({
channel: 'subsub_chan',
message: 'hello world'
});

}, 1000);

</script>
</body>
</html>
31 changes: 31 additions & 0 deletions bower_components/subsub/subsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var subsub = {
observers: {},
message: function(message, env, channel) {

for(var i in subsub.observers[channel]) {
subsub.observers[channel][i](message, env, channel);
}

},
subscribe: function(pubnub, channel, connect, callback) {

if(typeof(subsub.observers[channel]) == "undefined") {

subsub.observers[channel] = [callback];

connect = connect || function(){};

pubnub.subscribe({
channel: channel,
connect: connect,
message: function(message, env, channel) {
subsub.message(message, env, channel );
}
});

} else {
subsub.observers[channel].push(callback);
}

}
};
1 change: 1 addition & 0 deletions examples/custom_keys.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<script src="../bower_components/pubnub/web/pubnub.min.js"></script>
<script src="../bower_components/d3/d3.min.js"></script>
<script src="../bower_components/c3/c3.min.js"></script>
<script src="../bower_components/subsub/subsub.js"></script>

<script src="../pubnub-c3.js"></script>

Expand Down
1 change: 1 addition & 0 deletions examples/distributed.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script src="../bower_components/pubnub/web/pubnub.min.js"></script>
<script src="../bower_components/d3/d3.min.js"></script>
<script src="../bower_components/c3/c3.min.js"></script>
<script src="../bower_components/subsub/subsub.js"></script>

<script src="../pubnub-c3.js"></script>

Expand Down
1 change: 1 addition & 0 deletions examples/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script src="../bower_components/pubnub/web/pubnub.min.js"></script>
<script src="../bower_components/d3/d3.min.js"></script>
<script src="../bower_components/c3/c3.min.js"></script>
<script src="../bower_components/subsub/subsub.js"></script>

<script src="../pubnub-c3.js"></script>

Expand Down
1 change: 1 addition & 0 deletions examples/transform.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script src="../bower_components/pubnub/web/pubnub.min.js"></script>
<script src="../bower_components/d3/d3.min.js"></script>
<script src="../bower_components/c3/c3.min.js"></script>
<script src="../bower_components/subsub/subsub.js"></script>

<script src="../pubnub-c3.js"></script>

Expand Down
32 changes: 2 additions & 30 deletions pubnub-c3.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
"use strict";

var eon = eon || {};
eon.subsub = eon.subsub || subsub;
eon.c = {
observers: {},
message: function(message, env, channel) {

var i = 0;
while(i < eon.c.observers[channel].length) {
eon.c.observers[channel][i](message, env, channel);
i++;
}

},
subscribe: function(pubnub, channel, connect, callback) {

if(typeof(eon.c.observers[channel]) == "undefined") {

eon.c.observers[channel] = [callback];

pubnub.subscribe({
channel: channel,
connect: connect,
message: function(message, env, channel) {
eon.c.message(message, env, channel );
}
});

} else {
eon.c.observers[channel].push(callback);
}

},
create: function(options) {

var self = this;
Expand Down Expand Up @@ -263,7 +235,7 @@ eon.c = {
page();
}

eon.c.subscribe(self.pubnub, options.channel, options.connect, function(message, env, channel) {
subsub.subscribe(self.pubnub, options.channel, options.connect, function(message, env, channel) {

var message = options.transform(message);

Expand Down

0 comments on commit d5d03b4

Please sign in to comment.