Skip to content

Commit 99d2ab4

Browse files
committed
No longer just a gist!
0 parents  commit 99d2ab4

19 files changed

+12453
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

.jshintrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"curly": true,
3+
"eqeqeq": true,
4+
"immed": true,
5+
"latedef": true,
6+
"newcap": true,
7+
"noarg": true,
8+
"sub": true,
9+
"undef": true,
10+
"unused": true,
11+
"boss": true,
12+
"eqnull": true,
13+
"node": true,
14+
"es5": true
15+
}

CONTRIBUTING.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Contributing
2+
3+
## Important notes
4+
Please don't edit files in the `dist` subdirectory as they are generated via grunt. You'll find source code in the `src` subdirectory!
5+
6+
### Code style
7+
Regarding code style like indentation and whitespace, **follow the conventions you see used in the source already.**
8+
9+
### PhantomJS
10+
While grunt can run the included unit tests via [PhantomJS](http://phantomjs.org/), this shouldn't be considered a substitute for the real thing. Please be sure to test the `test/*.html` unit test file(s) in _actual_ browsers.
11+
12+
See the [Why does grunt complain that PhantomJS isn't installed?](https://github.com/gruntjs/grunt/blob/master/docs/faq.md#why-does-grunt-complain-that-phantomjs-isnt-installed) guide in the [Grunt FAQ](https://github.com/gruntjs/grunt/blob/master/docs/faq.md) for help with installing or troubleshooting PhantomJS.
13+
14+
## Modifying the code
15+
First, ensure that you have the latest [Node.js](http://nodejs.org/) and [npm](http://npmjs.org/) installed.
16+
17+
Test that grunt is installed globally by running `grunt --version` at the command-line. If grunt isn't installed globally, run `npm install -g grunt` to install the latest version. _You may need to run `sudo npm install -g grunt`._
18+
19+
_Note that in Windows, you may have to run `grunt.cmd` instead of `grunt`._
20+
21+
1. Fork and clone the repo.
22+
1. Run `npm install` to install all dependencies (including grunt).
23+
1. Run `grunt` to grunt this project.
24+
25+
Assuming that you don't see any red, you're ready to go. Just be sure to run `grunt` after making any changes, to ensure that nothing is broken.
26+
27+
## Submitting pull requests
28+
29+
1. Create a new branch, please don't work in your `master` branch directly.
30+
1. Add failing tests for the change you want to make. Run `grunt` to see the tests fail.
31+
1. Fix stuff.
32+
1. Run `grunt` to see if the tests pass. Repeat steps 2-4 until done.
33+
1. Open `test/*.html` unit test file(s) in actual browser to ensure tests pass everywhere.
34+
1. Update the documentation to reflect any changes.
35+
1. Push to your fork and submit a pull request.

Gruntfile.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
module.exports = function(grunt) {
4+
5+
// Project configuration.
6+
grunt.initConfig({
7+
// Metadata.
8+
pkg: grunt.file.readJSON('tiny-pubsub.jquery.json'),
9+
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
10+
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
11+
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
12+
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
13+
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
14+
// Task configuration.
15+
clean: {
16+
src: ['dist']
17+
},
18+
concat: {
19+
options: {
20+
banner: '<%= banner %>',
21+
stripBanners: true
22+
},
23+
dist: {
24+
src: ['src/<%= pkg.name %>.js'],
25+
dest: 'dist/ba-<%= pkg.name %>.js'
26+
},
27+
},
28+
uglify: {
29+
options: {
30+
banner: '<%= banner %>'
31+
},
32+
dist: {
33+
src: '<%= concat.dist.dest %>',
34+
dest: 'dist/ba-<%= pkg.name %>.min.js'
35+
},
36+
},
37+
qunit: {
38+
files: ['test/**/*.html']
39+
},
40+
jshint: {
41+
gruntfile: {
42+
options: {
43+
jshintrc: '.jshintrc'
44+
},
45+
src: 'Gruntfile.js'
46+
},
47+
src: {
48+
options: {
49+
jshintrc: 'src/.jshintrc'
50+
},
51+
src: ['src/**/*.js']
52+
},
53+
test: {
54+
options: {
55+
jshintrc: 'test/.jshintrc'
56+
},
57+
src: ['test/**/*.js']
58+
},
59+
},
60+
watch: {
61+
gruntfile: {
62+
files: '<%= jshint.gruntfile.src %>',
63+
tasks: ['jshint:gruntfile']
64+
},
65+
src: {
66+
files: '<%= jshint.src.src %>',
67+
tasks: ['jshint:src', 'qunit']
68+
},
69+
test: {
70+
files: '<%= jshint.test.src %>',
71+
tasks: ['jshint:test', 'qunit']
72+
},
73+
},
74+
});
75+
76+
// These plugins provide necessary tasks.
77+
grunt.loadNpmTasks('grunt-contrib-clean');
78+
grunt.loadNpmTasks('grunt-contrib-concat');
79+
grunt.loadNpmTasks('grunt-contrib-uglify');
80+
grunt.loadNpmTasks('grunt-contrib-qunit');
81+
grunt.loadNpmTasks('grunt-contrib-jshint');
82+
grunt.loadNpmTasks('grunt-contrib-watch');
83+
84+
// Default task.
85+
grunt.registerTask('default', ['jshint', 'qunit', 'clean', 'concat', 'uglify']);
86+
87+
};

LICENSE-MIT

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 "Cowboy" Ben Alman
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# jQuery Tiny Pub/Sub
2+
3+
A really, really, REALLY tiny pub/sub implementation for jQuery.
4+
5+
_(see the [original gist](https://gist.github.com/661855))_
6+
7+
## Getting Started
8+
Download the [production version][min] or the [development version][max].
9+
10+
[min]: https://raw.github.com/cowboy/jquery-tiny-pubsub/master/dist/tiny-pubsub.min.js
11+
[max]: https://raw.github.com/cowboy/jquery-tiny-pubsub/master/dist/tiny-pubsub.js
12+
13+
Example usage:
14+
15+
```html
16+
<script src="jquery.js"></script>
17+
<script src="dist/tiny-pubsub.min.js"></script>
18+
<script>
19+
// Creates a "named" logging function.
20+
function createLogger(name) {
21+
return function(_, a, b) {
22+
// Skip the first argument (event object) but log the name and other args.
23+
console.log(name, a, b);
24+
};
25+
}
26+
27+
// Subscribe to the "foo" topic (bind to the "foo" event, no namespace).
28+
$.subscribe('foo', createLogger('foo'));
29+
// Subscribe to the "foo.bar" topic (bind to the "foo" event, "bar" namespace).
30+
$.subscribe('foo.bar', createLogger('foo.bar'));
31+
// Subscribe to the "foo.baz" topic (bind to the "foo" event, "baz" namespace).
32+
$.subscribe('foo.baz', createLogger('foo.baz'));
33+
34+
// Publish arbitrary values.
35+
$.publish('foo', [1, 2]);
36+
// logs:
37+
// foo 1 2
38+
// foo.bar 1 2
39+
// foo.baz 1 2
40+
41+
$.publish('foo.bar', [3, 4]);
42+
// logs:
43+
// foo.bar 3 4
44+
45+
$.publish('foo.baz', [5, 6]);
46+
// logs:
47+
// foo.baz 5 6
48+
49+
$.unsubscribe('foo.bar');
50+
$.publish('foo', [7, 8]);
51+
// logs:
52+
// foo 7 8
53+
// foo.baz 7 8
54+
</script>
55+
```
56+
57+
## Documentation
58+
_Note: Ignore the first argument passed to your subscribed callbacks (the jQuery event object)._
59+
60+
_Another Note: [Previous versions](https://gist.github.com/661855/2c518edd29b744d04bff55ec9a2a5d12afe41595) (v0.4+) were written in an attempt to remove the first argument and create a more future-proof API, but unfortunately this resulted in much less elegant, larger and slower code. The point of this plugin is to be TINY, to be used in situations where only size (not performance or usability) is the primary concern (tweets, code golf, etc).**_
61+
62+
I frequently see comments about how jQuery's events system has unnecessary overhead that precludes it from being used as the core of a Pub/Sub implementation. The jQuery events system is tried-and-true, having been architected to be both fast and robust, and the vast majority of users of this plugin should never encounter any kind of performance issues.
63+
64+
Because this plugin's `$.subscribe`, `$.unsubscribe` and `$.publish` methods all use the jQuery [.on()](http://api.jquery.com/on/), [.off()](http://api.jquery.com/off/) and [.trigger()](http://api.jquery.com/trigger/) methods internally, those methods' complete signatures are available to you.
65+
66+
You can use [namespaces](http://docs.jquery.com/Namespaced_Events) for more control over unsubscribing and publishing.
67+
68+
Just use this handy terminology guide (jQuery events term → Pub/Sub term), and everything should make sense:
69+
70+
* on → subscribe
71+
* off → unsubscribe
72+
* trigger → publish
73+
* type → topic
74+
75+
In addition, should you need it, these methods are fully compatible with the [jQuery.proxy()](http://api.jquery.com/jQuery.proxy/) method, in case you not only want more control over to which context the subscribed callback is bound, but want to be able to very easily unsubscribe via callback reference.
76+
77+
Regarding performance: If at some point, your application starts processing so many messages that performance issues start to develop, you could always write a replacement "jQuery Not-So-Tiny Pub/Sub" plugin with the same API and just drop it in as a replacement to this plugin. But keep in mind that you'll also need to add in the aforementioned features, too.
78+
79+
## Release History
80+
2013-01-29 - v0.7.0 - First official release.

dist/ba-tiny-pubsub.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*! Tiny Pub/Sub - v0.7.0 - 2013-01-29
2+
* https://github.com/cowboy/jquery-tiny-pubsub
3+
* Copyright (c) 2013 "Cowboy" Ben Alman; Licensed MIT */
4+
(function($) {
5+
6+
var o = $({});
7+
8+
$.subscribe = function() {
9+
o.on.apply(o, arguments);
10+
};
11+
12+
$.unsubscribe = function() {
13+
o.off.apply(o, arguments);
14+
};
15+
16+
$.publish = function() {
17+
o.trigger.apply(o, arguments);
18+
};
19+
20+
}(jQuery));

dist/ba-tiny-pubsub.min.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*! Tiny Pub/Sub - v0.7.0 - 2013-01-29
2+
* https://github.com/cowboy/jquery-tiny-pubsub
3+
* Copyright (c) 2013 "Cowboy" Ben Alman; Licensed MIT */
4+
(function(n){var u=n({});n.subscribe=function(){u.on.apply(u,arguments)},n.unsubscribe=function(){u.off.apply(u,arguments)},n.publish=function(){u.trigger.apply(u,arguments)}})(jQuery);

libs/jquery-loader.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(function() {
2+
// Default to the local version.
3+
var path = '../libs/jquery/jquery.js';
4+
// Get any jquery=___ param from the query string.
5+
var jqversion = location.search.match(/[?&]jquery=(.*?)(?=&|$)/);
6+
// If a version was specified, use that version from code.jquery.com.
7+
if (jqversion) {
8+
path = 'http://code.jquery.com/jquery-' + jqversion[1] + '.js';
9+
}
10+
// This is the only time I'll ever use document.write, I promise!
11+
document.write('<script src="' + path + '"></script>');
12+
}());

0 commit comments

Comments
 (0)