diff --git a/packages/permissions/demo/index.html b/packages/permissions/demo/index.html index 4f3604d..5df5142 100644 --- a/packages/permissions/demo/index.html +++ b/packages/permissions/demo/index.html @@ -5,6 +5,8 @@
PermissionsAPI.isSupported
-// Checks if the Permissions API is supported in the current browser/environment.
+
+// Checks if the Permissions API is supported in the current browser/environment.
+
+const permissions = new PermissionsAPI()
-const support = PermissionsAPI.isSupported()
-console.log(support) // True or False
+const support = permissions.isSupported()
+console.log(support) // True or False
-
-// Exception example
+// Exception example
try {
- const support = PermissionsAPI.isSupported(true)
- console.log(support) // True
+ const support = permissions.isSupported(true)
+ console.log(support) // True
...
} catch(error) {
- console.log(error.message) // Permissions API is not supported in this browser/environment.
+ console.log(error.message) // Permissions API is not supported in this browser/environment.
}
-
+
+
PermissionsAPI.getPermission
-// Promise example ++// Promise example + +const permissions = new PermissionsAPI() -PermissionsAPI.getPermission({ name: 'geolocation' }) +permissions.getPermission({ name: 'geolocation' }) .then(({ error, permission }) => { if (error) { console.error('Error fetching permission status:', error.message) return } if (permission.state === 'denied') { - // can't use geolocation service, notify the user + // can't use geolocation service, notify the user console.log('permission:', permission.state) return } - // you can use the geolocation service here + // you can use the geolocation service here console.log('permission is granted or will prompt the user for access', permission.state) }) +-
-// Async await example ++// Async await example + +const permissions = new PermissionsAPI() async function geolocation() { - const { error, permission } = await PermissionsAPI.getPermission({ name: 'geolocation' }) + const { error, permission } = await permissions.getPermission({ name: 'geolocation' }) if (error) { console.error('Error fetching permission status:', error.message) } else if (permission.state === 'denied') { - // can't use geolocation service, notify the user + // can't use geolocation service, notify the user console.log('permission:', permission.state) } else { - // you can use the geolocation service here + // you can use the geolocation service here console.log('permission is granted or will prompt the user for access', permission.state) } } +
PermissionsAPI.getPermissionHandler
-// Callback example ++// Callback example + +const permissions = new PermissionsAPI() -const notificationHandler = PermissionsAPI.getPermissionHandler( +const notificationHandler = permissions.createHandler( { name: 'notifications' }, { granted: (permission) => { @@ -187,16 +188,19 @@-PermissionsAPI Usage Examples
} ) -// Initiate the handler by calling the handler function +// Initiate the handler by calling the handler function notificationHandler.getPermission() +
-// Event handling example ++// Event handling example -const notificationHandler = PermissionsAPI.getPermissionHandler({ name: 'notifications' }) +const permissions = new PermissionsAPI() + +const notificationHandler = permissions.createHandler({ name: 'notifications' }) notificationHandler.onPermissionChange((permission) => { - console.log('User change permission:', permission.state) // granted or denied + console.log('User change permission:', permission.state) // granted or denied }) notificationHandler.onPermissionGranted((permission) => { console.log('Permission:', permission.state) @@ -209,13 +213,16 @@+ +PermissionsAPI Usage Examples
console.error('Permission error:', error) }) -// Initiate the handler by calling the handler function +// Initiate the handler by calling the handler function notificationHandler.getPermission() ++// Callback and Event example -
-// Callback and Event example +const permissions = new PermissionsAPI() -const notificationHandler = PermissionsAPI.getPermissionHandler( +const notificationHandler = permissions.createHandler( { name: 'notifications' }, { granted: (permission) => { @@ -232,20 +239,24 @@PermissionsAPI Usage Examples
) notificationHandler.onPermissionChange((permission) => { - console.log('User change permission:', permission.state) // granted or denied + console.log('User change permission:', permission.state) // granted or denied }) -// Initiate the handler by calling the handler function +// Initiate the handler by calling the handler function notificationHandler.getPermission() +
PermissionsAPI.getAsyncPermissionHandler
-// Promise example ++// Promise example -const asyncCameraHandler = PermissionsAPI.getAsyncPermissionHandler({ name: 'camera' }) +const permissions = new PermissionsAPI() + +const asyncCameraHandler = permissions.createAsyncHandler({ name: 'camera' }) asyncCameraHandler.getPermission().then(({ error, permission }) => { if (error) { asyncCameraHandler.close() @@ -259,12 +270,16 @@+ +PermissionsAPI Usage Examples
console.log('Permission:', permission.state) } }) +-+ +
-// Async await example +// Async await example + +const permissions = new PermissionsAPI() async function camera() { - const asyncCameraHandler = PermissionsAPI.getAsyncPermissionHandler({ name: 'camera' }) + const asyncCameraHandler = permissions.createAsyncHandler({ name: 'camera' }) const { error, permission } = await asyncCameraHandler.getPermission() if (error) { @@ -276,15 +291,18 @@PermissionsAPI Usage Examples
console.log('Permission:', permission.state) } } ++// Event example -
-// Event example +const permissions = new PermissionsAPI() async function camera() { - const asyncCameraHandler = PermissionsAPI.getAsyncPermissionHandler({ name: 'camera' }) + const asyncCameraHandler = permissions.createAsyncHandler({ name: 'camera' }) asyncCameraHandler.onPermissionChange((permission) => { - console.log('User change permission:', permission.state) // granted or denied + console.log('User change permission:', permission.state) // granted or denied }) const { error, permission } = await asyncCameraHandler.getPermission() @@ -298,9 +316,11 @@PermissionsAPI Usage Examples
console.log('Permission:', permission.state) } } +