This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Support the old room sorting algorithm and SettingsStore watchers #2686
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2019 New Vector Ltd. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Generalized management class for dealing with watchers on a per-handler (per-level) | ||
* basis without duplicating code. Handlers are expected to push updates through this | ||
* class, which are then proxied outwards to any applicable watchers. | ||
*/ | ||
export class WatchManager { | ||
_watchers = {}; // { settingName: { roomId: callbackFns[] } } | ||
|
||
// Proxy for handlers to delegate changes to this manager | ||
watchSetting(settingName, roomId, cb) { | ||
if (!this._watchers[settingName]) this._watchers[settingName] = {}; | ||
if (!this._watchers[settingName][roomId]) this._watchers[settingName][roomId] = []; | ||
this._watchers[settingName][roomId].push(cb); | ||
} | ||
|
||
// Proxy for handlers to delegate changes to this manager | ||
unwatchSetting(cb) { | ||
for (const settingName of Object.keys(this._watchers)) { | ||
for (const roomId of Object.keys(this._watchers[settingName])) { | ||
let idx; | ||
while ((idx = this._watchers[settingName][roomId].indexOf(cb)) !== -1) { | ||
this._watchers[settingName][roomId].splice(idx, 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
notifyUpdate(settingName, inRoomId, newValue) { | ||
if (!this._watchers[settingName]) return; | ||
|
||
const roomWatchers = this._watchers[settingName]; | ||
const callbacks = []; | ||
|
||
if (inRoomId !== null && roomWatchers[inRoomId]) callbacks.push(...roomWatchers[inRoomId]); | ||
if (roomWatchers[null]) callbacks.push(...roomWatchers[null]); | ||
|
||
for (const callback of callbacks) { | ||
callback(inRoomId, newValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so if a handler at a lower level emits an update, but a higher level handler would still override that value, the update still goes out, right? I wonder why the handlers each have their own
WatchManager
, as opposed to them having a reference (or use the global) to theSettingsStore
to callnotifyUpdate
on... where you can then decide to emit an update bases on all the levels.Not a huge issue, as most code would just cal
getValue
again and read the same old value, but on first sight that would also be less code, would be easier to prevent the above "issue", but might be missing something.At least I would remove/change the newVal argument to the callback, because I suspect that as it stands it might report something different than what
getValue
would report.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to maintain the high flexibility provided by the SettingsStore for the admittedly few cases which need it. There's not currently a use case for needing to know when a specific level has changed, however things like notifications (which currently use a controller) might want to make use of this granularity. I'm a bit on the fence for changing this to only emit when there's a perceivable change as in future it would result in excessive updates but might be inconsequential.
The other reason each handler has a dedicated WatchManager is to support the idea of watching a setting at a particular level. This is also a rare case, and we can probably just split it out later if we need to.