@proteinjs/user-ui 1.9.6 → 1.10.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/CHANGELOG.md +11 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/src/AuthenticatedPageContainer.d.ts.map +1 -1
- package/dist/src/AuthenticatedPageContainer.js +7 -0
- package/dist/src/AuthenticatedPageContainer.js.map +1 -1
- package/dist/src/presence/UserPresenceReporter.d.ts +34 -0
- package/dist/src/presence/UserPresenceReporter.d.ts.map +1 -0
- package/dist/src/presence/UserPresenceReporter.js +131 -0
- package/dist/src/presence/UserPresenceReporter.js.map +1 -0
- package/dist/src/presence/installUserPresenceReporter.d.ts +8 -0
- package/dist/src/presence/installUserPresenceReporter.d.ts.map +1 -0
- package/dist/src/presence/installUserPresenceReporter.js +15 -0
- package/dist/src/presence/installUserPresenceReporter.js.map +1 -0
- package/dist/test/userPresenceReporter.test.d.ts +2 -0
- package/dist/test/userPresenceReporter.test.d.ts.map +1 -0
- package/dist/test/userPresenceReporter.test.js +226 -0
- package/dist/test/userPresenceReporter.test.js.map +1 -0
- package/index.ts +2 -0
- package/package.json +3 -3
- package/src/AuthenticatedPageContainer.tsx +8 -0
- package/src/presence/UserPresenceReporter.ts +80 -0
- package/src/presence/installUserPresenceReporter.ts +11 -0
- package/test/userPresenceReporter.test.ts +117 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.10.0](https://github.com/proteinjs/user/compare/@proteinjs/user-ui@1.9.6...@proteinjs/user-ui@1.10.0) (2026-09-12)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **presence:** last active = HUMAN INPUT — the page reports a person's pointer/key/touch/wheel through the new UserPresenceService, the ONE door onto user_activity; the per-request session build no longer stamps ([3dc355f](https://github.com/proteinjs/user/commit/3dc355fad62f2a964ebc1361c881f3c76490b55b))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [1.9.6](https://github.com/proteinjs/user/compare/@proteinjs/user-ui@1.9.5...@proteinjs/user-ui@1.9.6) (2026-09-03)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @proteinjs/user-ui
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './src/AuthenticatedPageContainer';
|
|
2
2
|
export * from './src/pageAuth';
|
|
3
|
+
export * from './src/presence/UserPresenceReporter';
|
|
4
|
+
export * from './src/presence/installUserPresenceReporter';
|
|
3
5
|
export * from './src/auth/AuthLayout';
|
|
4
6
|
export * from './src/auth/AuthTextField';
|
|
5
7
|
export * from './src/auth/AuthButton';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qCAAqC,CAAC;AACpD,cAAc,4CAA4C,CAAC;AAI3D,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./src/AuthenticatedPageContainer"), exports);
|
|
18
18
|
__exportStar(require("./src/pageAuth"), exports);
|
|
19
|
+
__exportStar(require("./src/presence/UserPresenceReporter"), exports);
|
|
20
|
+
__exportStar(require("./src/presence/installUserPresenceReporter"), exports);
|
|
19
21
|
// The logged-out surface's building blocks, exported so a consumer app's own public pages
|
|
20
22
|
// (e.g. n3xa's /request-invite) render with the same auth look instead of forking the styling.
|
|
21
23
|
__exportStar(require("./src/auth/AuthLayout"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mEAAiD;AACjD,iDAA+B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mEAAiD;AACjD,iDAA+B;AAC/B,sEAAoD;AACpD,6EAA2D;AAE3D,0FAA0F;AAC1F,+FAA+F;AAC/F,wDAAsC;AACtC,2DAAyC;AACzC,wDAAsC;AACtC,2DAAyC;AACzC,8DAA4C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthenticatedPageContainer.d.ts","sourceRoot":"","sources":["../../src/AuthenticatedPageContainer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAiB,kBAAkB,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"AuthenticatedPageContainer.d.ts","sourceRoot":"","sources":["../../src/AuthenticatedPageContainer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAiB,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAKlE,MAAM,MAAM,+BAA+B,GAAG,IAAI,CAAC,kBAAkB,EAAE,MAAM,CAAC,CAAC;AAE/E,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,+BAA+B,qBAsChF"}
|
|
@@ -66,10 +66,17 @@ var react_1 = __importDefault(require("react"));
|
|
|
66
66
|
var ui_1 = require("@proteinjs/ui");
|
|
67
67
|
var user_1 = require("@proteinjs/user");
|
|
68
68
|
var pageAuth_1 = require("./pageAuth");
|
|
69
|
+
var installUserPresenceReporter_1 = require("./presence/installUserPresenceReporter");
|
|
69
70
|
function AuthenticatedPageContainer(props) {
|
|
70
71
|
var _this = this;
|
|
71
72
|
var other = __rest(props, []);
|
|
72
73
|
var _a = react_1.default.useState(user_1.UserAuth.isLoggedIn()), isLoggedIn = _a[0], setIsLoggedIn = _a[1];
|
|
74
|
+
// "Last active" (UserActivityTable's contract): every authenticated page reports a person's
|
|
75
|
+
// input through the presence door. Installed here — the one root every consumer's
|
|
76
|
+
// authenticated pages share — so no app wires it per page; idempotent across remounts.
|
|
77
|
+
react_1.default.useEffect(function () {
|
|
78
|
+
(0, installUserPresenceReporter_1.installUserPresenceReporter)();
|
|
79
|
+
}, []);
|
|
73
80
|
return (react_1.default.createElement(ui_1.PageContainer, __assign({ auth: {
|
|
74
81
|
isLoggedIn: isLoggedIn,
|
|
75
82
|
canViewPage: pageAuth_1.canViewPage,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthenticatedPageContainer.js","sourceRoot":"","sources":["../../src/AuthenticatedPageContainer.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAA0B;AAC1B,oCAAkE;AAClE,wCAAkF;AAClF,uCAAyC;
|
|
1
|
+
{"version":3,"file":"AuthenticatedPageContainer.js","sourceRoot":"","sources":["../../src/AuthenticatedPageContainer.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAA0B;AAC1B,oCAAkE;AAClE,wCAAk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able's contract): a person's INPUT on the
|
|
3
|
+
* page — pointer, key, touch, wheel — becomes one presence report per interval through
|
|
4
|
+
* `UserPresenceService.recordPresence`, the only door that moves the `user_activity` stamp.
|
|
5
|
+
*
|
|
6
|
+
* Input, never transport: a tab left open polls, re-joins its socket rooms on every reconnect
|
|
7
|
+
* and reloads when a deploy lands — all of it over a live session with nobody there, and all
|
|
8
|
+
* of it read as presence when the stamp rode the request path (founder finding 2026-09-12:
|
|
9
|
+
* every user "active today"). Timers, focus and visibility are not input either (a tab can be
|
|
10
|
+
* visible with nobody in front of it), so nothing here fires without a hand on the device.
|
|
11
|
+
*
|
|
12
|
+
* Fail-open: a report that fails is dropped; the next input after {@link RETRY_AFTER_MS}
|
|
13
|
+
* tries again (never one request per keystroke against a down server). The listeners are
|
|
14
|
+
* passive capture listeners on the document — they never block scrolling or a handler.
|
|
15
|
+
*/
|
|
16
|
+
export declare class UserPresenceReporter {
|
|
17
|
+
/** Report at most this often — the same cadence class as the server stamp's own throttle. */
|
|
18
|
+
static readonly REPORT_INTERVAL_MS: number;
|
|
19
|
+
/** After a failed report, the next input retries only once this long has passed. */
|
|
20
|
+
static readonly RETRY_AFTER_MS: number;
|
|
21
|
+
/** The events that mean a person is here. */
|
|
22
|
+
static readonly INPUT_EVENTS: readonly string[];
|
|
23
|
+
private static instance;
|
|
24
|
+
static get(): UserPresenceReporter;
|
|
25
|
+
private installed;
|
|
26
|
+
private lastReportMs;
|
|
27
|
+
/** Listen for input on the document. Idempotent; a no-op outside a browser. */
|
|
28
|
+
install(): void;
|
|
29
|
+
/** Stop listening (tests, teardown). The throttle history is kept. */
|
|
30
|
+
uninstall(): void;
|
|
31
|
+
private onInput;
|
|
32
|
+
private report;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=UserPresenceReporter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserPresenceReporter.d.ts","sourceRoot":"","sources":["../../../src/presence/UserPresenceReporter.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,qBAAa,oBAAoB;IAC/B,6FAA6F;IAC7F,MAAM,CAAC,QAAQ,CAAC,kBAAkB,SAAiB;IACnD,oFAAoF;IACpF,MAAM,CAAC,QAAQ,CAAC,cAAc,SAAa;IAC3C,6CAA6C;IAC7C,MAAM,CAAC,QAAQ,CAAC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAqD;IAEpG,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAmC;IAE1D,MAAM,CAAC,GAAG,IAAI,oBAAoB;IAOlC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,YAAY,CAAK;IAEzB,+EAA+E;IAC/E,OAAO,IAAI,IAAI;IAUf,sEAAsE;IACtE,SAAS,IAAI,IAAI;IAUjB,OAAO,CAAC,OAAO,CAUb;YAEY,MAAM;CAQrB"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.UserPresenceReporter = void 0;
|
|
40
|
+
var user_1 = require("@proteinjs/user");
|
|
41
|
+
/**
|
|
42
|
+
* The browser half of "last active" (UserActivityTable's contract): a person's INPUT on the
|
|
43
|
+
* page — pointer, key, touch, wheel — becomes one presence report per interval through
|
|
44
|
+
* `UserPresenceService.recordPresence`, the only door that moves the `user_activity` stamp.
|
|
45
|
+
*
|
|
46
|
+
* Input, never transport: a tab left open polls, re-joins its socket rooms on every reconnect
|
|
47
|
+
* and reloads when a deploy lands — all of it over a live session with nobody there, and all
|
|
48
|
+
* of it read as presence when the stamp rode the request path (founder finding 2026-09-12:
|
|
49
|
+
* every user "active today"). Timers, focus and visibility are not input either (a tab can be
|
|
50
|
+
* visible with nobody in front of it), so nothing here fires without a hand on the device.
|
|
51
|
+
*
|
|
52
|
+
* Fail-open: a report that fails is dropped; the next input after {@link RETRY_AFTER_MS}
|
|
53
|
+
* tries again (never one request per keystroke against a down server). The listeners are
|
|
54
|
+
* passive capture listeners on the document — they never block scrolling or a handler.
|
|
55
|
+
*/
|
|
56
|
+
var UserPresenceReporter = /** @class */ (function () {
|
|
57
|
+
function UserPresenceReporter() {
|
|
58
|
+
var _this = this;
|
|
59
|
+
this.installed = false;
|
|
60
|
+
this.lastReportMs = 0;
|
|
61
|
+
this.onInput = function () {
|
|
62
|
+
var now = Date.now();
|
|
63
|
+
if (now - _this.lastReportMs < UserPresenceReporter.REPORT_INTERVAL_MS) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!user_1.UserAuth.isLoggedIn()) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
_this.lastReportMs = now;
|
|
70
|
+
void _this.report(now);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
UserPresenceReporter.get = function () {
|
|
74
|
+
if (!UserPresenceReporter.instance) {
|
|
75
|
+
UserPresenceReporter.instance = new UserPresenceReporter();
|
|
76
|
+
}
|
|
77
|
+
return UserPresenceReporter.instance;
|
|
78
|
+
};
|
|
79
|
+
/** Listen for input on the document. Idempotent; a no-op outside a browser. */
|
|
80
|
+
UserPresenceReporter.prototype.install = function () {
|
|
81
|
+
if (this.installed || typeof document === 'undefined') {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
this.installed = true;
|
|
85
|
+
for (var _i = 0, _a = UserPresenceReporter.INPUT_EVENTS; _i < _a.length; _i++) {
|
|
86
|
+
var type = _a[_i];
|
|
87
|
+
document.addEventListener(type, this.onInput, { capture: true, passive: true });
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
/** Stop listening (tests, teardown). The throttle history is kept. */
|
|
91
|
+
UserPresenceReporter.prototype.uninstall = function () {
|
|
92
|
+
if (!this.installed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.installed = false;
|
|
96
|
+
for (var _i = 0, _a = UserPresenceReporter.INPUT_EVENTS; _i < _a.length; _i++) {
|
|
97
|
+
var type = _a[_i];
|
|
98
|
+
document.removeEventListener(type, this.onInput, { capture: true });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
UserPresenceReporter.prototype.report = function (at) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
103
|
+
var error_1;
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
switch (_a.label) {
|
|
106
|
+
case 0:
|
|
107
|
+
_a.trys.push([0, 2, , 3]);
|
|
108
|
+
return [4 /*yield*/, (0, user_1.getUserPresenceService)().recordPresence()];
|
|
109
|
+
case 1:
|
|
110
|
+
_a.sent();
|
|
111
|
+
return [3 /*break*/, 3];
|
|
112
|
+
case 2:
|
|
113
|
+
error_1 = _a.sent();
|
|
114
|
+
// Dropped on purpose; the next input after the retry window reports again.
|
|
115
|
+
this.lastReportMs = at - UserPresenceReporter.REPORT_INTERVAL_MS + UserPresenceReporter.RETRY_AFTER_MS;
|
|
116
|
+
return [3 /*break*/, 3];
|
|
117
|
+
case 3: return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
/** Report at most this often — the same cadence class as the server stamp's own throttle. */
|
|
123
|
+
UserPresenceReporter.REPORT_INTERVAL_MS = 1000 * 60 * 5;
|
|
124
|
+
/** After a failed report, the next input retries only once this long has passed. */
|
|
125
|
+
UserPresenceReporter.RETRY_AFTER_MS = 1000 * 30;
|
|
126
|
+
/** The events that mean a person is here. */
|
|
127
|
+
UserPresenceReporter.INPUT_EVENTS = ['pointerdown', 'keydown', 'touchstart', 'wheel'];
|
|
128
|
+
return UserPresenceReporter;
|
|
129
|
+
}());
|
|
130
|
+
exports.UserPresenceReporter = UserPresenceReporter;
|
|
131
|
+
//# sourceMappingURL=UserPresenceReporter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserPresenceReporter.js","sourceRoot":"","sources":["../../../src/presence/UserPresenceReporter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAAmE;AAEnE;;;;;;;;;;;;;;GAcG;AACH;IAAA;QAAA,iBA8DC;QA7CS,cAAS,GAAG,KAAK,CAAC;QAClB,iBAAY,GAAG,CAAC,CAAC;QAwBjB,YAAO,GAAG;YAChB,IAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACvB,IAAI,GAAG,GAAG,KAAI,CAAC,YAAY,GAAG,oBAAoB,CAAC,kBAAkB,EAAE;gBACrE,OAAO;aACR;YACD,IAAI,CAAC,eAAQ,CAAC,UAAU,EAAE,EAAE;gBAC1B,OAAO;aACR;YACD,KAAI,CAAC,YAAY,GAAG,GAAG,CAAC;YACxB,KAAK,KAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC,CAAC;IAUJ,CAAC;IApDQ,wBAAG,GAAV;QACE,IAAI,CAAC,oBAAoB,CAAC,QAAQ,EAAE;YAClC,oBAAoB,CAAC,QAAQ,GAAG,IAAI,oBAAoB,EAAE,CAAC;SAC5D;QACD,OAAO,oBAAoB,CAAC,QAAQ,CAAC;IACvC,CAAC;IAKD,+EAA+E;IAC/E,sCAAO,GAAP;QACE,IAAI,IAAI,CAAC,SAAS,IAAI,OAAO,QAAQ,KAAK,WAAW,E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A9DD,IA8DC;AA9DY,oDAAoB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Install the browser half of "last active": the page's human-input report (see
|
|
3
|
+
* {@link UserPresenceReporter}). `AuthenticatedPageContainer` installs it on mount, so every
|
|
4
|
+
* consumer app's authenticated pages report presence without wiring; a consumer with its own
|
|
5
|
+
* page container calls this once at its root. Idempotent.
|
|
6
|
+
*/
|
|
7
|
+
export declare function installUserPresenceReporter(): void;
|
|
8
|
+
//# sourceMappingURL=installUserPresenceReporter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"installUserPresenceReporter.d.ts","sourceRoot":"","sources":["../../../src/presence/installUserPresenceReporter.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,2BAA2B,IAAI,IAAI,CAElD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.installUserPresenceReporter = void 0;
|
|
4
|
+
var UserPresenceReporter_1 = require("./UserPresenceReporter");
|
|
5
|
+
/**
|
|
6
|
+
* Install the browser half of "last active": the page's human-input report (see
|
|
7
|
+
* {@link UserPresenceReporter}). `AuthenticatedPageContainer` installs it on mount, so every
|
|
8
|
+
* consumer app's authenticated pages report presence without wiring; a consumer with its own
|
|
9
|
+
* page container calls this once at its root. Idempotent.
|
|
10
|
+
*/
|
|
11
|
+
function installUserPresenceReporter() {
|
|
12
|
+
UserPresenceReporter_1.UserPresenceReporter.get().install();
|
|
13
|
+
}
|
|
14
|
+
exports.installUserPresenceReporter = installUserPresenceReporter;
|
|
15
|
+
//# sourceMappingURL=installUserPresenceReporter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"installUserPresenceReporter.js","sourceRoot":"","sources":["../../../src/presence/installUserPresenceReporter.ts"],"names":[],"mappings":";;;AAAA,+DAA8D;AAE9D;;;;;GAKG;AACH,SAAgB,2BAA2B;IACzC,2CAAoB,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;AACvC,CAAC;AAFD,kEAEC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"userPresenceReporter.test.d.ts","sourceRoot":"","sources":["../../test/userPresenceReporter.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
/**
|
|
51
|
+
* @jest-environment jsdom
|
|
52
|
+
* @jest-environment-options {"customExportConditions": ["node", "node-addons"]}
|
|
53
|
+
*
|
|
54
|
+
* The browser half of "last active" (UserActivityTable's contract): presence is reported from a
|
|
55
|
+
* person's INPUT on the page and from nothing else. These pin the reporter's outcomes against
|
|
56
|
+
* the presence door (`getUserPresenceService` is the only mock; time is faked):
|
|
57
|
+
* a timer is not a person, an input is, the throttle holds, a logged-out page reports nothing,
|
|
58
|
+
* and a failed report retries on the next input after the retry window.
|
|
59
|
+
*/
|
|
60
|
+
var user_1 = require("@proteinjs/user");
|
|
61
|
+
var UserPresenceReporter_1 = require("../src/presence/UserPresenceReporter");
|
|
62
|
+
var recordPresence = jest.fn();
|
|
63
|
+
jest.mock('@proteinjs/user', function () { return (__assign(__assign({}, jest.requireActual('@proteinjs/user')), { getUserPresenceService: function () { return ({ recordPresence: recordPresence }); } })); });
|
|
64
|
+
var input = function (type) { return document.dispatchEvent(new Event(type, { bubbles: true })); };
|
|
65
|
+
/** Let the fire-and-forget report settle (one macrotask under fake timers). */
|
|
66
|
+
var settle = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0: return [4 /*yield*/, jest.advanceTimersByTimeAsync(0)];
|
|
70
|
+
case 1:
|
|
71
|
+
_a.sent();
|
|
72
|
+
return [2 /*return*/];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}); };
|
|
76
|
+
var reporter;
|
|
77
|
+
var isLoggedIn;
|
|
78
|
+
beforeEach(function () {
|
|
79
|
+
jest.useFakeTimers();
|
|
80
|
+
jest.setSystemTime(new Date('2026-09-12T10:00:00.000Z'));
|
|
81
|
+
recordPresence.mockReset().mockResolvedValue(undefined);
|
|
82
|
+
isLoggedIn = jest.spyOn(user_1.UserAuth, 'isLoggedIn').mockReturnValue(true);
|
|
83
|
+
reporter = new UserPresenceReporter_1.UserPresenceReporter();
|
|
84
|
+
reporter.install();
|
|
85
|
+
});
|
|
86
|
+
afterEach(function () {
|
|
87
|
+
reporter.uninstall();
|
|
88
|
+
isLoggedIn.mockRestore();
|
|
89
|
+
jest.useRealTimers();
|
|
90
|
+
});
|
|
91
|
+
describe('UserPresenceReporter — input is presence, transport is not', function () {
|
|
92
|
+
it('a timer is not a person: an installed page with no input reports nothing, however long it sits', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
switch (_a.label) {
|
|
95
|
+
case 0: return [4 /*yield*/, jest.advanceTimersByTimeAsync(UserPresenceReporter_1.UserPresenceReporter.REPORT_INTERVAL_MS * 6)];
|
|
96
|
+
case 1:
|
|
97
|
+
_a.sent();
|
|
98
|
+
expect(recordPresence).not.toHaveBeenCalled();
|
|
99
|
+
return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}); });
|
|
103
|
+
it('a pointer on the page reports presence once', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
switch (_a.label) {
|
|
106
|
+
case 0:
|
|
107
|
+
input('pointerdown');
|
|
108
|
+
return [4 /*yield*/, settle()];
|
|
109
|
+
case 1:
|
|
110
|
+
_a.sent();
|
|
111
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
112
|
+
return [2 /*return*/];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}); });
|
|
116
|
+
it('every input kind counts — key, touch, wheel — each first-in-window input reports', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
117
|
+
var _i, _a, type;
|
|
118
|
+
return __generator(this, function (_b) {
|
|
119
|
+
switch (_b.label) {
|
|
120
|
+
case 0:
|
|
121
|
+
_i = 0, _a = ['keydown', 'touchstart', 'wheel'];
|
|
122
|
+
_b.label = 1;
|
|
123
|
+
case 1:
|
|
124
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
125
|
+
type = _a[_i];
|
|
126
|
+
input(type);
|
|
127
|
+
return [4 /*yield*/, settle()];
|
|
128
|
+
case 2:
|
|
129
|
+
_b.sent();
|
|
130
|
+
jest.advanceTimersByTime(UserPresenceReporter_1.UserPresenceReporter.REPORT_INTERVAL_MS);
|
|
131
|
+
_b.label = 3;
|
|
132
|
+
case 3:
|
|
133
|
+
_i++;
|
|
134
|
+
return [3 /*break*/, 1];
|
|
135
|
+
case 4:
|
|
136
|
+
expect(recordPresence).toHaveBeenCalledTimes(3);
|
|
137
|
+
return [2 /*return*/];
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}); });
|
|
141
|
+
it('throttles: more input inside the interval reports nothing; the first input after it reports again', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
142
|
+
return __generator(this, function (_a) {
|
|
143
|
+
switch (_a.label) {
|
|
144
|
+
case 0:
|
|
145
|
+
input('pointerdown');
|
|
146
|
+
input('keydown');
|
|
147
|
+
jest.advanceTimersByTime(UserPresenceReporter_1.UserPresenceReporter.REPORT_INTERVAL_MS - 1000);
|
|
148
|
+
input('wheel');
|
|
149
|
+
return [4 /*yield*/, settle()];
|
|
150
|
+
case 1:
|
|
151
|
+
_a.sent();
|
|
152
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
153
|
+
jest.advanceTimersByTime(1000);
|
|
154
|
+
input('touchstart');
|
|
155
|
+
return [4 /*yield*/, settle()];
|
|
156
|
+
case 2:
|
|
157
|
+
_a.sent();
|
|
158
|
+
expect(recordPresence).toHaveBeenCalledTimes(2);
|
|
159
|
+
return [2 /*return*/];
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}); });
|
|
163
|
+
it('a logged-out page reports nothing, and does not spend its throttle window', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
164
|
+
return __generator(this, function (_a) {
|
|
165
|
+
switch (_a.label) {
|
|
166
|
+
case 0:
|
|
167
|
+
isLoggedIn.mockReturnValue(false);
|
|
168
|
+
input('pointerdown');
|
|
169
|
+
return [4 /*yield*/, settle()];
|
|
170
|
+
case 1:
|
|
171
|
+
_a.sent();
|
|
172
|
+
expect(recordPresence).not.toHaveBeenCalled();
|
|
173
|
+
// The person logs in and the next input reports at once — no stale window from the guest input.
|
|
174
|
+
isLoggedIn.mockReturnValue(true);
|
|
175
|
+
input('pointerdown');
|
|
176
|
+
return [4 /*yield*/, settle()];
|
|
177
|
+
case 2:
|
|
178
|
+
_a.sent();
|
|
179
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
180
|
+
return [2 /*return*/];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}); });
|
|
184
|
+
it('fail-open: a failed report is dropped, and the next input after the retry window reports again', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
185
|
+
return __generator(this, function (_a) {
|
|
186
|
+
switch (_a.label) {
|
|
187
|
+
case 0:
|
|
188
|
+
recordPresence.mockRejectedValueOnce(new Error('server away'));
|
|
189
|
+
input('pointerdown');
|
|
190
|
+
return [4 /*yield*/, settle()];
|
|
191
|
+
case 1:
|
|
192
|
+
_a.sent();
|
|
193
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
194
|
+
// Inside the retry window: still nothing (never a request per keystroke against a down server).
|
|
195
|
+
jest.advanceTimersByTime(UserPresenceReporter_1.UserPresenceReporter.RETRY_AFTER_MS - 1000);
|
|
196
|
+
input('keydown');
|
|
197
|
+
return [4 /*yield*/, settle()];
|
|
198
|
+
case 2:
|
|
199
|
+
_a.sent();
|
|
200
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
201
|
+
jest.advanceTimersByTime(1000);
|
|
202
|
+
input('keydown');
|
|
203
|
+
return [4 /*yield*/, settle()];
|
|
204
|
+
case 3:
|
|
205
|
+
_a.sent();
|
|
206
|
+
expect(recordPresence).toHaveBeenCalledTimes(2);
|
|
207
|
+
return [2 /*return*/];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}); });
|
|
211
|
+
it('install is idempotent: a second install adds no second listener', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
212
|
+
return __generator(this, function (_a) {
|
|
213
|
+
switch (_a.label) {
|
|
214
|
+
case 0:
|
|
215
|
+
reporter.install();
|
|
216
|
+
input('pointerdown');
|
|
217
|
+
return [4 /*yield*/, settle()];
|
|
218
|
+
case 1:
|
|
219
|
+
_a.sent();
|
|
220
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
221
|
+
return [2 /*return*/];
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}); });
|
|
225
|
+
});
|
|
226
|
+
//# sourceMappingURL=userPresenceReporter.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"userPresenceReporter.test.js","sourceRoot":"","sources":["../../test/userPresenceReporter.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;GASG;AACH,wCAA2C;AAC3C,6EAA4E;AAE5E,IAAM,cAAc,GAAG,IAAI,CAAC,EAAE,EAAqB,CAAC;AAEpD,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,cAAM,OAAA,uBAC9B,IAAI,CAAC,aAAa,CAAC,iBAAiB,CAAC,KACxC,sBAAsB,EAAE,cAAM,OAAA,CAAC,EAAE,cAAc,gBAAA,EAAE,CAAC,EAApB,CAAoB,IAClD,EAHiC,CAGjC,CAAC,CAAC;AAEJ,IAAM,KAAK,GAAG,UAAC,IAAY,IAAK,OAAA,QAAQ,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAA1D,CAA0D,CAAC;AAC3F,+EAA+E;AAC/E,IAAM,MAAM,GAAG;;;oBACb,qBAAM,IAAI,CAAC,wBAAwB,CAAC,CAAC,CAAC,EAAA;;gBAAtC,SAAsC,CAAC;;;;KACxC,CAAC;AAEF,IAAI,QAA8B,CAAC;AACnC,IAAI,UAAyC,CAAC;AAE9C,UAAU,CAAC;IACT,IAAI,CAAC,aAAa,EAAE,CAAC;IACrB,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,CAAC,0BAA0B,CAAC,CAAC,CAAC;IACzD,cAAc,CAAC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I,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC;oBAC/D,KAAK,CAAC,aAAa,CAAC,CAAC;oBACrB,qBAAM,MAAM,EAAE,EAAA;;oBAAd,SAAc,CAAC;oBACf,MAAM,CAAC,cAAc,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;oBAEhD,gGAAgG;oBAChG,IAAI,CAAC,mBAAmB,CAAC,2CAAoB,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;oBACrE,KAAK,CAAC,SAAS,CAAC,CAAC;oBACjB,qBAAM,MAAM,EAAE,EAAA;;oBAAd,SAAc,CAAC;oBACf,MAAM,CAAC,cAAc,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;oBAEhD,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC;oBAC/B,KAAK,CAAC,SAAS,CAAC,CAAC;oBACjB,qBAAM,MAAM,EAAE,EAAA;;oBAAd,SAAc,CAAC;oBACf,MAAM,CAAC,cAAc,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;;;;SACjD,CAAC,CAAC;IAEH,EAAE,CAAC,iEAAiE,EAAE;;;;oBACpE,QAAQ,CAAC,OAAO,EAAE,CAAC;oBACnB,KAAK,CAAC,aAAa,CAAC,CAAC;oBACrB,qBAAM,MAAM,EAAE,EAAA;;oBAAd,SAAc,CAAC;oBACf,MAAM,CAAC,cAAc,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;;;;SACjD,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './src/AuthenticatedPageContainer';
|
|
2
2
|
export * from './src/pageAuth';
|
|
3
|
+
export * from './src/presence/UserPresenceReporter';
|
|
4
|
+
export * from './src/presence/installUserPresenceReporter';
|
|
3
5
|
|
|
4
6
|
// The logged-out surface's building blocks, exported so a consumer app's own public pages
|
|
5
7
|
// (e.g. n3xa's /request-invite) render with the same auth look instead of forking the styling.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proteinjs/user-ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "User ui components",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@proteinjs/reflection": "^1.2.0",
|
|
30
30
|
"@proteinjs/server-api": "^3.0.11",
|
|
31
31
|
"@proteinjs/ui": "^4.14.0",
|
|
32
|
-
"@proteinjs/user": "^1.
|
|
32
|
+
"@proteinjs/user": "^1.19.0",
|
|
33
33
|
"@proteinjs/util": "^1.6.1",
|
|
34
34
|
"moment": "2.29.4",
|
|
35
35
|
"react": "18.2.0",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
},
|
|
57
57
|
"main": "./dist/generated/index.js",
|
|
58
58
|
"types": "./dist/generated/index.d.ts",
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "2e08e46b9d8b5fe449323f0be7c5d545d0ad4b9b"
|
|
60
60
|
}
|
|
@@ -2,6 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { PageContainer, PageContainerProps } from '@proteinjs/ui';
|
|
3
3
|
import { routes, guestUser, UserAuth, UserRepo, uiRoutes } from '@proteinjs/user';
|
|
4
4
|
import { canViewPage } from './pageAuth';
|
|
5
|
+
import { installUserPresenceReporter } from './presence/installUserPresenceReporter';
|
|
5
6
|
|
|
6
7
|
export type AuthenticatedPageContainerProps = Omit<PageContainerProps, 'auth'>;
|
|
7
8
|
|
|
@@ -9,6 +10,13 @@ export function AuthenticatedPageContainer(props: AuthenticatedPageContainerProp
|
|
|
9
10
|
const { ...other } = props;
|
|
10
11
|
const [isLoggedIn, setIsLoggedIn] = React.useState(UserAuth.isLoggedIn());
|
|
11
12
|
|
|
13
|
+
// "Last active" (UserActivityTable's contract): every authenticated page reports a person's
|
|
14
|
+
// input through the presence door. Installed here — the one root every consumer's
|
|
15
|
+
// authenticated pages share — so no app wires it per page; idempotent across remounts.
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
installUserPresenceReporter();
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
12
20
|
return (
|
|
13
21
|
<PageContainer
|
|
14
22
|
auth={{
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { UserAuth, getUserPresenceService } from '@proteinjs/user';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The browser half of "last active" (UserActivityTable's contract): a person's INPUT on the
|
|
5
|
+
* page — pointer, key, touch, wheel — becomes one presence report per interval through
|
|
6
|
+
* `UserPresenceService.recordPresence`, the only door that moves the `user_activity` stamp.
|
|
7
|
+
*
|
|
8
|
+
* Input, never transport: a tab left open polls, re-joins its socket rooms on every reconnect
|
|
9
|
+
* and reloads when a deploy lands — all of it over a live session with nobody there, and all
|
|
10
|
+
* of it read as presence when the stamp rode the request path (founder finding 2026-09-12:
|
|
11
|
+
* every user "active today"). Timers, focus and visibility are not input either (a tab can be
|
|
12
|
+
* visible with nobody in front of it), so nothing here fires without a hand on the device.
|
|
13
|
+
*
|
|
14
|
+
* Fail-open: a report that fails is dropped; the next input after {@link RETRY_AFTER_MS}
|
|
15
|
+
* tries again (never one request per keystroke against a down server). The listeners are
|
|
16
|
+
* passive capture listeners on the document — they never block scrolling or a handler.
|
|
17
|
+
*/
|
|
18
|
+
export class UserPresenceReporter {
|
|
19
|
+
/** Report at most this often — the same cadence class as the server stamp's own throttle. */
|
|
20
|
+
static readonly REPORT_INTERVAL_MS = 1000 * 60 * 5;
|
|
21
|
+
/** After a failed report, the next input retries only once this long has passed. */
|
|
22
|
+
static readonly RETRY_AFTER_MS = 1000 * 30;
|
|
23
|
+
/** The events that mean a person is here. */
|
|
24
|
+
static readonly INPUT_EVENTS: readonly string[] = ['pointerdown', 'keydown', 'touchstart', 'wheel'];
|
|
25
|
+
|
|
26
|
+
private static instance: UserPresenceReporter | undefined;
|
|
27
|
+
|
|
28
|
+
static get(): UserPresenceReporter {
|
|
29
|
+
if (!UserPresenceReporter.instance) {
|
|
30
|
+
UserPresenceReporter.instance = new UserPresenceReporter();
|
|
31
|
+
}
|
|
32
|
+
return UserPresenceReporter.instance;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private installed = false;
|
|
36
|
+
private lastReportMs = 0;
|
|
37
|
+
|
|
38
|
+
/** Listen for input on the document. Idempotent; a no-op outside a browser. */
|
|
39
|
+
install(): void {
|
|
40
|
+
if (this.installed || typeof document === 'undefined') {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.installed = true;
|
|
44
|
+
for (const type of UserPresenceReporter.INPUT_EVENTS) {
|
|
45
|
+
document.addEventListener(type, this.onInput, { capture: true, passive: true });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Stop listening (tests, teardown). The throttle history is kept. */
|
|
50
|
+
uninstall(): void {
|
|
51
|
+
if (!this.installed) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
this.installed = false;
|
|
55
|
+
for (const type of UserPresenceReporter.INPUT_EVENTS) {
|
|
56
|
+
document.removeEventListener(type, this.onInput, { capture: true });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private onInput = (): void => {
|
|
61
|
+
const now = Date.now();
|
|
62
|
+
if (now - this.lastReportMs < UserPresenceReporter.REPORT_INTERVAL_MS) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (!UserAuth.isLoggedIn()) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.lastReportMs = now;
|
|
69
|
+
void this.report(now);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
private async report(at: number): Promise<void> {
|
|
73
|
+
try {
|
|
74
|
+
await getUserPresenceService().recordPresence();
|
|
75
|
+
} catch (error) {
|
|
76
|
+
// Dropped on purpose; the next input after the retry window reports again.
|
|
77
|
+
this.lastReportMs = at - UserPresenceReporter.REPORT_INTERVAL_MS + UserPresenceReporter.RETRY_AFTER_MS;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UserPresenceReporter } from './UserPresenceReporter';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Install the browser half of "last active": the page's human-input report (see
|
|
5
|
+
* {@link UserPresenceReporter}). `AuthenticatedPageContainer` installs it on mount, so every
|
|
6
|
+
* consumer app's authenticated pages report presence without wiring; a consumer with its own
|
|
7
|
+
* page container calls this once at its root. Idempotent.
|
|
8
|
+
*/
|
|
9
|
+
export function installUserPresenceReporter(): void {
|
|
10
|
+
UserPresenceReporter.get().install();
|
|
11
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
* @jest-environment-options {"customExportConditions": ["node", "node-addons"]}
|
|
4
|
+
*
|
|
5
|
+
* The browser half of "last active" (UserActivityTable's contract): presence is reported from a
|
|
6
|
+
* person's INPUT on the page and from nothing else. These pin the reporter's outcomes against
|
|
7
|
+
* the presence door (`getUserPresenceService` is the only mock; time is faked):
|
|
8
|
+
* a timer is not a person, an input is, the throttle holds, a logged-out page reports nothing,
|
|
9
|
+
* and a failed report retries on the next input after the retry window.
|
|
10
|
+
*/
|
|
11
|
+
import { UserAuth } from '@proteinjs/user';
|
|
12
|
+
import { UserPresenceReporter } from '../src/presence/UserPresenceReporter';
|
|
13
|
+
|
|
14
|
+
const recordPresence = jest.fn<Promise<void>, []>();
|
|
15
|
+
|
|
16
|
+
jest.mock('@proteinjs/user', () => ({
|
|
17
|
+
...jest.requireActual('@proteinjs/user'),
|
|
18
|
+
getUserPresenceService: () => ({ recordPresence }),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
const input = (type: string) => document.dispatchEvent(new Event(type, { bubbles: true }));
|
|
22
|
+
/** Let the fire-and-forget report settle (one macrotask under fake timers). */
|
|
23
|
+
const settle = async () => {
|
|
24
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let reporter: UserPresenceReporter;
|
|
28
|
+
let isLoggedIn: jest.SpyInstance<boolean, []>;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
jest.useFakeTimers();
|
|
32
|
+
jest.setSystemTime(new Date('2026-09-12T10:00:00.000Z'));
|
|
33
|
+
recordPresence.mockReset().mockResolvedValue(undefined);
|
|
34
|
+
isLoggedIn = jest.spyOn(UserAuth, 'isLoggedIn').mockReturnValue(true);
|
|
35
|
+
reporter = new UserPresenceReporter();
|
|
36
|
+
reporter.install();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
reporter.uninstall();
|
|
41
|
+
isLoggedIn.mockRestore();
|
|
42
|
+
jest.useRealTimers();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('UserPresenceReporter — input is presence, transport is not', () => {
|
|
46
|
+
it('a timer is not a person: an installed page with no input reports nothing, however long it sits', async () => {
|
|
47
|
+
await jest.advanceTimersByTimeAsync(UserPresenceReporter.REPORT_INTERVAL_MS * 6);
|
|
48
|
+
expect(recordPresence).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('a pointer on the page reports presence once', async () => {
|
|
52
|
+
input('pointerdown');
|
|
53
|
+
await settle();
|
|
54
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('every input kind counts — key, touch, wheel — each first-in-window input reports', async () => {
|
|
58
|
+
for (const type of ['keydown', 'touchstart', 'wheel']) {
|
|
59
|
+
input(type);
|
|
60
|
+
await settle();
|
|
61
|
+
jest.advanceTimersByTime(UserPresenceReporter.REPORT_INTERVAL_MS);
|
|
62
|
+
}
|
|
63
|
+
expect(recordPresence).toHaveBeenCalledTimes(3);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('throttles: more input inside the interval reports nothing; the first input after it reports again', async () => {
|
|
67
|
+
input('pointerdown');
|
|
68
|
+
input('keydown');
|
|
69
|
+
jest.advanceTimersByTime(UserPresenceReporter.REPORT_INTERVAL_MS - 1000);
|
|
70
|
+
input('wheel');
|
|
71
|
+
await settle();
|
|
72
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
73
|
+
|
|
74
|
+
jest.advanceTimersByTime(1000);
|
|
75
|
+
input('touchstart');
|
|
76
|
+
await settle();
|
|
77
|
+
expect(recordPresence).toHaveBeenCalledTimes(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('a logged-out page reports nothing, and does not spend its throttle window', async () => {
|
|
81
|
+
isLoggedIn.mockReturnValue(false);
|
|
82
|
+
input('pointerdown');
|
|
83
|
+
await settle();
|
|
84
|
+
expect(recordPresence).not.toHaveBeenCalled();
|
|
85
|
+
|
|
86
|
+
// The person logs in and the next input reports at once — no stale window from the guest input.
|
|
87
|
+
isLoggedIn.mockReturnValue(true);
|
|
88
|
+
input('pointerdown');
|
|
89
|
+
await settle();
|
|
90
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('fail-open: a failed report is dropped, and the next input after the retry window reports again', async () => {
|
|
94
|
+
recordPresence.mockRejectedValueOnce(new Error('server away'));
|
|
95
|
+
input('pointerdown');
|
|
96
|
+
await settle();
|
|
97
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
98
|
+
|
|
99
|
+
// Inside the retry window: still nothing (never a request per keystroke against a down server).
|
|
100
|
+
jest.advanceTimersByTime(UserPresenceReporter.RETRY_AFTER_MS - 1000);
|
|
101
|
+
input('keydown');
|
|
102
|
+
await settle();
|
|
103
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
104
|
+
|
|
105
|
+
jest.advanceTimersByTime(1000);
|
|
106
|
+
input('keydown');
|
|
107
|
+
await settle();
|
|
108
|
+
expect(recordPresence).toHaveBeenCalledTimes(2);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('install is idempotent: a second install adds no second listener', async () => {
|
|
112
|
+
reporter.install();
|
|
113
|
+
input('pointerdown');
|
|
114
|
+
await settle();
|
|
115
|
+
expect(recordPresence).toHaveBeenCalledTimes(1);
|
|
116
|
+
});
|
|
117
|
+
});
|