@@ -2,22 +2,36 @@ import { Injectable } from '@angular/core'
22import { Subject } from 'rxjs'
33import { ICookieService , StringDict } from './common'
44import { CookieAttributes , getJSON , remove , set } from 'js-cookie'
5+ import { filter } from 'rxjs/operators'
6+
7+ export interface KeyValue {
8+ readonly key : string
9+ readonly value : any
10+ }
511
612// tslint:disable:no-this
713// tslint:disable-next-line:no-class
814@Injectable ( )
915export class CookieService implements ICookieService {
1016 private readonly cookieSource = new Subject < StringDict > ( )
17+ private readonly changeSource = new Subject < KeyValue > ( )
18+ public readonly valueChange = this . changeSource . asObservable ( )
1119 public readonly valueChanges = this . cookieSource . asObservable ( )
1220
21+ targetValueChange ( key : string ) {
22+ return this . valueChange . pipe ( filter ( a => a && a . key === key ) )
23+ }
24+
1325 public set ( name : string , value : any , opts ?: CookieAttributes ) : void {
1426 set ( name , value , opts )
1527 this . updateSource ( )
28+ this . broadcastChange ( name )
1629 }
1730
1831 public remove ( name : string , opts ?: CookieAttributes ) : void {
1932 remove ( name , opts )
2033 this . updateSource ( )
34+ this . broadcastChange ( name )
2135 }
2236
2337 public get ( name : string ) : any {
@@ -31,4 +45,11 @@ export class CookieService implements ICookieService {
3145 private updateSource ( ) {
3246 this . cookieSource . next ( this . getAll ( ) )
3347 }
48+
49+ private broadcastChange ( key : string ) {
50+ this . changeSource . next ( {
51+ key,
52+ value : this . get ( key )
53+ } )
54+ }
3455}
0 commit comments