0

quick help if you can. Trying to implement calls to third party native function that accepts an object that has to implement a certain interface with a callback function. How exactly would you create an object like that on the Nativescript side and pass it to that native function?

Java example:

public class Logger {
    public static void setListener(LogListener listener) {
        // native code
    }
}

public interface LogListener {
    void onMessageLogged(LogMessage message);
}
nunob
  • 592
  • 7
  • 24

1 Answers1

1

So you want to pass in a LogListener to setListener?

In TypeScript that would be:

const myLogListener = new change.packagename.LogListener({
   onMessageLogged: message => {
     console.log(message);
   }
});

logger.setListener(myLogListener);
Eddy Verbruggen
  • 3,550
  • 1
  • 15
  • 27
  • Thanks for the reply but that doesn't seem to work. LogListener is declared as an interface in the Java library and not as a class so I get an error when trying to instantiate using "new" keyword. So the real question here is how can I create a class on Typescript and pass it to the Java library so that it accepts it as an implementation of the LogListener interface. – nunob Nov 20 '17 at 13:24
  • That's strange.. take a look at this example of a Listener interface: https://github.com/EddyVerbruggen/nativescript-mapbox/blob/60845febd4dee237fe5f20ef54c52025a413f387/src/mapbox.android.ts#L264-L274 – Eddy Verbruggen Nov 20 '17 at 13:35
  • After rechecking I saw a typo in the way the class was being created. I'll set this answer as correct. thanks. – nunob Nov 20 '17 at 14:04