InteractionMixin.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const InteractionManager = require('./InteractionManager');
  12. import {type Handle} from './InteractionManager';
  13. /**
  14. * This mixin provides safe versions of InteractionManager start/end methods
  15. * that ensures `clearInteractionHandle` is always called
  16. * once per start, even if the component is unmounted.
  17. */
  18. const InteractionMixin = {
  19. componentWillUnmount: function() {
  20. while (this._interactionMixinHandles.length) {
  21. InteractionManager.clearInteractionHandle(
  22. this._interactionMixinHandles.pop(),
  23. );
  24. }
  25. },
  26. _interactionMixinHandles: ([]: Array<number>),
  27. createInteractionHandle: function(): Handle {
  28. const handle = InteractionManager.createInteractionHandle();
  29. this._interactionMixinHandles.push(handle);
  30. return handle;
  31. },
  32. clearInteractionHandle: function(clearHandle: number): void {
  33. InteractionManager.clearInteractionHandle(clearHandle);
  34. this._interactionMixinHandles = this._interactionMixinHandles.filter(
  35. handle => handle !== clearHandle,
  36. );
  37. },
  38. /**
  39. * Schedule work for after all interactions have completed.
  40. *
  41. * @param {function} callback
  42. */
  43. runAfterInteractions: function(callback: Function): void {
  44. InteractionManager.runAfterInteractions(callback);
  45. },
  46. };
  47. module.exports = InteractionMixin;