main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | import { useEffect, useState } from 'react';
|
---|
2 | import { noop, off, on } from './misc/util';
|
---|
3 | // const usePermission = <T extends PermissionDescriptor>(permissionDesc: T): IState => {
|
---|
4 | var usePermission = function (permissionDesc) {
|
---|
5 | var _a = useState(''), state = _a[0], setState = _a[1];
|
---|
6 | useEffect(function () {
|
---|
7 | var mounted = true;
|
---|
8 | var permissionStatus = null;
|
---|
9 | var onChange = function () {
|
---|
10 | if (!mounted) {
|
---|
11 | return;
|
---|
12 | }
|
---|
13 | setState(function () { var _a; return (_a = permissionStatus === null || permissionStatus === void 0 ? void 0 : permissionStatus.state) !== null && _a !== void 0 ? _a : ''; });
|
---|
14 | };
|
---|
15 | navigator.permissions
|
---|
16 | .query(permissionDesc)
|
---|
17 | .then(function (status) {
|
---|
18 | permissionStatus = status;
|
---|
19 | on(permissionStatus, 'change', onChange);
|
---|
20 | onChange();
|
---|
21 | })
|
---|
22 | .catch(noop);
|
---|
23 | return function () {
|
---|
24 | permissionStatus && off(permissionStatus, 'change', onChange);
|
---|
25 | mounted = false;
|
---|
26 | permissionStatus = null;
|
---|
27 | };
|
---|
28 | }, [permissionDesc]);
|
---|
29 | return state;
|
---|
30 | };
|
---|
31 | export default usePermission;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.