latest.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package rx
  2. type KeyTrackedActionVector struct {
  3. HasKey func(key string) bool
  4. IterateKeys func(func(string))
  5. CloneKeys func() ([] string)
  6. GetAction func(key string, index_source Observable) Observable
  7. }
  8. func (e Observable) WithLatestFrom(source Observable) Observable {
  9. return Observable { func(sched Scheduler, ob *observer) {
  10. var ctx, dispose = ob.context.create_disposable_child()
  11. var c = new_collector(ob, dispose)
  12. var current Optional
  13. c.new_child()
  14. sched.run(source, &observer {
  15. context: ctx,
  16. next: func(value Object) {
  17. current = Optional { true, value }
  18. },
  19. error: func(err Object) {
  20. c.throw(err)
  21. },
  22. complete: func() {
  23. c.delete_child()
  24. },
  25. })
  26. c.new_child()
  27. sched.run(e, &observer {
  28. context: ctx,
  29. next: func(obj Object) {
  30. c.pass(Pair { obj, current })
  31. },
  32. error: func(err Object) {
  33. c.throw(err)
  34. },
  35. complete: func() {
  36. c.delete_child()
  37. },
  38. })
  39. c.parent_complete()
  40. } }
  41. }
  42. func CombineLatest(actions ([] Observable)) Observable {
  43. if len(actions) == 0 {
  44. return NewConstant(make([] Optional, 0))
  45. }
  46. return Observable { func(sched Scheduler, ob *observer) {
  47. var ctx, dispose = ob.context.create_disposable_child()
  48. var c = new_collector(ob, dispose)
  49. var values = make([] Optional, len(actions))
  50. for i_, e := range actions {
  51. var i = i_
  52. c.new_child()
  53. sched.run(e, &observer {
  54. context: ctx,
  55. next: func(obj Object) {
  56. var has_saved = &(values[i].HasValue)
  57. var saved_latest = &(values[i].Value)
  58. *saved_latest = obj
  59. *has_saved = true
  60. var values_clone = make([] Optional, len(values))
  61. copy(values_clone, values)
  62. c.pass(values_clone)
  63. },
  64. error: func(err Object) {
  65. c.throw(err)
  66. },
  67. complete: func() {
  68. c.delete_child()
  69. },
  70. })
  71. }
  72. c.parent_complete()
  73. } }
  74. }
  75. func CombineLatestWaitReady(actions ([] Observable)) Observable {
  76. return CombineLatest(actions).ConcatMap(func(values_ Object) Observable {
  77. var values = values_.([] Optional)
  78. var ready_values = make([] Object, len(values))
  79. var ok = true
  80. for i := 0; i < len(values); i += 1 {
  81. var opt = values[i]
  82. ready_values[i] = opt.Value
  83. if !(opt.HasValue) {
  84. ok = false
  85. }
  86. }
  87. if ok {
  88. return NewSyncSequence(func(next func(Object)) (bool, Object) {
  89. next(ready_values)
  90. return true, nil
  91. })
  92. } else {
  93. return NewSyncSequence(func(next func(Object)) (bool, Object) {
  94. return true, nil
  95. })
  96. }
  97. })
  98. }
  99. func KeyTrackedDynamicCombineLatestWaitReady(e Observable) Observable {
  100. return Observable { func(sched Scheduler, ob *observer) {
  101. var ctx, dispose = ob.context.create_disposable_child()
  102. var c = new_collector(ob, dispose)
  103. var running = make(map[string] disposeFunc)
  104. var values = make(map[string] Object)
  105. var indexes = make(map[string] ReactiveEntity)
  106. var keys = make([] string, 0)
  107. var first = true
  108. var emit_if_all_ready = func() {
  109. var ready_values = make([] Object, len(keys))
  110. var all_ready = true
  111. for i, k := range keys {
  112. var v, exists = values[k]
  113. if exists {
  114. ready_values[i] = v
  115. } else {
  116. all_ready = false
  117. break
  118. }
  119. }
  120. if all_ready {
  121. c.pass(ready_values)
  122. } else {
  123. // do nothing
  124. }
  125. }
  126. sched.run(e, &observer {
  127. context: ctx,
  128. next: func(obj Object) {
  129. var vec = obj.(KeyTrackedActionVector)
  130. var keys_changed = false
  131. for _, key := range keys {
  132. if !(vec.HasKey(key)) {
  133. keys_changed = true
  134. running[key](behaviour_cancel)
  135. delete(running, key)
  136. indexes[key].Emit(nil)
  137. delete(indexes, key)
  138. delete(values, key) // no-op when entry not existing
  139. }
  140. }
  141. var new_subscriptions = make([] func(), 0)
  142. var index_updates = make([] func(), 0)
  143. var i = 0
  144. vec.IterateKeys(func(key string) {
  145. var key_index = uint(i)
  146. var key_added_or_index_changed = false
  147. if i >= len(keys) || keys[i] != key {
  148. keys_changed = true
  149. key_added_or_index_changed = true
  150. }
  151. i += 1
  152. var _, is_running = running[key]
  153. if is_running && key_added_or_index_changed {
  154. var update = func() {
  155. indexes[key].commit(key_index)
  156. }
  157. index_updates = append(index_updates, update)
  158. }
  159. if !(is_running) {
  160. keys_changed = true
  161. var this_ctx, this_dispose = ctx.create_disposable_child()
  162. running[key] = this_dispose
  163. var index = CreateReactive(key_index)
  164. indexes[key] = index
  165. var index_source =
  166. DistinctWatch(index, func(a Object, b Object) bool {
  167. return a == b
  168. }).CompleteWhen(func(obj Object) bool {
  169. return obj == nil
  170. })
  171. c.new_child()
  172. var this_effect = vec.GetAction(key, index_source)
  173. var run = func() {
  174. sched.run(this_effect, &observer {
  175. context: this_ctx,
  176. next: func(obj Object) {
  177. values[key] = obj
  178. emit_if_all_ready()
  179. },
  180. error: func(err Object) {
  181. c.throw(err)
  182. },
  183. complete: func() {
  184. c.delete_child()
  185. },
  186. })
  187. }
  188. new_subscriptions = append(new_subscriptions, run)
  189. }
  190. })
  191. if keys_changed {
  192. keys = vec.CloneKeys()
  193. if len(new_subscriptions) == 0 {
  194. emit_if_all_ready()
  195. }
  196. } else {
  197. if first {
  198. c.pass(make([] Object, 0))
  199. }
  200. }
  201. first = false
  202. for _, subscribe := range new_subscriptions {
  203. // subscription should happen after `keys` updated
  204. subscribe()
  205. }
  206. for _, index_update := range index_updates {
  207. index_update()
  208. }
  209. },
  210. error: func(err Object) {
  211. c.throw(err)
  212. },
  213. complete: func() {
  214. c.parent_complete()
  215. },
  216. })
  217. } }
  218. }