appMsgRecvCtrl.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * 负责app之间的消息的接收模块
  3. */
  4. import { nanoid } from "nanoid";
  5. import { Controller } from "../core/controller";
  6. import { useCtx } from "../ctx";
  7. export type AssetSendedCallback = (assetUri:string)=>Promise<boolean>;
  8. export type AssetWebpackUriSendedCallback = (uri:string, name:string, thumbnail:string)=>Promise<boolean>;
  9. export type AssetType = "empty" | "image" | "webpackuri"
  10. const RevcChangeEvent = "app.recv.change"
  11. class AssetListener {
  12. id = "";
  13. type = "empty" as AssetType;
  14. constructor(public actionName:string, public callback: AssetSendedCallback){
  15. this.id = actionName;
  16. }
  17. toJson() {
  18. return {
  19. id: this.id,
  20. type: this.type,
  21. action: this.actionName,
  22. }
  23. }
  24. }
  25. export class AppMsgRecvController extends Controller {
  26. listeners = [] as AssetListener[];
  27. appGuid = "";
  28. async onReady() {
  29. const {deviceCtrl, natsCtrl} = useCtx();
  30. this.appGuid = deviceCtrl.profile.appGuid;
  31. natsCtrl.subscribe(`send.${this.appGuid}`, async (msg:{id:string,fromKey:string, uri:string, name?:string, thumbnail?:string})=>{
  32. const listen = this.listeners.find(item=>item.id == msg.id)
  33. if (!listen) return {
  34. isOk: false,
  35. error: "nolistener"
  36. }
  37. if (listen.type == "image") {
  38. const callback = listen.callback as AssetSendedCallback
  39. const ok = await callback(msg.uri);
  40. return JSON.stringify({ isOk: ok})
  41. }
  42. if (listen.type == "webpackuri" ) {
  43. const callback = listen.callback as AssetWebpackUriSendedCallback
  44. const ok = await callback(msg.uri, msg.name as string, msg.thumbnail as string);
  45. return JSON.stringify({ isOk: ok})
  46. }
  47. })
  48. natsCtrl.subscribe(`recv.actions.${this.appGuid}`, async ()=>{
  49. return JSON.stringify(this.listeners.map(item=>item.toJson()))
  50. })
  51. }
  52. //清除所有监听者
  53. clearListeners() {
  54. this.listeners = [];
  55. }
  56. emitChange() {
  57. const {natsCtrl} = useCtx();
  58. natsCtrl.publish(RevcChangeEvent, JSON.stringify({Guid: this.appGuid}))
  59. }
  60. //添加图片监听者
  61. addImageListener(actionName:string, handle: AssetSendedCallback) {
  62. let listen = this.listeners.find(item=>item.actionName == actionName);
  63. if (listen) {
  64. listen.callback = handle;
  65. return listen.id;
  66. }
  67. listen = new AssetListener(actionName, handle)
  68. listen.type = "image";
  69. this.listeners.push(listen);
  70. }
  71. //添加webpackUri听者
  72. addWebpackuriListener(actionName:string, handle: AssetWebpackUriSendedCallback) {
  73. let listen = this.listeners.find(item=>item.actionName == actionName);
  74. if (listen) {
  75. listen.callback = handle as any;
  76. return listen.id;
  77. }
  78. listen = new AssetListener(actionName, handle as any)
  79. listen.type = "webpackuri";
  80. this.listeners.push(listen);
  81. }
  82. removeListener(id:string) {
  83. let listen = this.listeners.find(item=>item.id == id);
  84. if (!listen) {
  85. return;
  86. }
  87. this.listeners.splice(this.listeners.indexOf(listen), 1);
  88. }
  89. }