@@ -51,8 +51,12 @@ class Subscriber {
51
51
element ;
52
52
/** @type {string } Randomly generated id assigned to JQuery HTMLElement (provided or created through constructor) */
53
53
id ;
54
+ /** @type {T } Maintains the original function that was created through constructor */
55
+ original ;
54
56
/** @type {T } */
55
57
fire ;
58
+ /** @type {boolean } Used to keep a backup */
59
+ cleared = false ;
56
60
/** @type {string[] } Element selectors that this subscription is subscribed to. */
57
61
subscriptions = [ ] ;
58
62
@@ -128,7 +132,7 @@ class Subscriber {
128
132
this . element = el ;
129
133
this . id = id ?? /** @type {JQuery<HTMLElement> }*/ ( el ) . attr ( "id" ) ;
130
134
}
131
- this . fire = f ;
135
+ this . original = this . fire = f ;
132
136
}
133
137
134
138
/**
@@ -159,12 +163,15 @@ class Subscriber {
159
163
* console.log($("#my-element").val()); // yields "0"
160
164
*/
161
165
subscribe ( f = null , ...selectors ) {
162
- console . log ( f , selectors ) ;
166
+ if ( this . cleared ) {
167
+ this . fire = this . original ;
168
+ this . cleared = false ;
169
+ }
163
170
const sub = ( /** @type {string } */ sel ) => {
164
171
$ ( document ) . on ( "input change" , sel , ( e ) => {
165
172
/** @type {number|string } */
166
173
let val ;
167
- if ( typeof ( f ) !== "string" ) {
174
+ if ( f != null && typeof ( f ) !== "string" ) {
168
175
val = f ( e ) ;
169
176
} else if ( this . fire ) {
170
177
val = this . fire ( e ) ;
@@ -175,33 +182,32 @@ class Subscriber {
175
182
$ ( this . selector ( ) ) . val ( val ) ;
176
183
$ ( this . selector ( ) ) . trigger ( "change" ) ;
177
184
} ) ;
185
+ this . subscriptions = [ ...this . subscriptions , sel ] ;
178
186
}
179
187
if ( typeof ( f ) === "string" ) sub ( f ) ;
180
188
for ( let sel of selectors ) {
181
189
sub ( sel ) ;
182
190
}
183
- this . subscriptions = [ ...this . subscriptions , ...selectors ] ;
184
191
}
185
192
186
193
/**
187
194
* Unsubscribes from all elements that this Subscriber subscribed to.
188
195
*/
189
196
unsubscribe ( ) {
190
- const unsub = ( /** @type {string } */ sel ) => {
191
- $ ( document ) . off ( "input change" , sel ) ;
192
- }
193
- for ( let sel of this . subscriptions ) {
194
- unsub ( sel ) ;
195
- }
197
+ this . fire = ( e ) => parseInt ( /** @type {string } */ ( $ ( this . selector ( ) ) . val ( ) ) ) ;
196
198
}
197
199
198
200
/**
199
201
* Resubscribes to all subscriptions that have previously been subscribed to.
200
202
*/
201
203
resubscribe ( ) {
202
- const selectors = [ ...this . subscriptions ] ;
203
- this . clear ( ) ;
204
- this . subscribe ( selectors ) ;
204
+ if ( ! this . cleared ) {
205
+ const selectors = [ ...this . subscriptions ] ;
206
+ this . unsubscribe ( ) ;
207
+ this . subscriptions = [ ] ;
208
+ this . subscribe ( null , ...selectors ) ;
209
+ this . fire = this . original ;
210
+ }
205
211
}
206
212
207
213
/**
@@ -210,6 +216,7 @@ class Subscriber {
210
216
clear ( ) {
211
217
this . unsubscribe ( ) ;
212
218
this . subscriptions = [ ] ;
219
+ this . cleared = true ;
213
220
}
214
221
215
222
/**
0 commit comments