rx.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package rx
  2. type Object = interface{}
  3. type Observable struct {
  4. effect func(Scheduler, *observer)
  5. }
  6. type Scheduler interface {
  7. dispatch(event)
  8. commit(task)
  9. run(Observable, *observer)
  10. }
  11. type observer struct {
  12. context *Context
  13. next func(Object)
  14. error func(Object)
  15. complete func()
  16. }
  17. type Context struct {
  18. children map[*Context] struct{}
  19. disposed bool
  20. cancel chan struct{}
  21. terminate chan struct{}
  22. hooks [] func()
  23. }
  24. type disposeFunc func(disposeBehaviour)
  25. type disposeBehaviour int
  26. const (
  27. behaviour_cancel disposeBehaviour = iota
  28. behaviour_terminate
  29. )
  30. var background = &Context {
  31. children: make(map[*Context] struct{}),
  32. disposed: false,
  33. cancel: nil,
  34. terminate: nil,
  35. }
  36. func Background() *Context {
  37. return background
  38. }
  39. func (ctx *Context) disposable() bool {
  40. return (ctx != background)
  41. }
  42. func (ctx *Context) dispose_recursively(behaviour disposeBehaviour) {
  43. if !(ctx.disposable()) { panic("something went wrong") }
  44. if !(ctx.disposed) {
  45. ctx.disposed = true
  46. for child, _ := range ctx.children {
  47. child.dispose_recursively(behaviour)
  48. }
  49. switch behaviour {
  50. case behaviour_cancel:
  51. close(ctx.cancel)
  52. case behaviour_terminate:
  53. close(ctx.terminate)
  54. default:
  55. panic("impossible branch")
  56. }
  57. }
  58. }
  59. func (ctx *Context) create_disposable_child() (*Context, disposeFunc) {
  60. var child = &Context {
  61. children: make(map[*Context] struct{}),
  62. disposed: false,
  63. cancel: make(chan struct{}),
  64. terminate: make(chan struct{}),
  65. }
  66. if ctx.disposable() && ctx.disposed {
  67. child.dispose_recursively(behaviour_cancel)
  68. return child, func(disposeBehaviour) {}
  69. }
  70. ctx.children[child] = struct{}{}
  71. return child, func(behaviour disposeBehaviour) {
  72. if !(child.disposed) {
  73. delete(ctx.children, child)
  74. child.dispose_recursively(behaviour)
  75. for len(child.hooks) > 0 {
  76. var l = len(child.hooks)
  77. if behaviour == behaviour_cancel {
  78. child.hooks[l-1]()
  79. }
  80. child.hooks[l-1] = nil
  81. child.hooks = child.hooks[:l-1]
  82. }
  83. }
  84. }
  85. }
  86. func (ctx *Context) CancelSignal() (<- chan struct{}) {
  87. if ctx.disposable() {
  88. return ctx.cancel
  89. } else {
  90. return nil
  91. }
  92. }
  93. func (ctx *Context) AlreadyCancelled() bool {
  94. if ctx.disposable() {
  95. select {
  96. case <- ctx.cancel:
  97. return true
  98. default:
  99. return false
  100. }
  101. } else {
  102. return false
  103. }
  104. }
  105. func (ctx *Context) WaitDispose(handleCancel func()) {
  106. if ctx.disposable() {
  107. select {
  108. case <- ctx.cancel:
  109. handleCancel()
  110. case <- ctx.terminate:
  111. // do nothing
  112. }
  113. }
  114. }
  115. func (ctx *Context) push_cancel_hook(h func()) {
  116. // TODO: revise this method
  117. if !(ctx.disposable()) { return }
  118. if !(ctx.disposed) {
  119. ctx.hooks = append(ctx.hooks, h)
  120. }
  121. }
  122. type Sender struct {
  123. ob *observer
  124. sched Scheduler
  125. }
  126. type Receiver struct {
  127. Context *Context
  128. Values chan <- Object
  129. Error chan <- Object
  130. Terminate chan <- bool
  131. }
  132. func (s Sender) Context() *Context {
  133. return s.ob.context
  134. }
  135. func (s Sender) Scheduler() Scheduler {
  136. return s.sched
  137. }
  138. func (s Sender) Next(x Object) {
  139. s.sched.dispatch(event {
  140. kind: ev_next,
  141. payload: x,
  142. observer: s.ob,
  143. })
  144. }
  145. func (s Sender) Error(e Object) {
  146. s.sched.dispatch(event {
  147. kind: ev_error,
  148. payload: e,
  149. observer: s.ob,
  150. })
  151. }
  152. func (s Sender) Complete() {
  153. s.sched.dispatch(event {
  154. kind: ev_complete,
  155. payload: nil,
  156. observer: s.ob,
  157. })
  158. }
  159. func Schedule(action Observable, sched Scheduler, r Receiver) {
  160. sched.commit(func() {
  161. sched.run(action, &observer {
  162. context: r.Context,
  163. next: func(x Object) {
  164. if r.Values != nil {
  165. r.Values <- x
  166. }
  167. },
  168. error: func(e Object) {
  169. if r.Error != nil {
  170. r.Error <- e
  171. close(r.Error)
  172. }
  173. if r.Terminate != nil {
  174. r.Terminate <- false
  175. }
  176. },
  177. complete: func() {
  178. if r.Values != nil {
  179. close(r.Values)
  180. }
  181. if r.Terminate != nil {
  182. r.Terminate <- true
  183. }
  184. },
  185. })
  186. })
  187. }
  188. func ScheduleBackground(action Observable, sched Scheduler) {
  189. Schedule(action, sched, Receiver {
  190. Context: Background(),
  191. })
  192. }
  193. func ScheduleBackgroundWaitTerminate(action Observable, sched Scheduler) bool {
  194. var wait = make(chan bool)
  195. Schedule(action, sched, Receiver {
  196. Context: Background(),
  197. Terminate: wait,
  198. })
  199. return <- wait
  200. }
  201. func Noop() Observable {
  202. return Observable { func(sched Scheduler, ob *observer) {
  203. ob.complete()
  204. } }
  205. }
  206. func NewGoroutine(action func(Sender)) Observable {
  207. return Observable { func(sched Scheduler, ob *observer) {
  208. go action(Sender { sched: sched, ob: ob })
  209. } }
  210. }
  211. func NewGoroutineSingle(action func(ctx *Context)(Object,bool)) Observable {
  212. return Observable { func(sched Scheduler, ob *observer) {
  213. var sender = Sender { sched: sched, ob: ob }
  214. go (func() {
  215. var result, ok = action(sender.Context())
  216. if ok {
  217. sender.Next(result)
  218. sender.Complete()
  219. } else {
  220. sender.Error(result)
  221. }
  222. })()
  223. }}
  224. }
  225. func NewQueued(w *Worker, action func()(Object,bool)) Observable {
  226. return Observable { func(sched Scheduler, ob *observer) {
  227. var sender = Sender { sched: sched, ob: ob }
  228. w.Do(func() {
  229. var result, ok = action()
  230. if ok {
  231. sender.Next(result)
  232. sender.Complete()
  233. } else {
  234. sender.Error(result)
  235. }
  236. })
  237. } }
  238. }
  239. func NewQueuedNoValue(w *Worker, action func()(bool,Object)) Observable {
  240. return Observable { func(sched Scheduler, ob *observer) {
  241. var sender = Sender { sched: sched, ob: ob }
  242. w.Do(func() {
  243. var ok, err = action()
  244. if ok {
  245. sender.Complete()
  246. } else {
  247. sender.Error(err)
  248. }
  249. })
  250. } }
  251. }
  252. func NewCallback(action func(func(Object))) Observable {
  253. return Observable { func(sched Scheduler, ob *observer) {
  254. var sender = Sender { sched: sched, ob: ob }
  255. action(func(value Object) {
  256. sender.Next(value)
  257. sender.Complete()
  258. })
  259. }}
  260. }
  261. func NewSubscription(action func(func(Object))(func())) Observable {
  262. return Observable { func(sched Scheduler, ob *observer) {
  263. var h = action(ob.next)
  264. if h != nil {
  265. ob.context.push_cancel_hook(h)
  266. }
  267. } }
  268. }
  269. func NewSubscriptionWithSender(action func(Sender)(func())) Observable {
  270. return Observable { func(sched Scheduler, ob *observer) {
  271. var h = action(Sender { sched: sched, ob: ob })
  272. if h != nil {
  273. ob.context.push_cancel_hook(h)
  274. }
  275. } }
  276. }
  277. func NewSync(action func()(Object,bool)) Observable {
  278. return Observable { func(sched Scheduler, ob *observer) {
  279. var result, ok = action()
  280. if ok {
  281. ob.next(result)
  282. ob.complete()
  283. } else {
  284. ob.error(result)
  285. }
  286. } }
  287. }
  288. func NewSyncSequence(action func(func(Object))(bool,Object)) Observable {
  289. return Observable { func(sched Scheduler, ob *observer) {
  290. var ok, err = action(ob.next)
  291. if ok {
  292. ob.complete()
  293. } else {
  294. ob.error(err)
  295. }
  296. } }
  297. }
  298. func NewSyncWithSender(action func(Sender)) Observable {
  299. return Observable { func(sched Scheduler, ob *observer) {
  300. action(Sender { sched: sched, ob: ob })
  301. } }
  302. }
  303. func NewConstant(values... Object) Observable {
  304. return Observable { func(sched Scheduler, ob *observer) {
  305. for _, value := range values {
  306. ob.next(value)
  307. }
  308. ob.complete()
  309. } }
  310. }