Skip to content

Instantly share code, notes, and snippets.

View gauravbehere's full-sized avatar
🥁

Gaurav gauravbehere

🥁
View GitHub Profile
// Create a channel for communication
const channel = new BroadcastChannel('TOKEN_EXCHANGE');
const getAToken = () => {
const promise = new Promise((resolve, reject) => {
// Listen to token response
channel.onmessage = (e) => {
resolve(e.data);
};
// Send a token request to the main thread
@gauravbehere
gauravbehere / taking-to-the-main-app.js
Last active May 24, 2021 13:17
Getting token from the main app.
// Create a channel for communication
const channel = new BroadcastChannel('TOKEN_EXCHANGE');
const getAToken = () => {
const promise = new Promise((resolve, reject) => {
// Listen to token response
channel.onmessage = (e) => {
resolve(e.data);
};
// Send a token request to the main thread
@gauravbehere
gauravbehere / service-worker-dummy-auth.js
Created May 24, 2021 12:13
Capturing network requests and responding with a new request container a dummy auth token
self.addEventListener('fetch', (event) => {
const token = "some dummy token"; // This needs to be requested from MSAL library
// Responding with a custom promise
const promise = new Promise((resolve, reject) => {
// edit event.request & respond with a fetch of a new request with new headers
let sourceHeaders = {};
for (var pair of event.request.headers.entries()) {
sourceHeaders[pair[0]] = pair[1];
}
@gauravbehere
gauravbehere / arraySortWithObjectKey.js
Created January 5, 2021 07:38
array sort with object key
// @param order(number) = 1 -> ascending
// @param order(number) = 2 -> descending
// @param objectSort(boolean)
const sortArray = (array, objectSort, key, order=1) => {
if(objectSort) {
if(order === 1)
return array.sort((a,b) => a[key] - b[key]);
else if(order === 2)
return array.sort((a,b) => b[key] - a[key]);
else
@gauravbehere
gauravbehere / arraySortWithOrder.js
Last active January 5, 2021 07:38
array sort with order
// order = 1 -> ascending
// order = 2 -> descending
const sortArray = (array, order=1) => {
if(order === 1)
return array.sort();
else if(order === 2)
return array.sort((a,b) => b - a);
else
console.error("Unsupported sort order provided")
}
setTimeout(() => {
console.log(1);
}, 0);
Promise.resolve(1).then(() => {
console.log(2);
});
console.log(3);
self.addEventListener('sync', function(event) {
console.info('Event: Sync', event);
/**
* Add logic to send requests to backend when sync happens
*/
self.registration.showNotification("Syncing Now");
});
function registerSync() {
new Promise(function (resolve, reject) {
Notification.requestPermission(function (result) {
if (result !== 'granted') return reject(Error("Denied notification permission"));
resolve();
})
}).then(function () {
return navigator.serviceWorker.ready;
}).then(function (reg) {
/*
(function () {
if (!("Notification" in window)) {
alert("Browser does not support notifications");
}
else if (Notification.permission === "granted") {
navigator.serviceWorker.ready
.then(function (registration) {
/**
* Notifying the user with a sample notification
* Can be a greeting :)
@gauravbehere
gauravbehere / firebase-config.js
Created February 1, 2020 06:42
Web Push Firebase Config
// Setup firebase config
var config = {
apiKey: "<Your API KEY>",
authDomain: "<FIREBASE PROJECT NAME>.firebaseapp.com",
databaseURL: "https://<FIREBASE PROJECT NAME>.firebaseio.com",
projectId: "<FIREBASE PROJECT NAME>",
storageBucket: "topics/push_notifications",
messagingSenderId: "<YOUR SENDER ID>"
};
firebase.initializeApp(config);