File size: 1,139 Bytes
f3d45a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {
  LitElement,
  html,
} from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';

class AsyncAction extends LitElement {
  static properties = {
    startedEvent: {type: String},
    finishedEvent: {type: String},
    // Storing as string due to https://github.com/google/mesop/issues/730
    // Format: {action: String, duration_seconds: Number}
    action: {type: String},
    isRunning: {type: Boolean},
  };

  render() {
    return html`<div></div>`;
  }

  firstUpdated() {
    if (this.action) {
      this.runTimeout(this.action);
    }
  }

  updated(changedProperties) {
    if (changedProperties.has('action') && this.action) {
      this.runTimeout(this.action);
    }
  }

  runTimeout(actionJson) {
    const action = JSON.parse(actionJson);
    this.dispatchEvent(
      new MesopEvent(this.startedEvent, {
        action: action,
      }),
    );
    setTimeout(() => {
      this.dispatchEvent(
        new MesopEvent(this.finishedEvent, {
          action: action.value,
        }),
      );
    }, action.duration_seconds * 1000);
  }
}

customElements.define('async-action-component', AsyncAction);