@@ -2,22 +2,36 @@ import { Injectable } from '@angular/core'
2
2
import { Subject } from 'rxjs'
3
3
import { ICookieService , StringDict } from './common'
4
4
import { 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
+ }
5
11
6
12
// tslint:disable:no-this
7
13
// tslint:disable-next-line:no-class
8
14
@Injectable ( )
9
15
export class CookieService implements ICookieService {
10
16
private readonly cookieSource = new Subject < StringDict > ( )
17
+ private readonly changeSource = new Subject < KeyValue > ( )
18
+ public readonly valueChange = this . changeSource . asObservable ( )
11
19
public readonly valueChanges = this . cookieSource . asObservable ( )
12
20
21
+ targetValueChange ( key : string ) {
22
+ return this . valueChange . pipe ( filter ( a => a && a . key === key ) )
23
+ }
24
+
13
25
public set ( name : string , value : any , opts ?: CookieAttributes ) : void {
14
26
set ( name , value , opts )
15
27
this . updateSource ( )
28
+ this . broadcastChange ( name )
16
29
}
17
30
18
31
public remove ( name : string , opts ?: CookieAttributes ) : void {
19
32
remove ( name , opts )
20
33
this . updateSource ( )
34
+ this . broadcastChange ( name )
21
35
}
22
36
23
37
public get ( name : string ) : any {
@@ -31,4 +45,11 @@ export class CookieService implements ICookieService {
31
45
private updateSource ( ) {
32
46
this . cookieSource . next ( this . getAll ( ) )
33
47
}
48
+
49
+ private broadcastChange ( key : string ) {
50
+ this . changeSource . next ( {
51
+ key,
52
+ value : this . get ( key )
53
+ } )
54
+ }
34
55
}
0 commit comments