123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /**
- * 负责app之间的消息的接收模块
- */
- import { nanoid } from "nanoid";
- import { Controller } from "../core/controller";
- import { useCtx } from "../ctx";
- export type AssetSendedCallback = (assetUri:string)=>Promise<boolean>;
- export type AssetWebpackUriSendedCallback = (uri:string, name:string, thumbnail:string)=>Promise<boolean>;
- export type AssetType = "empty" | "image" | "webpackuri"
- const RevcChangeEvent = "app.recv.change"
- class AssetListener {
- id = "";
- type = "empty" as AssetType;
- constructor(public actionName:string, public callback: AssetSendedCallback){
- this.id = actionName;
- }
-
- toJson() {
- return {
- id: this.id,
- type: this.type,
- action: this.actionName,
- }
- }
- }
- export class AppMsgRecvController extends Controller {
- listeners = [] as AssetListener[];
- appGuid = "";
- async onReady() {
- const {deviceCtrl, natsCtrl} = useCtx();
- this.appGuid = deviceCtrl.profile.appGuid;
- natsCtrl.subscribe(`send.${this.appGuid}`, async (msg:{id:string,fromKey:string, uri:string, name?:string, thumbnail?:string})=>{
- const listen = this.listeners.find(item=>item.id == msg.id)
- if (!listen) return {
- isOk: false,
- error: "nolistener"
- }
- if (listen.type == "image") {
- const callback = listen.callback as AssetSendedCallback
- const ok = await callback(msg.uri);
- return JSON.stringify({ isOk: ok})
- }
- if (listen.type == "webpackuri" ) {
- const callback = listen.callback as AssetWebpackUriSendedCallback
- const ok = await callback(msg.uri, msg.name as string, msg.thumbnail as string);
- return JSON.stringify({ isOk: ok})
- }
- })
- natsCtrl.subscribe(`recv.actions.${this.appGuid}`, async ()=>{
- return JSON.stringify(this.listeners.map(item=>item.toJson()))
- })
- }
- //清除所有监听者
- clearListeners() {
- this.listeners = [];
- }
- emitChange() {
- const {natsCtrl} = useCtx();
- natsCtrl.publish(RevcChangeEvent, JSON.stringify({Guid: this.appGuid}))
- }
- //添加图片监听者
- addImageListener(actionName:string, handle: AssetSendedCallback) {
- let listen = this.listeners.find(item=>item.actionName == actionName);
- if (listen) {
- listen.callback = handle;
- return listen.id;
- }
- listen = new AssetListener(actionName, handle)
- listen.type = "image";
- this.listeners.push(listen);
- }
- //添加webpackUri听者
- addWebpackuriListener(actionName:string, handle: AssetWebpackUriSendedCallback) {
- let listen = this.listeners.find(item=>item.actionName == actionName);
- if (listen) {
- listen.callback = handle as any;
- return listen.id;
- }
- listen = new AssetListener(actionName, handle as any)
- listen.type = "webpackuri";
- this.listeners.push(listen);
- }
-
- removeListener(id:string) {
- let listen = this.listeners.find(item=>item.id == id);
- if (!listen) {
- return;
- }
- this.listeners.splice(this.listeners.indexOf(listen), 1);
- }
- }
|