Skip to content

Commit

Permalink
CCA: Migrate type.js and util.js to TypeScript
Browse files Browse the repository at this point in the history
Bug: b:172340451
Test: tast run <ip> camera.CCAUISmoke* camera.CCAUIStress*
Change-Id: I181672dc9be6db1123ab220dcc41cd8ec631847b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3291015
Reviewed-by: Shik Chen <shik@chromium.org>
Commit-Queue: Pi-Hsun Shih <pihsun@chromium.org>
Cr-Commit-Position: refs/heads/main@{#949997}
  • Loading branch information
peter50216 authored and Chromium LUCI CQ committed Dec 9, 2021
1 parent c06211d commit 67f4e4b
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 511 deletions.
4 changes: 2 additions & 2 deletions ash/webui/camera_app_ui/resources/js/js.gni
Expand Up @@ -63,13 +63,13 @@ compile_js_files = [
"timer.js",
"toast.js",
"tooltip.js",
"type.js",
"type.ts",
"unload.js",
"untrusted_ga_helper.js",
"untrusted_helper_interfaces.js",
"untrusted_script_loader.js",
"untrusted_video_processor_helper.js",
"util.js",
"util.ts",
"views/camera_intent.js",
"views/camera.js",
"views/camera/layout.js",
Expand Down
58 changes: 23 additions & 35 deletions ash/webui/camera_app_ui/resources/js/main.js
Expand Up @@ -40,6 +40,7 @@ import {
} from './type.js';
import {addUnloadCallback} from './unload.js';
import * as util from './util.js';
import {checkEnumVariant} from './util.js';
import {Camera} from './views/camera.js';
import {CameraIntent} from './views/camera_intent.js';
import {Dialog} from './views/dialog.js';
Expand Down Expand Up @@ -396,27 +397,9 @@ function parseSearchParams() {
const url = new URL(window.location.href);
const params = url.searchParams;

// TODO(pihsun): Intent.create has almost same code for checking a string is
// an enum variant, extract them to a util function when we change TypeScript
// since the util function type is hard to be described in closure compiler
// due to lack of generic type bounds.
/** @type {?Facing} */
const facing = (() => {
const facing = params.get('facing');
if (facing === null || !Object.values(Facing).includes(facing)) {
return null;
}
return /** @type {!Facing} */ (facing);
})();
const facing = checkEnumVariant(Facing, params.get('facing'));

/** @type {?Mode} */
const mode = (() => {
const mode = params.get('mode');
if (mode === null || !Object.values(Mode).includes(mode)) {
return null;
}
return /** @type {!Mode} */ (mode);
})();
const mode = checkEnumVariant(Mode, params.get('mode'));

/** @type {?Intent} */
const intent = (() => {
Expand Down Expand Up @@ -486,28 +469,33 @@ let instance = null;
appWindow.reportPerf({event, duration, perfInfo});
}
});
const states = Object.values(PerfEvent);
states.push(state.State.TAKING);
states.forEach((s) => {
state.addObserver(s, (val, extras) => {
let event = s;
if (s === state.State.TAKING) {
// 'taking' state indicates either taking photo or video. Skips for
// video-taking case since we only want to collect the metrics of
// photo-taking.
if (state.get(Mode.VIDEO)) {
return;
}
event = PerfEvent.PHOTO_TAKING;
}

state.addObserver(state.State.TAKING, (val, extras) => {
// 'taking' state indicates either taking photo or video. Skips for
// video-taking case since we only want to collect the metrics of
// photo-taking.
if (state.get(Mode.VIDEO)) {
return;
}
const event = PerfEvent.PHOTO_TAKING;

if (val) {
perfLogger.start(event);
} else {
perfLogger.stop(event, extras);
}
});

const states = Object.values(PerfEvent);
for (const event of states) {
state.addObserver(event, (val, extras) => {
if (val) {
perfLogger.start(event);
} else {
perfLogger.stop(event, extras);
}
});
});
}

instance = new App({perfLogger, intent, facing, mode});
await instance.start(
Expand Down
7 changes: 2 additions & 5 deletions ash/webui/camera_app_ui/resources/js/mojo/device_operator.js
Expand Up @@ -681,10 +681,7 @@ export class DeviceOperator {
/**
* Creates a new instance of DeviceOperator if it is not set. Returns the
* exist instance.
* TODO(b/172340451): Use force casting rather than template for the type
* checking of Proxy after switching to TypeScript.
* @return {!Promise<?T>} The singleton instance.
* @template T
* @return {!Promise<!DeviceOperator>} The singleton instance.
*/
static async getInstance() {
await readyEvent.wait();
Expand All @@ -705,7 +702,7 @@ export class DeviceOperator {
return target[property];
},
};
return /** @type {!T} */ (new Proxy(instance, deviceOperatorWrapper));
return new Proxy(instance, deviceOperatorWrapper);
}

/**
Expand Down

0 comments on commit 67f4e4b

Please sign in to comment.