Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to create a context from a SocketlessServer #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
93 changes: 84 additions & 9 deletions packages/socketless/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ class SenderContext<TResponse extends WebsocketMessage>
{
private feeds: string[] = [];
private clients: string[] = [];
private messages: z.infer<typeof WebhookMessageResponseSchema>[];

constructor(messages: z.infer<typeof WebhookMessageResponseSchema>[]) {
this.messages = messages;
private sendMessage: (
message: z.infer<typeof WebhookMessageResponseSchema>,
) => void;

constructor(
sendMessage: (
message: z.infer<typeof WebhookMessageResponseSchema>,
) => void,
) {
this.sendMessage = sendMessage;
}

toFeed(feed: string) {
Expand Down Expand Up @@ -60,7 +66,7 @@ class SenderContext<TResponse extends WebsocketMessage>
}

send(message: TResponse): void {
this.messages.push({
this.sendMessage({
message,
clients: this.clients,
feeds: this.feeds,
Expand Down Expand Up @@ -143,31 +149,31 @@ function createContext<
* Builder to send messages to clients or feeds
*/
toClient(identifier) {
const sendContext = new SenderContext<TResponse>(messagesToSend);
const sendContext = new SenderContext<TResponse>(messagesToSend.push);

return sendContext.toClient(identifier);
},
/**
* Builder to send messages to clients or feeds
*/
toClients(identifier: string[]) {
const sendContext = new SenderContext<TResponse>(messagesToSend);
const sendContext = new SenderContext<TResponse>(messagesToSend.push);

return sendContext.toClients(identifier);
},
/**
* Builder to send messages to clients or feeds
*/
toFeed(feed: string) {
const sendContext = new SenderContext<TResponse>(messagesToSend);
const sendContext = new SenderContext<TResponse>(messagesToSend.push);

return sendContext.toFeed(feed);
},
/**
* Builder to send messages to clients or feeds
*/
toFeeds(feeds: string[]) {
const sendContext = new SenderContext<TResponse>(messagesToSend);
const sendContext = new SenderContext<TResponse>(messagesToSend.push);

return sendContext.toFeeds(feeds);
},
Expand Down Expand Up @@ -496,6 +502,75 @@ class SocketlessServer<
throw new Error(`Failed to send message ${req.status} ${req.statusText}`);
}
}

public createContext(): PublicSocketlessContext<TResponse> {
const manageFeedsSimplified = (
feeds: string | string[],
clients: string | string[] | undefined,
action: WebhookFeedsManageResponseType["action"],
) => {
if (
clients === undefined ||
(Array.isArray(clients) && clients.length === 0)
) {
throw new Error(
"You must specify at least one client to join the feed",
);
}

void this.manageFeeds([
{
feeds: feeds,
action: action,
clients: clients,
},
]);
};

const send = (...args: Parameters<typeof this.sendMessage>) => {
void this.sendMessage(...args);
};

const sendMessageContext = (
message: z.infer<typeof WebhookMessageResponseSchema>,
) => {
void send(message.message as unknown as TResponse, {
identifiers: message.clients,
feeds: message.feeds,
});
};

return {
send: send,
joinFeed: (feed, clients) => manageFeedsSimplified(feed, clients, "join"),
joinFeeds: (feeds, clients) =>
manageFeedsSimplified(feeds, clients, "join"),
leaveFeed: (feed, clients) =>
manageFeedsSimplified(feed, clients, "leave"),
leaveFeeds: (feeds, clients) =>
manageFeedsSimplified(feeds, clients, "leave"),
toClient: (identifier) => {
const sendContext = new SenderContext<TResponse>(sendMessageContext);

return sendContext.toClient(identifier);
},
toClients: (identifiers) => {
const sendContext = new SenderContext<TResponse>(sendMessageContext);

return sendContext.toClients(identifiers);
},
toFeed: (feed) => {
const sendContext = new SenderContext<TResponse>(sendMessageContext);

return sendContext.toFeed(feed);
},
toFeeds: (feeds) => {
const sendContext = new SenderContext<TResponse>(sendMessageContext);

return sendContext.toFeeds(feeds);
},
};
}
}

export function createSocketless<
Expand Down