index.uts 1.3 KB
Newer Older
X
xty 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

import { UniUTSJS } from 'DCloudUniappRuntime'
import { NSNumber } from 'Foundation';

let MessageChannel : string = "UtsMessage"
var revFromJS = ""

export function getMessageChannel() : string {
  return MessageChannel
}

export function getRevJsMessage() : string {
  return revFromJS
}

export function emitUtsMessage(arg : string) {
  uni.$emit(MessageChannel, arg);
}

var callbackId : NSNumber | null = null
export function onJsMessage (messageChannel : string ) {
  revFromJS = ""
  if (callbackId != null) {
    uni.$off(messageChannel, callbackId!)
    callbackId = null
  }

  callbackId = uni.$on(messageChannel, function (...spreadArgs : Any) {
    if (spreadArgs.length > 0) {
      let arg0 = spreadArgs[0] as String | null
      if ( arg0 != null ){
        revFromJS = arg0!;
      }
    }
  })
}

export function onJsMessageOnce (messageChannel : string ) {
  revFromJS = ""
  uni.$once(messageChannel, function (...spreadArgs : Any) {
    if (spreadArgs.length > 0) {
      let arg0 = spreadArgs[0] as String | null
      if ( arg0 != null ){
        revFromJS = arg0!;
      }
    }
  })
}

export function offJsMessage(messageChannel : string) {
  if (callbackId != null) {
    uni.$off(messageChannel, callbackId!)
  }
}

export function clearJsMessage() {
  revFromJS = ""
}