Skip to content

Commit a29933b

Browse files
authored
Update subscriber.js
1 parent 0f2802d commit a29933b

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

subscriber.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ class Subscriber {
5151
element;
5252
/** @type {string} Randomly generated id assigned to JQuery HTMLElement (provided or created through constructor) */
5353
id;
54+
/** @type {T} Maintains the original function that was created through constructor */
55+
original;
5456
/** @type {T} */
5557
fire;
58+
/** @type {boolean} Used to keep a backup */
59+
cleared = false;
5660
/** @type {string[]} Element selectors that this subscription is subscribed to. */
5761
subscriptions = [];
5862

@@ -128,7 +132,7 @@ class Subscriber {
128132
this.element = el;
129133
this.id = id ?? /** @type {JQuery<HTMLElement>}*/(el).attr("id");
130134
}
131-
this.fire = f;
135+
this.original = this.fire = f;
132136
}
133137

134138
/**
@@ -159,12 +163,15 @@ class Subscriber {
159163
* console.log($("#my-element").val()); // yields "0"
160164
*/
161165
subscribe(f=null, ...selectors) {
162-
console.log(f, selectors);
166+
if(this.cleared) {
167+
this.fire = this.original;
168+
this.cleared = false;
169+
}
163170
const sub = (/** @type {string} */sel) => {
164171
$(document).on("input change", sel, (e) => {
165172
/** @type {number|string} */
166173
let val;
167-
if(typeof(f) !== "string") {
174+
if(f != null && typeof(f) !== "string") {
168175
val = f(e);
169176
} else if(this.fire) {
170177
val = this.fire(e);
@@ -175,33 +182,32 @@ class Subscriber {
175182
$(this.selector()).val(val);
176183
$(this.selector()).trigger("change");
177184
});
185+
this.subscriptions = [...this.subscriptions, sel];
178186
}
179187
if(typeof(f) === "string") sub(f);
180188
for(let sel of selectors) {
181189
sub(sel);
182190
}
183-
this.subscriptions = [...this.subscriptions, ...selectors];
184191
}
185192

186193
/**
187194
* Unsubscribes from all elements that this Subscriber subscribed to.
188195
*/
189196
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()));
196198
}
197199

198200
/**
199201
* Resubscribes to all subscriptions that have previously been subscribed to.
200202
*/
201203
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+
}
205211
}
206212

207213
/**
@@ -210,6 +216,7 @@ class Subscriber {
210216
clear() {
211217
this.unsubscribe();
212218
this.subscriptions = [];
219+
this.cleared = true;
213220
}
214221

215222
/**

0 commit comments

Comments
 (0)