@@ -17,7 +17,6 @@ export type State = {
1717
1818export type SubscribeCallback = ( partialState : Partial < State > ) => void
1919export type WatchCallback < P extends keyof State > = ( valueNew : State [ P ] ) => void
20- type WatchTracker < P extends keyof State > = { name : P ; callback : WatchCallback < P > }
2120
2221const initState = ( ) : State => ( {
2322 selectedCorpusIds : [ ] ,
@@ -29,7 +28,6 @@ angular.module("korpApp").factory("store", [
2928 function ( $rootScope : RootScope , $timeout : ITimeoutService ) : StoreService {
3029 $rootScope . store = initState ( )
3130 const subscribers : SubscribeCallback [ ] = [ ]
32- const watchTrackers : WatchTracker < keyof State > [ ] = [ ]
3331
3432 /** Call all subscribers and affected watchers */
3533 function notify ( names : ( keyof State ) [ ] ) : void {
@@ -38,9 +36,6 @@ angular.module("korpApp").factory("store", [
3836 // In next tick
3937 $timeout ( ( ) => {
4038 subscribers . forEach ( ( callback ) => callback ( partialState ) )
41- watchTrackers
42- . filter ( ( tracker ) => names . includes ( tracker . name ) )
43- . forEach ( ( tracker ) => tracker . callback ( $rootScope . store [ tracker . name ] ) )
4439 } )
4540 }
4641
@@ -57,7 +52,7 @@ angular.module("korpApp").factory("store", [
5752 notify ( namesChanged )
5853 }
5954
60- // Placing function bodies here means less repeated typing.
55+ // Placing function bodies here means less repeated typing
6156 return {
6257 get ( name ) {
6358 return $rootScope . store [ name ]
@@ -74,7 +69,10 @@ angular.module("korpApp").factory("store", [
7469 } ,
7570
7671 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+ } )
7876 } ,
7977 }
8078 } ,
0 commit comments