Skip to content

Commit

Permalink
Merge branch 'develop' into regression/contextualbar-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Nov 11, 2024
2 parents d166bda + 6fa0873 commit b83bd6e
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 166 deletions.
10 changes: 5 additions & 5 deletions apps/meteor/app/emoji-custom/client/lib/emojiCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { escapeRegExp } from '@rocket.chat/string-helpers';
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';

import { onLoggedIn } from '../../../../client/lib/loggedIn';
import { emoji, removeFromRecent, replaceEmojiInRecent } from '../../../emoji/client';
import { CachedCollectionManager } from '../../../ui-cached-collection/client';
import { getURL } from '../../../utils/client';
import { sdk } from '../../../utils/client/lib/SDKClient';

Expand Down Expand Up @@ -144,8 +144,8 @@ emoji.packages.emojiCustom = {
renderPicker: customRender,
};

Meteor.startup(() =>
CachedCollectionManager.onLogin(async () => {
Meteor.startup(() => {
onLoggedIn(async () => {
try {
const {
emojis: { update: emojis },
Expand All @@ -167,5 +167,5 @@ Meteor.startup(() =>
} catch (e) {
console.error('Error getting custom emoji', e);
}
}),
);
});
});
2 changes: 1 addition & 1 deletion apps/meteor/app/models/client/models/CachedChatRoom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IOmnichannelRoom, IRoom, IRoomWithRetentionPolicy } from '@rocket.chat/core-typings';
import { DEFAULT_SLA_CONFIG, LivechatPriorityWeight } from '@rocket.chat/core-typings';

import { CachedCollection } from '../../../ui-cached-collection/client/models/CachedCollection';
import { CachedCollection } from '../../../../client/lib/cachedCollections/CachedCollection';
import { CachedChatSubscription } from './CachedChatSubscription';

class CachedChatRoom extends CachedCollection<IRoom> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IOmnichannelRoom, IRoomWithRetentionPolicy, ISubscription } from '
import { DEFAULT_SLA_CONFIG, LivechatPriorityWeight } from '@rocket.chat/core-typings';
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';

import { CachedCollection } from '../../../ui-cached-collection/client/models/CachedCollection';
import { CachedCollection } from '../../../../client/lib/cachedCollections/CachedCollection';
import { CachedChatRoom } from './CachedChatRoom';

declare module '@rocket.chat/core-typings' {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/models/client/models/ChatPermissions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IPermission } from '@rocket.chat/core-typings';

import { CachedCollection } from '../../../ui-cached-collection/client';
import { CachedCollection } from '../../../../client/lib/cachedCollections';

export const AuthzCachedCollection = new CachedCollection<IPermission>({
name: 'permissions',
Expand Down
2 changes: 0 additions & 2 deletions apps/meteor/app/ui-cached-collection/client/index.ts

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion apps/meteor/client/importPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ import '../app/utils/client';
import '../app/settings/client';
import '../app/models/client';
import '../app/ui-utils/client';
import '../app/ui-cached-collection/client';
import '../app/reactions/client';
import '../app/livechat/client';
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { StreamNames } from '@rocket.chat/ddp-client';
import { Emitter } from '@rocket.chat/emitter';
import localforage from 'localforage';
import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ReactiveVar } from 'meteor/reactive-var';

import type { MinimongoCollection } from '../../../../client/definitions/MinimongoCollection';
import { baseURI } from '../../../../client/lib/baseURI';
import { getConfig } from '../../../../client/lib/utils/getConfig';
import { isTruthy } from '../../../../lib/isTruthy';
import { withDebouncing } from '../../../../lib/utils/highOrderFunctions';
import { sdk } from '../../../utils/client/lib/SDKClient';
import { Tracker } from 'meteor/tracker';

import { sdk } from '../../../app/utils/client/lib/SDKClient';
import { isTruthy } from '../../../lib/isTruthy';
import { withDebouncing } from '../../../lib/utils/highOrderFunctions';
import type { MinimongoCollection } from '../../definitions/MinimongoCollection';
import { baseURI } from '../baseURI';
import { onLoggedIn } from '../loggedIn';
import { getConfig } from '../utils/getConfig';
import { CachedCollectionManager } from './CachedCollectionManager';

export type EventType = 'notify-logged' | 'notify-all' | 'notify-user';
Expand All @@ -35,34 +36,30 @@ const hasUnserializedUpdatedAt = <T>(record: T): record is T & { _updatedAt: Con
'_updatedAt' in record &&
!((record as unknown as { _updatedAt: unknown })._updatedAt instanceof Date);

localforage.config({
name: baseURI,
});
localforage.config({ name: baseURI });

export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<{ changed: T; removed: T }> {
export class CachedCollection<T extends { _id: string }, U = T> {
private static MAX_CACHE_TIME = 60 * 60 * 24 * 30;

public collection: MinimongoCollection<T>;

public ready = new ReactiveVar(false);

public name: Name;
protected name: Name;

public eventType: StreamNames;
protected eventType: StreamNames;

public version = 18;
protected version = 18;

public userRelated: boolean;
protected userRelated: boolean;

public updatedAt = new Date(0);
protected updatedAt = new Date(0);

public log: (...args: any[]) => void;
protected log: (...args: any[]) => void;

public timer: ReturnType<typeof setTimeout>;
private timer: ReturnType<typeof setTimeout>;

constructor({ name, eventType = 'notify-user', userRelated = true }: { name: Name; eventType?: StreamNames; userRelated?: boolean }) {
super();

this.collection = new Mongo.Collection(null) as MinimongoCollection<T>;

this.name = name;
Expand All @@ -80,9 +77,14 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
return;
}

CachedCollectionManager.onLogin(() => {
if (process.env.NODE_ENV === 'test') {
return;
}

onLoggedIn(() => {
void this.init();
});

Accounts.onLogout(() => {
this.ready.set(false);
});
Expand Down Expand Up @@ -187,7 +189,6 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<

const { _id } = newRecord;
this.collection.upsert({ _id } as Mongo.Selector<T>, newRecord);
this.emit('changed', newRecord as any); // TODO: investigate why this is needed

if (hasUpdatedAt(newRecord) && newRecord._updatedAt > this.updatedAt) {
this.updatedAt = newRecord._updatedAt;
Expand Down Expand Up @@ -301,7 +302,6 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
if (actionTime > this.updatedAt) {
this.updatedAt = actionTime;
}
this.emit(action, newRecord as any); // TODO: investigate why this is needed
},
timestamp: actionTime.getTime(),
});
Expand All @@ -326,7 +326,6 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
if (actionTime > this.updatedAt) {
this.updatedAt = actionTime;
}
this.emit(action, newRecord as any); // TODO: investigate why this is needed
},
timestamp: actionTime.getTime(),
});
Expand All @@ -353,10 +352,22 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<

this.ready.set(true);

CachedCollectionManager.onReconnect(() => {
this.trySync();
this.reconnectionComputation?.stop();
let wentOffline = Tracker.nonreactive(() => Meteor.status().status === 'offline');
this.reconnectionComputation = Tracker.autorun(() => {
const { status } = Meteor.status();

if (status === 'offline') {
wentOffline = true;
}

if (status === 'connected' && wentOffline) {
this.trySync();
}
});

return this.setupListener();
}

private reconnectionComputation: Tracker.Computation | undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { CachedCollection } from './CachedCollection';

class CachedCollectionManager {
private items = new Set<CachedCollection<any>>();

register(cachedCollection: CachedCollection<any>) {
this.items.add(cachedCollection);
}

clearAllCachesOnLogout() {
for (const item of this.items) {
item.clearCacheOnLogout();
}
}
}

const instance = new CachedCollectionManager();

export {
/** @deprecated */
instance as CachedCollectionManager,
};
2 changes: 2 additions & 0 deletions apps/meteor/client/lib/cachedCollections/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { CachedCollection } from './CachedCollection';
export { CachedCollectionManager } from './CachedCollectionManager';
9 changes: 2 additions & 7 deletions apps/meteor/client/lib/fetchFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { CachedCollectionManager } from '../../app/ui-cached-collection/client';
import { sdk } from '../../app/utils/client/lib/SDKClient';
import { whenLoggedIn } from './loggedIn';

export const fetchFeatures = (): Promise<string[]> =>
new Promise((resolve, reject) => {
CachedCollectionManager.onLogin(() => {
sdk.call('license:getModules').then(resolve, reject);
});
});
export const fetchFeatures = (): Promise<string[]> => whenLoggedIn().then(() => sdk.call('license:getModules'));
40 changes: 40 additions & 0 deletions apps/meteor/client/lib/loggedIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';

const isLoggedIn = () => {
const uid = Tracker.nonreactive(() => Meteor.userId());
return uid !== null;
};

export const whenLoggedIn = () => {
if (isLoggedIn()) {
return Promise.resolve();
}

return new Promise<void>((resolve) => {
const subscription = Accounts.onLogin(() => {
subscription.stop();
resolve();
});
});
};

export const onLoggedIn = (cb: (() => () => void) | (() => Promise<() => void>) | (() => void)) => {
let cleanup: (() => void) | undefined;
const handler = async () => {
cleanup?.();
const ret = await cb();
if (typeof ret === 'function') {
cleanup = ret;
}
};

const subscription = Accounts.onLogin(handler);
if (isLoggedIn()) handler();

return () => {
subscription.stop();
cleanup?.();
};
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ISetting } from '@rocket.chat/core-typings';

import { CachedCollection } from '../../../app/ui-cached-collection/client';
import { sdk } from '../../../app/utils/client/lib/SDKClient';
import { CachedCollection } from '../cachedCollections';

class PrivateSettingsCachedCollection extends CachedCollection<ISetting> {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ISetting } from '@rocket.chat/core-typings';

import { CachedCollection } from '../../../app/ui-cached-collection/client';
import { CachedCollection } from '../cachedCollections';

class PublicSettingsCachedCollection extends CachedCollection<ISetting> {
constructor() {
Expand Down
Loading

0 comments on commit b83bd6e

Please sign in to comment.