goog.provide('goog.observable');
/**
* @interface
* @param {*=} opt_value
*/
goog.iObservable = function(opt_value) {};
/**
* @type {number}
*/
goog.iObservable.prototype.subscriptionID;
/**
* @type {Object}
*/
goog.iObservable.prototype.subscriptions;
/**
* @type {function(function(*))}
*/
goog.iObservable.prototype.subscribe;
/**
* @type {function(function(*))}
*/
goog.iObservable.prototype.unsubscribe;
/**
* @param {*} initValue
* @returns {??????????}
*/
goog.observable = function(initValue) {
var currentValue = initValue;
var observable = null;
observable = (function() {
if (arguments.length > 0) {
currentValue = arguments[0];
for ( var key in observable.subscriptions) {
observable.subscriptions[key](arguments[0]);
}
} else {
return currentValue;
}
});
observable.subscriptionID = 0;
observable.subscriptions = {};
observable.subscribe = function(callback) {
observable.subscriptions[observable.subscriptionID++] = callback;
return observable.subscriptionID - 1;
};
observable.unsubscribe = function(subscriptionID) {
if (observable.subscriptions[subscriptionID]) {
observable.subscriptions[subscriptionID] = null;
delete observable.subscriptions[subscriptionID];
}
};
return observable;
};
var person = {
name: goog.observable('john'),
age: goog.observable(37)
};
alert(test);
var sid1 = person.name.subscribe(function(newValue){
alert(newValue);
});
var sid2 = person.age.subscribe(function(newValue){
alert(newValue);
});
person.age(36);
person.name.unsubscribe(sid1);
person.name.unsubscribe(sid2);
}
I tried so many different annotations, but always get compiler errors - Only if I remove the @returns from goog.observable it compiles, but I would rather prefer to figure out how to annotate it correctly.