onChannelMessageReceived method

dynamic onChannelMessageReceived(
  1. String message
)

Implementation

onChannelMessageReceived(String message) async {
  Map<String, dynamic>? map;
  try {
    map = json.decode(message) as Map<String, dynamic>;
  } catch (e) {/**/}
  if (map == null) {
    debugPrint("post message unaccepted: $message");
    return;
  }
  String resFunc;
  try {
    final name = mwbConvert<String>(map["name"]);
    final handle = _handleMap[name];
    if (handle == null) {
      throw MWBException('handle $handle not found.');
    }
    MWBParams? params;
    try {
      params = mwbConvert<MWBParams>(map["params"]);
    } catch (e) {
      throw MWBException('params must be map or null.');
    }
    final data = await handle(params ?? {});
    final dataStr = data != null ? json.encode(data) : "";
    resFunc = 'resolve($dataStr)';
  } on MWBException catch (e) {
    resFunc = 'reject(${e.jsonString})';
  } catch (e) {
    final error = MWBException('unhandle error.');
    debugPrint("$e");
    resFunc = 'reject(${error.jsonString})';
  }

  final cbid = mwbConvert<String>(map["cbid"]) ?? "";
  if (cbid.isNotEmpty) {
    final js = '\$app._cbs["$cbid"].$resFunc; delete \$app._cbs["$cbid"];';
    runJs(js);
  }
}