Skip to content

Commit ce9be37

Browse files
committed
Rename methods
1 parent 911fecc commit ce9be37

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

apps/meteor/app/slackbridge/client/slackbridge_import.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { settings } from '../../../client/lib/settings';
22
import { slashCommands } from '../../utils/client/slashCommand';
33

4-
settings.onload('SlackBridge_Enabled', (_key, value) => {
4+
settings.observe('SlackBridge_Enabled', (_key, value) => {
55
if (value) {
66
slashCommands.add({
77
command: 'slackbridge-import',

apps/meteor/client/lib/settings/settings.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,45 @@ class Settings {
1919

2020
for (const _id of removedIds) {
2121
delete Meteor.settings[_id];
22-
this.load(_id, undefined);
22+
this.handleChange(_id, undefined);
2323
}
2424

2525
for (const setting of state.records.values()) {
2626
if (setting.value !== Meteor.settings[setting._id]) {
2727
Meteor.settings[setting._id] = setting.value;
28-
this.load(setting._id, setting.value);
28+
this.handleChange(setting._id, setting.value);
2929
}
3030
}
3131
});
3232
}
3333

34-
private callbacks = new Map<string, SettingCallback[]>();
34+
private callbacks = new Map<string, Set<SettingCallback>>();
3535

36-
load(key: string, value: SettingValue): void {
37-
['*', key].forEach((item) => {
38-
const callbacks = this.callbacks.get(item);
39-
if (callbacks) {
40-
callbacks.forEach((callback) => callback(key, value));
41-
}
42-
});
36+
private handleChange(key: string, value: SettingValue): void {
37+
for (const _key of ['*', key]) {
38+
const callbacks = this.callbacks.get(_key);
39+
callbacks?.forEach((callback) => callback(_key, value));
40+
}
4341
}
4442

45-
onload(key: string, callback: SettingCallback): void {
43+
observe(key: string, callback: SettingCallback): () => void {
4644
// if key is '*'
4745
// for key, value in Meteor.settings
4846
// callback key, value, false
4947
// else if Meteor.settings?[_id]?
5048
// callback key, Meteor.settings[_id], false
5149

5250
if (!this.callbacks.has(key)) {
53-
this.callbacks.set(key, []);
51+
this.callbacks.set(key, new Set());
5452
}
55-
this.callbacks.get(key)?.push(callback);
53+
this.callbacks.get(key)?.add(callback);
54+
55+
return () => {
56+
this.callbacks.get(key)?.delete(callback);
57+
if (this.callbacks.get(key)?.size === 0) {
58+
this.callbacks.delete(key);
59+
}
60+
};
5661
}
5762
}
5863

0 commit comments

Comments
 (0)