SocketProvider -> WebsocketProvider
This commit is contained in:
@@ -17,7 +17,6 @@ import {
|
|||||||
PartialWithId,
|
PartialWithId,
|
||||||
WebsocketCollectionUpdateIndexEvent,
|
WebsocketCollectionUpdateIndexEvent,
|
||||||
WebsocketCollectionUserEvent,
|
WebsocketCollectionUserEvent,
|
||||||
WebsocketDocumentDeletedEvent,
|
|
||||||
WebsocketEntitiesEvent,
|
WebsocketEntitiesEvent,
|
||||||
WebsocketEntityDeletedEvent,
|
WebsocketEntityDeletedEvent,
|
||||||
} from "~/types";
|
} from "~/types";
|
||||||
@@ -28,14 +27,14 @@ type SocketWithAuthentication = Socket & {
|
|||||||
authenticated?: boolean;
|
authenticated?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const SocketContext = React.createContext<SocketWithAuthentication | null>(
|
export const WebsocketContext = React.createContext<SocketWithAuthentication | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
type Props = RootStore;
|
type Props = RootStore;
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
class SocketProvider extends React.Component<Props> {
|
class WebsocketProvider extends React.Component<Props> {
|
||||||
@observable
|
@observable
|
||||||
socket: SocketWithAuthentication | null;
|
socket: SocketWithAuthentication | null;
|
||||||
|
|
||||||
@@ -242,19 +241,14 @@ class SocketProvider extends React.Component<Props> {
|
|||||||
|
|
||||||
this.socket.on(
|
this.socket.on(
|
||||||
"documents.delete",
|
"documents.delete",
|
||||||
action((event: WebsocketDocumentDeletedEvent) => {
|
action((event: PartialWithId<Document>) => {
|
||||||
const document = documents.get(event.modelId);
|
documents.add(event);
|
||||||
|
policies.remove(event.id);
|
||||||
|
|
||||||
if (event.collectionId) {
|
if (event.collectionId) {
|
||||||
const collection = collections.get(event.collectionId);
|
const collection = collections.get(event.collectionId);
|
||||||
collection?.removeDocument(event.modelId);
|
collection?.removeDocument(event.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document) {
|
|
||||||
document.deletedAt = new Date().toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
policies.remove(event.modelId);
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -428,11 +422,11 @@ class SocketProvider extends React.Component<Props> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<SocketContext.Provider value={this.socket}>
|
<WebsocketContext.Provider value={this.socket}>
|
||||||
{this.props.children}
|
{this.props.children}
|
||||||
</SocketContext.Provider>
|
</WebsocketContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withStores(SocketProvider);
|
export default withStores(WebsocketProvider);
|
||||||
@@ -11,7 +11,7 @@ import Layout from "~/components/AuthenticatedLayout";
|
|||||||
import CenteredContent from "~/components/CenteredContent";
|
import CenteredContent from "~/components/CenteredContent";
|
||||||
import PlaceholderDocument from "~/components/PlaceholderDocument";
|
import PlaceholderDocument from "~/components/PlaceholderDocument";
|
||||||
import Route from "~/components/ProfiledRoute";
|
import Route from "~/components/ProfiledRoute";
|
||||||
import SocketProvider from "~/components/SocketProvider";
|
import WebsocketProvider from "~/components/WebsocketProvider";
|
||||||
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
||||||
import usePolicy from "~/hooks/usePolicy";
|
import usePolicy from "~/hooks/usePolicy";
|
||||||
import { matchDocumentSlug as slug } from "~/utils/routeHelpers";
|
import { matchDocumentSlug as slug } from "~/utils/routeHelpers";
|
||||||
@@ -67,7 +67,7 @@ function AuthenticatedRoutes() {
|
|||||||
const can = usePolicy(team);
|
const can = usePolicy(team);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SocketProvider>
|
<WebsocketProvider>
|
||||||
<Layout>
|
<Layout>
|
||||||
<React.Suspense
|
<React.Suspense
|
||||||
fallback={
|
fallback={
|
||||||
@@ -116,7 +116,7 @@ function AuthenticatedRoutes() {
|
|||||||
</Switch>
|
</Switch>
|
||||||
</React.Suspense>
|
</React.Suspense>
|
||||||
</Layout>
|
</Layout>
|
||||||
</SocketProvider>
|
</WebsocketProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { USER_PRESENCE_INTERVAL } from "@shared/constants";
|
import { USER_PRESENCE_INTERVAL } from "@shared/constants";
|
||||||
import { SocketContext } from "~/components/SocketProvider";
|
import { WebsocketContext } from "~/components/WebsocketProvider";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
documentId: string;
|
documentId: string;
|
||||||
@@ -8,9 +8,9 @@ type Props = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default class SocketPresence extends React.Component<Props> {
|
export default class SocketPresence extends React.Component<Props> {
|
||||||
static contextType = SocketContext;
|
static contextType = WebsocketContext;
|
||||||
|
|
||||||
previousContext: typeof SocketContext;
|
previousContext: typeof WebsocketContext;
|
||||||
|
|
||||||
editingInterval: ReturnType<typeof setInterval>;
|
editingInterval: ReturnType<typeof setInterval>;
|
||||||
|
|
||||||
|
|||||||
@@ -188,11 +188,6 @@ export type WebsocketEntityDeletedEvent = {
|
|||||||
modelId: string;
|
modelId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WebsocketDocumentDeletedEvent = WebsocketEntityDeletedEvent & {
|
|
||||||
modelId: string;
|
|
||||||
collectionId: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type WebsocketEntitiesEvent = {
|
export type WebsocketEntitiesEvent = {
|
||||||
documentIds: { id: string; updatedAt?: string }[];
|
documentIds: { id: string; updatedAt?: string }[];
|
||||||
collectionIds: { id: string; updatedAt?: string }[];
|
collectionIds: { id: string; updatedAt?: string }[];
|
||||||
|
|||||||
@@ -55,26 +55,6 @@ export default class WebsocketsProcessor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
case "documents.delete": {
|
|
||||||
const document = await Document.findByPk(event.documentId, {
|
|
||||||
paranoid: false,
|
|
||||||
});
|
|
||||||
if (!document) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return socketio
|
|
||||||
.to(
|
|
||||||
document.publishedAt
|
|
||||||
? `collection-${document.collectionId}`
|
|
||||||
: `user-${document.createdById}`
|
|
||||||
)
|
|
||||||
.emit(event.name, {
|
|
||||||
modelId: event.documentId,
|
|
||||||
collectionId: event.collectionId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
case "documents.permanent_delete": {
|
case "documents.permanent_delete": {
|
||||||
return socketio
|
return socketio
|
||||||
.to(`collection-${event.collectionId}`)
|
.to(`collection-${event.collectionId}`)
|
||||||
@@ -84,6 +64,7 @@ export default class WebsocketsProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "documents.archive":
|
case "documents.archive":
|
||||||
|
case "documents.delete":
|
||||||
case "documents.update": {
|
case "documents.update": {
|
||||||
const document = await Document.findByPk(event.documentId, {
|
const document = await Document.findByPk(event.documentId, {
|
||||||
paranoid: false,
|
paranoid: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user