summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/super_sidebar/user_counts_manager.js
blob: 40c9fc43252339baac99ddaf73978fb6dd8d39d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Vue from 'vue';
import { getUserCounts } from '~/api/user_api';

export const userCounts = Vue.observable({
  last_update: 0,
  // The following fields are part of
  // https://docs.gitlab.com/ee/api/users.html#user-counts
  todos: 0,
  assigned_issues: 0,
  assigned_merge_requests: 0,
  review_requested_merge_requests: 0,
});

function updateCounts(payload = {}) {
  if ((payload.last_update ?? 0) < userCounts.last_update) {
    return;
  }
  for (const key in userCounts) {
    if (Number.isInteger(payload[key])) {
      userCounts[key] = payload[key];
    }
  }
}

let broadcastChannel = null;

function broadcastUserCounts(data) {
  broadcastChannel?.postMessage(data);
}

async function retrieveUserCountsFromApi() {
  try {
    const lastUpdate = Date.now();
    const { data } = await getUserCounts();
    const payload = { ...data, last_update: lastUpdate };
    updateCounts(payload);
    broadcastUserCounts(userCounts);
  } catch (e) {
    // eslint-disable-next-line no-console, @gitlab/require-i18n-strings
    console.error('Error retrieving user counts', e);
  }
}

export function destroyUserCountsManager() {
  document.removeEventListener('userCounts:fetch', retrieveUserCountsFromApi);
  broadcastChannel?.close();
  broadcastChannel = null;
}

/**
 * The createUserCountsManager does three things:
 * 1. Set the initial state of userCounts
 * 2. Create a broadcast channel to communicate user count updates across tabs
 * 3. Add event listeners for other parts in the app which:
 *     - Update todos
 *     - Trigger a refetch of all counts
 */
export function createUserCountsManager() {
  destroyUserCountsManager();
  document.addEventListener('userCounts:fetch', retrieveUserCountsFromApi);

  if (window.BroadcastChannel && gon?.current_user_id) {
    broadcastChannel = new BroadcastChannel(`user_counts_${gon?.current_user_id}`);
    broadcastChannel.onmessage = (ev) => {
      updateCounts(ev.data);
    };
    broadcastUserCounts(userCounts);
  }
}