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 @@
import * as dom from '../dom.js';
import {ViewName} from '../type.js'; // eslint-disable-line no-unused-vars
import {WaitableEvent} from '../waitable_event.js';
/* eslint-disable no-unused-vars */
......@@ -54,7 +55,8 @@ export class View {
this.rootElement_ = dom.get(`#${name}`, HTMLElement);
/**
* @type {?Promise}
* Signal it to ends the session.
* @type {?WaitableEvent<*>}
* @private
*/
this.session_ = null;
......@@ -131,17 +133,11 @@ export class View {
enter(options) {
// The session is started by entering the view and ended by leaving the
// view.
if (!this.session_) {
let end;
this.session_ = new Promise((resolve) => {
end = resolve;
});
this.session_.end = (result) => {
end(result);
};
if (this.session_ === null) {
this.session_ = new WaitableEvent();
}
this.entering(options);
return this.session_;
return this.session_.wait();
}
/**
......@@ -160,8 +156,8 @@ export class View {
* @return {boolean} Whether able to leaving the view or not.
*/
leave(condition) {
if (this.session_ && this.leaving(condition)) {
this.session_.end(condition);
if (this.session_ !== null && this.leaving(condition)) {
this.session_.signal(condition);
this.session_ = null;
return true;
}
......
......@@ -4,6 +4,7 @@
/**
* A waitable event for synchronization between asynchronous jobs.
* @template T
*/
export class WaitableEvent {
/**
......@@ -17,13 +18,13 @@ export class WaitableEvent {
this.isSignaled_ = false;
/**
* @type {function(): void}
* @type {function(T): void}
* @private
*/
this.resolve_;
/**
* @type {!Promise}
* @type {!Promise<T>}
* @private
*/
this.promise_ = new Promise((resolve) => {
......@@ -40,19 +41,20 @@ export class WaitableEvent {
/**
* Signals the event.
* @param {T=} value
*/
signal() {
signal(value) {
if (this.isSignaled_) {
return;
}
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() {
await this.promise_;
wait() {
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