Files
react-native/website/versioned_docs/0.45/interactionmanager.md
T
2017-10-27 11:51:58 -07:00

31 lines
6.2 KiB
Markdown

---
id: version-0.45-interactionmanager
title: interactionmanager
original_id: interactionmanager
---
<a id="content"></a><h1><a class="anchor" name="interactionmanager"></a>InteractionManager <a class="hash-link" href="docs/interactionmanager.html#interactionmanager">#</a></h1><div><div><p>InteractionManager allows long-running work to be scheduled after any
interactions/animations have completed. In particular, this allows JavaScript
animations to run smoothly.</p><p>Applications can schedule tasks to run after interactions with the following:</p><div class="prism language-javascript">InteractionManager<span class="token punctuation">.</span><span class="token function">runAfterInteractions<span class="token punctuation">(</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // ...long-running synchronous task...
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></div><p>Compare this to other scheduling alternatives:</p><ul><li>requestAnimationFrame(): for code that animates a view over time.</li><li>setImmediate/setTimeout(): run code later, note this may delay animations.</li><li>runAfterInteractions(): run code later, without delaying active animations.</li></ul><p>The touch handling system considers one or more active touches to be an
'interaction' and will delay <code>runAfterInteractions()</code> callbacks until all
touches have ended or been cancelled.</p><p>InteractionManager also allows applications to register animations by
creating an interaction 'handle' on animation start, and clearing it upon
completion:</p><div class="prism language-javascript"><span class="token keyword">var</span> handle <span class="token operator">=</span> InteractionManager<span class="token punctuation">.</span><span class="token function">createInteractionHandle<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">
// run animation... (`runAfterInteractions` tasks are queued)
</span><span class="token comment" spellcheck="true">// later, on animation completion:
</span>InteractionManager<span class="token punctuation">.</span><span class="token function">clearInteractionHandle<span class="token punctuation">(</span></span>handle<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">
// queued tasks run if all handles were cleared</span></div><p><code>runAfterInteractions</code> takes either a plain callback function, or a
<code>PromiseTask</code> object with a <code>gen</code> method that returns a <code>Promise</code>. If a
<code>PromiseTask</code> is supplied, then it is fully resolved (including asynchronous
dependencies that also schedule more tasks via <code>runAfterInteractions</code>) before
starting on the next task that might have been queued up synchronously
earlier.</p><p>By default, queued tasks are executed together in a loop in one
<code>setImmediate</code> batch. If <code>setDeadline</code> is called with a positive number, then
tasks will only be executed until the deadline (in terms of js event loop run
time) approaches, at which point execution will yield via setTimeout,
allowing events such as touches to start interactions and block queued tasks
from executing, making apps more responsive.</p></div><span><h3><a class="anchor" name="methods"></a>Methods <a class="hash-link" href="docs/interactionmanager.html#methods">#</a></h3><div class="props"><div class="prop"><h4 class="methodTitle"><a class="anchor" name="runafterinteractions"></a><span class="methodType">static </span>runAfterInteractions<span class="methodType">(task)</span> <a class="hash-link" href="docs/interactionmanager.html#runafterinteractions">#</a></h4><div><p>Schedule a function to run after all interactions have completed. Returns a cancellable
"promise".</p></div></div><div class="prop"><h4 class="methodTitle"><a class="anchor" name="createinteractionhandle"></a><span class="methodType">static </span>createInteractionHandle<span class="methodType">()</span> <a class="hash-link" href="docs/interactionmanager.html#createinteractionhandle">#</a></h4><div><p>Notify manager that an interaction has started.</p></div></div><div class="prop"><h4 class="methodTitle"><a class="anchor" name="clearinteractionhandle"></a><span class="methodType">static </span>clearInteractionHandle<span class="methodType">(handle)</span> <a class="hash-link" href="docs/interactionmanager.html#clearinteractionhandle">#</a></h4><div><p>Notify manager that an interaction has completed.</p></div></div><div class="prop"><h4 class="methodTitle"><a class="anchor" name="setdeadline"></a><span class="methodType">static </span>setDeadline<span class="methodType">(deadline)</span> <a class="hash-link" href="docs/interactionmanager.html#setdeadline">#</a></h4><div><p>A positive number will use setTimeout to schedule any tasks after the
eventLoopRunningTime hits the deadline value, otherwise all tasks will be
executed in one setImmediate batch (default).</p></div></div></div></span><span><h3><a class="anchor" name="properties"></a>Properties <a class="hash-link" href="docs/interactionmanager.html#properties">#</a></h3><div class="props"><div class="prop"><h4 class="propTitle"><a class="anchor" name="events"></a>Events<span class="propType">: CallExpression</span> <a class="hash-link" href="docs/interactionmanager.html#events">#</a></h4></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="addlistener"></a>addListener<span class="propType">: CallExpression</span> <a class="hash-link" href="docs/interactionmanager.html#addlistener">#</a></h4></div></div></span></div><p class="edit-page-block">You can <a target="_blank" href="https://github.com/facebook/react-native/blob/master/Libraries/Interaction/InteractionManager.js">edit the content above on GitHub</a> and send us a pull request!</p><div class="docs-prevnext"><a class="docs-prev" href="docs/imagestore.html#content">← Prev</a><a class="docs-next" href="docs/keyboard.html#content">Next →</a></div>