-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: refactor TerminalLogs component to use EventBus for event han…
…dling and add showLogs prop
- Loading branch information
Showing
11 changed files
with
127 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { VariantProps } from 'class-variance-authority'; | ||
|
||
import { wrapperTerminalLogsVariants } from './TerminalLogs.variants'; | ||
import { TerminalEvent, TerminalEventCallback } from './event-bus'; | ||
|
||
export type TerminalLogsProps = VariantProps< | ||
typeof wrapperTerminalLogsVariants | ||
> & { | ||
className?: string; | ||
listeners?: Record<TerminalEvent, TerminalEventCallback<TerminalEvent>>; | ||
showLogs?: boolean; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export * from './terminal/terminal'; | ||
export * from './events/events'; | ||
export * from './events/events.types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { Content } from '@radix-ui/react-tabs'; | ||
import { FC, PropsWithChildren } from 'react'; | ||
import { ComponentRef, forwardRef } from 'react'; | ||
|
||
export const Body: FC<PropsWithChildren> = ({ children }) => { | ||
export const Body = forwardRef<ComponentRef<'div'>>((_, ref) => { | ||
return ( | ||
<div className="mt-5 rounded-md overflow-hidden w-full relative"> | ||
<Content className="flex-1 bg-slate-200" value="tab-1"> | ||
{children} | ||
</Content> | ||
<Content className="flex-1 bg-slate-200" value="tab-2"> | ||
{children} | ||
<div className="mt-5 rounded overflow-hidden w-full relative"> | ||
<Content className="flex-1 bg-[#0F172A] text-white" value="tab-1"> | ||
<p>Hello World</p> | ||
</Content> | ||
<Content className="flex-1 bg-[#0F172A] p-3" value="tab-2" ref={ref} /> | ||
</div> | ||
); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,61 @@ | ||
import { EventEmitter } from 'events'; | ||
|
||
import { IEventEmitter } from './event-bus.types'; | ||
|
||
export const eventEmitter = (): IEventEmitter => { | ||
const emitter = new EventEmitter(); | ||
|
||
return { | ||
on(event, callback) { | ||
emitter.on(event, callback); | ||
|
||
return () => { | ||
return emitter.off(event, callback); | ||
}; | ||
}, | ||
|
||
off(event, callback) { | ||
emitter.off(event, callback); | ||
}, | ||
|
||
emit(event, data?) { | ||
const eventData = { | ||
type: event, | ||
timestamp: Date.now(), | ||
...data, | ||
}; | ||
|
||
emitter.emit(event, eventData); | ||
}, | ||
|
||
removeAllListeners() { | ||
emitter.removeAllListeners(); | ||
}, | ||
}; | ||
}; | ||
import { | ||
EventEmitterExtraDataArgs, | ||
IEventEmitter, | ||
TerminalEvent, | ||
TerminalEventCallback, | ||
} from './event-bus.types'; | ||
|
||
export class EventBus implements IEventEmitter { | ||
private readonly emitter: EventEmitter; | ||
private readonly showLogs: boolean; | ||
|
||
constructor({ showLogs }: { showLogs: boolean }) { | ||
this.emitter = new EventEmitter(); | ||
this.showLogs = showLogs; | ||
} | ||
|
||
on<T extends TerminalEvent>( | ||
event: T, | ||
callback: TerminalEventCallback<T>, | ||
): VoidFunction { | ||
const handleCallback: TerminalEventCallback<T> = (data) => { | ||
if (this.showLogs) { | ||
console.log('Event:', event, data); | ||
} | ||
callback(data); | ||
}; | ||
|
||
this.emitter.on(event, handleCallback); | ||
|
||
return () => { | ||
return this.emitter.off(event, callback); | ||
}; | ||
} | ||
|
||
off<T extends TerminalEvent>( | ||
event: T, | ||
callback: TerminalEventCallback<T>, | ||
): void { | ||
this.emitter.off(event, callback); | ||
} | ||
|
||
emit<T extends TerminalEvent>( | ||
event: T, | ||
...data: EventEmitterExtraDataArgs<T> | ||
) { | ||
const [extraData] = data; | ||
const eventData = { | ||
type: event, | ||
timestamp: Date.now(), | ||
...extraData, | ||
}; | ||
|
||
this.emitter.emit(event, eventData); | ||
} | ||
|
||
removeAllListeners(): void { | ||
this.emitter.removeAllListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters