Skip to content

Commit

Permalink
Cleaned up exports
Browse files Browse the repository at this point in the history
also added control for auto mapping of destinations.
  • Loading branch information
daveshanley committed Feb 7, 2025
1 parent 9328f83 commit 19da849
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 25 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"description": "A tiny event bus to connect to the ranch",
"type": "module",
"main": "./dist/ranch.js",
"types": "./dist/index.d.ts",
"types": "./dist/ranch.d.ts",
"exports": {
".": {
"import": "./dist/ranch.js",
"types": "./dist/ranch.d.ts"
}
},
"scripts": {
"dev": "vite",
"build": "vite build",
Expand Down
70 changes: 68 additions & 2 deletions src/bus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Client, IPublishParams, StompConfig} from "@stomp/stompjs";
import {ranch} from "./bus_engine.ts";
import {ranch} from "./bus_engine.js";

/**
* A callback for a message received on a channel
Expand Down Expand Up @@ -32,26 +32,92 @@ export interface Subscriber {
callback: BusCallback
}

/**
* Message is what is sent over each channel, it wraps a payload, ID and command.
*/
export interface Message<T = any> {
id?: string
command?: string
payload?: T
}

/**
* Channel has two methods, publish and subscribe. Subscriptions return a Subscription
* and require a BusCallback.
*/
export interface Channel {
name: string

/**
* Subscribe to the channel with a callback for every message
* @param callback
*/
subscribe(callback: BusCallback): Subscription

/**
* Publish a Message to this channel for all subscribers.
* @param message
*/
publish(message: Message): void
}

export class RanchConfig extends StompConfig {
public mapChannelsOnConnect?: boolean = true;
}

/**
* Bus is the core event bus of the ranch. It extends across the UI and the backend.
*/
export interface Bus {

/**
* Available channels
*/
channels: Channel[]

/**
* Create a new channel on the bus with a channel name. Returns a Channel
* @param channelName
*/
createChannel(channelName: string): Channel

/**
* Get a Channel from the bus using its string name.
* @param channelName
*/
getChannel(channelName: string): Channel
connectToBroker(config: StompConfig): void;

/**
* Connect to a ranch compatible broker (ranch talks STOMP).
* @param config the configuration for the ranch broker.
*
*/
connectToBroker(config: RanchConfig): void;

/**
* Map a Channel to a destination on the ranch broker. This creates a 'fusion' between the destination
* on the broker, and the local event bus. It then allows subscriptions of messages on local channels.
*
* @param destination the full destination of the topic/queue on the broker.
* @param channel the name of the channel on the bus to map to the desintation.
*/
mapChannelToBrokerDestination(destination: string, channel: string): void

/**
* Return the STOMP client used under the covers.
*/
getClient(): Client | undefined

/**
* Publish something
* @param params
*/
publish(params: IPublishParams): void

/**
* Manually map channels, if turned off in RanchConfig.
*/
mapChannels(): void;
}

export interface CommandResponse<T = any> {
Expand Down
48 changes: 31 additions & 17 deletions src/bus_engine.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {Client, IPublishParams, StompConfig} from "@stomp/stompjs";
import {RanchUtils} from "./utils.ts";
import {Bus, BusCallback, Channel, Subscriber, Subscription} from "./bus.ts";
import {Client, IPublishParams} from "@stomp/stompjs";
import {RanchUtils} from "./utils.js";
import {Bus, BusCallback, Channel, RanchConfig, Subscriber, Subscription} from "./bus.js";

export class ranch implements Bus {
private _channels: Channel[] = []
private _stompClient: Client | undefined = undefined;
private _stompClient: Client | undefined = undefined;
private _preMappedChannels: Map<string, string>

constructor() {
this._preMappedChannels = new Map<string, string>()
}
Expand All @@ -17,6 +18,7 @@ export class ranch implements Bus {
get channels(): Channel[] {
return this._channels
}

createChannel(channelName: string): Channel {
const chan = new channel(channelName)
this._channels.push(chan)
Expand All @@ -32,13 +34,13 @@ export class ranch implements Bus {
}
}

connectToBroker(config: StompConfig) {
connectToBroker(config: RanchConfig) {
this._stompClient = new Client(config)
this._stompClient.activate()
this._stompClient.onConnect = (frame) => {
this._preMappedChannels.forEach((channel: string, destination: string) => {
this._mapDestination(destination, channel)
});
if (config.mapChannelsOnConnect) {
this.mapChannels();
}
if (config.onConnect) {
config.onConnect(frame);
}
Expand All @@ -58,16 +60,13 @@ export class ranch implements Bus {
this._stompClient.activate()
}

private _mapDestination(destination: string, channel: string) {
if (this._stompClient) {
this._stompClient.subscribe(destination, message => {
const chan = this._channels.find(c => c.name === channel)
if (chan) {
chan.publish({payload: JSON.parse(message.body)})
}
});
}
mapChannels() {
this._preMappedChannels.forEach((channel: string, destination: string) => {
this._mapDestination(destination, channel)
});
this._preMappedChannels.clear()
}

mapChannelToBrokerDestination(destination: string, channel: string) {
if (!this._stompClient || !this._stompClient.connected) {
this._preMappedChannels.set(destination, channel)
Expand All @@ -81,17 +80,31 @@ export class ranch implements Bus {
this._stompClient.publish(params)
}
}

private _mapDestination(destination: string, channel: string) {
if (this._stompClient) {
this._stompClient.subscribe(destination, message => {
const chan = this._channels.find(c => c.name === channel)
if (chan) {
chan.publish({payload: JSON.parse(message.body)})
}
});
}
}
}

class channel implements Channel {
private subscribers: Subscriber[] = []
private readonly _name: string

constructor(channelName: string) {
this._name = channelName
}

get name(): string {
return this._name
}

subscribe(callback: BusCallback): Subscription {
const subscriber: Subscriber = {
name: RanchUtils.genUUID(),
Expand All @@ -105,6 +118,7 @@ class channel implements Channel {
}
}
}

publish(message: any): void {
this.subscribers.forEach((s: Subscriber) => s.callback(message))
}
Expand Down
File renamed without changes.
15 changes: 10 additions & 5 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
build: {
minify: true,
lib: {
entry: resolve(__dirname, 'src/index.ts'),
entry: resolve(__dirname, 'src/ranch.ts'),
name: 'ranch',
fileName: 'ranch',
formats: ['es'],
Expand All @@ -16,12 +16,17 @@ export default defineConfig({
rollupOptions: {
output: {
globals: {
"@stomp/stompjs": "@stomp/stompjs"},
"@stomp/stompjs": "@stomp/stompjs",
},
},
external: [
"@stomp/stompjs",
],
}
},
},
plugins: [dts()]
});
plugins: [
dts({
//rollupTypes: true, // This bundles the declarations into a file named ranch.d.ts in dist/
}),
],
});

0 comments on commit 19da849

Please sign in to comment.