lamp-core-lst 2025.11.14 → 2025.11.17
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/TESTING_DISPATCH_EVENT.md +192 -0
- package/dist/service/Activity.service.js +15 -13
- package/package.json +1 -1
- package/src/service/Activity.service.ts +12 -10
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# Testing LAMP.dispatchEvent in Browser Console
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
`LAMP.dispatchEvent` is an event system that allows components to communicate via custom events. You can test it directly from the browser console when the LAMP-dashboard is running.
|
|
5
|
+
|
|
6
|
+
## Basic Usage
|
|
7
|
+
|
|
8
|
+
### 1. Add an Event Listener
|
|
9
|
+
```javascript
|
|
10
|
+
// Listen for a custom event
|
|
11
|
+
LAMP.addEventListener("MY_TEST_EVENT", (event) => {
|
|
12
|
+
console.log("Event received:", event.detail);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Listen for LOGIN events (already used by LAMP)
|
|
16
|
+
LAMP.addEventListener("LOGIN", (event) => {
|
|
17
|
+
console.log("Login event:", event.detail);
|
|
18
|
+
console.log("Identity:", event.detail.identityObject);
|
|
19
|
+
console.log("Server:", event.detail.serverAddress);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Listen for LOGOUT events
|
|
23
|
+
LAMP.addEventListener("LOGOUT", (event) => {
|
|
24
|
+
console.log("Logout event:", event.detail);
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Dispatch a Test Event
|
|
29
|
+
```javascript
|
|
30
|
+
// Dispatch a simple test event
|
|
31
|
+
LAMP.dispatchEvent("MY_TEST_EVENT", { message: "Hello from console!" });
|
|
32
|
+
|
|
33
|
+
// Dispatch with more complex data
|
|
34
|
+
LAMP.dispatchEvent("MY_TEST_EVENT", {
|
|
35
|
+
timestamp: Date.now(),
|
|
36
|
+
data: { key: "value" },
|
|
37
|
+
user: "test-user"
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 3. Remove an Event Listener
|
|
42
|
+
```javascript
|
|
43
|
+
// Create a named function for the listener
|
|
44
|
+
const myHandler = (event) => {
|
|
45
|
+
console.log("Event:", event.detail);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Add listener
|
|
49
|
+
LAMP.addEventListener("MY_TEST_EVENT", myHandler);
|
|
50
|
+
|
|
51
|
+
// Remove listener
|
|
52
|
+
LAMP.removeEventListener("MY_TEST_EVENT", myHandler);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Testing Existing Events
|
|
56
|
+
|
|
57
|
+
### Test LOGIN Event
|
|
58
|
+
```javascript
|
|
59
|
+
// Add listener first
|
|
60
|
+
LAMP.addEventListener("LOGIN", (event) => {
|
|
61
|
+
console.log("LOGIN Event Details:", event.detail);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Trigger login (this will also dispatch LOGIN event automatically)
|
|
65
|
+
// But you can also manually test it:
|
|
66
|
+
LAMP.dispatchEvent("LOGIN", {
|
|
67
|
+
authorizationToken: "test:token",
|
|
68
|
+
identityObject: { id: "test-user", name: "Test User" },
|
|
69
|
+
serverAddress: "https://api.lamp.digital",
|
|
70
|
+
accessToken: "access-token",
|
|
71
|
+
refreshToken: "refresh-token"
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Test LOGOUT Event
|
|
76
|
+
```javascript
|
|
77
|
+
// Add listener
|
|
78
|
+
LAMP.addEventListener("LOGOUT", (event) => {
|
|
79
|
+
console.log("LOGOUT Event:", event.detail);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Dispatch logout
|
|
83
|
+
LAMP.dispatchEvent("LOGOUT", {
|
|
84
|
+
deleteCache: true
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Complete Testing Example
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
// Step 1: Set up listeners for all events you want to test
|
|
92
|
+
const eventLog = [];
|
|
93
|
+
|
|
94
|
+
// Generic event logger
|
|
95
|
+
const logEvent = (eventName) => (event) => {
|
|
96
|
+
const logEntry = {
|
|
97
|
+
event: eventName,
|
|
98
|
+
timestamp: new Date().toISOString(),
|
|
99
|
+
detail: event.detail
|
|
100
|
+
};
|
|
101
|
+
eventLog.push(logEntry);
|
|
102
|
+
console.log(`[${eventName}]`, logEntry);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Add listeners
|
|
106
|
+
LAMP.addEventListener("LOGIN", logEvent("LOGIN"));
|
|
107
|
+
LAMP.addEventListener("LOGOUT", logEvent("LOGOUT"));
|
|
108
|
+
LAMP.addEventListener("MY_TEST_EVENT", logEvent("MY_TEST_EVENT"));
|
|
109
|
+
|
|
110
|
+
// Step 2: Dispatch test events
|
|
111
|
+
LAMP.dispatchEvent("MY_TEST_EVENT", { test: "data" });
|
|
112
|
+
LAMP.dispatchEvent("LOGIN", {
|
|
113
|
+
identityObject: { id: "test" },
|
|
114
|
+
serverAddress: "https://test.com"
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Step 3: View all logged events
|
|
118
|
+
console.log("All Events:", eventLog);
|
|
119
|
+
|
|
120
|
+
// Step 4: Check if LAMP object is available
|
|
121
|
+
console.log("LAMP object:", LAMP);
|
|
122
|
+
console.log("LAMP.dispatchEvent:", LAMP.dispatchEvent);
|
|
123
|
+
console.log("LAMP.addEventListener:", LAMP.addEventListener);
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Debugging Tips
|
|
127
|
+
|
|
128
|
+
### Check if LAMP is loaded
|
|
129
|
+
```javascript
|
|
130
|
+
// In browser console
|
|
131
|
+
typeof LAMP !== 'undefined' && LAMP.dispatchEvent
|
|
132
|
+
// Should return: function dispatchEvent() { ... }
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Monitor all dispatched events
|
|
136
|
+
```javascript
|
|
137
|
+
// Override dispatchEvent temporarily to log all events
|
|
138
|
+
const originalDispatch = LAMP.dispatchEvent;
|
|
139
|
+
LAMP.dispatchEvent = function(event, detail) {
|
|
140
|
+
console.log("Dispatching:", event, detail);
|
|
141
|
+
return originalDispatch.call(this, event, detail);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Now all events will be logged
|
|
145
|
+
LAMP.dispatchEvent("TEST", { data: "test" });
|
|
146
|
+
|
|
147
|
+
// Restore original
|
|
148
|
+
LAMP.dispatchEvent = originalDispatch;
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Check event bus
|
|
152
|
+
```javascript
|
|
153
|
+
// The internal event bus is a DOM element
|
|
154
|
+
// You can inspect it (though it's private)
|
|
155
|
+
// Events are dispatched using CustomEvent API
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Common Use Cases
|
|
159
|
+
|
|
160
|
+
### 1. Test Activity Completion Events
|
|
161
|
+
```javascript
|
|
162
|
+
LAMP.addEventListener("ACTIVITY_COMPLETE", (event) => {
|
|
163
|
+
console.log("Activity completed:", event.detail);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
LAMP.dispatchEvent("ACTIVITY_COMPLETE", {
|
|
167
|
+
activityId: "test-activity-123",
|
|
168
|
+
participantId: "test-participant",
|
|
169
|
+
timestamp: Date.now()
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### 2. Test Data Sync Events
|
|
174
|
+
```javascript
|
|
175
|
+
LAMP.addEventListener("DATA_SYNC", (event) => {
|
|
176
|
+
console.log("Data synced:", event.detail);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
LAMP.dispatchEvent("DATA_SYNC", {
|
|
180
|
+
type: "activities",
|
|
181
|
+
count: 10,
|
|
182
|
+
timestamp: Date.now()
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Notes
|
|
187
|
+
- Events use the browser's native CustomEvent API
|
|
188
|
+
- The event bus is an internal HTMLElement (`_bus`)
|
|
189
|
+
- Events are synchronous - listeners are called immediately
|
|
190
|
+
- Multiple listeners can be attached to the same event
|
|
191
|
+
- Event details are passed in the `detail` property of the event object
|
|
192
|
+
|
|
@@ -90,11 +90,11 @@ var ActivityService = /** @class */ (function () {
|
|
|
90
90
|
* @param participantId
|
|
91
91
|
*/
|
|
92
92
|
ActivityService.prototype.allByParticipant = function (participantId, transform, ignore_binary, limit, offset) {
|
|
93
|
-
var _a
|
|
93
|
+
var _a;
|
|
94
94
|
return __awaiter(this, void 0, void 0, function () {
|
|
95
95
|
var auth_2, credential, output, total, paginatedOutput, params, queryString, result, activitiesArray, totalCount;
|
|
96
|
-
return __generator(this, function (
|
|
97
|
-
switch (
|
|
96
|
+
return __generator(this, function (_b) {
|
|
97
|
+
switch (_b.label) {
|
|
98
98
|
case 0:
|
|
99
99
|
if (participantId === null || participantId === undefined)
|
|
100
100
|
throw new Error("Required parameter participantId was null or undefined when calling activityAllByParticipant.");
|
|
@@ -130,22 +130,24 @@ var ActivityService = /** @class */ (function () {
|
|
|
130
130
|
}
|
|
131
131
|
queryString = params.toString();
|
|
132
132
|
return [4 /*yield*/, Fetch_1.Fetch.get("/participant/" + participantId + "/activity?" + queryString, this.configuration)
|
|
133
|
-
// Handle the response structure:
|
|
134
|
-
// - With pagination: { data: Activity[], total: number }
|
|
135
|
-
// - Without pagination: { data: Activity[] } (
|
|
133
|
+
// Handle the response structure based on _select function behavior:
|
|
134
|
+
// - With pagination (limit > 0 OR offset >= 0): { data: Activity[], total: number }
|
|
135
|
+
// - Without pagination: { data: Activity[] } (total property is missing)
|
|
136
|
+
// The server-side ActivityService always wraps the response in { data: ... }
|
|
136
137
|
];
|
|
137
138
|
case 1:
|
|
138
|
-
result =
|
|
139
|
+
result = _b.sent();
|
|
139
140
|
activitiesArray = [];
|
|
140
141
|
totalCount = 0;
|
|
141
|
-
if (result && result.data) {
|
|
142
|
-
// result.data is always an array
|
|
143
|
-
activitiesArray =
|
|
144
|
-
// total exists only when pagination was provided
|
|
145
|
-
|
|
142
|
+
if (result && result.data && Array.isArray(result.data)) {
|
|
143
|
+
// result.data is always an array (server wraps it)
|
|
144
|
+
activitiesArray = result.data;
|
|
145
|
+
// total exists only when pagination was provided (limit > 0 OR offset >= 0)
|
|
146
|
+
// When no pagination, _select returns array, server wraps as { data: array } (no total)
|
|
147
|
+
totalCount = typeof result.total === 'number' ? result.total : activitiesArray.length;
|
|
146
148
|
}
|
|
147
149
|
else if (Array.isArray(result)) {
|
|
148
|
-
// Legacy fallback: if result is directly an array
|
|
150
|
+
// Legacy fallback: if result is directly an array (shouldn't happen with current server)
|
|
149
151
|
activitiesArray = result;
|
|
150
152
|
totalCount = result.length;
|
|
151
153
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lamp-core-lst",
|
|
3
|
-
"version": "2025.11.
|
|
3
|
+
"version": "2025.11.17",
|
|
4
4
|
"author": "BIDMC Division of Digital Psychiatry <team@digitalpsych.org>",
|
|
5
5
|
"description": "The JavaScript and TypeScript API client for the LAMP Platform.",
|
|
6
6
|
"homepage": "https://docs.lamp.digital/",
|
|
@@ -73,24 +73,26 @@ export class ActivityService {
|
|
|
73
73
|
params.append('offset', offset.toString())
|
|
74
74
|
}
|
|
75
75
|
const queryString = params.toString()
|
|
76
|
-
const result: any = await Fetch.get<{ data: Activity[]
|
|
76
|
+
const result: any = await Fetch.get<{ data: Activity[], total?: number }>(
|
|
77
77
|
`/participant/${participantId}/activity?${queryString}`,
|
|
78
78
|
this.configuration
|
|
79
79
|
)
|
|
80
80
|
|
|
81
|
-
// Handle the response structure:
|
|
82
|
-
// - With pagination: { data: Activity[], total: number }
|
|
83
|
-
// - Without pagination: { data: Activity[] } (
|
|
81
|
+
// Handle the response structure based on _select function behavior:
|
|
82
|
+
// - With pagination (limit > 0 OR offset >= 0): { data: Activity[], total: number }
|
|
83
|
+
// - Without pagination: { data: Activity[] } (total property is missing)
|
|
84
|
+
// The server-side ActivityService always wraps the response in { data: ... }
|
|
84
85
|
let activitiesArray: any[] = []
|
|
85
86
|
let totalCount = 0
|
|
86
87
|
|
|
87
|
-
if (result && result.data) {
|
|
88
|
-
// result.data is always an array
|
|
89
|
-
activitiesArray =
|
|
90
|
-
// total exists only when pagination was provided
|
|
91
|
-
|
|
88
|
+
if (result && result.data && Array.isArray(result.data)) {
|
|
89
|
+
// result.data is always an array (server wraps it)
|
|
90
|
+
activitiesArray = result.data
|
|
91
|
+
// total exists only when pagination was provided (limit > 0 OR offset >= 0)
|
|
92
|
+
// When no pagination, _select returns array, server wraps as { data: array } (no total)
|
|
93
|
+
totalCount = typeof result.total === 'number' ? result.total : activitiesArray.length
|
|
92
94
|
} else if (Array.isArray(result)) {
|
|
93
|
-
// Legacy fallback: if result is directly an array
|
|
95
|
+
// Legacy fallback: if result is directly an array (shouldn't happen with current server)
|
|
94
96
|
activitiesArray = result
|
|
95
97
|
totalCount = result.length
|
|
96
98
|
}
|