Skip to content

Commit

Permalink
fix: Fix async wrapping function
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 16, 2019
1 parent 5e59e5a commit 865ce0d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 88 deletions.
154 changes: 77 additions & 77 deletions src/components/Camera.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {GPCameraCaptureType, GPCodes} from "../driver";
import {PointerCamera, RefCamera, StructCameraText} from "../driver/modules";
import {GPCameraCaptureType, GPCodes, PointerCamera, RefCamera, StructCameraText} from "../driver";
import {ILiveviewOptions} from "../interfaces";
import {CameraAbilities} from "./CameraAbilities";
import {CameraFile} from "./CameraFile";
Expand All @@ -13,12 +12,13 @@ import {PortInfo} from "./PortInfo";
export class Camera extends PointerWrapper<PointerCamera> {
private initialized: boolean = false;
private closed: boolean = false;
private _widgets: CameraWidgets;

constructor() {
super({method: "gp_camera", refType: RefCamera});
}

private _widgets: CameraWidgets;

get widgets(): CameraWidgets {
this.checkNotClosed();

Expand Down Expand Up @@ -87,24 +87,6 @@ export class Camera extends PointerWrapper<PointerCamera> {
return this;
}

/**
*
*/
private checkNotClosed() {
if (this.closed) {
throw new Error("Invalid state: closed");
}
}

/**
*
*/
private checkIsInitialized() {
if (!this.initialized) {
throw new Error("Invalid state: not initialized");
}
}

public liveview(options: ILiveviewOptions): Liveview {
this.checkNotClosed();
this.checkIsInitialized();
Expand Down Expand Up @@ -218,62 +200,6 @@ export class Camera extends PointerWrapper<PointerCamera> {
return this.captureAsync(GPCameraCaptureType.GP_CAPTURE_SOUND);
}

/**
*
* @param {GPCameraCaptureType} type
* @param path
* @returns {CameraFile | undefined}
*/
protected capture(type: GPCameraCaptureType, path?: string): CameraFile | undefined {
this.checkNotClosed();
const cFilePath = new CameraFilePath();

this.call("capture", type, cFilePath.buffer, Context.get().pointer);

const cFile = cFilePath.newFile(this.pointer);

if (path && cFile) {
try {
cFile.save(path);
} finally {
this.deinitialize();
this.initialize();
cFile.closeQuietly();
cFilePath.close();
}
}

return cFile;
}

/**
*
* @param {GPCameraOperation} type
* @param path
* @returns {CameraFile | undefined}
*/
protected async captureAsync(type: GPCameraCaptureType, path?: string): Promise<CameraFile | undefined> {
this.checkNotClosed();
const cFilePath = new CameraFilePath();

await this.callAsync("capture", type, cFilePath.buffer, Context.get().pointer);

const cFile = await cFilePath.newFileAsync(this.pointer);

if (path && cFile) {
try {
await cFile.saveAsync(path);
} finally {
this.deinitialize();
this.initialize();
cFile.closeQuietly();
cFilePath.close();
}
}

return cFile;
}

/**
*
*/
Expand Down Expand Up @@ -383,4 +309,78 @@ export class Camera extends PointerWrapper<PointerCamera> {
toString(): string {
return "Camera: " + this.getAbilities().model;
}

/**
*
* @param {GPCameraCaptureType} type
* @param path
* @returns {CameraFile | undefined}
*/
protected capture(type: GPCameraCaptureType, path?: string): CameraFile | undefined {
this.checkNotClosed();
const cFilePath = new CameraFilePath();

this.call("capture", type, cFilePath.buffer, Context.get().pointer);

const cFile = cFilePath.newFile(this.pointer);

if (path && cFile) {
try {
cFile.save(path);
} finally {
this.deinitialize();
this.initialize();
cFile.closeQuietly();
cFilePath.close();
}
}

return cFile;
}

/**
*
* @param {GPCameraOperation} type
* @param path
* @returns {CameraFile | undefined}
*/
protected async captureAsync(type: GPCameraCaptureType, path?: string): Promise<CameraFile | undefined> {
this.checkNotClosed();
const cFilePath = new CameraFilePath();

await this.callAsync("capture", type, cFilePath.buffer, Context.get().pointer);

const cFile = await cFilePath.newFileAsync(this.pointer);

if (path && cFile) {
try {
await cFile.saveAsync(path);
} finally {
this.deinitialize();
this.initialize();
cFile.closeQuietly();
cFilePath.close();
}
}

return cFile;
}

/**
*
*/
private checkNotClosed() {
if (this.closed) {
throw new Error("Invalid state: closed");
}
}

/**
*
*/
private checkIsInitialized() {
if (!this.initialized) {
throw new Error("Invalid state: not initialized");
}
}
}
14 changes: 4 additions & 10 deletions src/driver/GPhoto2Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./modules";

const ffi = require("ffi-napi");
const util = require("util");

/**
*
Expand All @@ -32,7 +33,7 @@ export interface GPhoto2Driver
IGPPortInfoModule {}

// tslint:disable-next-line
export const GPhoto2Driver: GPhoto2Driver = ffi.Library("libgphoto2", {
export const GPhoto2Driver: GPhoto2Driver & {[key: string]: any} = ffi.Library("libgphoto2", {
// CONTEXT
...GPContextModuleDescription,

Expand All @@ -55,13 +56,6 @@ export const GPhoto2Driver: GPhoto2Driver = ffi.Library("libgphoto2", {
...GPWidgetModuleDescription
});

[].concat(GP_CAMERA_MODULE_ASYNC_KEYS as any, GP_FILE_MODULE_ASYNC_KEYS as any).forEach(key => {
// console.debug("[GPDRIVER] Bind async method", key);
(GPhoto2Driver as any)[key + "_async"] = function async(...args: any[]) {
// console.debug("[GPDRIVER] Call async method", key, args.length);

return new Promise(resolve => {
(GPhoto2Driver as any)[key].async(...args, resolve);
});
};
[...GP_CAMERA_MODULE_ASYNC_KEYS, ...GP_FILE_MODULE_ASYNC_KEYS].forEach(key => {
GPhoto2Driver[`${key}_async`] = util.promisify(GPhoto2Driver[key].async);
});
1 change: 0 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
--require ts-node/register/transpile-only
--require tsconfig-paths/register
--require scripts/mocha/register
--recursive
--reporter dot
Expand Down

0 comments on commit 865ce0d

Please sign in to comment.