Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible for bridging object return some value in functions? #74

Open
bounin opened this issue Feb 26, 2017 · 6 comments
Open

Is it possible for bridging object return some value in functions? #74

bounin opened this issue Feb 26, 2017 · 6 comments

Comments

@bounin
Copy link

bounin commented Feb 26, 2017

I tried to use your framework. Everything work perfectly except all functions returning something to javascript always show result as undefined in java script. For object properties everything is OK.

For example if my object has func1 returning string and in javascript I do:
var a = bridgingobject.func1(); // I get result undefined even the func1 defined as returning String (or NSString *). But if use property is starts working:
var a = bridgingobject.prop1; // exactly doing the same I get correct result. It just defined as property in Swift or Objective-C code, for objective-c it is even using same getter.
Thanks

@bounin
Copy link
Author

bounin commented Feb 26, 2017

Cannot be that related to the fact I loading web page from local storage?

@xwv
Copy link
Contributor

xwv commented Feb 26, 2017

Please see #52.

@bounin
Copy link
Author

bounin commented Feb 26, 2017

But how is the getting the value of the property works? It is essentially just getter function internally. And you get no problem with asynchronous calling it and get result back in one step.

@shirakaba
Copy link

shirakaba commented Feb 26, 2017

I believe this is simply because when Javascript accesses a property on the bridging object, it just visits the address in memory for it and takes the value that is available at that exact moment. As you say, in this case, it works effectively like a getter.

Edit: Got confused when writing the initial reply - have removed this paragraph.

I think that invoking a Swift bridging object's method via JS should be thought of as the dispatching of an event (eg. EventTarget.dispatchEvent()) or request (eg. xmlhttprequest) rather than the running of a synchronous function. Could @xwv please clarify whether bridging object methods are similar to these two examples; and do they in fact have a return value, but only return asynchronously?

I recall that @xwv mentioned somewhere that it would theoretically be possible to block the JS environment so that it would wait synchronously for the return of the function from the Swift environment's bridging object, but that it would be very bad practice and undesirable for performance (and I agree!).

@xwv
Copy link
Contributor

xwv commented Feb 28, 2017

From javascript perspective, invoking a native function (or a setter) is sending a message. Somewhat like the behavior of event or xmlhttprequest, but totally different. The messaging interface is only available in WKWebKit framework. Script continues running while native is processing messages. To pass result back to javascript, native has to evaluate a callback function (in global scope).

Each property of bridging object has a 'cache' in JavaScript side. The getter merely returns the value in cache. XWebView tries to sync the cache with native value whenever it get changed by script or native. The syncing process is also async, so sometimes may cause problem.

@bounin
Copy link
Author

bounin commented Mar 2, 2017

Thank you, now it is clear. Property getter is only used by native part. And looking through the code you update JS counterpart property via KVO when value changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants