Version: 1.5
语言 : 中文
Call TypeScript plug-in code from C# scripts
Extend the default Tuanjie ability

Custom method Call UI thread worker from TypeScript plug-in code (.etslib)

Tuanjie provides TypeScript interface that communicate with the UI thread worker. That you can use to interact with your custom method in TypeScript plug-in code (.etslib).

Tuanjie TypeScript function Introduction

If you want to call your custom method in Tuanjie UI thread worker, you can use this function : POST_MESSAGE_TO_HOST

Import Tuanjie TypeScript function

import {POST_MESSAGE_TO_HOST} from './workers/HostProxy'

parameters in msg (ESObject):

  • modulePath:string : Import module path;
  • funcName:string : The function which you want to call, format is muduleName.functionName;
  • args:Array<ESObject> : Function params, default value is empty [ ];
  • callback:ESObject : Function callBack handler, default value is null;
  • timeout:number : If you need a synchronous call (timeout = –1 ), or if you need an asynchronous call, please fill in the timeout (ms), default value is –1;

Demo

Code of Custom TypeScript Plug-in(.etslib)

// in TestClass.etslib
import {POST_MESSAGE_TO_HOST} from './workers/HostProxy'
export class ClassObjectTest {
    Func(arg1: ESObject, arg1: ESObject, callback: (...args: Array<ESObject>) => ESObject) {
        let args:Array<ESObject> = [arg1, arg1];
        let msg:ESObject = {
          modulePath:'modulePath',
          funcName: 'moduleName.CustomFunc',
          args: args,
          callback:callback
          timeoutMs: 100000,
        }
        POST_MESSAGE_TO_HOST(msg);
    }
}
export function RegisterTestClass() {
  const register :Record<string, Object> = {};
  register["ClassObjectTest"] = ClassObjectTest;
  return register;
}

If you need a callback to Tuanjie , Make sure the return type is Array<ESObject>

Code in Custom TypeScript Module
//in CustomModule
static async CustomFunc(arg1: ESObject, arg1: ESObject):Promise<Array<ESObject>>
{
  return new Promise((resolve) => {
    //if you need a context, you can use [globalThis.Abilitycontext] or [globalThis.UIContext]
    CustomCall(arg1, arg1, globalThis.Abilitycontext).then((result:Array<ESObject>) => {
      resolve(result);
    }).catch((error: BusinessError) => {
      let errorMsg :string = JSON.stringify(error);
      let result:Array<ESObject> = [errorMsg];
      resolve(result);
    });
  });
}
Code in Custom TypeScript Module
//in CustomModule
static CustomFunc(arg1: ESObject, arg1: ESObject)
{
  let result:Array<ESObject> = CustomCall(arg1, arg1);
  return result;
}
Call TypeScript plug-in code from C# scripts
Extend the default Tuanjie ability