Permalink
Browse files

No longer just a gist!

0 parents commit 99d2ab439e56a49f166f1534754e3cc60eb6f3cf @cowboy committed Jan 29, 2013
Showing with 12,453 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +15 −0 .jshintrc
  3. +35 −0 CONTRIBUTING.md
  4. +87 −0 Gruntfile.js
  5. +22 −0 LICENSE-MIT
  6. +80 −0 README.md
  7. +20 −0 dist/ba-tiny-pubsub.js
  8. +4 −0 dist/ba-tiny-pubsub.min.js
  9. +12 −0 libs/jquery-loader.js
  10. +9,555 −0 libs/jquery/jquery.js
  11. +244 −0 libs/qunit/qunit.css
  12. +2,152 −0 libs/qunit/qunit.js
  13. +19 −0 package.json
  14. +15 −0 src/.jshintrc
  15. +25 −0 src/tiny-pubsub.js
  16. +32 −0 test/.jshintrc
  17. +27 −0 test/tiny-pubsub.html
  18. +82 −0 test/tiny-pubsub_test.js
  19. +26 −0 tiny-pubsub.jquery.json
@@ -0,0 +1 @@
+/node_modules/
@@ -0,0 +1,15 @@
+{
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "undef": true,
+ "unused": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true,
+ "es5": true
+}
@@ -0,0 +1,35 @@
+# Contributing
+
+## Important notes
+Please don't edit files in the `dist` subdirectory as they are generated via grunt. You'll find source code in the `src` subdirectory!
+
+### Code style
+Regarding code style like indentation and whitespace, **follow the conventions you see used in the source already.**
+
+### PhantomJS
+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.
+
+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.
+
+## Modifying the code
+First, ensure that you have the latest [Node.js](http://nodejs.org/) and [npm](http://npmjs.org/) installed.
+
+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`._
+
+_Note that in Windows, you may have to run `grunt.cmd` instead of `grunt`._
+
+1. Fork and clone the repo.
+1. Run `npm install` to install all dependencies (including grunt).
+1. Run `grunt` to grunt this project.
+
+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.
+
+## Submitting pull requests
+
+1. Create a new branch, please don't work in your `master` branch directly.
+1. Add failing tests for the change you want to make. Run `grunt` to see the tests fail.
+1. Fix stuff.
+1. Run `grunt` to see if the tests pass. Repeat steps 2-4 until done.
+1. Open `test/*.html` unit test file(s) in actual browser to ensure tests pass everywhere.
+1. Update the documentation to reflect any changes.
+1. Push to your fork and submit a pull request.
@@ -0,0 +1,87 @@
+'use strict';
+
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ // Metadata.
+ pkg: grunt.file.readJSON('tiny-pubsub.jquery.json'),
+ banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
+ '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
+ '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
+ '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
+ ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
+ // Task configuration.
+ clean: {
+ src: ['dist']
+ },
+ concat: {
+ options: {
+ banner: '<%= banner %>',
+ stripBanners: true
+ },
+ dist: {
+ src: ['src/<%= pkg.name %>.js'],
+ dest: 'dist/ba-<%= pkg.name %>.js'
+ },
+ },
+ uglify: {
+ options: {
+ banner: '<%= banner %>'
+ },
+ dist: {
+ src: '<%= concat.dist.dest %>',
+ dest: 'dist/ba-<%= pkg.name %>.min.js'
+ },
+ },
+ qunit: {
+ files: ['test/**/*.html']
+ },
+ jshint: {
+ gruntfile: {
+ options: {
+ jshintrc: '.jshintrc'
+ },
+ src: 'Gruntfile.js'
+ },
+ src: {
+ options: {
+ jshintrc: 'src/.jshintrc'
+ },
+ src: ['src/**/*.js']
+ },
+ test: {
+ options: {
+ jshintrc: 'test/.jshintrc'
+ },
+ src: ['test/**/*.js']
+ },
+ },
+ watch: {
+ gruntfile: {
+ files: '<%= jshint.gruntfile.src %>',
+ tasks: ['jshint:gruntfile']
+ },
+ src: {
+ files: '<%= jshint.src.src %>',
+ tasks: ['jshint:src', 'qunit']
+ },
+ test: {
+ files: '<%= jshint.test.src %>',
+ tasks: ['jshint:test', 'qunit']
+ },
+ },
+ });
+
+ // These plugins provide necessary tasks.
+ grunt.loadNpmTasks('grunt-contrib-clean');
+ grunt.loadNpmTasks('grunt-contrib-concat');
+ grunt.loadNpmTasks('grunt-contrib-uglify');
+ grunt.loadNpmTasks('grunt-contrib-qunit');
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-contrib-watch');
+
+ // Default task.
+ grunt.registerTask('default', ['jshint', 'qunit', 'clean', 'concat', 'uglify']);
+
+};
@@ -0,0 +1,22 @@
+Copyright (c) 2013 "Cowboy" Ben Alman
+
+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.
@@ -0,0 +1,80 @@
+# jQuery Tiny Pub/Sub
+
+A really, really, REALLY tiny pub/sub implementation for jQuery.
+
+_(see the [original gist](https://gist.github.com/661855))_
+
+## Getting Started
+Download the [production version][min] or the [development version][max].
+
+[min]: https://raw.github.com/cowboy/jquery-tiny-pubsub/master/dist/tiny-pubsub.min.js
+[max]: https://raw.github.com/cowboy/jquery-tiny-pubsub/master/dist/tiny-pubsub.js
+
+Example usage:
+
+```html
+<script src="jquery.js"></script>
+<script src="dist/tiny-pubsub.min.js"></script>
+<script>
+// Creates a "named" logging function.
+function createLogger(name) {
+ return function(_, a, b) {
+ // Skip the first argument (event object) but log the name and other args.
+ console.log(name, a, b);
+ };
+}
+
+// Subscribe to the "foo" topic (bind to the "foo" event, no namespace).
+$.subscribe('foo', createLogger('foo'));
+// Subscribe to the "foo.bar" topic (bind to the "foo" event, "bar" namespace).
+$.subscribe('foo.bar', createLogger('foo.bar'));
+// Subscribe to the "foo.baz" topic (bind to the "foo" event, "baz" namespace).
+$.subscribe('foo.baz', createLogger('foo.baz'));
+
+// Publish arbitrary values.
+$.publish('foo', [1, 2]);
+// logs:
+// foo 1 2
+// foo.bar 1 2
+// foo.baz 1 2
+
+$.publish('foo.bar', [3, 4]);
+// logs:
+// foo.bar 3 4
+
+$.publish('foo.baz', [5, 6]);
+// logs:
+// foo.baz 5 6
+
+$.unsubscribe('foo.bar');
+$.publish('foo', [7, 8]);
+// logs:
+// foo 7 8
+// foo.baz 7 8
+</script>
+```
+
+## Documentation
+_Note: Ignore the first argument passed to your subscribed callbacks (the jQuery event object)._
+
+_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).**_
+
+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.
+
+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.
+
+You can use [namespaces](http://docs.jquery.com/Namespaced_Events) for more control over unsubscribing and publishing.
+
+Just use this handy terminology guide (jQuery events term → Pub/Sub term), and everything should make sense:
+
+ * on → subscribe
+ * off → unsubscribe
+ * trigger → publish
+ * type → topic
+
+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.
+
+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.
+
+## Release History
+2013-01-29 - v0.7.0 - First official release.
@@ -0,0 +1,20 @@
+/*! Tiny Pub/Sub - v0.7.0 - 2013-01-29
+* https://github.com/cowboy/jquery-tiny-pubsub
+* Copyright (c) 2013 "Cowboy" Ben Alman; Licensed MIT */
+(function($) {
+
+ var o = $({});
+
+ $.subscribe = function() {
+ o.on.apply(o, arguments);
+ };
+
+ $.unsubscribe = function() {
+ o.off.apply(o, arguments);
+ };
+
+ $.publish = function() {
+ o.trigger.apply(o, arguments);
+ };
+
+}(jQuery));
@@ -0,0 +1,4 @@
+/*! Tiny Pub/Sub - v0.7.0 - 2013-01-29
+* https://github.com/cowboy/jquery-tiny-pubsub
+* Copyright (c) 2013 "Cowboy" Ben Alman; Licensed MIT */
+(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);
@@ -0,0 +1,12 @@
+(function() {
+ // Default to the local version.
+ var path = '../libs/jquery/jquery.js';
+ // Get any jquery=___ param from the query string.
+ var jqversion = location.search.match(/[?&]jquery=(.*?)(?=&|$)/);
+ // If a version was specified, use that version from code.jquery.com.
+ if (jqversion) {
+ path = 'http://code.jquery.com/jquery-' + jqversion[1] + '.js';
+ }
+ // This is the only time I'll ever use document.write, I promise!
+ document.write('<script src="' + path + '"></script>');
+}());
Oops, something went wrong.

0 comments on commit 99d2ab4

Please sign in to comment.