Improve folder structure

This commit is contained in:
ICheered
2023-08-04 17:49:09 +02:00
parent d0841c4c85
commit 97210914c9
19 changed files with 134 additions and 129 deletions

2
.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
build/* binary
build/* linguist-generated=true

View File

@@ -15,7 +15,7 @@ export LDFLAGS += $(COMMON_FLAGS)
## Main API module ## Main API module
ui/libapi.mjs: api.o $(SYSROOT)/lib/libltdl.la $(SYSROOT)/lib/libgphoto2.la build/libapi.mjs: api.o $(SYSROOT)/lib/libltdl.la $(SYSROOT)/lib/libgphoto2.la
libtool --verbose --mode=link $(LD) $(LDFLAGS) -o $@ $+ \ libtool --verbose --mode=link $(LD) $(LDFLAGS) -o $@ $+ \
-fexceptions --bind -s ASYNCIFY -s ALLOW_MEMORY_GROWTH \ -fexceptions --bind -s ASYNCIFY -s ALLOW_MEMORY_GROWTH \
-dlpreopen $(SYSROOT)/lib/libgphoto2/2.5.28.1/ptp2.la \ -dlpreopen $(SYSROOT)/lib/libgphoto2/2.5.28.1/ptp2.la \

View File

View File

1
examples/preact/build Symbolic link
View File

@@ -0,0 +1 @@
../../build

View File

@@ -22,9 +22,9 @@ import { connect, rethrowIfCritical } from './ops.js';
import { Preview } from './preview.js'; import { Preview } from './preview.js';
import { Widget } from './widget.js'; import { Widget } from './widget.js';
/** @typedef {import('../libapi.mjs').Context} Context */ /** @typedef {import('./build/libapi.mjs').Context} Context */
/** @typedef {import('../libapi.mjs').Config} Config */ /** @typedef {import('./build/libapi.mjs').Config} Config */
/** @typedef {import('./ops').Connection} Connection */ /** @typedef {import('./ops.js').Connection} Connection */
export const isDebug = new URLSearchParams(location.search).has('debug'); export const isDebug = new URLSearchParams(location.search).has('debug');

View File

@@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
import initModule from '../libapi.mjs'; import initModule from './build/libapi.mjs';
/** @typedef {import('../libapi.mjs').Context} Context */ /** @typedef {import('../libapi.mjs').Context} Context */
@@ -25,7 +25,8 @@ const ModulePromise = initModule();
export function rethrowIfCritical(err) { export function rethrowIfCritical(err) {
// If it's precisely Error, it's a custom error; anything else - SyntaxError, // If it's precisely Error, it's a custom error; anything else - SyntaxError,
// WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here. // WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here.
if (err.constructor !== Error) { if (err.constructor !== Error)
{
throw err; throw err;
} }
} }
@@ -44,7 +45,8 @@ export async function connect() {
* @param {(ctx: Context) => Promise<T>} op * @param {(ctx: Context) => Promise<T>} op
* @returns {Promise<T>} * @returns {Promise<T>}
*/ */
function schedule(op) { function schedule(op)
{
let res = queue.then(() => op(context)); let res = queue.then(() => op(context));
queue = res.catch(rethrowIfCritical); queue = res.catch(rethrowIfCritical);
return res; return res;

View File

@@ -18,7 +18,7 @@
import { h, Component, createRef } from 'preact'; import { h, Component, createRef } from 'preact';
/** @typedef {import('../libapi.mjs').Config} Config */ /** @typedef {import('./build/libapi.mjs').Config} Config */
/** /**
* @param {Config} config * @param {Config} config

View File

@@ -2,8 +2,8 @@
"name": "web-gphoto2", "name": "web-gphoto2",
"version": "0.1.0", "version": "0.1.0",
"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",
"main": "ui/camera.js", "main": "src/camera.js",
"types": "ui/libapi.mjs.d.ts", "types": "src/libapi.mjs.d.ts",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
@@ -23,8 +23,8 @@
}, },
"homepage": "https://github.com/GoogleChromeLabs/web-gphoto2#readme", "homepage": "https://github.com/GoogleChromeLabs/web-gphoto2#readme",
"files": [ "files": [
"ui/libapi.*", "src/*",
"ui/camera.js" "build/*"
], ],
"devDependencies": { "devDependencies": {
"@types/emscripten": "^1.39.5", "@types/emscripten": "^1.39.5",
@@ -32,4 +32,4 @@
"@types/stats.js": "^0.17.0", "@types/stats.js": "^0.17.0",
"preact": "^10.5.14" "preact": "^10.5.14"
} }
} }

116
src/camera.js Normal file
View File

@@ -0,0 +1,116 @@
/*
* Copyright 2021 Google LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/** @typedef {import('./libapi.mjs.d.ts').Context} Context */
// To avoid errors for users who use SSR or Hybrid rendering (e.g. Nuxt.js), we need to check if we're in the browser.
let initModule;
async function initializeModule() {
if (typeof window !== "undefined") {
const module = await import("../build/libapi.mjs");
initModule = module.default;
return initModule;
} else {
console.warn("web-gphoto2 is only available in the browser");
return null;
}
}
export function rethrowIfCritical(err) {
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
// WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here.
if (err.constructor !== Error) {
throw err;
}
}
class Camera {
constructor() {
/** @type {Promise<unknown>} */
this.queue = Promise.resolve();
this.Module = null;
this.context = null;
this.ModulePromise = null;
}
async connect() {
if (!this.ModulePromise) {
this.ModulePromise = initializeModule().then((initModule) => {
if (initModule) {
return initModule();
} else {
return null;
}
});
}
this.Module = await this.ModulePromise;
this.context = await new this.Module.Context();
}
/** Schedules an exclusive async operation on the global context.
* @template T
* @param {(ctx: Context) => Promise<T>} op
* @returns {Promise<T>}
*/
async schedule(op) {
let res = this.queue.then(() => op(this.context));
this.queue = res.catch(rethrowIfCritical);
return res;
}
async disconnect() {
if (!this.context.isDeleted()) {
this.context.delete();
}
}
async getConfig() {
return this.schedule((context) => context.configToJS());
}
async getSupportedOps() {
if (this.context) {
return await this.context.supportedOps();
}
throw new Error("You need to connect to the camera first");
}
async setConfigValue(name, value) {
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
const setResult = this.schedule((context) =>
context.setConfigValue(name, value)
);
// wait for both the config set operation and the timeout to complete
return Promise.all([setResult, uiTimeout]);
}
async capturePreviewAsBlob() {
return this.schedule((context) => context.capturePreviewAsBlob());
}
async captureImageAsFile() {
return this.schedule((context) => context.captureImageAsFile());
}
async consumeEvents() {
return this.schedule((context) => context.consumeEvents());
}
}
export default Camera;

View File

@@ -1,116 +0,0 @@
/*
* Copyright 2021 Google LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/** @typedef {import('./libapi.mjs').Context} Context */
// To avoid errors for users who use SSR or Hybrid rendering (e.g. Nuxt.js), we need to check if we're in the browser.
let initModule;
async function initializeModule() {
if (typeof window !== 'undefined') {
const module = await import('./libapi.mjs');
initModule = module.default;
return initModule;
} else {
console.warn("web-gphoto2 is only available in the browser");
return null;
}
}
export function rethrowIfCritical(err) {
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
// WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here.
if (err.constructor !== Error) {
throw err;
}
}
class Camera {
constructor() {
/** @type {Promise<unknown>} */
this.queue = Promise.resolve();
this.Module = null;
this.context = null;
this.ModulePromise = null;
}
async connect() {
if (!this.ModulePromise) {
this.ModulePromise = initializeModule().then(initModule => {
if (initModule) {
return initModule();
} else {
return null;
}
});
}
this.Module = await this.ModulePromise;
this.context = await new this.Module.Context();
}
/** Schedules an exclusive async operation on the global context.
* @template T
* @param {(ctx: Context) => Promise<T>} op
* @returns {Promise<T>}
*/
async schedule(op) {
let res = this.queue.then(() => op(this.context));
this.queue = res.catch(rethrowIfCritical);
return res;
}
async disconnect() {
if (!this.context.isDeleted()) {
this.context.delete();
}
}
async getConfig() {
return this.schedule((context) => context.configToJS());
}
async getSupportedOps() {
if (this.context) {
return await this.context.supportedOps();
}
throw new Error("You need to connect to the camera first");
}
async setConfigValue(name, value) {
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
const setResult = this.schedule((context) => context.setConfigValue(name, value));
// wait for both the config set operation and the timeout to complete
return Promise.all([setResult, uiTimeout]);
}
async capturePreviewAsBlob() {
return this.schedule((context) => context.capturePreviewAsBlob());
}
async captureImageAsFile() {
return this.schedule((context) => context.captureImageAsFile());
}
async consumeEvents() {
return this.schedule((context) => context.consumeEvents());
}
}
export default Camera;