Skip to content

Commit b62e51b

Browse files
committed
feat: 키 입력 통계 api 추가 및 문서, 예시 플러그인 추가
1 parent 0ef260c commit b62e51b

6 files changed

Lines changed: 967 additions & 47 deletions

File tree

docs/content/en/api-reference/keys/page.mdx

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,79 @@ const unsub = dmn.keys.registerCustomTab({
147147

148148
---
149149

150+
## Stats API
151+
152+
Real-time key input statistics API. Provides KPS (Keys Per Second) and total key count for the current tab.
153+
154+
<Callout type="info">
155+
No performance overhead when there are no subscribers - statistics calculation
156+
is only performed when subscribed.
157+
</Callout>
158+
159+
### `dmn.stats.subscribe(callback): Unsubscribe`
160+
161+
Subscribes to real-time key input statistics.
162+
163+
- KPS values (kps, kpsAvg, kpsMax): Updated approximately every 50ms
164+
- total: Updated immediately when key counter changes
165+
166+
```typescript
167+
interface KeyStatsPayload {
168+
kps: number; // Current KPS (keys per second)
169+
kpsAvg: number; // Average KPS
170+
kpsMax: number; // Maximum KPS
171+
total: number; // Total key count for current tab
172+
}
173+
174+
const unsub = dmn.stats.subscribe(({ kps, kpsAvg, kpsMax, total }) => {
175+
console.log(`KPS: ${kps}, AVG: ${kpsAvg}, MAX: ${kpsMax}, TOTAL: ${total}`);
176+
});
177+
178+
// Unsubscribe
179+
unsub();
180+
```
181+
182+
### `dmn.stats.get(): KeyStatsPayload`
183+
184+
Gets current statistics immediately.
185+
186+
```javascript
187+
const stats = dmn.stats.get();
188+
console.log(`Current KPS: ${stats.kps}`);
189+
```
190+
191+
### `dmn.stats.reset(): void`
192+
193+
Resets KPS-related statistics (kps, kpsAvg, kpsMax). `total` is not reset as it's based on key counters.
194+
195+
```javascript
196+
dmn.stats.reset();
197+
```
198+
199+
<Callout type="warning">
200+
To reset `total`, use `dmn.keys.resetCounters()` or
201+
`dmn.keys.resetCountersMode(mode)`.
202+
</Callout>
203+
204+
---
205+
150206
## Example Usage
151207

152208
```javascript
153-
// KPS counter
209+
// KPS counter using stats API
154210
dmn.plugin.defineElement({
155211
name: "KPS Counter",
156212
maxInstances: 1,
157213

158214
template: (state, settings, { html }) => html`
159215
<div style="font-size: 24px; font-weight: bold;">
160-
KPS: ${(state.kps ?? 0).toFixed(1)}
216+
KPS: ${state.kps ?? 0} | MAX: ${state.kpsMax ?? 0}
161217
</div>
162218
`,
163219
164220
onMount: ({ setState }) => {
165-
const keyTimes = [];
166-
167-
const unsub = dmn.keys.onKeyState(({ state }) => {
168-
if (state !== "DOWN") return;
169-
170-
const now = Date.now();
171-
keyTimes.push(now);
172-
173-
// Keep only last 1 second
174-
while (keyTimes.length && keyTimes[0] < now - 1000) {
175-
keyTimes.shift();
176-
}
177-
178-
setState({ kps: keyTimes.length });
221+
const unsub = dmn.stats.subscribe(({ kps, kpsMax }) => {
222+
setState({ kps, kpsMax });
179223
});
180224
181225
return () => unsub();

docs/content/ko/api-reference/keys/page.mdx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,61 @@ await dmn.keys.customTabs.select("custom-123");
280280
const unsub = dmn.keys.customTabs.onChanged(
281281
({ customTabs, selectedKeyType }) => {
282282
console.log("선택된 탭:", selectedKeyType);
283-
}
283+
},
284284
);
285285
```
286+
287+
## 통계 (stats)
288+
289+
키 입력 통계를 실시간으로 제공하는 API입니다. KPS(Keys Per Second)와 현재 탭의 전체 키 카운트를 조회할 수 있습니다.
290+
291+
<Callout type="info">
292+
구독자가 없으면 통계 계산이 수행되지 않아 성능 오버헤드가 없습니다.
293+
</Callout>
294+
295+
### subscribe(listener)
296+
297+
키 입력 통계를 실시간으로 구독합니다.
298+
299+
- KPS 관련 값(kps, kpsAvg, kpsMax): 약 50ms 간격으로 업데이트
300+
- total: 키 카운터 변경 시 즉시 업데이트
301+
302+
```typescript
303+
interface KeyStatsPayload {
304+
kps: number; // 현재 KPS (초당 키 입력 수)
305+
kpsAvg: number; // 평균 KPS
306+
kpsMax: number; // 최대 KPS
307+
total: number; // 현재 탭의 전체 키 카운트 합계
308+
}
309+
```
310+
311+
```javascript
312+
const unsub = dmn.stats.subscribe(({ kps, kpsAvg, kpsMax, total }) => {
313+
console.log(`KPS: ${kps}, AVG: ${kpsAvg}, MAX: ${kpsMax}, TOTAL: ${total}`);
314+
});
315+
316+
// 구독 해제
317+
unsub();
318+
```
319+
320+
### get()
321+
322+
현재 통계값을 즉시 조회합니다.
323+
324+
```javascript
325+
const stats = dmn.stats.get();
326+
console.log(`현재 KPS: ${stats.kps}`);
327+
```
328+
329+
### reset()
330+
331+
KPS 관련 통계(kps, kpsAvg, kpsMax)를 초기화합니다. `total`은 키 카운터 기반이므로 초기화되지 않습니다.
332+
333+
```javascript
334+
dmn.stats.reset();
335+
```
336+
337+
<Callout type="warning">
338+
`total`을 초기화하려면 `dmn.keys.resetCounters()` 또는
339+
`dmn.keys.resetCountersMode(mode)`를 사용하세요.
340+
</Callout>

0 commit comments

Comments
 (0)