@@ -19,14 +19,15 @@ export async function runPython(code: string): Promise<string> {
1919}
2020
2121/**
22+ * Regeisters function on server and makes it available via `call.{jsFunctionName}`
2223 * @param {string } pythonFunctionCall - The python function call, can contain one dot
2324 * @param {number } [numberOfArgs] - Number of arguments, used for validation in pythons, use -1 to ignore this value
2425 * @param {string } [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
2526 */
26- export async function registerFunction ( pythonFunctionCall : string , numberOfArgs ?: number , jsFunctionName ?: string ) : Promise < string > {
27- if ( jsFunctionName === undefined ) {
28- jsFunctionName = pythonFunctionCall ;
29- }
27+ export async function registerFunction (
28+ pythonFunctionCall : string ,
29+ numberOfArgs ?: number ,
30+ jsFunctionName ?: string ) : Promise < string > {
3031 if ( numberOfArgs !== undefined && numberOfArgs < 0 ) {
3132 numberOfArgs = undefined ;
3233 }
@@ -35,29 +36,50 @@ export async function registerFunction(pythonFunctionCall: string, numberOfArgs?
3536 pythonFunctionCall,
3637 numberOfArgs
3738 } ,
38- } ) . then ( ( r :any ) => {
39- call [ jsFunctionName ] = function ( ... args : any [ ] ) { return callFunction ( pythonFunctionCall , args ) } ;
39+ } ) . then ( ( r : any ) => {
40+ registerJsOnly ( pythonFunctionCall , jsFunctionName ) ;
4041 return r . value ;
4142 } ) ;
4243}
4344
45+
46+
47+ /**
48+ * No server invokation - assumes that function has already been registered server-side
49+ * Makes function available as `call.{jsFunctionName}`
50+ * @param {string } pythonFunctionCall - The python function call, can contain one dot
51+ * @param {string } [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
52+ */
53+ export async function registerJsOnly ( pythonFunctionCall : string , jsFunctionName ?: string ) {
54+ if ( jsFunctionName === undefined ) {
55+ jsFunctionName = pythonFunctionCall . replace ( "." , "_" ) ;
56+ }
57+ call [ jsFunctionName ] = function ( ...args : any [ ] ) { return callFunction ( pythonFunctionCall , args ) } ;
58+ }
59+
60+ /**
61+ * calling previously registered function
62+ */
4463export async function callFunction ( functionName : string , args : any [ ] ) : Promise < string > {
4564 return invoke < { value : string } > ( 'plugin:python|call_function' , {
4665 payload : {
4766 functionName,
4867 args,
4968 } ,
50- } ) . then ( ( r :any ) => {
69+ } ) . then ( ( r : any ) => {
5170 return r . value ;
5271 } ) ;
5372}
5473
74+ /**
75+ * read variable name directly from python
76+ */
5577export async function readVariable ( value : string ) : Promise < string > {
5678 return invoke < { value : string } > ( 'plugin:python|read_variable' , {
5779 payload : {
5880 value,
5981 } ,
60- } ) . then ( ( r :any ) => {
82+ } ) . then ( ( r : any ) => {
6183 return r . value ;
6284 } ) ;
6385}
0 commit comments