Skip to content

Commit

Permalink
fix: Add cache value on Widget class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Aug 22, 2018
1 parent 5e3973c commit 812f869
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
13 changes: 13 additions & 0 deletions examples/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ if (cameraList.size) {
console.log(camera.toString());
console.log(camera.widgets.toString());

// change value
camera.widgets.get('/actions/autofocusdrive').value = true;
camera.widgets.get('/settings/autofocus').value = 'on';

// OR apply
camera.widgets.apply({
'/actions/autofocusdrive': true,
'/settings/autofocus': 'on'
});

console.log('/actions/autofocusdrive', camera.widgets.get('/actions/autofocusdrive').value);
console.log('/settings/autofocus', camera.widgets.get('/settings/autofocus').value);

// camera.widgets => Widget which inherit from Map class
const widgets = JSON.stringify(camera.widgets, null, 2);
fs.writeFileSync('../.tmp/widgets.json', widgets, { encoding: 'utf8' });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
"release": {
"branch": "production"
}
}
}
13 changes: 11 additions & 2 deletions src/components/CameraWidgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,19 @@ export class CameraWidgets extends Map<string, Widget> implements ICloseable {

/**
* If the settings are altered, they need to be applied to take effect.
*
* @param obj An object to define a group of configuration
*/
apply() {
apply(obj?: any) {
this.checkNotClosed();
checkCode(GPhoto2Driver.gp_camera_set_config(this.camera.pointer, this.rootWidget, Context.get().pointer), "gp_camera_set_config");

if (obj) {
Object.keys(obj).forEach(key => {
this.get(key).value = obj[key];
});
} else {
checkCode(GPhoto2Driver.gp_camera_set_config(this.camera.pointer, this.rootWidget, Context.get().pointer), "gp_camera_set_config");
}
}

close(): this {
Expand Down
21 changes: 16 additions & 5 deletions src/components/Widget.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {checkCode, GPhoto2Driver, GPPointerFloat, GPPointerInt, GPPointerRef, GPPointerString, PointerToString} from "../driver";
import {PointerCameraWidget} from "../driver/modules";
import {PointerOf} from "../driver/types";
import {classOf} from "../driver/utils/ObjectUtils";
import {nameOf} from "../driver/utils/ObjectUtils";
import {IWidget} from "../interfaces";
import {WidgetRange} from "./WidgetRange";
import {WidgetTypes} from "./WidgetTypes";

export class Widget implements IWidget {
cachedValue: any;

constructor(readonly path: string, readonly pointer: PointerCameraWidget, private cameraWidgets: any) {}

/**
Expand Down Expand Up @@ -86,6 +88,10 @@ export class Widget implements IWidget {

const type = this.type;

if (this.cachedValue) {
return this.cachedValue;
}

switch (type) {
case WidgetTypes.TEXT:
case WidgetTypes.RADIO:
Expand Down Expand Up @@ -122,7 +128,7 @@ export class Widget implements IWidget {
return null;

default:
throw new Error("Parameter path: invalid value " + name + ": unsupported type: " + type);
throw new Error("Parameter path: invalid value " + this.path + ": unsupported type: " + type);
}
}

Expand All @@ -136,14 +142,14 @@ export class Widget implements IWidget {
this.cameraWidgets.checkNotClosed();

if (this.readonly) {
throw new Error(`Parameter name: invalid value ${name}: read-only`);
throw new Error(`Parameter name: invalid value ${this.path} [read-only]`);
}

const type = this.type;

if (!type.acceptsValue(value)) {
throw new Error(
`Parameter value: invalid value ${value}: expected ${type.valueType} but got ${value == null ? "null" : classOf(value)}`
`Parameter value: invalid value ${value}: expected ${nameOf(type.valueType)} but got ${value == null ? "null" : nameOf(value)}`
);
}

Expand Down Expand Up @@ -183,7 +189,12 @@ export class Widget implements IWidget {

checkCode(GPhoto2Driver.gp_widget_set_value(this.pointer, ptr as PointerOf<any>));

this.apply();
try {
this.apply();
this.cachedValue = value;
} catch (er) {
throw new Error(`Parameter value: failed to apply value ${value} on ${this.path}. \nError: ` + er.message);
}
}

get changed(): boolean {
Expand Down

0 comments on commit 812f869

Please sign in to comment.