ui.base.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { _decorator } from "cc";
  2. import { DataProxy } from "../A-LIB/lib.b.data";
  3. import { UI } from "./ui.origin";
  4. const {ccclass, property} = _decorator;
  5. @ccclass
  6. export abstract class BaseUI extends UI{
  7. protected __listen_list__: ReturnType<typeof this.listening> = [];
  8. /** 注册监听 */
  9. protected listening(): ReturnType<typeof DataProxy.follow>[] {return []};
  10. /** 取消监听 */
  11. protected closeListen(){
  12. this.__listen_list__.forEach(reg=>DataProxy.cancel(reg));
  13. mtec.array.clear(this.__listen_list__);
  14. };
  15. protected onLoad(): void {
  16. this.__ui_loaded = false;
  17. super.onLoad();
  18. this.__listen_list__ = this.listening();
  19. this.__ui_loaded = true;
  20. this.call_cache.forEach(el=>{
  21. let [func, param, np] = el;
  22. np.resolve(Reflect.apply(func, this, param));
  23. });
  24. mtec.array.clear(this.call_cache);
  25. }
  26. protected onDestroy(){
  27. this.closeListen();
  28. }
  29. private __ui_loaded: boolean;
  30. /** ui加载状态 */
  31. protected get ui_loaded(){
  32. return this.__ui_loaded;
  33. };
  34. private call_cache: [Function, any[], mtec.NudityPromise<any>][] = [];
  35. protected call_func<F extends (...param: any)=>any, R extends ReturnType<F>>(func: F, ...param: Parameters<F>): R|Promise<R>{
  36. if(this.ui_loaded) return Reflect.apply(func, this, param);
  37. else{
  38. let np = new mtec.NudityPromise<R>();
  39. this.call_cache.push([func, param, np]);
  40. return np.promise;
  41. }
  42. }
  43. private __late_call_list__: Array<[Function, any[], Function]> = [];
  44. protected lateUpdate(dt: number): void {
  45. if(this.__late_call_list__.length>0){
  46. let [call, args, complete] = this.__late_call_list__.pop();
  47. typeof complete=='function' ? complete(call(...args), args) : call(...args);
  48. }
  49. }
  50. /**
  51. * 在帧后执行回调
  52. * @param call
  53. * @param args
  54. * @param complete
  55. */
  56. protected frameLateCall<C extends (...args: any[])=>any, Param extends Parameters<C>, R extends ReturnType<C>>(call: C, args: Param, complete?: (r: R, args: Param)=>void){
  57. this.__late_call_list__.unshift([call, args, complete]);
  58. }
  59. /**
  60. * 帧循环
  61. * @param list
  62. * @param call
  63. * @returns
  64. */
  65. protected frameWhile<T extends any[]>(list: T, call: (el: T[number], index: number, arr: T)=>void){
  66. return new Promise<0>(s=>list.forEach((e, i, a)=>this.frameLateCall(call, [e, i, list], (_, [e, i, a])=>i==list.length-1 ? s(0) : void 0)));
  67. }
  68. protected follow = DataProxy.follow;
  69. protected hasFollow = DataProxy.hasFollow;
  70. protected cancel = DataProxy.cancel;
  71. protected outFollow = DataProxy.out;
  72. protected monitor = DataProxy.monitor;
  73. protected freeMonitor = DataProxy.free;
  74. protected revokeProxy = DataProxy.revoke;
  75. protected proxy = DataProxy.proxy;
  76. protected hasProxy = DataProxy.hasProxy;
  77. }