 //
 
 import Foundation
+import Promissum
 
-struct ObservableSubject<T>: Equatable {
+struct ObservableSubject<ValueType>: Equatable {
   let guid = NSUUID().UUIDString
   let observeOn: dispatch_queue_t
-  let observable: (T) -> ()
+  let observable: (ValueType) -> ()
+  let once: Bool
 }
 
-func ==<T>(lhs: ObservableSubject<T>, rhs: ObservableSubject<T>) -> Bool {
+func ==<ValueType>(lhs: ObservableSubject<ValueType>, rhs: ObservableSubject<ValueType>) -> Bool {
   return lhs.guid == rhs.guid
 }
 
-class Observable<T where T: Equatable> {
+class Observable<ValueType where ValueType: Equatable> {
 
   private let queue = dispatch_queue_create("nl.tomasharkema.Observable", DISPATCH_QUEUE_SERIAL)
 
-  private var value: T? = nil {
+  private var value: ValueType? = nil {
     didSet {
       notifiy()
     }
   }
   
-  private var subjects = [ObservableSubject<T>]()
+  private var subjects = [ObservableSubject<ValueType>]()
 
   private func notifiy() {
-    dispatch_async(queue) { [weak self] in
-      if let value = self?.value, subjects = self?.subjects {
-        for subject in subjects {
-          dispatch_async(subject.observeOn) {
-            subject.observable(value)
-          }
+    if let value = value {
+      for subject in subjects {
+        dispatch_async(subject.observeOn) {
+          subject.observable(value)
         }
-      } else {
-        print("Notifying without value?!")
+        if subject.once {
+          unsubscribe(subject)
+        }
       }
+    } else {
+      print("Notifying without value?!")
     }
   }
 
-  func subscribe(observeOn: dispatch_queue_t = dispatch_get_main_queue(), subject: (T) -> ()) -> ObservableSubject<T> {
-    var subscription: ObservableSubject<T>!
+  func subscribe(observeOn: dispatch_queue_t = dispatch_get_main_queue(), subject: (ValueType) -> ()) -> ObservableSubject<ValueType> {
+    var subscription: ObservableSubject<ValueType>!
     dispatch_sync(queue) { [weak self] in
-      subscription = ObservableSubject(observeOn: observeOn, observable: subject)
+      subscription = ObservableSubject(observeOn: observeOn, observable: subject, once: false)
       self?.subjects.append(subscription)
-    }
-    if let value = value {
-      dispatch_async(observeOn) {
-        subject(value)
+
+      if let value = self?.value {
+        dispatch_async(observeOn) {
+          subject(value)
+        }
       }
     }
     return subscription
   }
 
-  func unsubscribe(subject: ObservableSubject<T>) {
+  func unsubscribe(subject: ObservableSubject<ValueType>) {
     dispatch_async(queue) { [weak self] in
       if let index = self?.subjects.indexOf(subject) {
         self?.subjects.removeAtIndex(index)


  init() {}

  init(initialValue: T) {
    value = initialValue
  }
}
    }
  }

  func next(value: ValueType) {
    dispatch_async(queue) { [weak self] in
      if value != self?.value {
        self?.value = value
