@wcstack/timer 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +184 -0
- package/README.md +191 -0
- package/dist/auto.js +3 -0
- package/dist/auto.min.js +3 -0
- package/dist/index.d.ts +180 -0
- package/dist/index.esm.js +522 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.esm.min.js +2 -0
- package/dist/index.esm.min.js.map +1 -0
- package/package.json +71 -0
package/README.ja.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# @wcstack/timer
|
|
2
|
+
|
|
3
|
+
`@wcstack/timer` は wcstack エコシステム向けのヘッドレスなタイマーコンポーネントです。
|
|
4
|
+
|
|
5
|
+
これは視覚的な UI ウィジェットではありません。
|
|
6
|
+
`@wcstack/fetch` がネットワークリクエストをリアクティブな状態に変えるのと同じように、**時間の経過をリアクティブな状態に変える非同期プリミティブノード**です。
|
|
7
|
+
|
|
8
|
+
`@wcstack/state` と組み合わせると、`<wcs-timer>` はパス契約を通じて直接バインドできます。
|
|
9
|
+
|
|
10
|
+
- **入力面**: `interval`, `once`, `repeat`, `immediate`, `manual`, `trigger`
|
|
11
|
+
- **出力状態面**: `tick`, `elapsed`, `running`
|
|
12
|
+
- **コマンド**: `start`, `stop`, `reset`, `pause`, `resume`
|
|
13
|
+
|
|
14
|
+
> `trigger` はコマンドではなく、モーメンタリな命令*プロパティ*(入力)です。`false`→`true` の書き込みでタイマーが開始します。状態から command-token プロトコルで起動する場合は `command.start:` を使ってください([コマンド](#コマンド)参照)。`command.trigger` は存在しません。
|
|
15
|
+
|
|
16
|
+
つまり、繰り返し処理を HTML 上で宣言的に表現でき、UI 層に `setInterval()` / `clearInterval()` や後始末のグルーコードを書く必要がありません。
|
|
17
|
+
|
|
18
|
+
`@wcstack/timer` は [CSBC](https://github.com/csbc-dev/arch/blob/main/README.md)(Core / Shell / Binding Contract)アーキテクチャに従います。
|
|
19
|
+
|
|
20
|
+
- **Core**(`TimerCore`)がスケジューリング・tick カウント・経過時間・pause/resume を担当
|
|
21
|
+
- **Shell**(`<wcs-timer>`)がその状態を DOM 属性・ライフサイクル・宣言的コマンドに接続
|
|
22
|
+
- **Binding Contract**(`static wcBindable`)が観測可能な `properties`・書き込み可能な `inputs`・呼び出し可能な `commands` を宣言
|
|
23
|
+
|
|
24
|
+
## なぜ存在するのか
|
|
25
|
+
|
|
26
|
+
タイマーは `fetch` と同様、時間とともに値を生み出す非同期ソースです。命令的に書くと、開始・クリア・カウント・切断時の後始末といったライフサイクル管理が必要になります。
|
|
27
|
+
|
|
28
|
+
`@wcstack/timer` はそのロジックを再利用可能なコンポーネントに押し込み、結果をバインド可能な状態として公開します。時間が命令的なイベント配線ではなく、**状態遷移**になります。
|
|
29
|
+
|
|
30
|
+
## インストール
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install @wcstack/timer
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## クイックスタート
|
|
37
|
+
|
|
38
|
+
### 1. 状態へのリアクティブな tick
|
|
39
|
+
|
|
40
|
+
`<wcs-timer>` が DOM に接続されると、自動的にインターバルタイマーを開始します。`tick` / `elapsed` / `running` を状態パスにバインドします。
|
|
41
|
+
|
|
42
|
+
```html
|
|
43
|
+
<script type="module" src="https://esm.run/@wcstack/state/auto"></script>
|
|
44
|
+
<script type="module" src="https://esm.run/@wcstack/timer/auto"></script>
|
|
45
|
+
|
|
46
|
+
<wcs-state>
|
|
47
|
+
<script type="module">
|
|
48
|
+
export default {
|
|
49
|
+
count: 0,
|
|
50
|
+
isRunning: false,
|
|
51
|
+
get statusLabel() {
|
|
52
|
+
return this.isRunning ? "動作中" : "停止";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
</script>
|
|
56
|
+
</wcs-state>
|
|
57
|
+
|
|
58
|
+
<wcs-timer
|
|
59
|
+
interval="1000"
|
|
60
|
+
data-wcs="tick: count; running: isRunning">
|
|
61
|
+
</wcs-timer>
|
|
62
|
+
|
|
63
|
+
<p data-wcs="textContent: count"></p>
|
|
64
|
+
<p data-wcs="textContent: statusLabel"></p>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. ワンショット(`setTimeout` 相当)
|
|
68
|
+
|
|
69
|
+
`once` は `interval` ミリ秒後に1回だけ tick して自動停止します(`once` は `repeat="1"` の糖衣構文です)。
|
|
70
|
+
|
|
71
|
+
```html
|
|
72
|
+
<wcs-timer interval="3000" once data-wcs="tick: showBanner"></wcs-timer>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. 回数制限付きの繰り返し
|
|
76
|
+
|
|
77
|
+
`repeat="N"` は N 回 tick して停止します(`running` が `false` になります)。
|
|
78
|
+
|
|
79
|
+
```html
|
|
80
|
+
<wcs-timer interval="1000" repeat="5" data-wcs="tick: countdownStep"></wcs-timer>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 4. 即時発火
|
|
84
|
+
|
|
85
|
+
`immediate` は最初の1回を、1インターバル待たずに開始時点で発火します。
|
|
86
|
+
|
|
87
|
+
```html
|
|
88
|
+
<wcs-timer interval="5000" immediate data-wcs="tick: pollNow"></wcs-timer>
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 属性 / Inputs
|
|
92
|
+
|
|
93
|
+
| 属性 | 型 | 既定値 | 説明 |
|
|
94
|
+
| ----------- | ------- | ------- | --------------------------------------------------------------- |
|
|
95
|
+
| `interval` | number | `1000` | tick の周期(ミリ秒)。有限かつ `> 0` であること。不正値(`0`・負数・非数値)は `1000` にフォールバック。 |
|
|
96
|
+
| `once` | boolean | `false` | 1回だけ発火して停止。`repeat="1"` の糖衣構文。 |
|
|
97
|
+
| `repeat` | number | `0` | N 回で停止(`0` = 無制限)。`once` より優先されます。 |
|
|
98
|
+
| `immediate` | boolean | `false` | 最初の tick を開始時点で発火(1インターバル待たない)。 |
|
|
99
|
+
| `manual` | boolean | `false` | 接続時に自動開始しない。コマンド / trigger で開始します。 |
|
|
100
|
+
|
|
101
|
+
## 観測プロパティ(出力)
|
|
102
|
+
|
|
103
|
+
| プロパティ | イベント | 説明 |
|
|
104
|
+
| ---------- | --------------------------- | ------------------------------------------------------------- |
|
|
105
|
+
| `tick` | `wcs-timer:tick` | 発火ごとに増えるカウンタ(`reset` で 0 に戻る)。 |
|
|
106
|
+
| `elapsed` | `wcs-timer:tick` | 最後の reset からの経過時間(ミリ秒)。 |
|
|
107
|
+
| `running` | `wcs-timer:running-changed` | tick 中は `true`、停止 / 一時停止中は `false`。 |
|
|
108
|
+
|
|
109
|
+
## コマンド
|
|
110
|
+
|
|
111
|
+
| コマンド | 説明 |
|
|
112
|
+
| --------- | ---------------------------------------------------------------- |
|
|
113
|
+
| `start` | tick を開始(既に動作中なら no-op)。 |
|
|
114
|
+
| `stop` | tick を停止。`tick` / `elapsed` は保持されます。 |
|
|
115
|
+
| `reset` | 停止し `tick` / `elapsed` を `0` に戻します。 |
|
|
116
|
+
| `pause` | 周期の途中経過と経過時間を保持したまま一時停止します。 |
|
|
117
|
+
| `resume` | `pause` から、周期の残り時間を尊重して再開します。 |
|
|
118
|
+
|
|
119
|
+
`interval` のライブ変更が即時反映されるのは**動作中**のときだけです。**一時停止中**に `interval` を変更しても現在の周期には影響せず、次の `start` で新しい値が反映されます。
|
|
120
|
+
|
|
121
|
+
状態駆動の呼び出しには command-token プロトコルを使います。
|
|
122
|
+
|
|
123
|
+
```html
|
|
124
|
+
<wcs-timer manual data-wcs="command.start: $command.beginPolling"></wcs-timer>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## 任意の DOM トリガ
|
|
128
|
+
|
|
129
|
+
`autoTrigger` が有効(既定)なら、`data-timertarget="<id>"` を持つ要素のクリックで、参照先の `<wcs-timer>` の `start()` が呼ばれます。
|
|
130
|
+
|
|
131
|
+
```html
|
|
132
|
+
<button data-timertarget="poll">ポーリング開始</button>
|
|
133
|
+
<wcs-timer id="poll" interval="5000" manual data-wcs="tick: pollNow"></wcs-timer>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
イベント委譲を使うため動的に追加された要素でも動作し、`closest()` によりネストした要素(ボタン内のアイコンなど)にも対応します。一致したクリックは `start()` の前に `event.preventDefault()` を呼ぶため、要素の既定動作は抑制されます。既定動作も必要な要素(実際の `<a href>` リンクや form 送信ボタンなど)には `data-timertarget` を付けないでください(キャンセルされます)。
|
|
137
|
+
|
|
138
|
+
## 設定
|
|
139
|
+
|
|
140
|
+
`bootstrapTimer()` が `<wcs-timer>` を登録し、必要に応じて既定値を上書きします。部分的な設定を渡せます。
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
import { bootstrapTimer } from "@wcstack/timer";
|
|
144
|
+
|
|
145
|
+
bootstrapTimer({
|
|
146
|
+
autoTrigger: true, // data-timertarget クリックトリガを有効化(既定: true)
|
|
147
|
+
triggerAttribute: "data-timertarget", // クリックトリガで走査する属性
|
|
148
|
+
tagNames: {
|
|
149
|
+
timer: "wcs-timer", // カスタム要素のタグ名
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`getConfig()` は現在の設定の deep-frozen なスナップショットを返します。
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
import { getConfig } from "@wcstack/timer";
|
|
158
|
+
|
|
159
|
+
const { autoTrigger, triggerAttribute, tagNames } = getConfig();
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
| オプション | 型 | 既定 | 説明 |
|
|
163
|
+
| ------------------ | ------- | ------------------ | -------------------------------------------- |
|
|
164
|
+
| `autoTrigger` | boolean | `true` | `data-timertarget` クリックトリガを有効化。 |
|
|
165
|
+
| `triggerAttribute` | string | `data-timertarget` | DOM クリックトリガで走査する属性。 |
|
|
166
|
+
| `tagNames.timer` | string | `wcs-timer` | 登録するカスタム要素のタグ名。 |
|
|
167
|
+
|
|
168
|
+
## ヘッドレス利用(`TimerCore`)
|
|
169
|
+
|
|
170
|
+
Core は DOM に依存せず、`@wc-bindable/core` の `bind()` と直接組み合わせられます。
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { TimerCore } from "@wcstack/timer";
|
|
174
|
+
|
|
175
|
+
const timer = new TimerCore();
|
|
176
|
+
timer.addEventListener("wcs-timer:tick", (e) => {
|
|
177
|
+
console.log((e as CustomEvent).detail); // { count, elapsed }
|
|
178
|
+
});
|
|
179
|
+
timer.start({ interval: 1000, repeat: 10 });
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## ライセンス
|
|
183
|
+
|
|
184
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# @wcstack/timer
|
|
2
|
+
|
|
3
|
+
`@wcstack/timer` is a headless timer component for the wcstack ecosystem.
|
|
4
|
+
|
|
5
|
+
It is not a visual UI widget.
|
|
6
|
+
It is an **async primitive node** that turns the passage of time into reactive state — the same way `@wcstack/fetch` turns a network request into reactive state.
|
|
7
|
+
|
|
8
|
+
With `@wcstack/state`, `<wcs-timer>` can be bound directly through path contracts:
|
|
9
|
+
|
|
10
|
+
- **input surface**: `interval`, `once`, `repeat`, `immediate`, `manual`, `trigger`
|
|
11
|
+
- **output state surface**: `tick`, `elapsed`, `running`
|
|
12
|
+
- **commands**: `start`, `stop`, `reset`, `pause`, `resume`
|
|
13
|
+
|
|
14
|
+
> `trigger` is a momentary command-*property* (an input), not a command: a `false`→`true` write starts the timer. To start from state via the command-token protocol use `command.start:` (see [Commands](#commands)) — there is no `command.trigger`.
|
|
15
|
+
|
|
16
|
+
This means recurring work can be expressed declaratively in HTML, without writing `setInterval()`, `clearInterval()`, or teardown glue in your UI layer.
|
|
17
|
+
|
|
18
|
+
`@wcstack/timer` follows the [CSBC](https://github.com/csbc-dev/arch/blob/main/README.md) (Core / Shell / Binding Contract) architecture:
|
|
19
|
+
|
|
20
|
+
- **Core** (`TimerCore`) handles scheduling, tick counting, elapsed time, and pause/resume
|
|
21
|
+
- **Shell** (`<wcs-timer>`) connects that state to DOM attributes, lifecycle, and declarative commands
|
|
22
|
+
- **Binding Contract** (`static wcBindable`) declares observable `properties`, writable `inputs`, and callable `commands`
|
|
23
|
+
|
|
24
|
+
## Why this exists
|
|
25
|
+
|
|
26
|
+
A timer is, like `fetch`, an asynchronous source of values over time. Imperatively it requires lifecycle management: starting, clearing, counting, and cleanup on disconnect.
|
|
27
|
+
|
|
28
|
+
`@wcstack/timer` moves that logic into a reusable component and exposes the result as bindable state. Time becomes a **state transition**, not imperative event wiring.
|
|
29
|
+
|
|
30
|
+
With `@wcstack/state`, the flow becomes:
|
|
31
|
+
|
|
32
|
+
1. `<wcs-timer>` is connected to the DOM and starts ticking
|
|
33
|
+
2. each tick increments `tick` and updates `elapsed`
|
|
34
|
+
3. UI binds to those paths with `data-wcs`
|
|
35
|
+
4. a state getter can react to `tick` changes and chain into other commands (e.g. trigger a `<wcs-fetch>` poll)
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @wcstack/timer
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Quick Start
|
|
44
|
+
|
|
45
|
+
### 1. Reactive ticking from state
|
|
46
|
+
|
|
47
|
+
When `<wcs-timer>` is connected to the DOM, it automatically starts an interval timer. Bind `tick` / `elapsed` / `running` to state paths.
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script type="module" src="https://esm.run/@wcstack/state/auto"></script>
|
|
51
|
+
<script type="module" src="https://esm.run/@wcstack/timer/auto"></script>
|
|
52
|
+
|
|
53
|
+
<wcs-state>
|
|
54
|
+
<script type="module">
|
|
55
|
+
export default {
|
|
56
|
+
count: 0,
|
|
57
|
+
isRunning: false,
|
|
58
|
+
get statusLabel() {
|
|
59
|
+
return this.isRunning ? "Running" : "Stopped";
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
</script>
|
|
63
|
+
</wcs-state>
|
|
64
|
+
|
|
65
|
+
<wcs-timer
|
|
66
|
+
interval="1000"
|
|
67
|
+
data-wcs="tick: count; running: isRunning">
|
|
68
|
+
</wcs-timer>
|
|
69
|
+
|
|
70
|
+
<p data-wcs="textContent: count"></p>
|
|
71
|
+
<p data-wcs="textContent: statusLabel"></p>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 2. One-shot timeout (`setTimeout` equivalent)
|
|
75
|
+
|
|
76
|
+
`once` fires exactly one tick after `interval` ms, then auto-stops. (`once` is sugar for `repeat="1"`.)
|
|
77
|
+
|
|
78
|
+
```html
|
|
79
|
+
<wcs-timer interval="3000" once data-wcs="tick: showBanner"></wcs-timer>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 3. Bounded repetition
|
|
83
|
+
|
|
84
|
+
`repeat="N"` fires `N` ticks and then stops (`running` becomes `false`).
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
<wcs-timer interval="1000" repeat="5" data-wcs="tick: countdownStep"></wcs-timer>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 4. Fire immediately
|
|
91
|
+
|
|
92
|
+
`immediate` fires the first tick at start instead of waiting one full interval.
|
|
93
|
+
|
|
94
|
+
```html
|
|
95
|
+
<wcs-timer interval="5000" immediate data-wcs="tick: pollNow"></wcs-timer>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Attributes / Inputs
|
|
99
|
+
|
|
100
|
+
| Attribute | Type | Default | Description |
|
|
101
|
+
| ----------- | ------- | ------- | ------------------------------------------------------------------ |
|
|
102
|
+
| `interval` | number | `1000` | Tick period in milliseconds. Must be a finite value `> 0`; invalid values (`0`, negative, non-numeric) fall back to `1000`. |
|
|
103
|
+
| `once` | boolean | `false` | Fire a single tick, then stop. Sugar for `repeat="1"`. |
|
|
104
|
+
| `repeat` | number | `0` | Stop after N ticks (`0` = unlimited). Takes precedence over `once`. |
|
|
105
|
+
| `immediate` | boolean | `false` | Fire one tick at start instead of waiting the first interval. |
|
|
106
|
+
| `manual` | boolean | `false` | Do not auto-start on connect; start via command / trigger. |
|
|
107
|
+
|
|
108
|
+
## Observable Properties (outputs)
|
|
109
|
+
|
|
110
|
+
| Property | Event | Description |
|
|
111
|
+
| --------- | --------------------------- | ------------------------------------------------------ |
|
|
112
|
+
| `tick` | `wcs-timer:tick` | Tick counter, increments on every fire (reset to 0 on `reset`). |
|
|
113
|
+
| `elapsed` | `wcs-timer:tick` | Running time in ms since the last reset. |
|
|
114
|
+
| `running` | `wcs-timer:running-changed` | `true` while ticking, `false` when stopped/paused. |
|
|
115
|
+
|
|
116
|
+
## Commands
|
|
117
|
+
|
|
118
|
+
| Command | Description |
|
|
119
|
+
| --------- | ----------------------------------------------------------------------- |
|
|
120
|
+
| `start` | Begin ticking (no-op if already running). |
|
|
121
|
+
| `stop` | Stop ticking; `tick` / `elapsed` are retained. |
|
|
122
|
+
| `reset` | Stop and reset `tick` / `elapsed` to `0`. |
|
|
123
|
+
| `pause` | Suspend ticking, preserving the partial period and elapsed time. |
|
|
124
|
+
| `resume` | Continue from a `pause`, honoring the remaining time of the period. |
|
|
125
|
+
|
|
126
|
+
A live `interval` change is applied immediately only while the timer is **running**. Changing `interval` while **paused** has no effect on the current period; the new value takes effect on the next `start`.
|
|
127
|
+
|
|
128
|
+
State-driven invocation uses the command-token protocol:
|
|
129
|
+
|
|
130
|
+
```html
|
|
131
|
+
<wcs-timer manual data-wcs="command.start: $command.beginPolling"></wcs-timer>
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Optional DOM Triggering
|
|
135
|
+
|
|
136
|
+
If `autoTrigger` is enabled (default), clicking an element carrying `data-timertarget="<id>"` calls `start()` on the referenced `<wcs-timer>`:
|
|
137
|
+
|
|
138
|
+
```html
|
|
139
|
+
<button data-timertarget="poll">Start polling</button>
|
|
140
|
+
<wcs-timer id="poll" interval="5000" manual data-wcs="tick: pollNow"></wcs-timer>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Event delegation is used, so it also works for dynamically added elements, and `closest()` handles nested targets (e.g. an icon inside the button). A matched click calls `event.preventDefault()` before starting the timer, so the element's default action is suppressed — do not put `data-timertarget` on an element whose default action you also want (a real `<a href>` link, a form-submit button), as it will be cancelled.
|
|
144
|
+
|
|
145
|
+
## Configuration
|
|
146
|
+
|
|
147
|
+
`bootstrapTimer()` registers `<wcs-timer>` and optionally overrides defaults. Pass a partial config:
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
import { bootstrapTimer } from "@wcstack/timer";
|
|
151
|
+
|
|
152
|
+
bootstrapTimer({
|
|
153
|
+
autoTrigger: true, // enable data-timertarget click triggering (default: true)
|
|
154
|
+
triggerAttribute: "data-timertarget", // attribute scanned for click triggering
|
|
155
|
+
tagNames: {
|
|
156
|
+
timer: "wcs-timer", // custom element tag name
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
`getConfig()` returns a deep-frozen snapshot of the current configuration:
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
import { getConfig } from "@wcstack/timer";
|
|
165
|
+
|
|
166
|
+
const { autoTrigger, triggerAttribute, tagNames } = getConfig();
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
| Option | Type | Default | Description |
|
|
170
|
+
| ------------------ | -------------------- | ------------------ | --------------------------------------------------- |
|
|
171
|
+
| `autoTrigger` | boolean | `true` | Enable `data-timertarget` click triggering. |
|
|
172
|
+
| `triggerAttribute` | string | `data-timertarget` | Attribute scanned for DOM click triggering. |
|
|
173
|
+
| `tagNames.timer` | string | `wcs-timer` | Custom element tag name to register. |
|
|
174
|
+
|
|
175
|
+
## Headless usage (`TimerCore`)
|
|
176
|
+
|
|
177
|
+
The Core has no DOM dependency and can be used directly with `bind()` from `@wc-bindable/core`:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { TimerCore } from "@wcstack/timer";
|
|
181
|
+
|
|
182
|
+
const timer = new TimerCore();
|
|
183
|
+
timer.addEventListener("wcs-timer:tick", (e) => {
|
|
184
|
+
console.log((e as CustomEvent).detail); // { count, elapsed }
|
|
185
|
+
});
|
|
186
|
+
timer.start({ interval: 1000, repeat: 10 });
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
MIT
|
package/dist/auto.js
ADDED
package/dist/auto.min.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
interface ITagNames {
|
|
2
|
+
readonly timer: string;
|
|
3
|
+
}
|
|
4
|
+
interface IWritableTagNames {
|
|
5
|
+
timer?: string;
|
|
6
|
+
}
|
|
7
|
+
interface IConfig {
|
|
8
|
+
readonly autoTrigger: boolean;
|
|
9
|
+
readonly triggerAttribute: string;
|
|
10
|
+
readonly tagNames: ITagNames;
|
|
11
|
+
}
|
|
12
|
+
interface IWritableConfig {
|
|
13
|
+
autoTrigger?: boolean;
|
|
14
|
+
triggerAttribute?: string;
|
|
15
|
+
tagNames?: IWritableTagNames;
|
|
16
|
+
}
|
|
17
|
+
interface IWcBindableProperty {
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly event: string;
|
|
20
|
+
readonly getter?: (event: Event) => any;
|
|
21
|
+
}
|
|
22
|
+
interface IWcBindableInput {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly attribute?: string;
|
|
25
|
+
}
|
|
26
|
+
interface IWcBindableCommand {
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly async?: boolean;
|
|
29
|
+
}
|
|
30
|
+
interface IWcBindable {
|
|
31
|
+
readonly protocol: "wc-bindable";
|
|
32
|
+
readonly version: number;
|
|
33
|
+
readonly properties: IWcBindableProperty[];
|
|
34
|
+
readonly inputs?: IWcBindableInput[];
|
|
35
|
+
readonly commands?: IWcBindableCommand[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Payload carried by the `wcs-timer:tick` event.
|
|
39
|
+
* `count` is the number of ticks fired since the last reset; `elapsed` is the
|
|
40
|
+
* milliseconds the timer has been running since the last reset.
|
|
41
|
+
*/
|
|
42
|
+
interface WcsTimerTickDetail {
|
|
43
|
+
count: number;
|
|
44
|
+
elapsed: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Value types for TimerCore (headless) — the observable state properties.
|
|
48
|
+
* Use with `bind()` from `@wc-bindable/core` for compile-time type checking.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const core = new TimerCore();
|
|
53
|
+
* bind(core, (name: keyof WcsTimerCoreValues, value) => { ... });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
interface WcsTimerCoreValues {
|
|
57
|
+
tick: number;
|
|
58
|
+
elapsed: number;
|
|
59
|
+
running: boolean;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Value types for the Shell (`<wcs-timer>`) — identical observable surface to
|
|
63
|
+
* the Core, plus the DOM-driven `trigger` command-property.
|
|
64
|
+
*/
|
|
65
|
+
interface WcsTimerValues extends WcsTimerCoreValues {
|
|
66
|
+
trigger: boolean;
|
|
67
|
+
}
|
|
68
|
+
interface WcsTimerInputs {
|
|
69
|
+
interval: number;
|
|
70
|
+
once: boolean;
|
|
71
|
+
repeat: number;
|
|
72
|
+
immediate: boolean;
|
|
73
|
+
manual: boolean;
|
|
74
|
+
trigger: boolean;
|
|
75
|
+
}
|
|
76
|
+
interface WcsTimerCoreCommands {
|
|
77
|
+
start(options?: {
|
|
78
|
+
interval?: number;
|
|
79
|
+
repeat?: number;
|
|
80
|
+
immediate?: boolean;
|
|
81
|
+
}): void;
|
|
82
|
+
stop(): void;
|
|
83
|
+
reset(): void;
|
|
84
|
+
pause(): void;
|
|
85
|
+
resume(): void;
|
|
86
|
+
}
|
|
87
|
+
interface WcsTimerCommands {
|
|
88
|
+
start(): void;
|
|
89
|
+
stop(): void;
|
|
90
|
+
reset(): void;
|
|
91
|
+
pause(): void;
|
|
92
|
+
resume(): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare function bootstrapTimer(userConfig?: IWritableConfig): void;
|
|
96
|
+
|
|
97
|
+
declare function getConfig(): IConfig;
|
|
98
|
+
|
|
99
|
+
interface TimerStartOptions {
|
|
100
|
+
interval?: number;
|
|
101
|
+
repeat?: number;
|
|
102
|
+
immediate?: boolean;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Headless timer primitive. A thin, framework-agnostic wrapper around
|
|
106
|
+
* `setInterval` exposed through the wc-bindable protocol: it streams `tick`
|
|
107
|
+
* (a monotonically increasing counter), `elapsed` (running time in ms) and a
|
|
108
|
+
* `running` flag, and is driven by the `start` / `stop` / `reset` / `pause` /
|
|
109
|
+
* `resume` commands.
|
|
110
|
+
*
|
|
111
|
+
* `tick` and `elapsed` are both surfaced via the single `wcs-timer:tick` event
|
|
112
|
+
* (read through getters, mirroring how FetchCore exposes value/status from one
|
|
113
|
+
* `wcs-fetch:response` event), so an observer that binds either property is
|
|
114
|
+
* notified on every fire.
|
|
115
|
+
*/
|
|
116
|
+
declare class TimerCore extends EventTarget {
|
|
117
|
+
static wcBindable: IWcBindable;
|
|
118
|
+
private _target;
|
|
119
|
+
private _timerId;
|
|
120
|
+
private _tick;
|
|
121
|
+
private _running;
|
|
122
|
+
private _paused;
|
|
123
|
+
private _runStartTick;
|
|
124
|
+
private _interval;
|
|
125
|
+
private _repeat;
|
|
126
|
+
private _accumulatedElapsed;
|
|
127
|
+
private _segmentStart;
|
|
128
|
+
constructor(target?: EventTarget);
|
|
129
|
+
get tick(): number;
|
|
130
|
+
get elapsed(): number;
|
|
131
|
+
get running(): boolean;
|
|
132
|
+
private _dispatchTick;
|
|
133
|
+
private _setRunning;
|
|
134
|
+
start(options?: TimerStartOptions): void;
|
|
135
|
+
changeInterval(interval: number): void;
|
|
136
|
+
stop(): void;
|
|
137
|
+
reset(): void;
|
|
138
|
+
pause(): void;
|
|
139
|
+
resume(): void;
|
|
140
|
+
private _onResumeBoundary;
|
|
141
|
+
private _fire;
|
|
142
|
+
private _clearTimer;
|
|
143
|
+
private _foldElapsed;
|
|
144
|
+
private _currentElapsed;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
declare class Timer extends HTMLElement {
|
|
148
|
+
static hasConnectedCallbackPromise: boolean;
|
|
149
|
+
static wcBindable: IWcBindable;
|
|
150
|
+
static get observedAttributes(): string[];
|
|
151
|
+
private _core;
|
|
152
|
+
private _trigger;
|
|
153
|
+
constructor();
|
|
154
|
+
get interval(): number;
|
|
155
|
+
set interval(value: number);
|
|
156
|
+
get once(): boolean;
|
|
157
|
+
set once(value: boolean);
|
|
158
|
+
get repeat(): number;
|
|
159
|
+
set repeat(value: number);
|
|
160
|
+
get immediate(): boolean;
|
|
161
|
+
set immediate(value: boolean);
|
|
162
|
+
get manual(): boolean;
|
|
163
|
+
set manual(value: boolean);
|
|
164
|
+
get tick(): number;
|
|
165
|
+
get elapsed(): number;
|
|
166
|
+
get running(): boolean;
|
|
167
|
+
get trigger(): boolean;
|
|
168
|
+
set trigger(value: boolean);
|
|
169
|
+
start(): void;
|
|
170
|
+
stop(): void;
|
|
171
|
+
reset(): void;
|
|
172
|
+
pause(): void;
|
|
173
|
+
resume(): void;
|
|
174
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
|
|
175
|
+
connectedCallback(): void;
|
|
176
|
+
disconnectedCallback(): void;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export { TimerCore, Timer as WcsTimer, bootstrapTimer, getConfig };
|
|
180
|
+
export type { IWritableConfig, IWritableTagNames, TimerStartOptions, WcsTimerCommands, WcsTimerCoreCommands, WcsTimerCoreValues, WcsTimerInputs, WcsTimerTickDetail, WcsTimerValues };
|