This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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]; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // @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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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") | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | setTimeout(() => { | |
| console.log(1); | |
| }, 0); | |
| Promise.resolve(1).then(() => { | |
| console.log(2); | |
| }); | |
| console.log(3); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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"); | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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) { | |
| /* | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (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 :) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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); | 
NewerOlder