Skip to content

Events

Although most of the VSCode APIs are covered by reactive-vscode, sometimes you still need to create or listen to the primitive VSCode events.

useEvent converts a raw event to an auto-disposed event:
ts
import { 
defineExtension
,
useEvent
} from 'reactive-vscode'
import { workspace } from 'vscode' const
onDidCreateFiles
=
useEvent
(workspace.
onDidCreateFiles
)
export =
defineExtension
(() => {
// No need to dispose the event
onDidCreateFiles
((
e
) => {
console
.
log
('Files created:',
e
.
files
)
}) })
useEventEmitter creates a friendly event emitter which still extends EventEmitter:
ts
import { 
defineExtension
,
useEventEmitter
} from 'reactive-vscode'
export =
defineExtension
(() => {
const
myEvent
=
useEventEmitter
<string>([/* optional listenrs */])
myEvent
.
addListener
((
msg
) => {
console
.
log
(`Received message: ${
msg
}`)
})
myEvent
.
fire
('Hello, World!')
someVscodeApi
({
onSomeEvent
:
myEvent
.
event
,
}) })

You can also convert a raw event to a friendly event emitter:

ts
import { 
defineExtension
,
useEventEmitter
} from 'reactive-vscode'
import {
EventEmitter
} from 'vscode'
export =
defineExtension
(() => {
const
rawEvent
= new
EventEmitter
<string>()
const
myEvent
=
useEventEmitter
(
rawEvent
, [/* optional listenrs */])
})