|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 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 | + * @providesModule Vibration |
| 8 | + * @flow |
| 9 | + * @jsdoc |
| 10 | + */ |
| 11 | +"use strict"; |
| 12 | + |
| 13 | +var RCTVibration = require("NativeModules").Vibration; |
| 14 | +var Platform = require("Platform"); |
| 15 | + |
| 16 | +/** |
| 17 | + * Vibration API |
| 18 | + * |
| 19 | + * See https://facebook.github.io/react-native/docs/vibration.html |
| 20 | + */ |
| 21 | + |
| 22 | +var _vibrating: boolean = false; |
| 23 | +var _id: number = 0; // _id is necessary to prevent race condition. |
| 24 | + |
| 25 | +function vibrateByPattern(pattern: Array<number>, repeat: boolean = false) { |
| 26 | + if (_vibrating) { |
| 27 | + return; |
| 28 | + } |
| 29 | + _vibrating = true; |
| 30 | + if (pattern[0] === 0) { |
| 31 | + RCTVibration.vibrate(); |
| 32 | + pattern = pattern.slice(1); |
| 33 | + } |
| 34 | + if (pattern.length === 0) { |
| 35 | + _vibrating = false; |
| 36 | + return; |
| 37 | + } |
| 38 | + setTimeout(() => vibrateScheduler(++_id, pattern, repeat, 1), pattern[0]); |
| 39 | +} |
| 40 | + |
| 41 | +function vibrateScheduler( |
| 42 | + id, |
| 43 | + pattern: Array<number>, |
| 44 | + repeat: boolean, |
| 45 | + nextIndex: number |
| 46 | +) { |
| 47 | + if (!_vibrating || id !== _id) { |
| 48 | + return; |
| 49 | + } |
| 50 | + RCTVibration.vibrate(); |
| 51 | + if (nextIndex >= pattern.length) { |
| 52 | + if (repeat) { |
| 53 | + nextIndex = 0; |
| 54 | + } else { |
| 55 | + _vibrating = false; |
| 56 | + return; |
| 57 | + } |
| 58 | + } |
| 59 | + setTimeout( |
| 60 | + () => vibrateScheduler(id, pattern, repeat, nextIndex + 1), |
| 61 | + pattern[nextIndex] |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +var Vibration = { |
| 66 | + /** |
| 67 | + * Trigger a vibration with specified `pattern`. |
| 68 | + * |
| 69 | + * See https://facebook.github.io/react-native/docs/vibration.html#vibrate |
| 70 | + */ |
| 71 | + vibrate: function( |
| 72 | + pattern: number | Array<number> = 400, |
| 73 | + repeat: boolean = false |
| 74 | + ) { |
| 75 | + if (Platform.OS === "android" || Platform.OS === "dom") { |
| 76 | + if (typeof pattern === "number") { |
| 77 | + RCTVibration.vibrate(pattern); |
| 78 | + } else if (Array.isArray(pattern)) { |
| 79 | + if (Platform.OS === "android") { |
| 80 | + RCTVibration.vibrateByPattern(pattern, repeat ? 0 : -1); |
| 81 | + } else { |
| 82 | + RCTVibration.vibrateByPattern(pattern, repeat); |
| 83 | + } |
| 84 | + } else { |
| 85 | + throw new Error("Vibration pattern should be a number or array"); |
| 86 | + } |
| 87 | + } else { |
| 88 | + if (_vibrating) { |
| 89 | + return; |
| 90 | + } |
| 91 | + if (typeof pattern === "number") { |
| 92 | + RCTVibration.vibrate(); |
| 93 | + } else if (Array.isArray(pattern)) { |
| 94 | + vibrateByPattern(pattern, repeat); |
| 95 | + } else { |
| 96 | + throw new Error("Vibration pattern should be a number or array"); |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | + /** |
| 101 | + * Stop vibration |
| 102 | + * |
| 103 | + * See https://facebook.github.io/react-native/docs/vibration.html#cancel |
| 104 | + */ |
| 105 | + cancel: function() { |
| 106 | + if (Platform.OS === "ios") { |
| 107 | + _vibrating = false; |
| 108 | + } else { |
| 109 | + RCTVibration.cancel(); |
| 110 | + } |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +module.exports = Vibration; |
0 commit comments