Skip to content

Instantly share code, notes, and snippets.

@valerysntx
Last active December 31, 2016 17:43
Show Gist options
  • Save valerysntx/1dc85d106fbc447162dff333ef791120 to your computer and use it in GitHub Desktop.
Save valerysntx/1dc85d106fbc447162dff333ef791120 to your computer and use it in GitHub Desktop.
Observable of any / no-generics
class Observer {
public action: (T) => any;
constructor(action: (T) => any) {
this.action = action;
}
public Set(value: any) {
this.action = value;
};
public Get() { return this.action };
};
class Observable {
value: any;
subscribers: Observer [];
constructor(any) {
this.value = any;
this.subscribers = [];
}
public Notify(o:Observer, value: any) {
o.Get()(value);
}
public notifySubscribersComputed(value:any){
if (this.subscribers!==null)
this.subscribers.forEach((observer:Observer) => {
this.Notify(observer,value);
});
};
public Subscribe(watch: Observer) {
this.subscribers.push(watch)
}
public get () {
return this.value
}
public set(value: any) {
this.value = value;
this.notifySubscribersComputed(value)
}
};
var observable = new Observable(3);
var observer = new Observer((t) => alert('observe changes:' + observable.get()));
observable.set(5);
observable.set(6);
observer.action(observable.get());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment