@@ -17,7 +17,6 @@ export type State = {
17
17
18
18
export type SubscribeCallback = ( partialState : Partial < State > ) => void
19
19
export type WatchCallback < P extends keyof State > = ( valueNew : State [ P ] ) => void
20
- type WatchTracker < P extends keyof State > = { name : P ; callback : WatchCallback < P > }
21
20
22
21
const initState = ( ) : State => ( {
23
22
selectedCorpusIds : [ ] ,
@@ -29,7 +28,6 @@ angular.module("korpApp").factory("store", [
29
28
function ( $rootScope : RootScope , $timeout : ITimeoutService ) : StoreService {
30
29
$rootScope . store = initState ( )
31
30
const subscribers : SubscribeCallback [ ] = [ ]
32
- const watchTrackers : WatchTracker < keyof State > [ ] = [ ]
33
31
34
32
/** Call all subscribers and affected watchers */
35
33
function notify ( names : ( keyof State ) [ ] ) : void {
@@ -38,9 +36,6 @@ angular.module("korpApp").factory("store", [
38
36
// In next tick
39
37
$timeout ( ( ) => {
40
38
subscribers . forEach ( ( callback ) => callback ( partialState ) )
41
- watchTrackers
42
- . filter ( ( tracker ) => names . includes ( tracker . name ) )
43
- . forEach ( ( tracker ) => tracker . callback ( $rootScope . store [ tracker . name ] ) )
44
39
} )
45
40
}
46
41
@@ -57,7 +52,7 @@ angular.module("korpApp").factory("store", [
57
52
notify ( namesChanged )
58
53
}
59
54
60
- // Placing function bodies here means less repeated typing.
55
+ // Placing function bodies here means less repeated typing
61
56
return {
62
57
get ( name ) {
63
58
return $rootScope . store [ name ]
@@ -74,7 +69,10 @@ angular.module("korpApp").factory("store", [
74
69
} ,
75
70
76
71
watch ( name , callback ) {
77
- watchTrackers . push ( { name, callback } )
72
+ // Call the given callback only if the named variable is changed.
73
+ subscribers . push ( ( partialState ) => {
74
+ if ( name in partialState ) callback ( partialState [ name ] ! )
75
+ } )
78
76
} ,
79
77
}
80
78
} ,
0 commit comments