Keep your UI in step with the camera without polling: Darkgrade Slate calls you when a setting changes, for example when someone turns a dial on the body, and when a new photo or clip lands on the card.
import { Camera } from '@darkgrade/slate'
const camera = new Camera()
await camera.connect()
camera.on('settingsChanged', async () => {
const { controls } = await camera.getCapabilities()
console.log(controls.iso?.value, controls.shutterSpeed?.value)
})
camera.on('mediaAdded', async () => {
const [newest] = await camera.listMedia() // newest first
console.log('New file', newest?.name)
})The two events
| Event | Fires when |
|---|---|
settingsChanged | A setting changed on the camera. Then read getCapabilities(). |
mediaAdded | A new photo or clip is on the card. Then read listMedia(). |
Handlers take no arguments. An event says that something changed, not what: read the camera for the new state.
Subscribe and unsubscribe
const refresh = () => console.log('settings changed')
camera.on('settingsChanged', refresh)
camera.off('settingsChanged', refresh) // stop this handler
camera.off('settingsChanged') // stop every handler of the eventYou can call on before connect(): the handlers start when the camera connects.
Save each photo to a folder as you shoot (for Lightroom Auto Import or Capture One)
To see each photo in your editor moments after you take it, copy every new file off the card into a folder the editor watches: a watched folder, which Capture One calls a hot folder. mediaAdded says a file landed, listMedia() names it and downloadMedia() copies it:
import fs from 'node:fs'
import path from 'node:path'
import { Camera } from '@darkgrade/slate'
const folder = '/Users/me/Pictures/Tethered'
fs.mkdirSync(folder, { recursive: true })
const camera = new Camera()
await camera.connect()
// Files already on the card are not copied.
const seen = new Set((await camera.listMedia()).map(item => item.id))
async function saveNew() {
for (const item of await camera.listMedia()) {
if (seen.has(item.id)) continue
const bytes = await camera.downloadMedia(item.id)
// Write under another name, then rename, so the
// editor never opens half a file.
const partial = path.join(folder, `.${item.name}.part`)
fs.writeFileSync(partial, bytes)
fs.renameSync(partial, path.join(folder, item.name))
seen.add(item.id)
}
}
// One run at a time: a camera can send several events
// for one photo.
let work = Promise.resolve()
camera.on('mediaAdded', () => {
work = work.then(saveNew).catch(console.error)
})The camera must be saving to its card: the event is about files on the card. Clips are copied too; skip them with item.kind. A file whose name is already in the folder is replaced.
Then point the editor at the folder. In Lightroom Classic, Auto Import takes it as its watched folder; Adobe says the folder must be empty when you choose it, and that Lightroom moves each photo out of it into a destination folder. In Capture One, Hot Folder monitors a folder for image files.
Cases that bite
disconnect()drops every handler. Subscribe again after you reconnect.- One change can fire several events. The handler runs once for each event the camera sends, and a camera may send more than one for a single change. Debounce a handler that reads the camera or redraws a UI.
- Nothing catches a handler's errors. A handler returns nothing, so a rejected promise from an
asynchandler is unhandled. Wrap the body intry/catch. - Nikon events are polled. A Nikon queues its events until they are read. While at least one handler is subscribed, Slate reads the queue every 500 ms, so an event can arrive up to half a second late.
From a shell, darkgrade camera watch streams the same two events: see Watch for events.
The camera's own events
settingsChanged and mediaAdded mean the same thing on every brand. Each camera also sends its own PTP events (capture complete, storage changes, vendor events), with parameters. To receive one, subscribe on the protocol session: see Listen to raw events. That entry is unsupported and its names can change between releases.