You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jsync_ord.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. {
  2. var dflt_opts = {
  3. // sort_fn:gen_sort_attr("override_level"),
  4. sort_fn:glob_u.fns.gen_sort_attr("override_level"),
  5. log_level:0,
  6. // ns:"",
  7. }
  8. class BoundObj{
  9. constructor(){
  10. this.init()
  11. }
  12. init(){
  13. let k,v,i,j
  14. for ([k,v] of Object.entries(Object.getOwnPropertyDescriptors(this.__proto__))){
  15. if (k.endsWith("_ub") && typeof(v.value) == "function"){
  16. // clog("UB",k,k.slice(1,-3))
  17. j = v.value.bind(this)
  18. j.meta_str = "bound_fn"
  19. this[k.slice(1,-3)] = j
  20. }
  21. }
  22. }
  23. /*
  24. mx2_ub(){
  25. clog("Mx2")
  26. }
  27. */
  28. }
  29. function resolveNow(){}
  30. async function resolveSoon(){}
  31. // class reg_cb_ord{
  32. class reg_cb_ord extends BoundObj{
  33. // con
  34. constructor(opts){
  35. super()
  36. this.opts = Object.assign({},dflt_opts,opts)
  37. this.r = Object.assign(this,dflt_opts,opts)
  38. // this.sort_fn = this.opts
  39. this.cb_sto = {}
  40. this.batch = {}
  41. this._orig_opts = opts
  42. // clog("v2 ",this)
  43. }
  44. _promise_test_ub(arg1){
  45. // clog()
  46. console.log("promise_test",{that:this,arg1})
  47. // console.log("promise_test",{that:this,opts:this.opts,arg1})
  48. }
  49. promise_test_init(){
  50. var p = new Promise((resolve,reject)=>{
  51. resolve("Promise.~")
  52. })
  53. p.then(this.promise_test)
  54. console.log("promise_test_init",{that:this,opts:this.opts})
  55. }
  56. _batch_run_ub(){
  57. this.batch.WillRun = false
  58. var batched_events = this.batch.batched_events
  59. var i,k,v
  60. for ([k,v] of Object.entries(batched_events.combinable)){
  61. this.run_cbs(v.key,v.arg_obj)
  62. }
  63. for ([k,v] of Object.entries(batched_events.unique)){
  64. this.run_cbs(v.key,v.arg_obj)
  65. }
  66. this.batch.batched_events = {
  67. combinable:{},
  68. unique:[],
  69. }
  70. }
  71. _req_batch_run_ub(key,arg_obj,combine){
  72. if (this.batch.WillRun != true) {
  73. this.batch.WillRun = true
  74. this.batch.batched_events = {
  75. combinable:{},
  76. unique:[],
  77. }
  78. new Promise((resolve,reject)=>{
  79. resolve("Promise.~")
  80. }).then(this.batch_run)
  81. }
  82. if (this.batch.WillRun == true){
  83. if (combine){
  84. this.batch.batched_events.combinable[key] = {key,arg_obj,combine}
  85. } else {
  86. this.batch.batched_events.unique.push({key,arg_obj,combine})
  87. }
  88. }
  89. if (!(this.batch.WillRun == true ||this.batch.WillRun == false )){
  90. console.error("::")
  91. throw "req_batch_run err"
  92. }
  93. }
  94. _reg_cb_ub(cb,key_name,cb_name,override_level = 100,push=1){
  95. if (typeof(cb) != "function"){
  96. console.error("callback is not a function. setting push=r","\ncb:",cb)
  97. push = "r"
  98. // console.error("callback:",cb," is not a function")
  99. }
  100. // var this.cb_sto = {}
  101. var cb_sort = nop
  102. if (!this.cb_sto[key_name]){
  103. this.cb_sto[key_name] = []
  104. }
  105. // this.cb_sto[key_name].find(find_html_cb,{fn_key:cb_name})
  106. // splice_out_ti(this.cb_sto[key_name],this.cb_sto[key_name].find(find_html_cb,{fn_key:cb_name}))
  107. // var cbo0= this.cb_sto[key_name].find(glob_td.fn.find_html_cb,{fn_key:cb_name})
  108. // var cbo0= this.cb_sto[key_name].find(find_attr,{fn_key:cb_name})
  109. var cbo0= this.cb_sto[key_name].find(glob_u.fns.find_attr,{attr:"fn_key",val:cb_name})
  110. // clog(cbo0)
  111. glob_u.fns.splice_out(this.cb_sto[key_name],cbo0)
  112. // tn2t.arr.
  113. // splice_out_cb
  114. let cb_obj = {
  115. cb,
  116. hkey:key_name,
  117. fn_key:cb_name,
  118. override_level,
  119. }
  120. if (push=="r"){
  121. } else if (push){
  122. this.cb_sto[key_name].push(cb_obj)
  123. } else {
  124. this.cb_sto[key_name].unshift(cb_obj)
  125. }
  126. this.cb_sto[key_name].sort(this.sort_fn)
  127. }
  128. _run_cbs_ub(key,arg_obj){
  129. var k,v
  130. var o = {
  131. // html_key:key,cbs:glob_mx.html_cb[key],
  132. }
  133. // dlog("RUN_HOOK_CB??",key,glob_mx.hook_cb[key])
  134. // clog("ORD:RUN",key,arg_obj)
  135. try {
  136. this.opts.log_level ? clog("run_cbs",arg_obj,o,{that:this,hkey:key,cbs:this.cb_sto[key]}) : 0
  137. var rmv_fns = []
  138. for ([k,v] of Object.entries(this.cb_sto[key]|| {}) ) {
  139. // dlog("RUN_HOOK_CB:",k)
  140. // clog("run_html_cbs:",k,v)
  141. this.opts.log_level > 1 ? clog("run_cbs_e",arg_obj,o,{that:this,hkey:key,cbs:this.cb_sto[key],k,v}) : 0
  142. v.cb(arg_obj,o,{that:this,hkey:key,cbs:this.cb_sto[key],k,v})
  143. if (v.remove_after_exe){
  144. rmv_fns.push(v)
  145. }
  146. // v(o)
  147. }
  148. for ([k,v] of Object.entries(rmv_fns|| {}) ) {
  149. glob_u.fns.splice_out(this.cb_sto[key],v)
  150. }
  151. // return {arg_obj,o,{that:this,hkey:key,cbs:this.cb_sto[key]}}
  152. return {arg_obj,o,info:{that:this,hkey:key,cbs:this.cb_sto[key],rmv_fns}}
  153. } catch(err){
  154. console.error("ti_v_cb err:",err)
  155. }
  156. }
  157. }
  158. window.glob_u.cls.reg_cb_ord = reg_cb_ord
  159. }