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

events triggered by calls into CodeMirror should be delivered asynchronously #70

Open
skybrian opened this issue Jan 11, 2016 · 5 comments

Comments

@skybrian
Copy link

mirror.onEvent("focus", false) returns a stream of events that happens whenever CodeMirror gets focus.

Calling mirror.focus() causes an event to be delivered synchronously (in the middle of the focus call). This isn't normal behavior for a Dart stream; usually events are delivered asynchronously (such as in a microtask).

#6      _RootZone.runUnaryGuarded (dart:async/zone.dart:1087)
#7      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#8      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:270)
#9      _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:381)
#10     _BroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:256)
#11     JsEventListener.stream.<anonymous closure>.<anonymous closure> (package:codemirror/src/js_utils.dart:62:27)
#12     JsObject._callMethod (dart:js:1132)
#13     JsObject.callMethod (dart:js:1071)
#14     ProxyHolder.call (package:codemirror/codemirror.dart:1207:46)
#15     CodeMirror.focus (package:codemirror/codemirror.dart:348:19)
@devoncarew
Copy link
Contributor

@skybrian, is this something you could create a PR for?

@skybrian
Copy link
Author

Maybe, but I'm not sure about the consequences. I think a one line change at [1] to remove "sync: true" would fix this issue. However, I don't know if that would affect performance or timing in a bad way for other users of the CodeMirror package. Also, it looks like you added that line as part of a large change to upgrade to CodeMirror 5.0. Do you remember why you added it?

(It's not a big priority for me as I've already worked around it, but I thought I'd report it.)

[1] https://github.com/google/codemirror.dart/blob/master/lib/src/js_utils.dart#L70

@devoncarew
Copy link
Contributor

Unfortunately I don't remember what motivated that change. I have in the past needed to make async user events sync in order to make sure I'm the one who handles the event (and can then cancel it), or because the browser only allows some actions when responding to a user event. Not sure if either was the case here.

@skybrian
Copy link
Author

A more conservative solution might be to set a flag when entering codemirror, and create a microtask in the callback if the flag is set. If the flag isn't set then the event was triggered from outside Dart and microtask is needed.

But since I've already worked around it, I think the easiest thing for now is just to leave the bug open.

@skybrian
Copy link
Author

More subtlety to think about: If some CodeMirror events are delivered via a microtask and others are not, they would get delivered out of order.

Also, in a regular HTML event handler, events are delivered synchronously, allowing you to call event.preventDefault(). I don't know if it's possible to handle an HTML event that is also delivered to CodeMirror, but if so, a microtask would change the order.

An interesting article about this:
https://plus.google.com/u/0/+MalteUbl/posts/NLyinvqwZHo

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

2 participants