Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

across tab 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 :: typescript 
Typescript :: typescript cheatsheet 
Typescript :: display moment in format dd/mm/yy only last two digits of year 
Typescript :: import luno pricing to google sheets api 
Typescript :: ncbi datasets command-line tool 
Typescript :: disable srr svelteKit 
Typescript :: s3.bucket objects filter top 10 
Typescript :: Add two (2) statements to display the data of the two (2) Car objects 
Typescript :: addObjects giving a fatal error when pushing data to algolia 
Typescript :: migrate to typescript 
Typescript :: uTorrent Default Download Folder - Linux 
Typescript :: Paint effects will render natively in maya software and maya hardware 2.0 render. Command will enable paint effects to render in Arnold or ay third-party render: 
Typescript :: beyondcode/laravel-websockets 1.12.0 requires pusher/pusher-php-server ^3.0|^4.0|^5.0 - found pusher/pusher-php-server[dev-master 
Typescript :: avoid hitting multiple same api hits angular 
Typescript :: Destructuring props in styled-components 
Typescript :: Could not resolve all artifacts for configuration 
Typescript :: studying for a sceince test 
Typescript :: What is the reason we are using properties file 
Typescript :: angular JSON.parse (<anonymous) 
Typescript :: ts push in array 
Typescript :: No query results for model 
Typescript :: vba check if two sheets are the same 
Typescript :: hhow to remove elements from java 
Typescript :: summary of investigation in contemporary world 
Typescript :: reverse a string if its value its greater than 3 
Typescript :: vscode Some Rust components not installed. Install? 
Typescript :: vector with N equal entries R 
Typescript :: typescript isvalidguid 
Typescript :: how to add lists haskell 
Typescript :: reports for market research 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =