Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

share data across tab through localstorage

    import { Injectable, OnDestroy } from '@angular/core';
    import { Subject } from 'rxjs/Subject';
    import { share } from 'rxjs/operators';
    
    @Injectable()
    export class StorageService implements OnDestroy {
      private onSubject = new Subject<{ key: string, value: any }>();
      public changes = this.onSubject.asObservable().pipe(share());
    
      constructor() {
        this.start();
      }
    
      ngOnDestroy() {
        this.stop();
      }
    
      public getStorage() {
        let s = [];
        for (let i = 0; i < localStorage.length; i++) {
          s.push({
            key: localStorage.key(i),
            value: JSON.parse(localStorage.getItem(localStorage.key(i)))
          });
        }
        return s;
      }
    
      public store(key: string, data: any): void {
        localStorage.setItem(key, JSON.stringify(data));
        this.onSubject.next({ key: key, value: data})
      }
    
      public clear(key) {
        localStorage.removeItem(key);
        this.onSubject.next({ key: key, value: null });
      }
    
    
      private start(): void {
        window.addEventListener("storage", this.storageEventListener.bind(this));
      }
    
      private storageEventListener(event: StorageEvent) {
        if (event.storageArea == localStorage) {
          let v;
          try { v = JSON.parse(event.newValue); }
          catch (e) { v = event.newValue; }
          this.onSubject.next({ key: event.key, value: v });
        }
      }
    
      private stop(): void {
        window.removeEventListener("storage", this.storageEventListener.bind(this));
        this.onSubject.complete();
      }
    }
Comment

PREVIOUS NEXT
Code Example
Typescript :: props react typescript 
Typescript :: typescript interview questions 
Typescript :: gatsby typescript starter hello world 
Typescript :: typescript document.getelementbyid object is possibly null 
Typescript :: java login attempts using for loop 
Typescript :: testing in different environments 
Typescript :: typeorm generated 
Typescript :: Roblox Script wait 
Typescript :: get keys of an array angualr 
Typescript :: wergensherts meaning 
Typescript :: Checking if multiple elements are rendering using jasmine 
Typescript :: count file lines in typescript 
Typescript :: When do you choose automated testing over manual testing? 
Typescript :: file attachements contac form 7 
Typescript :: how to convert an array of other types in java 8 
Typescript :: aruments in C# 
Typescript :: how to use typescript map 
Typescript :: sum the digits in c 
Typescript :: five elements in the finger 
Typescript :: DISTINQUISH BETWEEN THE AVERAGE CASE AND WORSE CASE RUNNING TIME AND THE FACTORS AFFECTING THAT AFFECTS THE RUNNING TIME OF AN ALGORITHM 
Typescript :: what is .align mips 
Typescript :: typescript react display array 
Typescript :: typescript filter conditionally 
Typescript :: Many plants obtain glucose through the process of ---- 
Typescript :: how to use array pop in typescript 
Typescript :: how many elements can be stored in an array 
Typescript :: its getting abort when im trying to open the webcame using opencv 
Typescript :: cheapest houses in usa 
Typescript :: running same tests against different browsers 
Typescript :: dependencymanagement imports mavenbom 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =