forked from NeoLSN/cordova-plugin-gyroscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceGyroscope.js
54 lines (42 loc) · 1.33 KB
/
deviceGyroscope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
angular
.module('deviceGyroscope', [])
.factory('$deviceGyroscope', ['$q', function($q) {
return {
getCurrent: function() {
var q = $q.defer();
if (angular.isUndefined(navigator.gyroscope) ||
!angular.isFunction(navigator.gyroscope.getCurrent)) {
q.reject('Device do not support watch');
}
navigator.gyroscope.getCurrent(function(result) {
q.resolve(result);
}, function(err) {
q.reject(err);
});
return q.promise;
},
watch: function(options) {
var q = $q.defer();
if (angular.isUndefined(navigator.gyroscope) ||
!angular.isFunction(navigator.gyroscope.watch)) {
q.reject('Device do not support watchGyroscope');
}
var watchID = navigator.gyroscope.watch(function(result) {
q.notify(result);
}, function(err) {
q.reject(err);
}, options);
q.promise.cancel = function() {
navigator.gyroscope.clearWatch(watchID);
};
q.promise.clearWatch = function(id) {
navigator.gyroscope.clearWatch(id || watchID);
};
q.promise.watchID = watchID;
return q.promise;
},
clearWatch: function(watchID) {
return navigator.gyroscope.clearWatch(watchID);
}
};
}]);