<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
var toString = {}.toString,
isFunction = function( it ) {
return toString.call( it ) == "[object Function]";
},
isString = function( it ) {
return toString.call( it ) == "[object String]";
};
var hub = (function() {
var identifier = 1,
channels = {},
counter = {};
return {
subscribe: function( name, listener ) {
if ( !isString(name) || !isFunction(listener) ) {
return;
}
var done = channels[name];
if ( !done ) {
done = channels[name] = {};
counter[name] = 0;
}
done[identifier] = listener;
counter[name] += 1;
return [ name, identifier++ ];
},
publish: function( name ) {
var done, args, index;
if ( !name || !isString(name) ) {
return;
}
done = channels[name];
args = [].slice.call( arguments, 1 );
for ( index in done ) {
done[index].apply( this, args );
}
},
unsubscribe: function unsubscribe( handle ) {
var isa = isArray( handle ),
name = isa ? handle[0] : handle,
done;
if ( !name || !isString(name) ) {
return;
}
done = channels[name];
if ( isa ) {
counter[name] -= 1;
delete done[handle[1]];
}
if ( !isa || counter[name] === 0 ) {
delete channels[name];
delete counter[name];
}
}
};
})();
hub.subscribe('a', function(){
console.log(123);
});
hub.subscribe('a', function(){
console.log(456);
});
console.log(hub)
hub.publish('a')
</script>
</body>
</html>
https://github.com/shichuan/javascript-patterns/blob/master/design-patterns/observer.html
http://icodon.com/observer-pattern-of-js.html
http://amplifyjs.com/api/pubsub/
http://rolfzhang.com/articles/927.html
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
(function( global, undefined ) {
var slice = [].slice,
subscriptions = {};
var amplify = global.amplify = {
publish: function( topic ) {
var args = slice.call( arguments, 1 ),
topicSubscriptions,
subscription,
length,
i = 0,
ret;
if ( !subscriptions[ topic ] ) {
return true;
}
topicSubscriptions = subscriptions[ topic ].slice();
for ( length = topicSubscriptions.length; i < length; i++ ) {
subscription = topicSubscriptions[ i ];
ret = subscription.callback.apply( subscription.context, args );
if ( ret === false ) {
break;
}
}
return ret !== false;
},
subscribe: function( topic, context, callback, priority ) {
if ( arguments.length === 3 && typeof callback === "number" ) {
priority = callback;
callback = context;
context = null;
}
if ( arguments.length === 2 ) {
callback = context;
context = null;
}
priority = priority || 10;
var topicIndex = 0,
topics = topic.split( /\s/ ),
topicLength = topics.length,
added;
for ( ; topicIndex < topicLength; topicIndex++ ) {
topic = topics[ topicIndex ];
added = false;
if ( !subscriptions[ topic ] ) {
subscriptions[ topic ] = [];
}
var i = subscriptions[ topic ].length - 1,
subscriptionInfo = {
callback: callback,
context: context,
priority: priority
};
for ( ; i >= 0; i-- ) {
if ( subscriptions[ topic ][ i ].priority <= priority ) {
subscriptions[ topic ].splice( i + 1, 0, subscriptionInfo );
added = true;
break;
}
}
if ( !added ) {
subscriptions[ topic ].unshift( subscriptionInfo );
}
}
return callback;
},
unsubscribe: function( topic, callback ) {
if ( !subscriptions[ topic ] ) {
return;
}
var length = subscriptions[ topic ].length,
i = 0;
for ( ; i < length; i++ ) {
if ( subscriptions[ topic ][ i ].callback === callback ) {
subscriptions[ topic ].splice( i, 1 );
break;
}
}
}
};
}(this));
amplify.subscribe('a', function(){
console.log(123);
});
amplify.subscribe('a', function(){
console.log(456);
});
console.log(amplify)
amplify.publish('a')
</script>
</body>
</html>