index.uts 1.6 KB
Newer Older
X
xty 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

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);
}

20 21 22 23 24 25 26 27 28 29 30 31
export function emitUtsMessageUTSObject(arg : UTSJSONObject) {
  uni.$emit(MessageChannel, arg);
}

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

export function emitUtsMessageNoArgument(){
   uni.$emit(MessageChannel);
}

X
xty 已提交
32 33 34 35 36
var callbackId : NSNumber | null = null
export function onJsMessage (messageChannel : string ) {
  revFromJS = ""
  if (callbackId != null) {
    uni.$off(messageChannel, callbackId!)
37
    callbackId  = null
X
xty 已提交
38 39 40
  }
  callbackId = uni.$on(messageChannel, function (...spreadArgs : Any) {
    if (spreadArgs.length > 0) {
41 42 43
      let arg0 = JSON.stringify(spreadArgs[0])
      if ( typeof arg0 == "string" ) {
        revFromJS = arg0!
X
xty 已提交
44 45 46 47 48 49 50 51 52
      }
    }
  })
}

export function onJsMessageOnce (messageChannel : string ) {
  revFromJS = ""
  uni.$once(messageChannel, function (...spreadArgs : Any) {
    if (spreadArgs.length > 0) {
53 54 55 56
      let arg0 = JSON.stringify(spreadArgs[0])
      if ( typeof arg0 == "string" ) {
        revFromJS = arg0!
      }
X
xty 已提交
57 58 59 60 61 62
    }
  })
}

export function offJsMessage(messageChannel : string) {
  if (callbackId != null) {
63 64
    uni.$off(messageChannel, callbackId!)
    callbackId = null
X
xty 已提交
65 66 67 68 69
  }
}

export function clearJsMessage() {
  revFromJS = ""
70
}