LynxMainThreadEvent
A directive that binds main-thread scripts to an element's native events, and passes them elements they can change directly. import { LynxMainThreadEvent } from '@blotch/angular-lynx';
Template handlers such as (bindtap) already run on the main thread. Main-thread scripts explains when you need this API instead.
Usage
API
Inputs
Each input takes a handle from mainThreadFn(), or a function with the 'main thread' directive.
- Any other function works too, such as a class method. The directive registers it on the main thread, and releases it when the input changes or the element is destroyed.
- A method passed this way has no
this. Use an arrow function property if the handler needs one. - Set an input to
nullorundefinedto remove its handler.
Bind inputs let the event bubble to ancestors. Catch inputs stop it at this element.
In the handler, event.target and event.currentTarget are MainThread.Element objects. When the element is destroyed, its handlers are removed.
Constraints
- The web preview fires mouse events, not touch events. A desktop browser never fires touch events, so bind the
mouse*inputs too for a drag that works there. Phones fire touch events, plus amousedownandmouseupafter a tap. - A handler doesn't run change detection. Change the element directly, through
event.currentTarget. - Each input takes one handler. Binding a new one replaces the old one, and
nullremoves it. - A script runs beside the element's other listeners for its event. A
(bindtap)next to a[mainThreadBindtap]runs too, in the order Angular added them. So does a listener added later, such as anIntersectionObserver's. - A
Catchinput stops the event at the element after all of them have run.
Types
MainThread is a namespace of the types from @lynx-js/types/main-thread, with getBoundingClientRect() added to MainThread.Element:
setStyleProperty()andsetAttribute()batch their changes, and Lynx applies them together after your handler returns.- On an element of your templates,
setAttribute()is a template attribute's equal: adata-*goes into the element's dataset, anid,classorstyledoes what it does in a template, an ARIA attribute gives VoiceOver and TalkBack theirs, and an attribute selector in your CSS matches it. The element keeps it when it's taken out and put back, as projected content in a toggled@ifis.nullremoves it. A wholestylesits under the styles set one at a time ([style.x]bindings andsetStyleProperty()), as a template'sstyle="…"does, so change one of those withsetStyleProperty(). - That includes the elements the build creates without Angular, the plain
<view>,<text>and<image>elements of a subtree with no binding, listener, local reference or directive anywhere in it, whichevent.targetandquerySelector()can reach: the attributes and styles a script sets on one are kept as on any other. invoke()calls one of the element's UI methods, such asscrollToon a<scroll-view>, and resolves with its result.getBoundingClientRect()resolves with the element's position and size, measured from the top-left corner of the LynxView. PassrelativeTo: 'screen', or another element'sid, to measure from there instead. Unlike the DOM's, it returns a promise: Lynx gives a script the layout position only through theboundingClientRectUI method, which this calls. Measure once when a gesture starts rather than on every move.
