init
This commit is contained in:
93
node_modules/matrix-js-sdk/lib/capabilityPoller.js
generated
vendored
Normal file
93
node_modules/matrix-js-sdk/lib/capabilityPoller.js
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
Copyright 2024 The Matrix.org Foundation C.I.C.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
import { TypedEventEmitter } from "./models/typed-event-emitter.js";
|
||||
import { deepCompare } from "./utils.js";
|
||||
|
||||
// How often we update the server capabilities.
|
||||
// 6 hours - an arbitrary value, but they should change very infrequently.
|
||||
const CAPABILITIES_CACHE_MS = 6 * 60 * 60 * 1000;
|
||||
|
||||
// How long we want before retrying if we couldn't fetch
|
||||
const CAPABILITIES_RETRY_MS = 30 * 1000;
|
||||
|
||||
/**
|
||||
* Manages storing and periodically refreshing the server capabilities.
|
||||
*/
|
||||
export class CapabilityPoller extends TypedEventEmitter {
|
||||
constructor(logger, http, name) {
|
||||
super();
|
||||
_defineProperty(this, "cached", void 0);
|
||||
_defineProperty(this, "retryTimeout", void 0);
|
||||
_defineProperty(this, "refreshTimeout", void 0);
|
||||
_defineProperty(this, "poll", async () => {
|
||||
this.logger.debug("Checking capabilites");
|
||||
try {
|
||||
const current = this.cached;
|
||||
await this.fetch();
|
||||
this.clearTimeouts();
|
||||
this.refreshTimeout = globalThis.setTimeout(this.poll, CAPABILITIES_CACHE_MS);
|
||||
this.logger.debug(`Fetched new server ${this.name}`);
|
||||
if (this.cached && !deepCompare(current, this.cached)) {
|
||||
this.emit("update", this.cached);
|
||||
}
|
||||
} catch (e) {
|
||||
this.clearTimeouts();
|
||||
const howLong = Math.floor(CAPABILITIES_RETRY_MS + Math.random() * 5000);
|
||||
this.retryTimeout = globalThis.setTimeout(this.poll, howLong);
|
||||
this.logger.warn(`Failed to refresh ${this.name}: retrying in ${howLong}ms`, e);
|
||||
}
|
||||
});
|
||||
this.logger = logger;
|
||||
this.http = http;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts periodically fetching the server capabilities.
|
||||
*/
|
||||
start() {
|
||||
this.poll().then();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the service
|
||||
*/
|
||||
stop() {
|
||||
this.clearTimeouts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cached capabilities, or undefined if none are cached.
|
||||
* @returns the current capabilities, if any.
|
||||
*/
|
||||
getCached() {
|
||||
return this.cached;
|
||||
}
|
||||
clearTimeouts() {
|
||||
if (this.refreshTimeout) {
|
||||
clearInterval(this.refreshTimeout);
|
||||
this.refreshTimeout = undefined;
|
||||
}
|
||||
if (this.retryTimeout) {
|
||||
clearTimeout(this.retryTimeout);
|
||||
this.retryTimeout = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=capabilityPoller.js.map
|
||||
Reference in New Issue
Block a user