Commit 0d17d119 authored by Kuo Jen Wei's avatar Kuo Jen Wei Committed by Commit Bot

CCA: Use WaitableEvent as session type in view.js

Bug: 1122444
Test: tast run <DUT> "camera.CCAUI*"
Change-Id: Ia6c99c524569a027cc52b38ce7bf24e2c8469f88
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2379425
Auto-Submit: Inker Kuo <inker@chromium.org>
Commit-Queue: Shik Chen <shik@chromium.org>
Reviewed-by: default avatarShik Chen <shik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803807}
parent 9e7397d9
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import * as dom from '../dom.js'; import * as dom from '../dom.js';
import {ViewName} from '../type.js'; // eslint-disable-line no-unused-vars import {ViewName} from '../type.js'; // eslint-disable-line no-unused-vars
import {WaitableEvent} from '../waitable_event.js';
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
...@@ -54,7 +55,8 @@ export class View { ...@@ -54,7 +55,8 @@ export class View {
this.rootElement_ = dom.get(`#${name}`, HTMLElement); this.rootElement_ = dom.get(`#${name}`, HTMLElement);
/** /**
* @type {?Promise} * Signal it to ends the session.
* @type {?WaitableEvent<*>}
* @private * @private
*/ */
this.session_ = null; this.session_ = null;
...@@ -131,17 +133,11 @@ export class View { ...@@ -131,17 +133,11 @@ export class View {
enter(options) { enter(options) {
// The session is started by entering the view and ended by leaving the // The session is started by entering the view and ended by leaving the
// view. // view.
if (!this.session_) { if (this.session_ === null) {
let end; this.session_ = new WaitableEvent();
this.session_ = new Promise((resolve) => {
end = resolve;
});
this.session_.end = (result) => {
end(result);
};
} }
this.entering(options); this.entering(options);
return this.session_; return this.session_.wait();
} }
/** /**
...@@ -160,8 +156,8 @@ export class View { ...@@ -160,8 +156,8 @@ export class View {
* @return {boolean} Whether able to leaving the view or not. * @return {boolean} Whether able to leaving the view or not.
*/ */
leave(condition) { leave(condition) {
if (this.session_ && this.leaving(condition)) { if (this.session_ !== null && this.leaving(condition)) {
this.session_.end(condition); this.session_.signal(condition);
this.session_ = null; this.session_ = null;
return true; return true;
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
/** /**
* A waitable event for synchronization between asynchronous jobs. * A waitable event for synchronization between asynchronous jobs.
* @template T
*/ */
export class WaitableEvent { export class WaitableEvent {
/** /**
...@@ -17,13 +18,13 @@ export class WaitableEvent { ...@@ -17,13 +18,13 @@ export class WaitableEvent {
this.isSignaled_ = false; this.isSignaled_ = false;
/** /**
* @type {function(): void} * @type {function(T): void}
* @private * @private
*/ */
this.resolve_; this.resolve_;
/** /**
* @type {!Promise} * @type {!Promise<T>}
* @private * @private
*/ */
this.promise_ = new Promise((resolve) => { this.promise_ = new Promise((resolve) => {
...@@ -40,19 +41,20 @@ export class WaitableEvent { ...@@ -40,19 +41,20 @@ export class WaitableEvent {
/** /**
* Signals the event. * Signals the event.
* @param {T=} value
*/ */
signal() { signal(value) {
if (this.isSignaled_) { if (this.isSignaled_) {
return; return;
} }
this.isSignaled_ = true; this.isSignaled_ = true;
this.resolve_(); this.resolve_(value);
} }
/** /**
* @return {!Promise} Resolved when the event is signaled. * @return {!Promise<T>} Resolved when the event is signaled.
*/ */
async wait() { wait() {
await this.promise_; return this.promise_;
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment