eventual-context.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /** Concept each channel can have child channels ??**/
  2. const chs = ':';
  3. class EventualChannel {
  4. constructor(name,parent) {
  5. this.name = name;
  6. this.parent = parent;
  7. this.callbacks = [], // Local callbacks
  8. this.childs = {}
  9. this.listeners = [];
  10. }
  11. // createChannel based on name
  12. //
  13. getChannel(name,create) { // Should return listeners to mess up
  14. var [child, ...sub]= name.split(chs);
  15. if(this.childs[child] == undefined) {
  16. if(create==undefined) return []; // Maybe empty?
  17. this.childs[child] = new EventualChannel(this);
  18. }
  19. if(sub.length != 0) { // We could do this without recursion
  20. return this.childs[child].getChannel(sub.join(chs),create);
  21. }
  22. return this.childs[child].listeners;
  23. }
  24. getChannel2(name,create) {
  25. var nodes = name.split(chs);
  26. var cur = this;
  27. nodes.forEach((v) => {
  28. if( cur.childs[v] == undefined) {
  29. if(create == undefined) return []; // Empty
  30. cur.childs[v] = new EventualChannel(name,this);
  31. }
  32. cur = cur.childs[v]; // next;
  33. });
  34. return cur.listeners;
  35. }
  36. search(name) {
  37. return this.getChannel2(name);
  38. }
  39. // Registers
  40. on(name,cb) {
  41. var listeners = this.getChannel2(name,true);
  42. listeners.push(cb);
  43. }
  44. emit(name,args) {
  45. var listeners = this.search(name);
  46. listeners.forEach((l) => {
  47. l(args);
  48. });
  49. // Something to process callback as the xevents
  50. }
  51. }
  52. class EventualContext2 extends EventualChannel {
  53. constructor(eventual) {
  54. super();
  55. this.eventual = eventual;
  56. }
  57. broadcast(name,...args) {
  58. this.eventual._events.ctx.forEach((c) => {
  59. c.emit(name,...args);
  60. });
  61. }
  62. destroy() {
  63. this.childs = null;
  64. this.listeners = null;
  65. var i = this.eventual._events.ctx.indexOf(this);
  66. if(i!=-1) this.eventual._events.ctx.splice(i,1);
  67. }
  68. }
  69. class EventualContext extends EventualChannel {
  70. constructor(eventual) {
  71. super();
  72. this.eventual = eventual;
  73. // Contextual listeners, it will invalidate regexp?
  74. this.listeners = [];
  75. }
  76. process(tocall,args,donecb) {
  77. tocall.forEach((l) => {
  78. l.callback(...args);
  79. });
  80. }
  81. broadcast(name,...args) {
  82. this.eventual._events.ctx.forEach((c) => {
  83. c.emit(name,...args);
  84. });
  85. }
  86. /** flat model **/
  87. on(name,cb) {
  88. // Any listener will be placed here
  89. this.listeners.push({
  90. name: name,
  91. callback:cb
  92. });
  93. return this;
  94. }
  95. emit(name,...args) {
  96. // We need to go trought all contexts in Eventual
  97. var tocall = [];
  98. this.eventual._events.ctx.forEach((c) => {
  99. c.listeners.forEach((l) => {
  100. if(l.name == name) {
  101. tocall.push(l);
  102. }
  103. })
  104. });
  105. this.process(tocall,name,args);
  106. }/**/
  107. destroy() {
  108. var i = this.eventual._events.ctx.indexOf(this);
  109. if(i!=-1) this.eventual._events.ctx.splice(i,1);
  110. this.listeners = null;
  111. }
  112. }
  113. module.exports = EventualContext;