1+ import type { Store } from 'redux'
12import { getBatch } from './batch'
3+ import type { Node } from './autotracking/tracking'
4+
5+ import {
6+ createCache ,
7+ TrackingCache ,
8+ $REVISION ,
9+ } from './autotracking/autotracking'
10+ import { updateNode } from './autotracking/proxy'
211
312// encapsulates the subscription logic for connecting a component to the redux store, as
413// well as nesting subscriptions of descendant components, so that we can ensure the
@@ -10,6 +19,9 @@ type Listener = {
1019 callback : VoidFunc
1120 next : Listener | null
1221 prev : Listener | null
22+ trigger : 'always' | 'tracked'
23+ selectorCache ?: TrackingCache
24+ subscriberCache ?: TrackingCache
1325}
1426
1527function createListenerCollection ( ) {
@@ -24,10 +36,29 @@ function createListenerCollection() {
2436 } ,
2537
2638 notify ( ) {
39+ //console.log('Notifying subscribers')
2740 batch ( ( ) => {
2841 let listener = first
2942 while ( listener ) {
30- listener . callback ( )
43+ //console.log('Listener: ', listener)
44+ if ( listener . trigger == 'tracked' ) {
45+ if ( listener . selectorCache ! . needsRecalculation ( ) ) {
46+ console . log ( 'Calling subscriber due to recalc need' )
47+ // console.log(
48+ // 'Calling subscriber due to recalc. Revision before: ',
49+ // $REVISION
50+ // )
51+ listener . callback ( )
52+ //console.log('Revision after: ', $REVISION)
53+ } else {
54+ console . log (
55+ 'Skipping subscriber, no recalc: ' ,
56+ listener . selectorCache
57+ )
58+ }
59+ } else {
60+ listener . callback ( )
61+ }
3162 listener = listener . next
3263 }
3364 } )
@@ -43,13 +74,29 @@ function createListenerCollection() {
4374 return listeners
4475 } ,
4576
46- subscribe ( callback : ( ) => void ) {
77+ subscribe (
78+ callback : ( ) => void ,
79+ options : AddNestedSubOptions = { trigger : 'always' }
80+ ) {
4781 let isSubscribed = true
4882
83+ //console.log('Adding listener: ', options.trigger)
84+
4985 let listener : Listener = ( last = {
5086 callback,
5187 next : null ,
5288 prev : last ,
89+ trigger : options . trigger ,
90+ selectorCache :
91+ options . trigger === 'tracked' ? options . cache ! : undefined ,
92+ // subscriberCache:
93+ // options.trigger === 'tracked'
94+ // ? createCache(() => {
95+ // console.log('Calling subscriberCache')
96+ // listener.selectorCache!.get()
97+ // callback()
98+ // })
99+ // : undefined,
53100 } )
54101
55102 if ( listener . prev ) {
@@ -79,13 +126,18 @@ function createListenerCollection() {
79126
80127type ListenerCollection = ReturnType < typeof createListenerCollection >
81128
129+ interface AddNestedSubOptions {
130+ trigger : 'always' | 'tracked'
131+ cache ?: TrackingCache
132+ }
133+
82134export interface Subscription {
83- addNestedSub : ( listener : VoidFunc ) => VoidFunc
135+ addNestedSub : ( listener : VoidFunc , options ?: AddNestedSubOptions ) => VoidFunc
84136 notifyNestedSubs : VoidFunc
85137 handleChangeWrapper : VoidFunc
86138 isSubscribed : ( ) => boolean
87139 onStateChange ?: VoidFunc | null
88- trySubscribe : VoidFunc
140+ trySubscribe : ( options ?: AddNestedSubOptions ) => void
89141 tryUnsubscribe : VoidFunc
90142 getListeners : ( ) => ListenerCollection
91143}
@@ -95,16 +147,28 @@ const nullListeners = {
95147 get : ( ) => [ ] ,
96148} as unknown as ListenerCollection
97149
98- export function createSubscription ( store : any , parentSub ?: Subscription ) {
150+ export function createSubscription (
151+ store : Store ,
152+ parentSub ?: Subscription ,
153+ trackingNode ?: Node < any >
154+ ) {
99155 let unsubscribe : VoidFunc | undefined
100156 let listeners : ListenerCollection = nullListeners
101157
102- function addNestedSub ( listener : ( ) => void ) {
103- trySubscribe ( )
104- return listeners . subscribe ( listener )
158+ function addNestedSub (
159+ listener : ( ) => void ,
160+ options : AddNestedSubOptions = { trigger : 'always' }
161+ ) {
162+ //console.log('addNestedSub: ', options)
163+ trySubscribe ( options )
164+ return listeners . subscribe ( listener , options )
105165 }
106166
107167 function notifyNestedSubs ( ) {
168+ if ( store && trackingNode ) {
169+ //console.log('Updating node in notifyNestedSubs')
170+ updateNode ( trackingNode , store . getState ( ) )
171+ }
108172 listeners . notify ( )
109173 }
110174
@@ -118,10 +182,11 @@ export function createSubscription(store: any, parentSub?: Subscription) {
118182 return Boolean ( unsubscribe )
119183 }
120184
121- function trySubscribe ( ) {
185+ function trySubscribe ( options : AddNestedSubOptions = { trigger : 'always' } ) {
122186 if ( ! unsubscribe ) {
187+ //console.log('trySubscribe, parentSub: ', parentSub)
123188 unsubscribe = parentSub
124- ? parentSub . addNestedSub ( handleChangeWrapper )
189+ ? parentSub . addNestedSub ( handleChangeWrapper , options )
125190 : store . subscribe ( handleChangeWrapper )
126191
127192 listeners = createListenerCollection ( )
0 commit comments