@hadoku/ui-shell 0.1.96 → 0.1.97
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/dist/usage/index.d.ts +22 -2
- package/dist/usage/index.js +23 -7
- package/package.json +2 -2
package/dist/usage/index.d.ts
CHANGED
|
@@ -5,28 +5,48 @@
|
|
|
5
5
|
* its ids stripped (`seat-3` → `seat`), so a new control is counted without anyone remembering to;
|
|
6
6
|
* a press on nothing named is not counted. A batch goes when it is full, on a timer, and when the
|
|
7
7
|
* page hides (`sendBeacon`, which survives the tab closing). A failed send is dropped.
|
|
8
|
+
*
|
|
9
|
+
* Every entry carries `at`, this page's clock when it happened, and the batch carries `sentAt`, the
|
|
10
|
+
* same clock when it left: the server places each entry at its own receipt time less
|
|
11
|
+
* `sentAt - at`, so a browser whose clock is wrong still lands its trail in the right order and
|
|
12
|
+
* at the right moment.
|
|
13
|
+
*
|
|
14
|
+
* The screen is read from the whole address (path and hash), not the hash alone: the app's routes
|
|
15
|
+
* are paths written with `history.pushState`, which fires no event, so the screen is re-read on
|
|
16
|
+
* `popstate`/`hashchange`, at every press (before it, so a press counts on the screen it was made
|
|
17
|
+
* on) and once just after it (so a press that navigates opens its new screen at once).
|
|
8
18
|
*/
|
|
9
19
|
export interface UsageEntry {
|
|
10
20
|
kind: 'view' | 'click';
|
|
11
21
|
screen: string;
|
|
12
22
|
target?: string;
|
|
23
|
+
/** This page's `Date.now()` when it happened. */
|
|
24
|
+
at: number;
|
|
13
25
|
}
|
|
14
26
|
export interface UsageBatch {
|
|
15
27
|
entries: UsageEntry[];
|
|
28
|
+
/** This page's `Date.now()` when the batch was sent. */
|
|
29
|
+
sentAt: number;
|
|
30
|
+
}
|
|
31
|
+
/** What `screenOf` is handed: the address bar's path and hash. */
|
|
32
|
+
export interface UsageAddress {
|
|
33
|
+
pathname: string;
|
|
34
|
+
hash: string;
|
|
16
35
|
}
|
|
17
36
|
/** A `data-testid` as a usage name, or null when what is left is not a name. */
|
|
18
37
|
export declare function targetName(testid: string): string | null;
|
|
19
38
|
export interface UsageDeps {
|
|
20
39
|
/** Where a batch is posted; the caller's own `/api/...` path. */
|
|
21
40
|
url: string;
|
|
22
|
-
/** The screen
|
|
23
|
-
screenOf: (
|
|
41
|
+
/** The screen an address names; the caller's own route parsing. */
|
|
42
|
+
screenOf: (address: UsageAddress) => string;
|
|
24
43
|
/** Entries per batch before an early flush; the caller's own schema limit. */
|
|
25
44
|
batchMax: number;
|
|
26
45
|
flushMs?: number;
|
|
27
46
|
doc?: Document;
|
|
28
47
|
win?: Window;
|
|
29
48
|
send?: (url: string, batch: UsageBatch) => void;
|
|
49
|
+
now?: () => number;
|
|
30
50
|
}
|
|
31
51
|
/** Start counting. Returns the stop, which flushes what is left. */
|
|
32
52
|
export declare function startUsage(deps: UsageDeps): () => void;
|
package/dist/usage/index.js
CHANGED
|
@@ -5,6 +5,16 @@
|
|
|
5
5
|
* its ids stripped (`seat-3` → `seat`), so a new control is counted without anyone remembering to;
|
|
6
6
|
* a press on nothing named is not counted. A batch goes when it is full, on a timer, and when the
|
|
7
7
|
* page hides (`sendBeacon`, which survives the tab closing). A failed send is dropped.
|
|
8
|
+
*
|
|
9
|
+
* Every entry carries `at`, this page's clock when it happened, and the batch carries `sentAt`, the
|
|
10
|
+
* same clock when it left: the server places each entry at its own receipt time less
|
|
11
|
+
* `sentAt - at`, so a browser whose clock is wrong still lands its trail in the right order and
|
|
12
|
+
* at the right moment.
|
|
13
|
+
*
|
|
14
|
+
* The screen is read from the whole address (path and hash), not the hash alone: the app's routes
|
|
15
|
+
* are paths written with `history.pushState`, which fires no event, so the screen is re-read on
|
|
16
|
+
* `popstate`/`hashchange`, at every press (before it, so a press counts on the screen it was made
|
|
17
|
+
* on) and once just after it (so a press that navigates opens its new screen at once).
|
|
8
18
|
*/
|
|
9
19
|
const NAME = /^[a-z0-9][a-z0-9_.:/-]*$/;
|
|
10
20
|
/** A `data-testid` as a usage name, or null when what is left is not a name. */
|
|
@@ -35,46 +45,52 @@ export function startUsage(deps) {
|
|
|
35
45
|
const doc = deps.doc ?? document;
|
|
36
46
|
const win = deps.win ?? window;
|
|
37
47
|
const send = deps.send ?? beacon;
|
|
48
|
+
const now = deps.now ?? Date.now;
|
|
38
49
|
let entries = [];
|
|
39
50
|
const flush = () => {
|
|
40
51
|
if (entries.length === 0)
|
|
41
52
|
return;
|
|
42
|
-
const batch = { entries };
|
|
53
|
+
const batch = { entries, sentAt: now() };
|
|
43
54
|
entries = [];
|
|
44
55
|
send(url, batch);
|
|
45
56
|
};
|
|
46
57
|
const push = (entry) => {
|
|
47
|
-
entries.push(entry);
|
|
58
|
+
entries.push({ ...entry, at: now() });
|
|
48
59
|
if (entries.length >= batchMax)
|
|
49
60
|
flush();
|
|
50
61
|
};
|
|
51
|
-
|
|
62
|
+
const read = () => screenOf({ pathname: win.location.pathname, hash: win.location.hash });
|
|
63
|
+
let screen = read();
|
|
52
64
|
push({ kind: 'view', screen });
|
|
53
|
-
const
|
|
54
|
-
const next =
|
|
65
|
+
const onMove = () => {
|
|
66
|
+
const next = read();
|
|
55
67
|
if (next === screen)
|
|
56
68
|
return;
|
|
57
69
|
screen = next;
|
|
58
70
|
push({ kind: 'view', screen });
|
|
59
71
|
};
|
|
60
72
|
const onClick = (event) => {
|
|
73
|
+
onMove();
|
|
61
74
|
const el = event.target instanceof Element ? event.target.closest('[data-testid]') : null;
|
|
62
75
|
const testid = el?.getAttribute('data-testid');
|
|
63
76
|
const target = testid ? targetName(testid) : null;
|
|
64
77
|
if (target !== null)
|
|
65
78
|
push({ kind: 'click', screen, target });
|
|
79
|
+
win.setTimeout(onMove, 0);
|
|
66
80
|
};
|
|
67
81
|
const onHide = () => {
|
|
68
82
|
if (doc.visibilityState === 'hidden')
|
|
69
83
|
flush();
|
|
70
84
|
};
|
|
71
|
-
win.addEventListener('hashchange',
|
|
85
|
+
win.addEventListener('hashchange', onMove);
|
|
86
|
+
win.addEventListener('popstate', onMove);
|
|
72
87
|
doc.addEventListener('click', onClick, { capture: true, passive: true });
|
|
73
88
|
doc.addEventListener('visibilitychange', onHide);
|
|
74
89
|
win.addEventListener('pagehide', flush);
|
|
75
90
|
const timer = win.setInterval(flush, flushMs);
|
|
76
91
|
return () => {
|
|
77
|
-
win.removeEventListener('hashchange',
|
|
92
|
+
win.removeEventListener('hashchange', onMove);
|
|
93
|
+
win.removeEventListener('popstate', onMove);
|
|
78
94
|
doc.removeEventListener('click', onClick, { capture: true });
|
|
79
95
|
doc.removeEventListener('visibilitychange', onHide);
|
|
80
96
|
win.removeEventListener('pagehide', flush);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/ui-shell",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.97",
|
|
4
|
+
"gitHead": "155c8c9335e64b29e1c0f0c9c18a8c79509093fb",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The parts of a game's UI any game keeps: chrome, trim, room shell, embed mount, theme and settings frame, UI scale, usage counting, the sound mixer, motion and generic components. One appId parameterizes its storage keys, events and chat prefix. MIT; imports only react, react-dom and @wolffm/*.",
|