Create types.d.ts for camera type definition
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
"description": "WebAssembly implementation of gphoto2 and libusb to control DSLR cameras over USB on the Web",
|
"description": "WebAssembly implementation of gphoto2 and libusb to control DSLR cameras over USB on the Web",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/camera.js",
|
"main": "src/camera.js",
|
||||||
"types": "src/libapi.mjs.d.ts",
|
"types": "src/types.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -16,11 +16,6 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import('./libapi.mjs').Config} Config
|
|
||||||
* @typedef {import('./libapi.mjs').SupportedOps} SupportedOps
|
|
||||||
* @typedef {import('./libapi.mjs').Context} Context
|
|
||||||
*/
|
|
||||||
import initModule from "../build/libapi.mjs";
|
import initModule from "../build/libapi.mjs";
|
||||||
|
|
||||||
export function rethrowIfCritical(err) {
|
export function rethrowIfCritical(err) {
|
||||||
@@ -36,21 +31,13 @@ const INTERFACE_SUBCLASS = 1; // MTP
|
|||||||
|
|
||||||
let ModulePromise = null;
|
let ModulePromise = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* This class provides methods for interacting with the camera.
|
|
||||||
*/
|
|
||||||
class Camera {
|
class Camera {
|
||||||
constructor() {
|
constructor() {
|
||||||
/** @type {Promise<unknown>} */
|
|
||||||
this.queue = Promise.resolve();
|
this.queue = Promise.resolve();
|
||||||
this.Module = null;
|
this.Module = null;
|
||||||
this.context = null;
|
this.context = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method shows the camera picker.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async showCameraPicker() {
|
async showCameraPicker() {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
await navigator.usb.requestDevice({
|
await navigator.usb.requestDevice({
|
||||||
@@ -63,10 +50,6 @@ class Camera {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method connects to the camera.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async connect() {
|
async connect() {
|
||||||
if (!ModulePromise) {
|
if (!ModulePromise) {
|
||||||
ModulePromise = initModule()
|
ModulePromise = initModule()
|
||||||
@@ -75,41 +58,22 @@ class Camera {
|
|||||||
this.context = await new this.Module.Context();
|
this.context = await new this.Module.Context();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method schedules an exclusive async operation on the global context.
|
|
||||||
* @template T
|
|
||||||
* @param {(ctx: Context) => Promise<T>} op
|
|
||||||
* @returns {Promise<T>}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
async schedule(op) {
|
async schedule(op) {
|
||||||
let res = this.queue.then(() => op(this.context));
|
let res = this.queue.then(() => op(this.context));
|
||||||
this.queue = res.catch(rethrowIfCritical);
|
this.queue = res.catch(rethrowIfCritical);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method disconnects from the camera.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async disconnect() {
|
async disconnect() {
|
||||||
if (!this.context.isDeleted()) {
|
if (!this.context.isDeleted()) {
|
||||||
this.context.delete();
|
this.context.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method gets the camera configuration.
|
|
||||||
* @returns {Promise<Config>}
|
|
||||||
*/
|
|
||||||
async getConfig() {
|
async getConfig() {
|
||||||
return this.schedule((context) => context.configToJS());
|
return this.schedule((context) => context.configToJS());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method gets the supported operations of the camera.
|
|
||||||
* @returns {Promise<SupportedOps>}
|
|
||||||
*/
|
|
||||||
async getSupportedOps() {
|
async getSupportedOps() {
|
||||||
if (this.context) {
|
if (this.context) {
|
||||||
return await this.context.supportedOps();
|
return await this.context.supportedOps();
|
||||||
@@ -117,12 +81,6 @@ class Camera {
|
|||||||
throw new Error("You need to connect to the camera first");
|
throw new Error("You need to connect to the camera first");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method sets a configuration value on the camera.
|
|
||||||
* @param {string} name
|
|
||||||
* @param {number | string | boolean} value
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async setConfigValue(name, value) {
|
async setConfigValue(name, value) {
|
||||||
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
|
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
|
||||||
const setResult = this.schedule((context) =>
|
const setResult = this.schedule((context) =>
|
||||||
@@ -131,29 +89,17 @@ class Camera {
|
|||||||
return Promise.all([setResult, uiTimeout]);
|
return Promise.all([setResult, uiTimeout]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method captures a preview as a Blob.
|
|
||||||
* @returns {Promise<Blob>}
|
|
||||||
*/
|
|
||||||
async capturePreviewAsBlob() {
|
async capturePreviewAsBlob() {
|
||||||
return this.schedule((context) => context.capturePreviewAsBlob());
|
return this.schedule((context) => context.capturePreviewAsBlob());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method captures an image as a File.
|
|
||||||
* @returns {Promise<File>}
|
|
||||||
*/
|
|
||||||
async captureImageAsFile() {
|
async captureImageAsFile() {
|
||||||
return this.schedule((context) => context.captureImageAsFile());
|
return this.schedule((context) => context.captureImageAsFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method consumes camera events.
|
|
||||||
* @returns {Promise<boolean>}
|
|
||||||
*/
|
|
||||||
async consumeEvents() {
|
async consumeEvents() {
|
||||||
return this.schedule((context) => context.consumeEvents());
|
return this.schedule((context) => context.consumeEvents());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Camera;
|
export { Camera };
|
||||||
|
|||||||
25
src/types.d.ts
vendored
Normal file
25
src/types.d.ts
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// Import types Context and SupportedOps ./libapi.mjs.d.ts
|
||||||
|
import type { Config, Context, SupportedOps } from './libapi.mjs.d.ts';
|
||||||
|
|
||||||
|
export declare class Camera {
|
||||||
|
constructor();
|
||||||
|
|
||||||
|
showCameraPicker(): Promise<void>;
|
||||||
|
connect(): Promise<void>;
|
||||||
|
disconnect(): Promise<void>;
|
||||||
|
|
||||||
|
getConfig(): Promise<Config>;
|
||||||
|
|
||||||
|
getSupportedOps(): SupportedOps;
|
||||||
|
|
||||||
|
setConfigValue(name: string, value: number | string | boolean): Promise<void>;
|
||||||
|
|
||||||
|
capturePreviewAsBlob(): Promise<Blob>;
|
||||||
|
captureImageAsFile(): Promise<File>;
|
||||||
|
consumeEvents(): Promise<boolean>;
|
||||||
|
|
||||||
|
private rethrowIfCritical(err: any): void; // any error type can be replaced by more specific if available
|
||||||
|
private schedule<T>(op: (ctx: any) => Promise<T>): Promise<T>; // ctx and T types can be replaced by more specific if available
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { Config, Context, SupportedOps };
|
||||||
Reference in New Issue
Block a user