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 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for watching for changes in settings
This implements a dream of one day being able to listen for changes in a settings to react to them, regardless of which device actually changed the setting. The use case for this kind of thing is extremely limited, but when it is needed it should be more than powerful enough.
- Loading branch information
Showing
14 changed files
with
483 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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
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
Oops, something went wrong.