mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-12 11:52:46 +03:00
0e49c4343c
Fixes https://github.com/vector-im/riot-web/issues/14411 The act of setting/changing the algorithm was causing the update function to be marked, meaning we wouldn't trigger an update until something else happened later. To get around this, and still support internal functions spamming calls without multiple updates, we simply move the guts to an internalized function and make the public interface do a trigger.
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
/*
|
|
Copyright 2020 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.
|
|
*/
|
|
|
|
/**
|
|
* A utility to ensure that a function is only called once triggered with
|
|
* a mark applied. Multiple marks can be applied to the function, however
|
|
* the function will only be called once upon trigger().
|
|
*
|
|
* The function starts unmarked.
|
|
*/
|
|
export class MarkedExecution {
|
|
private marked = false;
|
|
|
|
/**
|
|
* Creates a MarkedExecution for the provided function.
|
|
* @param fn The function to be called upon trigger if marked.
|
|
*/
|
|
constructor(private fn: () => void) {
|
|
}
|
|
|
|
/**
|
|
* Resets the mark without calling the function.
|
|
*/
|
|
public reset() {
|
|
this.marked = false;
|
|
}
|
|
|
|
/**
|
|
* Marks the function to be called upon trigger().
|
|
*/
|
|
public mark() {
|
|
this.marked = true;
|
|
}
|
|
|
|
/**
|
|
* If marked, the function will be called, otherwise this does nothing.
|
|
*/
|
|
public trigger() {
|
|
if (!this.marked) return;
|
|
this.reset(); // reset first just in case the fn() causes a trigger()
|
|
this.fn();
|
|
}
|
|
}
|