robert_muench

override subscribe

Oct 21st, 2019
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env dub
  2. /+ dub.sdl:
  3.     name "rx_filter_subject"
  4.   dependency "rx" version="*"
  5.   dependency "rtree" version="*"
  6. +/
  7.  
  8. import std.stdio : writeln;
  9. import rx;
  10. import rtree;
  11.  
  12. struct message {
  13.   int x;
  14.   int y;
  15. }
  16.  
  17. static class myWidget : Observer!message {
  18.   this(int x0, int y0, int x1, int y1){
  19.     x[0] = x0;
  20.     x[1] = x1;
  21.  
  22.     y[0] = y0;
  23.     y[1] = y1;
  24.   }
  25.  
  26.   void put(message obj) {hit();}
  27.   void completed() {}
  28.   void failure(Exception ex) {}
  29.  
  30.   void hit(){writeln("Hit!");}
  31.  
  32.   int[2] x;
  33.   int[2] y;
  34. }
  35.  
  36. alias SI = RTree!(long, int, 2, float); // datatype, index element type, dimensions, fractional index element type
  37.  
  38. static class FilterSubject : SubjectObject!message {
  39.  
  40.   SI spitialIndex;
  41.  
  42.   this(){
  43.     spitialIndex = new SI();
  44.   }
  45.  
  46.   // alias subscribe = typeof(super).subscribe;
  47.   Disposable subscribe(myWidget observer){
  48.     spitialIndex.insert(observer.x, observer.y, cast(long)cast(void*)observer);
  49.  
  50.     return NopDisposable.instance;
  51.   }
  52. }
  53.  
  54. void main() {
  55.   auto rx_message = new FilterSubject;
  56.  
  57.   message a;
  58.   a.x = 1;
  59.   a.y = 1;
  60.  
  61.   // nothing happens, nothing subscribed
  62.   rx_message.put(a);
  63.  
  64.   // nothing happens, subscribe is a noop
  65.   auto widget = new myWidget(1,1,3,3);
  66.   rx_message.subscribe(widget);
  67.   rx_message.put(a);
  68. }
RAW Paste Data