Compare commits

...

9 commits

Author SHA1 Message Date
SleeplessOne1917
b694931ae4
Merge branch 'main' into long_polling 2023-06-15 00:20:00 +00:00
Dessalines
00ee931c61
Merge branch 'main' into long_polling 2023-06-14 20:07:23 -04:00
Dessalines
d19817b00a Merge branch 'long_polling' of https://github.com/LemmyNet/lemmy-ui into long_polling 2023-06-14 20:07:10 -04:00
Dessalines
ee7f54bb0d Adding window visibility check, removing generic sleep. 2023-06-14 20:06:50 -04:00
SleeplessOne1917
aa28788b8d
Update src/shared/utils.ts
Co-authored-by: Sander Saarend <sander@saarend.com>
2023-06-14 23:36:35 +00:00
Dessalines
f849f376bb
Merge branch 'main' into long_polling 2023-06-14 12:20:40 -04:00
Dessalines
caf445cae7 Using async for polling. 2023-06-14 12:19:33 -04:00
Dessalines
10b78acb3d Add long-polling to update unread counts in navbar.
- Fixes #1148
2023-06-14 11:35:10 -04:00
Dessalines
4152928331 Upping version. 2023-06-14 08:21:52 -04:00
3 changed files with 47 additions and 28 deletions

View file

@ -1,6 +1,6 @@
{
"name": "lemmy-ui",
"version": "0.18.0-beta.6",
"version": "0.18.0-rc.1",
"description": "An isomorphic UI for lemmy",
"repository": "https://github.com/LemmyNet/lemmy-ui",
"license": "AGPL-3.0",

View file

@ -16,8 +16,10 @@ import {
isBrowser,
myAuth,
numToSI,
poll,
showAvatars,
toast,
updateUnreadCountsInterval,
} from "../../utils";
import { Icon } from "../common/icon";
import { PictrsImage } from "../common/pictrs-image";
@ -64,7 +66,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
if (isBrowser()) {
// On the first load, check the unreads
this.requestNotificationPermission();
await this.fetchUnreads();
this.fetchUnreads();
this.requestNotificationPermission();
document.addEventListener("mouseup", this.handleOutsideMenuClick);
@ -406,10 +408,11 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
return amAdmin() || moderatesS;
}
async fetchUnreads() {
fetchUnreads() {
poll(async () => {
if (window.document.visibilityState !== "hidden") {
const auth = myAuth();
if (auth) {
this.setState({ unreadInboxCountRes: { state: "loading" } });
this.setState({
unreadInboxCountRes: await HttpService.client.getUnreadCount({
auth,
@ -417,7 +420,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
});
if (this.moderatesSomething) {
this.setState({ unreadReportCountRes: { state: "loading" } });
this.setState({
unreadReportCountRes: await HttpService.client.getReportCount({
auth,
@ -426,7 +428,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
}
if (amAdmin()) {
this.setState({ unreadApplicationCountRes: { state: "loading" } });
this.setState({
unreadApplicationCountRes:
await HttpService.client.getUnreadRegistrationApplicationCount({
@ -436,6 +437,8 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
}
}
}
}, updateUnreadCountsInterval);
}
get unreadInboxCount(): number {
if (this.state.unreadInboxCountRes.state == "success") {

View file

@ -75,6 +75,7 @@ export const commentTreeMaxDepth = 8;
export const markdownFieldCharacterLimit = 50000;
export const maxUploadImages = 20;
export const concurrentImageUpload = 4;
export const updateUnreadCountsInterval = 30000;
export const relTags = "noopener nofollow";
@ -1490,3 +1491,18 @@ export function newVote(voteType: VoteType, myVote?: number): number {
return myVote == -1 ? 0 : -1;
}
}
function sleep(millis: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, millis));
}
/**
* Polls / repeatedly runs a promise, every X milliseconds
*/
export async function poll(promiseFn: any, millis: number) {
if (window.document.visibilityState !== "hidden") {
await promiseFn();
}
await sleep(millis);
return poll(promiseFn, millis);
}