Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions client/contexts/AuthorizationContext.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createContext, useContext, useMemo, useCallback } from 'react';
import { useSubscription, Subscription, Unsubscribe } from 'use-subscription';
import { Emitter, Handler } from '@rocket.chat/emitter';
import { Emitter } from '@rocket.chat/emitter';

import { IRole } from '../../definition/IUser';

type IRoles = { [_id: string]: IRole }


export class RoleStore extends Emitter {
export class RoleStore extends Emitter<{
change: IRoles;
}> {
roles: IRoles = {};
}

Expand Down Expand Up @@ -91,7 +93,7 @@ export const useRolesDescription = (): (ids: Array<string>) => [string] => {
const subscription = useMemo(
() => ({
getCurrentValue: (): IRoles => roleStore.roles,
subscribe: (callback: Handler): () => void => {
subscribe: (callback: () => void): () => void => {
roleStore.on('change', callback);
return (): void => {
roleStore.off('change', callback);
Expand Down
5 changes: 4 additions & 1 deletion client/lib/banners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ type BannerPayload = LegacyBannerPayload | UiKitBannerPayload;
export const isLegacyPayload = (payload: BannerPayload): payload is LegacyBannerPayload => !('blocks' in payload);

const queue: BannerPayload[] = [];
const emitter = new Emitter();
const emitter = new Emitter<{
update: undefined;
'update-first': undefined;
}>();

export const firstSubscription: Subscription<BannerPayload | null> = {
getCurrentValue: () => queue[0] ?? null,
Expand Down
4 changes: 3 additions & 1 deletion client/lib/createValueSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ type ValueSubscription<T> = Subscription<T> & {

export const createValueSubscription = <T>(initialValue: T): ValueSubscription<T> => {
let value: T = initialValue;
const emitter = new Emitter();
const emitter = new Emitter<{
update: undefined;
}>();

return {
getCurrentValue: (): T => value,
Expand Down
42 changes: 29 additions & 13 deletions client/lib/presence.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { Emitter, EventType, Handler } from '@rocket.chat/emitter';
import { Emitter, EventHandlerOf } from '@rocket.chat/emitter';

import { APIClient } from '../../app/utils/client';
import { IUser } from '../../definition/IUser';
import { USER_STATUS } from '../../definition/UserStatus';

const emitter = new Emitter();
type InternalEvents = {
remove: IUser['_id'];
reset: undefined;
restart: undefined;
};

type ExternalEvents = {
[key: string]: UserPresence | undefined;
};

type Events = InternalEvents & ExternalEvents;

const emitter = new Emitter<Events>();

const store = new Map<string, UserPresence>();

Expand All @@ -14,7 +27,7 @@ type UsersPresencePayload = {
full: boolean;
};

const isUid = (eventType: EventType): eventType is UserPresence['_id'] =>
const isUid = (eventType: keyof Events): eventType is UserPresence['_id'] =>
Boolean(eventType) && typeof eventType === 'string' && !['reset', 'restart', 'remove'].includes(eventType);

const uids = new Set<UserPresence['_id']>();
Expand All @@ -41,7 +54,7 @@ const getPresence = ((): ((uid: UserPresence['_id']) => void) => {
});

currentUids.forEach((uid) => {
emitter.emit(uid, { _id: uid, status: 'offline' });
emitter.emit(uid, { _id: uid, status: USER_STATUS.OFFLINE });
});

currentUids.clear();
Expand All @@ -68,6 +81,10 @@ const getPresence = ((): ((uid: UserPresence['_id']) => void) => {

emitter.on('reset', () => {
store.clear();
emitter.events()
.filter(isUid).forEach((uid) => {
emitter.emit(uid, undefined);
});
emitter.once('restart', () => {
emitter.events()
.filter(isUid)
Expand All @@ -78,36 +95,35 @@ const getPresence = ((): ((uid: UserPresence['_id']) => void) => {
return get;
})();

const update: Handler<UserPresence> = (update) => {
const update: EventHandlerOf<ExternalEvents, string> = (update) => {
if (update?._id) {
store.set(update._id, update);
uids.delete(update._id);
}
};

const listen = (uid: UserPresence['_id'], handler: Handler<UserPresence>): void => {
const listen = (uid: UserPresence['_id'], handler: EventHandlerOf<ExternalEvents, UserPresence['_id']> | (() => void)): void => {
emitter.on(uid, update);
emitter.on(uid, handler);
emitter.on('reset', handler);

if (store.has(uid)) {
return handler(store.get(uid));
const user = store.has(uid) && store.get(uid);
if (user) {
return handler(user);
}

getPresence(uid);
};

const stop = (uid: UserPresence['_id'], handler: Handler<UserPresence>): void => {
const stop = (uid: UserPresence['_id'], handler: EventHandlerOf<ExternalEvents, UserPresence['_id']> | (() => void)): void => {
setTimeout(() => {
emitter.off(uid, handler);
emitter.off(uid, update);
emitter.off('reset', handler);
emitter.emit('remove', uid);
}, 5000);
};

const reset = (): void => {
emitter.emit('reset', {});
emitter.emit('reset');
store.clear();
};

Expand All @@ -126,7 +142,7 @@ const notify = (update: UserPresence): void => {
};

const get = async (uid: UserPresence['_id']): Promise<UserPresence | undefined> => new Promise((resolve) => {
const callback: Handler<UserPresence> = (args): void => {
const callback: EventHandlerOf<ExternalEvents, UserPresence['_id']> = (args): void => {
resolve(args);
stop(uid, callback);
};
Expand Down
1 change: 0 additions & 1 deletion client/views/room/contexts/RoomContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createContext } from 'react';
// import { Handler } from '@rocket.chat/emitter';

import { IRoom } from '../../../../definition/IRoom';

Expand Down
8 changes: 5 additions & 3 deletions client/views/room/lib/Toolbox/ToolboxContext.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createContext } from 'react';
import { Handler } from '@rocket.chat/emitter';
import { EventHandlerOf } from '@rocket.chat/emitter';

import { actions, listen, ToolboxActionConfig, ToolboxAction } from '.';
import { actions, listen, ToolboxActionConfig, ToolboxAction, Events } from '.';
import './defaultActions';


export type ToolboxEventHandler = (handler: EventHandlerOf<Events, 'change'>) => Function;

export type ChannelContextValue = {
actions: Map<ToolboxActionConfig['id'], ToolboxAction>;
listen: (handler: Handler) => Function;
listen: ToolboxEventHandler;
tabBar?: any;
context?: any;
open: Function;
Expand Down
18 changes: 11 additions & 7 deletions client/views/room/lib/Toolbox/generator.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Emitter, Handler } from '@rocket.chat/emitter';
import { Emitter, EventHandlerOf } from '@rocket.chat/emitter';


export type Store<T> = Map<string, T>;

export const generator = function generator<T>(name?: string):
{

export type Events<T> = {
change: Store<T>;
};

export const generator = function generator<T>(name?: string): ({
store: Store<T>;
add: (id: string, action: T) => Store<T>;
remove: (id: string) => boolean;
listen: (handler: Handler) => Function;
listen: (handler: EventHandlerOf<Events<T>, 'change'>) => Function;
name: string | undefined;
} {
}) {
const store: Store<T> = new Map();
const emitter = new Emitter();
const emitter = new Emitter<Events<T>>();

const add = (id: string, action: T): Store<T> => {
store.set(id, action);
Expand All @@ -26,7 +30,7 @@ export const generator = function generator<T>(name?: string):
return result;
};

const listen = (handler: Handler): Function => emitter.on('change', handler);
const listen = (handler: EventHandlerOf<Events<T>, 'change'>): Function => emitter.on('change', handler);

return Object.freeze({
store,
Expand Down
4 changes: 3 additions & 1 deletion client/views/room/lib/Toolbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC, LazyExoticComponent, ReactNode, MouseEvent } from 'react';
import { BoxProps, OptionProps } from '@rocket.chat/fuselage';

import { IRoom } from '../../../../../definition/IRoom';
import { generator } from './generator';
import { generator, Events as GeneratorEvents } from './generator';


type ToolboxHook = ({ room }: { room: IRoom }) => ToolboxActionConfig | null
Expand Down Expand Up @@ -38,4 +38,6 @@ export type ToolboxAction = ToolboxHook | ToolboxActionConfig;

const { listen, add: addAction, remove: deleteAction, store: actions } = generator<ToolboxAction>();

export type Events = GeneratorEvents<ToolboxAction>;

export { listen, addAction, deleteAction, actions };
5 changes: 2 additions & 3 deletions client/views/room/providers/ToolboxProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { ReactNode, useContext, useMemo, useState, useCallback, useLayoutEffect } from 'react';
import { useDebouncedState, useMutableCallback, useSafely } from '@rocket.chat/fuselage-hooks';
import { Handler } from '@rocket.chat/emitter';

import { ToolboxContext } from '../lib/Toolbox/ToolboxContext';
import { ToolboxContext, ToolboxEventHandler } from '../lib/Toolbox/ToolboxContext';
import { ToolboxAction, ToolboxActionConfig } from '../lib/Toolbox/index';
import { IRoom } from '../../../../definition/IRoom';
import { useCurrentRoute, useRoute } from '../../../contexts/RouterContext';
Expand Down Expand Up @@ -37,7 +36,7 @@ const VirtualAction = React.memo(({ handleChange, room, action, id }: { id: stri
return null;
});

const useToolboxActions = (room: IRoom): { listen: (handler: Handler<any>) => Function; actions: Array<[string, ToolboxAction]> } => {
const useToolboxActions = (room: IRoom): { listen: ToolboxEventHandler; actions: Array<[string, ToolboxAction]> } => {
const { listen, actions } = useContext(ToolboxContext);
const [state, setState] = useState<Array<[string, ToolboxAction]>>(Array.from(actions.entries()));

Expand Down
Loading