Skip to content

Instantly share code, notes, and snippets.

@ranwahle
ranwahle / can-go-on-implementation.js
Created June 9, 2019 19:06
canGoOn implementation
async canGoOn(routeData, guard) {
if (guard) {
return await guard(routeData)
}
return true;
}
@ranwahle
ranwahle / navigatge-function-with-guard.js
Created June 9, 2019 19:00
Navigate function with guard
async navigate(url) {
try {
url = url === '/' ? url : new URL(url).pathname;
} catch (err) {
throw Error(`Cannot construct url from ${url}`)
}
this.currentSnapshot = this.routingSnapshotTreeBuilder.buildRouteTree(url);
@ranwahle
ranwahle / router-configuration-with-guard.js
Created June 9, 2019 18:56
Router configuration with guard
{
path: 'image/:index',
element: 'div',
attributes: {is: 'detailed-image'},
guard: imageExistGuard
}
@ranwahle
ranwahle / is-image-exist-guard.js
Last active June 9, 2019 18:52
Is image exists guard
const imageExistGuard = async path => {
if (!path) {
console.error('no path')
return false
}
const imageId = path.split('/')[2]
if (!imageId) {
return false
const imageExistGuard = async path => {
if (!path) {
console.error('no path')
return false
}
const imageId = path.split('/')[2]
if (!imageId) {
return false
@ranwahle
ranwahle / handle-permission-change.js
Created May 30, 2019 11:58
Handle permission change
async function letMeNotifyYouAndPleaseDontRevoke() {
// Get the permission status
const status = await navigator.permissions.request({name: 'notifications'});
//Handle the change
status.onchange = changeEvent => {
// Although we have the status on the outer scope, we will use it as the target
const newState = changeEvent.target.state;
// Show alert if the permission is not granted
@ranwahle
ranwahle / revoke-a-permission.js
Created May 30, 2019 11:47
Revoke a permission
async function stopNotifying() {
return await navigator.permissions.revoke({name: 'notifications'})
}
// Will return a promise of an update PermissionStatus,
// while its state will be 'prompt' if it has been previously granted.
@ranwahle
ranwahle / request-all-code.js
Created May 30, 2019 11:31
RequestAll Code
async function pleaseLetMeUseBothNotificationsAndCamera() {
return await navigator.permissions.requestAll([{name: 'camera'},
{name: 'notifications'}]).
}
// This will return a promise of an array of two permission statuses
@ranwahle
ranwahle / requesting-a-permission.js
Created May 30, 2019 11:17
Requesting a permission
async function pleaseLetMeNotify() {
return await navigator.permissions.request({name: 'notifications'});
}
// Retuns a promise of a PermissionStatusObject
@ranwahle
ranwahle / query-notification-permission.js
Created May 30, 2019 11:03
Query Notification Permission
async function mayINotify() {
return await navigator.permission.query({name: 'notifications'});
}
// This will return a promise of PermssionStatus object