@rootintf/protocol-2phw 2.1.5-api.2 → 2.3.4-api.2
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/.claude/scheduled_tasks.lock +1 -0
- package/antora-docs/antora.yml +2 -0
- package/antora-docs/modules/npm/pages/index.adoc +7 -0
- package/dist/twoPhW.cjs.js +48 -128
- package/dist/twoPhW.d.ts +2 -3
- package/dist/twoPhW.es.mjs +49 -128
- package/package.json +9 -7
- package/rollup.config.mjs +7 -2
- package/src/twoPhW.ts +20 -21
- package/tests/twoPhW-new.spec.mjs +22 -22
- package/dist/EventLog-mem.d.mts +0 -28
- package/src/EventLog-mem.mjs +0 -133
- package/tests/event-log-mem.spec.mjs +0 -239
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"sessionId":"665ff5cb-5985-468b-b076-b14bb79155c0","pid":29412,"acquiredAt":1782215798789}
|
package/dist/twoPhW.cjs.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var eventlog = require('@rootintf/eventlog');
|
|
4
|
+
|
|
3
5
|
const schema_MsgWarp = validate19;
|
|
4
6
|
function validate19(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate19.evaluated; if (evaluated0.dynamicProps) {
|
|
5
7
|
evaluated0.props = undefined;
|
|
@@ -314,6 +316,39 @@ function l_RR_(obj, x) {
|
|
|
314
316
|
}
|
|
315
317
|
return Object.freeze(obj_new);
|
|
316
318
|
}
|
|
319
|
+
function l_assert_(actual, required) {
|
|
320
|
+
if (!actual || typeof actual !== 'object')
|
|
321
|
+
return false;
|
|
322
|
+
if (!required || typeof required !== 'object')
|
|
323
|
+
return false;
|
|
324
|
+
const actualEntries = Object.entries(actual);
|
|
325
|
+
const requiredEntries = Object.entries(required);
|
|
326
|
+
// if (actualEntries.length === 0 && expectedEntries.length > 0)
|
|
327
|
+
// return false;
|
|
328
|
+
const actualValues = new Set();
|
|
329
|
+
for (const [, v] of actualEntries) {
|
|
330
|
+
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
331
|
+
return false;
|
|
332
|
+
actualValues.add(v);
|
|
333
|
+
}
|
|
334
|
+
const usedRequiredValues = new Set();
|
|
335
|
+
for (const [k, v] of requiredEntries) {
|
|
336
|
+
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
337
|
+
return false;
|
|
338
|
+
if (k in actual) {
|
|
339
|
+
if (actual[k] !== v)
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
if (!actualValues.has(v))
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
if (usedRequiredValues.has(v))
|
|
347
|
+
return false;
|
|
348
|
+
usedRequiredValues.add(v);
|
|
349
|
+
}
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
317
352
|
//-------------------------------------------------------------------------------------------------
|
|
318
353
|
function handler_default_( /* ... */) {
|
|
319
354
|
// https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
|
|
@@ -537,6 +572,7 @@ var logr_es = /*#__PURE__*/Object.freeze({
|
|
|
537
572
|
l_LL: l_LL_,
|
|
538
573
|
l_RR: l_RR_,
|
|
539
574
|
l_array: l_array_,
|
|
575
|
+
l_assert: l_assert_,
|
|
540
576
|
l_concat: l_concat_,
|
|
541
577
|
l_length: l_length_,
|
|
542
578
|
l_merge: l_merge_
|
|
@@ -754,7 +790,7 @@ function requireJsonMsg_cjs () {
|
|
|
754
790
|
|
|
755
791
|
//-------------------------------------------------------------------------------------------------
|
|
756
792
|
const LOGR_ = bitlogr.LOGR.get_instance();
|
|
757
|
-
const logr_ = LOGR_.create({ labels: bitlogr.l_array(['VALIDATION'
|
|
793
|
+
const logr_ = LOGR_.create({ labels: bitlogr.l_array(['VALIDATION']) });
|
|
758
794
|
const l_ = logr_.l;
|
|
759
795
|
// console.log('File location:', import.meta.url, '[Implementation]');
|
|
760
796
|
//-------------------------------------------------------------------------------------------------
|
|
@@ -836,113 +872,6 @@ function requireJsonMsg_cjs () {
|
|
|
836
872
|
|
|
837
873
|
var jsonMsg_cjsExports = requireJsonMsg_cjs();
|
|
838
874
|
|
|
839
|
-
//-------------------------------------------------------------------------------------------------
|
|
840
|
-
let LOGR_$1 = LOGR.get_instance();
|
|
841
|
-
const logr_$1 = LOGR_$1.create({ labels: l_array_(['LOG_EVENTS', 'TIMEOUTS']) });
|
|
842
|
-
const l_$1 = logr_$1.l;
|
|
843
|
-
//-------------------------------------------------------------------------------------------------
|
|
844
|
-
class EventLog {
|
|
845
|
-
constructor() {
|
|
846
|
-
this.arr_events = []; // In-memory event store
|
|
847
|
-
this.map_index_id = new Map(); // Map of id -> array of indices into arr_events; arr_events[idx]
|
|
848
|
-
this.map_timeouts = new Map(); // Store timeout references: idx -> timeout_id
|
|
849
|
-
}
|
|
850
|
-
addEvent(type, id, data, timeout_ms = null, timeout_callback = null) {
|
|
851
|
-
const evt = {
|
|
852
|
-
type,
|
|
853
|
-
dt: Date.now(),
|
|
854
|
-
id,
|
|
855
|
-
data,
|
|
856
|
-
timeout_id: null // Will store timeout reference if set
|
|
857
|
-
};
|
|
858
|
-
const idx = this.arr_events.push(evt) - 1; // More efficient than .length before push
|
|
859
|
-
let arr_indices = this.map_index_id.get(id);
|
|
860
|
-
if (!arr_indices) {
|
|
861
|
-
arr_indices = [];
|
|
862
|
-
this.map_index_id.set(id, arr_indices);
|
|
863
|
-
}
|
|
864
|
-
arr_indices.push(idx);
|
|
865
|
-
// Set timeout if specified
|
|
866
|
-
if (timeout_ms && timeout_callback) {
|
|
867
|
-
evt.timeout_id = setTimeout(() => {
|
|
868
|
-
if (this.map_timeouts.delete(idx)) {
|
|
869
|
-
evt.timeout_id = null;
|
|
870
|
-
timeout_callback({ idx, evt });
|
|
871
|
-
logr_$1.log(l_$1.LOG_EVENTS, () => [`Event DELETED[${idx}]: ${type}`, evt]);
|
|
872
|
-
}
|
|
873
|
-
}, timeout_ms);
|
|
874
|
-
this.map_timeouts.set(idx, evt.timeout_id);
|
|
875
|
-
}
|
|
876
|
-
// the error "toISOString is not a function" occurs because Date(evt.dt) is not doing what you expect.
|
|
877
|
-
// When you pass a number to the Date constructor without using new, it doesn’t return a Date object—it returns a string. Specifically, it’s equivalent to new Date(evt.dt).toString().
|
|
878
|
-
// Date(evt.dt).toISOString() -> new Date(evt.dt).toISOString()
|
|
879
|
-
logr_$1.log(l_$1.LOG_EVENTS, () => [`Event added[${idx}] @${new Date(evt.dt).toISOString()}: ${type}`, evt]);
|
|
880
|
-
return { idx, evt };
|
|
881
|
-
}
|
|
882
|
-
cancelTimeout(idx) {
|
|
883
|
-
const timeout_id = this.map_timeouts.get(idx);
|
|
884
|
-
if (timeout_id) {
|
|
885
|
-
clearTimeout(timeout_id);
|
|
886
|
-
this.map_timeouts.delete(idx);
|
|
887
|
-
if (this.arr_events[idx])
|
|
888
|
-
this.arr_events[idx].timeout_id = null;
|
|
889
|
-
return true;
|
|
890
|
-
}
|
|
891
|
-
return false;
|
|
892
|
-
}
|
|
893
|
-
cancelAllTimeouts() {
|
|
894
|
-
for (const [idx, timeout_id] of this.map_timeouts) {
|
|
895
|
-
clearTimeout(timeout_id);
|
|
896
|
-
if (this.arr_events[idx])
|
|
897
|
-
this.arr_events[idx].timeout_id = null;
|
|
898
|
-
}
|
|
899
|
-
this.map_timeouts.clear();
|
|
900
|
-
}
|
|
901
|
-
getEventsById(id, lookback_ms = null) {
|
|
902
|
-
const arr_indices = this.map_index_id.get(id);
|
|
903
|
-
if (!arr_indices?.length)
|
|
904
|
-
return [];
|
|
905
|
-
const result = [];
|
|
906
|
-
const cutoff = lookback_ms !== null ? Date.now() - lookback_ms : -Infinity;
|
|
907
|
-
// Single loop with early cutoff
|
|
908
|
-
for (let i = arr_indices.length - 1; i >= 0; i--) {
|
|
909
|
-
const idx = arr_indices[i];
|
|
910
|
-
const evt = this.arr_events[idx];
|
|
911
|
-
if (evt.dt < cutoff)
|
|
912
|
-
break;
|
|
913
|
-
result.push({ idx, evt });
|
|
914
|
-
}
|
|
915
|
-
return result; // Newest to oldest order
|
|
916
|
-
}
|
|
917
|
-
// getRecentEvents(id, lookbackMs) {
|
|
918
|
-
// const cutoff = Date.now() - lookbackMs;
|
|
919
|
-
// return this.events.filter(e => e.id === id && e.dt >= cutoff);
|
|
920
|
-
// }
|
|
921
|
-
getEvents(lookback_ms, predicate) {
|
|
922
|
-
const cutoff = Date.now() - lookback_ms;
|
|
923
|
-
const result = [];
|
|
924
|
-
// Iterate backwards from the end
|
|
925
|
-
for (let idx = this.arr_events.length - 1; idx >= 0; idx--) {
|
|
926
|
-
const evt = this.arr_events[idx];
|
|
927
|
-
if (evt.dt < cutoff) {
|
|
928
|
-
break; // Stop once we pass the cutoff
|
|
929
|
-
}
|
|
930
|
-
if (predicate(evt)) {
|
|
931
|
-
result.push({ idx, evt });
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
return result; // Newest to oldest order
|
|
935
|
-
}
|
|
936
|
-
// setLogr(obj_logr) {
|
|
937
|
-
// this.logr.toggled = obj_logr;
|
|
938
|
-
// }
|
|
939
|
-
clear() {
|
|
940
|
-
this.cancelAllTimeouts(); // Clear all timeouts before clearing events
|
|
941
|
-
this.arr_events.length = 0;
|
|
942
|
-
this.map_index_id.clear();
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
|
|
946
875
|
// interface Event extends Required<EventMinimal> {
|
|
947
876
|
// type : string;
|
|
948
877
|
// id : string | number;
|
|
@@ -950,11 +879,11 @@ class EventLog {
|
|
|
950
879
|
// };
|
|
951
880
|
const k_TTL = 20000;
|
|
952
881
|
//-------------------------------------------------------------------------------------------------
|
|
953
|
-
const obj_labels_merge_ = l_merge_(
|
|
882
|
+
const obj_labels_merge_ = l_merge_(eventlog.logr.lref.get(), jsonMsg_cjsExports.logr.lref.get()); // VALIDATION : 4
|
|
954
883
|
const obj_labels_concat_ = l_concat_(obj_labels_merge_, l_array_(['HANDLERS', 'DROPS', 'PROTOCOL_STATE', 'RECVER', 'SENDER']));
|
|
955
884
|
const lref_ = lRef(obj_labels_concat_);
|
|
956
885
|
jsonMsg_cjsExports.logr.lref = lref_;
|
|
957
|
-
|
|
886
|
+
eventlog.logr.lref = lref_;
|
|
958
887
|
const LOGR_ = LOGR.get_instance();
|
|
959
888
|
const logr_ = LOGR_.create(); // don't let create() make a new ref, assign lref_ from here
|
|
960
889
|
logr_.lref = lref_;
|
|
@@ -1325,22 +1254,22 @@ class TwoPhW_EventLog {
|
|
|
1325
1254
|
// _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : ( args? : {idx_event : number, event : EventMinimal} ) => {}) {
|
|
1326
1255
|
_addEvent(msg, json_msg, timeout_ms, timeout_callback) {
|
|
1327
1256
|
const { str_type, json_msgWarp } = this._warp(json_msg);
|
|
1328
|
-
const { idx } = this._eventLog.
|
|
1257
|
+
const { idx } = this._eventLog.add_Event(str_type, json_msgWarp.warp.id, { msg, json_msg }, timeout_ms, timeout_callback);
|
|
1329
1258
|
logr_.log(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
|
|
1330
1259
|
}
|
|
1331
1260
|
_dropEvent(json_msg) {
|
|
1332
1261
|
const { json_msgWarp } = this._warp(json_msg);
|
|
1333
1262
|
if (!json_msgWarp)
|
|
1334
1263
|
return;
|
|
1335
|
-
this._eventLog.
|
|
1264
|
+
this._eventLog.add_Event('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
|
|
1336
1265
|
}
|
|
1337
1266
|
_cancelTimeout(idx_event) {
|
|
1338
1267
|
logr_.log(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
|
|
1339
|
-
this._eventLog.
|
|
1268
|
+
this._eventLog.cancel_timeout(idx_event);
|
|
1340
1269
|
}
|
|
1341
1270
|
traverse_State(json_msgWarp) {
|
|
1342
1271
|
const protocolState = new ProtocolStateState();
|
|
1343
|
-
const arr_evts = this._eventLog.
|
|
1272
|
+
const arr_evts = this._eventLog.get_Events_by_id(json_msgWarp.warp.id, k_TTL);
|
|
1344
1273
|
logr_.log(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
|
|
1345
1274
|
//TODO: what is the right TTL? should it be a constant or calculated
|
|
1346
1275
|
for (let i = arr_evts.length - 1; i >= 0; i--) {
|
|
@@ -1542,7 +1471,7 @@ class TwoPhW {
|
|
|
1542
1471
|
}
|
|
1543
1472
|
//-------------------------------------------------------------------------------------------------
|
|
1544
1473
|
handler_json_msgAck(msg, json_msgAck) {
|
|
1545
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msgAck: ', json_msgAck]);
|
|
1474
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgAck: ', json_msgAck]);
|
|
1546
1475
|
if (JSON_MsgAckWarp.validate(json_msgAck) && this._handler_msgAckWarp(msg, json_msgAck))
|
|
1547
1476
|
return true;
|
|
1548
1477
|
if (JSON_MsgAckEngage.validate(json_msgAck) && this._handler_msgAckEngage(msg, json_msgAck))
|
|
@@ -1550,9 +1479,9 @@ class TwoPhW {
|
|
|
1550
1479
|
return false;
|
|
1551
1480
|
}
|
|
1552
1481
|
handler_json_msgEffect(msg, json_msgEffect) {
|
|
1553
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msgEffect: ', json_msgEffect]);
|
|
1554
|
-
if (
|
|
1555
|
-
|
|
1482
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgEffect: ', json_msgEffect]);
|
|
1483
|
+
// if (JSON_MsgAck.validate(json_msgEffect) && this.handler_json_msgAck(msg, json_msgEffect) )
|
|
1484
|
+
// return true;
|
|
1556
1485
|
if (JSON_MsgEngage.validate(json_msgEffect) && this._handler_msgEngage(msg, json_msgEffect))
|
|
1557
1486
|
return true;
|
|
1558
1487
|
// if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
|
|
@@ -1560,7 +1489,7 @@ class TwoPhW {
|
|
|
1560
1489
|
return false;
|
|
1561
1490
|
}
|
|
1562
1491
|
handler_json_msg(msg, json_msg) {
|
|
1563
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msg: ', json_msg]);
|
|
1492
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msg: ', json_msg]);
|
|
1564
1493
|
if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
|
|
1565
1494
|
return true;
|
|
1566
1495
|
// https://attacomsian.com/blog/javascript-iterate-objects
|
|
@@ -1573,17 +1502,8 @@ class TwoPhW {
|
|
|
1573
1502
|
// });
|
|
1574
1503
|
return false;
|
|
1575
1504
|
}
|
|
1576
|
-
handler_json(msg, json) {
|
|
1577
|
-
logr_.log(l_.HANDLERS, () => ['handler_json: ', json]);
|
|
1578
|
-
if (jsonMsg_cjsExports.JSON_MsgEffect.validate(json) && this.handler_json_msgEffect(msg, json))
|
|
1579
|
-
return true;
|
|
1580
|
-
if (jsonMsg_cjsExports.JSON_Msg.validate(json) && this.handler_json_msg(msg, json))
|
|
1581
|
-
return true;
|
|
1582
|
-
return false;
|
|
1583
|
-
}
|
|
1584
1505
|
}
|
|
1585
1506
|
|
|
1586
|
-
exports.EventLog = EventLog;
|
|
1587
1507
|
exports.JSON_Msg = jsonMsg_cjsExports.JSON_Msg;
|
|
1588
1508
|
exports.JSON_MsgAck = jsonMsg_cjsExports.JSON_MsgAck;
|
|
1589
1509
|
exports.JSON_MsgAckEngage = JSON_MsgAckEngage;
|
package/dist/twoPhW.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JSON_Msg, JSON_MsgEffect, JSON_MsgAck } from '@rootintf/json-msg';
|
|
2
|
-
import { EventLog } from '
|
|
2
|
+
import { EventLog } from '@rootintf/eventlog';
|
|
3
3
|
type t_msg = string;
|
|
4
4
|
interface EventMinimal {
|
|
5
5
|
dt: number;
|
|
@@ -127,6 +127,5 @@ declare class TwoPhW {
|
|
|
127
127
|
handler_json_msgAck(msg: t_msg, json_msgAck: any): boolean;
|
|
128
128
|
handler_json_msgEffect(msg: t_msg, json_msgEffect: any): boolean;
|
|
129
129
|
handler_json_msg(msg: t_msg, json_msg: any): boolean;
|
|
130
|
-
handler_json(msg: t_msg, json: any): boolean;
|
|
131
130
|
}
|
|
132
|
-
export {
|
|
131
|
+
export { time_ttl_rtt_ as t_ttl_rtt, TwoPhW, JSON_MsgOptions, JSON_MsgWarp, JSON_MsgEngage, JSON_MsgAckWarp, JSON_MsgAckEngage, logr_ as logr, JSON_Msg, JSON_MsgEffect, JSON_MsgAck, StateAckEngage, StateEngage, StateAckWarp, StateWarp, StateINVALID, StateInitialState };
|
package/dist/twoPhW.es.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { logr } from '@rootintf/eventlog';
|
|
2
|
+
|
|
1
3
|
const schema_MsgWarp = validate19;
|
|
2
4
|
function validate19(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate19.evaluated; if (evaluated0.dynamicProps) {
|
|
3
5
|
evaluated0.props = undefined;
|
|
@@ -312,6 +314,39 @@ function l_RR_(obj, x) {
|
|
|
312
314
|
}
|
|
313
315
|
return Object.freeze(obj_new);
|
|
314
316
|
}
|
|
317
|
+
function l_assert_(actual, required) {
|
|
318
|
+
if (!actual || typeof actual !== 'object')
|
|
319
|
+
return false;
|
|
320
|
+
if (!required || typeof required !== 'object')
|
|
321
|
+
return false;
|
|
322
|
+
const actualEntries = Object.entries(actual);
|
|
323
|
+
const requiredEntries = Object.entries(required);
|
|
324
|
+
// if (actualEntries.length === 0 && expectedEntries.length > 0)
|
|
325
|
+
// return false;
|
|
326
|
+
const actualValues = new Set();
|
|
327
|
+
for (const [, v] of actualEntries) {
|
|
328
|
+
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
329
|
+
return false;
|
|
330
|
+
actualValues.add(v);
|
|
331
|
+
}
|
|
332
|
+
const usedRequiredValues = new Set();
|
|
333
|
+
for (const [k, v] of requiredEntries) {
|
|
334
|
+
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
335
|
+
return false;
|
|
336
|
+
if (k in actual) {
|
|
337
|
+
if (actual[k] !== v)
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
if (!actualValues.has(v))
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
if (usedRequiredValues.has(v))
|
|
345
|
+
return false;
|
|
346
|
+
usedRequiredValues.add(v);
|
|
347
|
+
}
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
315
350
|
//-------------------------------------------------------------------------------------------------
|
|
316
351
|
function handler_default_( /* ... */) {
|
|
317
352
|
// https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
|
|
@@ -535,6 +570,7 @@ var logr_es = /*#__PURE__*/Object.freeze({
|
|
|
535
570
|
l_LL: l_LL_,
|
|
536
571
|
l_RR: l_RR_,
|
|
537
572
|
l_array: l_array_,
|
|
573
|
+
l_assert: l_assert_,
|
|
538
574
|
l_concat: l_concat_,
|
|
539
575
|
l_length: l_length_,
|
|
540
576
|
l_merge: l_merge_
|
|
@@ -752,7 +788,7 @@ function requireJsonMsg_cjs () {
|
|
|
752
788
|
|
|
753
789
|
//-------------------------------------------------------------------------------------------------
|
|
754
790
|
const LOGR_ = bitlogr.LOGR.get_instance();
|
|
755
|
-
const logr_ = LOGR_.create({ labels: bitlogr.l_array(['VALIDATION'
|
|
791
|
+
const logr_ = LOGR_.create({ labels: bitlogr.l_array(['VALIDATION']) });
|
|
756
792
|
const l_ = logr_.l;
|
|
757
793
|
// console.log('File location:', import.meta.url, '[Implementation]');
|
|
758
794
|
//-------------------------------------------------------------------------------------------------
|
|
@@ -834,113 +870,6 @@ function requireJsonMsg_cjs () {
|
|
|
834
870
|
|
|
835
871
|
var jsonMsg_cjsExports = requireJsonMsg_cjs();
|
|
836
872
|
|
|
837
|
-
//-------------------------------------------------------------------------------------------------
|
|
838
|
-
let LOGR_$1 = LOGR.get_instance();
|
|
839
|
-
const logr_$1 = LOGR_$1.create({ labels: l_array_(['LOG_EVENTS', 'TIMEOUTS']) });
|
|
840
|
-
const l_$1 = logr_$1.l;
|
|
841
|
-
//-------------------------------------------------------------------------------------------------
|
|
842
|
-
class EventLog {
|
|
843
|
-
constructor() {
|
|
844
|
-
this.arr_events = []; // In-memory event store
|
|
845
|
-
this.map_index_id = new Map(); // Map of id -> array of indices into arr_events; arr_events[idx]
|
|
846
|
-
this.map_timeouts = new Map(); // Store timeout references: idx -> timeout_id
|
|
847
|
-
}
|
|
848
|
-
addEvent(type, id, data, timeout_ms = null, timeout_callback = null) {
|
|
849
|
-
const evt = {
|
|
850
|
-
type,
|
|
851
|
-
dt: Date.now(),
|
|
852
|
-
id,
|
|
853
|
-
data,
|
|
854
|
-
timeout_id: null // Will store timeout reference if set
|
|
855
|
-
};
|
|
856
|
-
const idx = this.arr_events.push(evt) - 1; // More efficient than .length before push
|
|
857
|
-
let arr_indices = this.map_index_id.get(id);
|
|
858
|
-
if (!arr_indices) {
|
|
859
|
-
arr_indices = [];
|
|
860
|
-
this.map_index_id.set(id, arr_indices);
|
|
861
|
-
}
|
|
862
|
-
arr_indices.push(idx);
|
|
863
|
-
// Set timeout if specified
|
|
864
|
-
if (timeout_ms && timeout_callback) {
|
|
865
|
-
evt.timeout_id = setTimeout(() => {
|
|
866
|
-
if (this.map_timeouts.delete(idx)) {
|
|
867
|
-
evt.timeout_id = null;
|
|
868
|
-
timeout_callback({ idx, evt });
|
|
869
|
-
logr_$1.log(l_$1.LOG_EVENTS, () => [`Event DELETED[${idx}]: ${type}`, evt]);
|
|
870
|
-
}
|
|
871
|
-
}, timeout_ms);
|
|
872
|
-
this.map_timeouts.set(idx, evt.timeout_id);
|
|
873
|
-
}
|
|
874
|
-
// the error "toISOString is not a function" occurs because Date(evt.dt) is not doing what you expect.
|
|
875
|
-
// When you pass a number to the Date constructor without using new, it doesn’t return a Date object—it returns a string. Specifically, it’s equivalent to new Date(evt.dt).toString().
|
|
876
|
-
// Date(evt.dt).toISOString() -> new Date(evt.dt).toISOString()
|
|
877
|
-
logr_$1.log(l_$1.LOG_EVENTS, () => [`Event added[${idx}] @${new Date(evt.dt).toISOString()}: ${type}`, evt]);
|
|
878
|
-
return { idx, evt };
|
|
879
|
-
}
|
|
880
|
-
cancelTimeout(idx) {
|
|
881
|
-
const timeout_id = this.map_timeouts.get(idx);
|
|
882
|
-
if (timeout_id) {
|
|
883
|
-
clearTimeout(timeout_id);
|
|
884
|
-
this.map_timeouts.delete(idx);
|
|
885
|
-
if (this.arr_events[idx])
|
|
886
|
-
this.arr_events[idx].timeout_id = null;
|
|
887
|
-
return true;
|
|
888
|
-
}
|
|
889
|
-
return false;
|
|
890
|
-
}
|
|
891
|
-
cancelAllTimeouts() {
|
|
892
|
-
for (const [idx, timeout_id] of this.map_timeouts) {
|
|
893
|
-
clearTimeout(timeout_id);
|
|
894
|
-
if (this.arr_events[idx])
|
|
895
|
-
this.arr_events[idx].timeout_id = null;
|
|
896
|
-
}
|
|
897
|
-
this.map_timeouts.clear();
|
|
898
|
-
}
|
|
899
|
-
getEventsById(id, lookback_ms = null) {
|
|
900
|
-
const arr_indices = this.map_index_id.get(id);
|
|
901
|
-
if (!arr_indices?.length)
|
|
902
|
-
return [];
|
|
903
|
-
const result = [];
|
|
904
|
-
const cutoff = lookback_ms !== null ? Date.now() - lookback_ms : -Infinity;
|
|
905
|
-
// Single loop with early cutoff
|
|
906
|
-
for (let i = arr_indices.length - 1; i >= 0; i--) {
|
|
907
|
-
const idx = arr_indices[i];
|
|
908
|
-
const evt = this.arr_events[idx];
|
|
909
|
-
if (evt.dt < cutoff)
|
|
910
|
-
break;
|
|
911
|
-
result.push({ idx, evt });
|
|
912
|
-
}
|
|
913
|
-
return result; // Newest to oldest order
|
|
914
|
-
}
|
|
915
|
-
// getRecentEvents(id, lookbackMs) {
|
|
916
|
-
// const cutoff = Date.now() - lookbackMs;
|
|
917
|
-
// return this.events.filter(e => e.id === id && e.dt >= cutoff);
|
|
918
|
-
// }
|
|
919
|
-
getEvents(lookback_ms, predicate) {
|
|
920
|
-
const cutoff = Date.now() - lookback_ms;
|
|
921
|
-
const result = [];
|
|
922
|
-
// Iterate backwards from the end
|
|
923
|
-
for (let idx = this.arr_events.length - 1; idx >= 0; idx--) {
|
|
924
|
-
const evt = this.arr_events[idx];
|
|
925
|
-
if (evt.dt < cutoff) {
|
|
926
|
-
break; // Stop once we pass the cutoff
|
|
927
|
-
}
|
|
928
|
-
if (predicate(evt)) {
|
|
929
|
-
result.push({ idx, evt });
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
return result; // Newest to oldest order
|
|
933
|
-
}
|
|
934
|
-
// setLogr(obj_logr) {
|
|
935
|
-
// this.logr.toggled = obj_logr;
|
|
936
|
-
// }
|
|
937
|
-
clear() {
|
|
938
|
-
this.cancelAllTimeouts(); // Clear all timeouts before clearing events
|
|
939
|
-
this.arr_events.length = 0;
|
|
940
|
-
this.map_index_id.clear();
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
|
|
944
873
|
// interface Event extends Required<EventMinimal> {
|
|
945
874
|
// type : string;
|
|
946
875
|
// id : string | number;
|
|
@@ -948,11 +877,11 @@ class EventLog {
|
|
|
948
877
|
// };
|
|
949
878
|
const k_TTL = 20000;
|
|
950
879
|
//-------------------------------------------------------------------------------------------------
|
|
951
|
-
const obj_labels_merge_ = l_merge_(
|
|
880
|
+
const obj_labels_merge_ = l_merge_(logr.lref.get(), jsonMsg_cjsExports.logr.lref.get()); // VALIDATION : 4
|
|
952
881
|
const obj_labels_concat_ = l_concat_(obj_labels_merge_, l_array_(['HANDLERS', 'DROPS', 'PROTOCOL_STATE', 'RECVER', 'SENDER']));
|
|
953
882
|
const lref_ = lRef(obj_labels_concat_);
|
|
954
883
|
jsonMsg_cjsExports.logr.lref = lref_;
|
|
955
|
-
|
|
884
|
+
logr.lref = lref_;
|
|
956
885
|
const LOGR_ = LOGR.get_instance();
|
|
957
886
|
const logr_ = LOGR_.create(); // don't let create() make a new ref, assign lref_ from here
|
|
958
887
|
logr_.lref = lref_;
|
|
@@ -1323,22 +1252,22 @@ class TwoPhW_EventLog {
|
|
|
1323
1252
|
// _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : ( args? : {idx_event : number, event : EventMinimal} ) => {}) {
|
|
1324
1253
|
_addEvent(msg, json_msg, timeout_ms, timeout_callback) {
|
|
1325
1254
|
const { str_type, json_msgWarp } = this._warp(json_msg);
|
|
1326
|
-
const { idx } = this._eventLog.
|
|
1255
|
+
const { idx } = this._eventLog.add_Event(str_type, json_msgWarp.warp.id, { msg, json_msg }, timeout_ms, timeout_callback);
|
|
1327
1256
|
logr_.log(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
|
|
1328
1257
|
}
|
|
1329
1258
|
_dropEvent(json_msg) {
|
|
1330
1259
|
const { json_msgWarp } = this._warp(json_msg);
|
|
1331
1260
|
if (!json_msgWarp)
|
|
1332
1261
|
return;
|
|
1333
|
-
this._eventLog.
|
|
1262
|
+
this._eventLog.add_Event('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
|
|
1334
1263
|
}
|
|
1335
1264
|
_cancelTimeout(idx_event) {
|
|
1336
1265
|
logr_.log(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
|
|
1337
|
-
this._eventLog.
|
|
1266
|
+
this._eventLog.cancel_timeout(idx_event);
|
|
1338
1267
|
}
|
|
1339
1268
|
traverse_State(json_msgWarp) {
|
|
1340
1269
|
const protocolState = new ProtocolStateState();
|
|
1341
|
-
const arr_evts = this._eventLog.
|
|
1270
|
+
const arr_evts = this._eventLog.get_Events_by_id(json_msgWarp.warp.id, k_TTL);
|
|
1342
1271
|
logr_.log(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
|
|
1343
1272
|
//TODO: what is the right TTL? should it be a constant or calculated
|
|
1344
1273
|
for (let i = arr_evts.length - 1; i >= 0; i--) {
|
|
@@ -1540,7 +1469,7 @@ class TwoPhW {
|
|
|
1540
1469
|
}
|
|
1541
1470
|
//-------------------------------------------------------------------------------------------------
|
|
1542
1471
|
handler_json_msgAck(msg, json_msgAck) {
|
|
1543
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msgAck: ', json_msgAck]);
|
|
1472
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgAck: ', json_msgAck]);
|
|
1544
1473
|
if (JSON_MsgAckWarp.validate(json_msgAck) && this._handler_msgAckWarp(msg, json_msgAck))
|
|
1545
1474
|
return true;
|
|
1546
1475
|
if (JSON_MsgAckEngage.validate(json_msgAck) && this._handler_msgAckEngage(msg, json_msgAck))
|
|
@@ -1548,9 +1477,9 @@ class TwoPhW {
|
|
|
1548
1477
|
return false;
|
|
1549
1478
|
}
|
|
1550
1479
|
handler_json_msgEffect(msg, json_msgEffect) {
|
|
1551
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msgEffect: ', json_msgEffect]);
|
|
1552
|
-
if (
|
|
1553
|
-
|
|
1480
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgEffect: ', json_msgEffect]);
|
|
1481
|
+
// if (JSON_MsgAck.validate(json_msgEffect) && this.handler_json_msgAck(msg, json_msgEffect) )
|
|
1482
|
+
// return true;
|
|
1554
1483
|
if (JSON_MsgEngage.validate(json_msgEffect) && this._handler_msgEngage(msg, json_msgEffect))
|
|
1555
1484
|
return true;
|
|
1556
1485
|
// if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
|
|
@@ -1558,7 +1487,7 @@ class TwoPhW {
|
|
|
1558
1487
|
return false;
|
|
1559
1488
|
}
|
|
1560
1489
|
handler_json_msg(msg, json_msg) {
|
|
1561
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msg: ', json_msg]);
|
|
1490
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msg: ', json_msg]);
|
|
1562
1491
|
if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
|
|
1563
1492
|
return true;
|
|
1564
1493
|
// https://attacomsian.com/blog/javascript-iterate-objects
|
|
@@ -1571,17 +1500,9 @@ class TwoPhW {
|
|
|
1571
1500
|
// });
|
|
1572
1501
|
return false;
|
|
1573
1502
|
}
|
|
1574
|
-
handler_json(msg, json) {
|
|
1575
|
-
logr_.log(l_.HANDLERS, () => ['handler_json: ', json]);
|
|
1576
|
-
if (jsonMsg_cjsExports.JSON_MsgEffect.validate(json) && this.handler_json_msgEffect(msg, json))
|
|
1577
|
-
return true;
|
|
1578
|
-
if (jsonMsg_cjsExports.JSON_Msg.validate(json) && this.handler_json_msg(msg, json))
|
|
1579
|
-
return true;
|
|
1580
|
-
return false;
|
|
1581
|
-
}
|
|
1582
1503
|
}
|
|
1583
1504
|
|
|
1584
1505
|
var JSON_Msg = jsonMsg_cjsExports.JSON_Msg;
|
|
1585
1506
|
var JSON_MsgAck = jsonMsg_cjsExports.JSON_MsgAck;
|
|
1586
1507
|
var JSON_MsgEffect = jsonMsg_cjsExports.JSON_MsgEffect;
|
|
1587
|
-
export {
|
|
1508
|
+
export { JSON_Msg, JSON_MsgAck, JSON_MsgAckEngage, JSON_MsgAckWarp, JSON_MsgEffect, JSON_MsgEngage, JSON_MsgOptions, JSON_MsgWarp, StateAckEngage, StateAckWarp, StateEngage, StateINVALID, StateInitialState, StateWarp, TwoPhW, logr_ as logr, time_ttl_rtt_ as t_ttl_rtt };
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rootintf/protocol-2phw",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.4-api.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/twoPhW.cjs.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"browser": "dist/twoPhW.es.mjs",
|
|
8
8
|
"scripts": {
|
|
9
|
+
"gv:all" : "gv --js --antora --tag api && git add package.json ./antora-docs/antora.yml",
|
|
9
10
|
"build": "node src/ajv-gen-validate.cjs && rollup --config",
|
|
10
11
|
"script": "node --experimental-json-modules tests/twoPhW.test.mjs",
|
|
11
12
|
"test": "jasmine --config=jasmine.json",
|
|
@@ -27,8 +28,9 @@
|
|
|
27
28
|
},
|
|
28
29
|
"homepage": "https://gitlab.com/interface-x/npm-protocol-2PhW.git#readme",
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@knev/bitlogr": "^3.
|
|
31
|
-
"@rootintf/
|
|
31
|
+
"@knev/bitlogr": "^3.2.2",
|
|
32
|
+
"@rootintf/eventlog": "^0.0.3",
|
|
33
|
+
"@rootintf/json-msg": "^2.1.7",
|
|
32
34
|
"ajv": "^8.17.1"
|
|
33
35
|
},
|
|
34
36
|
"devDependencies": {
|
|
@@ -36,12 +38,12 @@
|
|
|
36
38
|
"@rollup/plugin-json": "^6.1.0",
|
|
37
39
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
38
40
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
39
|
-
"@types/ajv": "^
|
|
40
|
-
"@types/node": "^
|
|
41
|
+
"@types/ajv": "^1.0.4",
|
|
42
|
+
"@types/node": "^25.1.0",
|
|
41
43
|
"ajv-cli": "^5.0.0",
|
|
42
44
|
"del-cli": "^7.0.0",
|
|
43
|
-
"jasmine": "^
|
|
44
|
-
"rollup": "^4.
|
|
45
|
+
"jasmine": "^6.0.0",
|
|
46
|
+
"rollup": "^4.57.0",
|
|
45
47
|
"tslib": "^2.8.1",
|
|
46
48
|
"typescript": "^5.9.3",
|
|
47
49
|
"uuid": "^13.0.0"
|
package/rollup.config.mjs
CHANGED
|
@@ -4,22 +4,27 @@ import commonjs from '@rollup/plugin-commonjs';
|
|
|
4
4
|
import json from '@rollup/plugin-json';
|
|
5
5
|
import typescript from '@rollup/plugin-typescript';
|
|
6
6
|
|
|
7
|
+
// EventLog now lives in its own package; keep it external instead of re-bundling it.
|
|
8
|
+
const external = ['@rootintf/eventlog'];
|
|
9
|
+
|
|
7
10
|
export default [
|
|
8
11
|
{
|
|
9
12
|
input: 'src/twoPhW.ts',
|
|
13
|
+
external,
|
|
10
14
|
output: {
|
|
11
15
|
file: 'dist/twoPhW.es.mjs',
|
|
12
16
|
format: 'es'
|
|
13
17
|
},
|
|
14
18
|
plugins: [
|
|
15
|
-
resolve(),
|
|
16
|
-
commonjs(),
|
|
19
|
+
resolve(),
|
|
20
|
+
commonjs(),
|
|
17
21
|
json(),
|
|
18
22
|
typescript({ tsconfig: './tsconfig.json' })
|
|
19
23
|
]
|
|
20
24
|
},
|
|
21
25
|
{
|
|
22
26
|
input: 'src/twoPhW.ts',
|
|
27
|
+
external,
|
|
23
28
|
output: {
|
|
24
29
|
file: 'dist/twoPhW.cjs.js',
|
|
25
30
|
format: 'cjs'
|
package/src/twoPhW.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
import { LOGR, lRef, l_merge, l_array, l_concat, l_LL } from '@knev/bitlogr';
|
|
12
12
|
import { JSON_Msg, JSON_MsgEffect, JSON_MsgAck, logr as logr_json_msg_ } from '@rootintf/json-msg'
|
|
13
13
|
|
|
14
|
-
import { EventLog, logr as logr_evtlog_ } from '
|
|
14
|
+
import { EventLog, logr as logr_evtlog_ } from '@rootintf/eventlog'
|
|
15
15
|
|
|
16
16
|
type t_msg = string;
|
|
17
17
|
|
|
@@ -456,7 +456,7 @@ class TwoPhW_EventLog {
|
|
|
456
456
|
// _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : ( args? : {idx_event : number, event : EventMinimal} ) => {}) {
|
|
457
457
|
_addEvent(msg: t_msg, json_msg, timeout_ms? : number, timeout_callback? : () => void) {
|
|
458
458
|
const { str_type, json_msgWarp }= this._warp(json_msg);
|
|
459
|
-
const { idx }= this._eventLog.
|
|
459
|
+
const { idx }= this._eventLog.add_Event(str_type, json_msgWarp.warp.id, {msg, json_msg}, timeout_ms, timeout_callback);
|
|
460
460
|
logr_.log(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
|
|
461
461
|
}
|
|
462
462
|
|
|
@@ -464,18 +464,18 @@ class TwoPhW_EventLog {
|
|
|
464
464
|
const { json_msgWarp }= this._warp(json_msg);
|
|
465
465
|
if (! json_msgWarp)
|
|
466
466
|
return;
|
|
467
|
-
this._eventLog.
|
|
467
|
+
this._eventLog.add_Event('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
|
|
468
468
|
}
|
|
469
469
|
|
|
470
470
|
_cancelTimeout(idx_event) {
|
|
471
471
|
logr_.log(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
|
|
472
|
-
this._eventLog.
|
|
472
|
+
this._eventLog.cancel_timeout(idx_event);
|
|
473
473
|
}
|
|
474
474
|
|
|
475
475
|
traverse_State(json_msgWarp) {
|
|
476
476
|
const protocolState = new ProtocolStateState();
|
|
477
477
|
|
|
478
|
-
const arr_evts= this._eventLog.
|
|
478
|
+
const arr_evts= this._eventLog.get_Events_by_id(json_msgWarp.warp.id, k_TTL);
|
|
479
479
|
logr_.log(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
|
|
480
480
|
//TODO: what is the right TTL? should it be a constant or calculated
|
|
481
481
|
|
|
@@ -747,7 +747,7 @@ class TwoPhW {
|
|
|
747
747
|
|
|
748
748
|
handler_json_msgAck(msg : t_msg, json_msgAck) : boolean
|
|
749
749
|
{
|
|
750
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msgAck: ', json_msgAck]);
|
|
750
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgAck: ', json_msgAck]);
|
|
751
751
|
|
|
752
752
|
if (JSON_MsgAckWarp.validate(json_msgAck) && this._handler_msgAckWarp(msg, json_msgAck) )
|
|
753
753
|
return true;
|
|
@@ -760,10 +760,10 @@ class TwoPhW {
|
|
|
760
760
|
|
|
761
761
|
handler_json_msgEffect(msg : t_msg, json_msgEffect) : boolean
|
|
762
762
|
{
|
|
763
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msgEffect: ', json_msgEffect]);
|
|
763
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgEffect: ', json_msgEffect]);
|
|
764
764
|
|
|
765
|
-
if (JSON_MsgAck.validate(json_msgEffect) && this.handler_json_msgAck(msg, json_msgEffect) )
|
|
766
|
-
|
|
765
|
+
// if (JSON_MsgAck.validate(json_msgEffect) && this.handler_json_msgAck(msg, json_msgEffect) )
|
|
766
|
+
// return true;
|
|
767
767
|
|
|
768
768
|
if (JSON_MsgEngage.validate(json_msgEffect) && this._handler_msgEngage(msg, json_msgEffect) )
|
|
769
769
|
return true;
|
|
@@ -776,7 +776,7 @@ class TwoPhW {
|
|
|
776
776
|
|
|
777
777
|
handler_json_msg(msg : t_msg, json_msg) : boolean
|
|
778
778
|
{
|
|
779
|
-
logr_.log(l_.HANDLERS, () => ['handler_json_msg: ', json_msg]);
|
|
779
|
+
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msg: ', json_msg]);
|
|
780
780
|
|
|
781
781
|
if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg) )
|
|
782
782
|
return true;
|
|
@@ -793,25 +793,24 @@ class TwoPhW {
|
|
|
793
793
|
return false;
|
|
794
794
|
}
|
|
795
795
|
|
|
796
|
-
handler_json(msg : t_msg, json) : boolean
|
|
797
|
-
{
|
|
798
|
-
|
|
796
|
+
// handler_json(msg : t_msg, json) : boolean
|
|
797
|
+
// {
|
|
798
|
+
// logr_.log(l_.HANDLERS, () => ['handler_json: ', json]);
|
|
799
799
|
|
|
800
|
-
|
|
801
|
-
|
|
800
|
+
// if (JSON_MsgEffect.validate(json) && this.handler_json_msgEffect(msg, json) )
|
|
801
|
+
// return true;
|
|
802
802
|
|
|
803
|
-
|
|
804
|
-
|
|
803
|
+
// if (JSON_Msg.validate(json) && this.handler_json_msg(msg, json) )
|
|
804
|
+
// return true;
|
|
805
805
|
|
|
806
|
-
|
|
807
|
-
}
|
|
806
|
+
// return false;
|
|
807
|
+
// }
|
|
808
808
|
|
|
809
809
|
};
|
|
810
810
|
|
|
811
811
|
//-------------------------------------------------------------------------------------------------
|
|
812
812
|
|
|
813
|
-
export {
|
|
814
|
-
EventLog,
|
|
813
|
+
export {
|
|
815
814
|
time_ttl_rtt_ as t_ttl_rtt,
|
|
816
815
|
TwoPhW,
|
|
817
816
|
JSON_MsgOptions,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
import { v4 as uuidv4 } from 'uuid';
|
|
3
3
|
import { LOGR, l_concat } from '@knev/bitlogr';
|
|
4
|
-
import {
|
|
4
|
+
import { EventLog } from '@rootintf/eventlog';
|
|
5
|
+
import {
|
|
5
6
|
StateAckEngage, StateEngage, StateAckWarp, StateWarp, StateInitialState, StateINVALID,
|
|
6
|
-
EventLog,
|
|
7
7
|
t_ttl_rtt,
|
|
8
8
|
TwoPhW, JSON_MsgOptions, JSON_MsgWarp, JSON_MsgEngage, JSON_MsgAckWarp, JSON_MsgAckEngage,
|
|
9
|
-
logr as logr_,
|
|
9
|
+
logr as logr_,
|
|
10
10
|
JSON_Msg, JSON_MsgEffect, JSON_MsgAck
|
|
11
11
|
} from '../dist/twoPhW.es.mjs';
|
|
12
12
|
|
|
@@ -32,7 +32,7 @@ const l_= logr_.l;
|
|
|
32
32
|
LOGR_.toggle(l_, {
|
|
33
33
|
VALIDATION : true
|
|
34
34
|
});
|
|
35
|
-
logr_.raw(
|
|
35
|
+
logr_.raw('twoPhW-new.spec: const l_ ', l_.get())
|
|
36
36
|
|
|
37
37
|
//-------------------------------------------------------------------------------------------------
|
|
38
38
|
|
|
@@ -196,7 +196,7 @@ describe('2PhW protocol', () => {
|
|
|
196
196
|
});
|
|
197
197
|
|
|
198
198
|
describe('basics', () => {
|
|
199
|
-
it('
|
|
199
|
+
it('_addEvent', () => {
|
|
200
200
|
const json_msgWarp= JSON_MsgWarp.create_with_id_user_hyperport(uuidv4(), '', '', TTL_MSEC_1500);
|
|
201
201
|
json_msgWarp.referer= karr_referer_src;
|
|
202
202
|
|
|
@@ -284,7 +284,7 @@ describe('2PhW protocol', () => {
|
|
|
284
284
|
|
|
285
285
|
it('receive JSON_MsgWarp', () => {
|
|
286
286
|
jasmine.clock().tick(TTL_MSEC_1500 -STT_MSEC_1000 -1); // 1499
|
|
287
|
-
twoPhW.
|
|
287
|
+
twoPhW.handler_json_msg(str_msgWarp_src, json_msgWarp_src);
|
|
288
288
|
expect(callback_msgWarp).toHaveBeenCalled();
|
|
289
289
|
expect(eventLog.arr_events.length).toBe(1);
|
|
290
290
|
expect(eventLog.arr_events[0].type).toBe('2PhW[JSON_MsgWarp]');
|
|
@@ -292,14 +292,14 @@ describe('2PhW protocol', () => {
|
|
|
292
292
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateWarp).toBe(true);
|
|
293
293
|
|
|
294
294
|
// dup
|
|
295
|
-
twoPhW.
|
|
295
|
+
twoPhW.handler_json_msg(str_msgWarp_src, json_msgWarp_src);
|
|
296
296
|
expect(eventLog.arr_events.length).toBe(1);
|
|
297
297
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateWarp).toBe(true);
|
|
298
298
|
});
|
|
299
299
|
|
|
300
300
|
it('receive JSON_MsgWarp expired', () => {
|
|
301
301
|
jasmine.clock().tick(TTL_MSEC_1500 -STT_MSEC_1000); // 1500
|
|
302
|
-
twoPhW.
|
|
302
|
+
twoPhW.handler_json_msg(str_msgWarp_src, json_msgWarp_src);
|
|
303
303
|
expect(callback_msgWarp).not.toHaveBeenCalled();
|
|
304
304
|
expect(eventLog.arr_events.length).toBe(0);
|
|
305
305
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateInitialState).toBe(true);
|
|
@@ -309,13 +309,13 @@ describe('2PhW protocol', () => {
|
|
|
309
309
|
const json_msgEngage_recvd= JSON_MsgEngage.create_with_Warp(json_msgWarp_src);
|
|
310
310
|
json_msgEngage_recvd.referer= karr_referer_src;
|
|
311
311
|
|
|
312
|
-
twoPhW.
|
|
312
|
+
twoPhW.handler_json_msgEffect(json_msgEngage_recvd, json_msgEngage_recvd);
|
|
313
313
|
expect(eventLog.arr_events.length).toBe(0);
|
|
314
314
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateInitialState).toBe(true);
|
|
315
315
|
});
|
|
316
316
|
|
|
317
317
|
it('traverse_State JSON_MsgWarp.compare(json_msgWarp, this._warp(json_msg).json_msgWarp )', () => {
|
|
318
|
-
twoPhW.
|
|
318
|
+
twoPhW.handler_json_msg(str_msgWarp_src, json_msgWarp_src);
|
|
319
319
|
expect(eventLog.arr_events.length).toBe(1);
|
|
320
320
|
expect(eventLog.arr_events[0].type).toBe('2PhW[JSON_MsgWarp]');
|
|
321
321
|
expect(eventLog.arr_events[0].data).toEqual({ msg : str_msgWarp_src, json_msg: json_msgWarp_src });
|
|
@@ -326,7 +326,7 @@ describe('2PhW protocol', () => {
|
|
|
326
326
|
};
|
|
327
327
|
jasmine.clock().tick(1); // 1001, make sure it is th wrong date
|
|
328
328
|
json_msgWarp_wrong_dt.dt= new Date().toISOString();
|
|
329
|
-
twoPhW.
|
|
329
|
+
twoPhW.handler_json_msg(json_msgWarp_wrong_dt, json_msgWarp_wrong_dt);
|
|
330
330
|
expect(eventLog.arr_events.length).toBe(1);
|
|
331
331
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateWarp).toBe(true);
|
|
332
332
|
});
|
|
@@ -350,7 +350,7 @@ describe('2PhW protocol', () => {
|
|
|
350
350
|
|
|
351
351
|
expect(Date.now()-T0).toBe(STT_MSEC_1000);
|
|
352
352
|
|
|
353
|
-
twoPhW.
|
|
353
|
+
twoPhW.handler_json_msg(str_msgWarp_src, json_msgWarp_src);
|
|
354
354
|
expect(callback_msgWarp).toHaveBeenCalled();
|
|
355
355
|
expect(eventLog.arr_events.length).toBe(1);
|
|
356
356
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateWarp).toBe(true);
|
|
@@ -381,7 +381,7 @@ describe('2PhW protocol', () => {
|
|
|
381
381
|
const json_msgEngage_src= JSON_MsgEngage.create_with_Warp(json_msgWarp_src);
|
|
382
382
|
json_msgEngage_src.referer= karr_referer_src;
|
|
383
383
|
|
|
384
|
-
twoPhW.
|
|
384
|
+
twoPhW.handler_json_msgEffect(json_msgEngage_src, json_msgEngage_src);
|
|
385
385
|
expect(eventLog.arr_events.length).toBe(2);
|
|
386
386
|
});
|
|
387
387
|
|
|
@@ -396,7 +396,7 @@ describe('2PhW protocol', () => {
|
|
|
396
396
|
json_msgEngage_src.referer= karr_referer_src;
|
|
397
397
|
|
|
398
398
|
jasmine.clock().tick(STT_MSEC_1000); // 3000, STT_MSEC_1000 *3
|
|
399
|
-
twoPhW.
|
|
399
|
+
twoPhW.handler_json_msgEffect(json_msgEngage_src, json_msgEngage_src);
|
|
400
400
|
expect(eventLog.arr_events.length).toBe(2);
|
|
401
401
|
});
|
|
402
402
|
|
|
@@ -451,7 +451,7 @@ describe('2PhW protocol', () => {
|
|
|
451
451
|
// - TTL= 1500, RTT= 2000; TTL*2 + RTT= 5000; timeout= 5000
|
|
452
452
|
// - expires at 5000
|
|
453
453
|
|
|
454
|
-
twoPhW.
|
|
454
|
+
twoPhW.handler_json_msgEffect(str_msgEngage_src, json_msgEngage_src);
|
|
455
455
|
expect(callback_msgEngage).not.toHaveBeenCalled();
|
|
456
456
|
expect(eventLog.arr_events.length).toBe(2);
|
|
457
457
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateAckWarp).toBe(true);
|
|
@@ -477,7 +477,7 @@ describe('2PhW protocol', () => {
|
|
|
477
477
|
|
|
478
478
|
jasmine.clock().tick(STT_MSEC_1000); // 3000, STT_MSEC_1000 *3
|
|
479
479
|
|
|
480
|
-
twoPhW.
|
|
480
|
+
twoPhW.handler_json_msgEffect(str_msgEngage_src, json_msgEngage_src);
|
|
481
481
|
expect(callback_msgEngage).toHaveBeenCalled();
|
|
482
482
|
expect(callback_msg_sent_expired).not.toHaveBeenCalled();
|
|
483
483
|
expect(eventLog.arr_events.length).toBe(3);
|
|
@@ -695,14 +695,14 @@ describe('2PhW protocol', () => {
|
|
|
695
695
|
expect(callback_msgAckWarp).not.toHaveBeenCalled();
|
|
696
696
|
expect(callback_msg_sent_expired).toHaveBeenCalled();
|
|
697
697
|
|
|
698
|
-
twoPhW.
|
|
698
|
+
twoPhW.handler_json_msgAck(json_msgAckWarp_dst, json_msgAckWarp_dst);
|
|
699
699
|
expect(callback_msgAckWarp).not.toHaveBeenCalled();
|
|
700
700
|
expect(eventLog.arr_events.length).toBe(1);
|
|
701
701
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateWarp).toBe(true);
|
|
702
702
|
});
|
|
703
703
|
|
|
704
704
|
it('send JSON_MsgWarp == NACK', () => {
|
|
705
|
-
twoPhW.
|
|
705
|
+
twoPhW.handler_json_msgAck(str_msgNackWarp_dst, json_msgNackWarp_dst);
|
|
706
706
|
expect(eventLog.arr_events.length).toBe(2);
|
|
707
707
|
expect(eventLog.arr_events[1].type).toBe('2PhW[JSON_MsgAckWarp]');
|
|
708
708
|
expect(eventLog.arr_events[1].data).toEqual({msg : str_msgNackWarp_dst, json_msg : json_msgNackWarp_dst });
|
|
@@ -717,7 +717,7 @@ describe('2PhW protocol', () => {
|
|
|
717
717
|
json_msgAckEngage_recv.referer= karr_referer_dst;
|
|
718
718
|
|
|
719
719
|
jasmine.clock().tick(STT_MSEC_1000); // 4000
|
|
720
|
-
twoPhW.
|
|
720
|
+
twoPhW.handler_json_msgAck(json_msgAckEngage_recv, json_msgAckEngage_recv);
|
|
721
721
|
expect(eventLog.arr_events.length).toBe(2);
|
|
722
722
|
});
|
|
723
723
|
|
|
@@ -731,7 +731,7 @@ describe('2PhW protocol', () => {
|
|
|
731
731
|
|
|
732
732
|
expect(Date.now()-T0).toBe(RTT_MSEC_2000);
|
|
733
733
|
|
|
734
|
-
twoPhW.
|
|
734
|
+
twoPhW.handler_json_msgAck(str_msgAckWarp_dst, json_msgAckWarp_dst);
|
|
735
735
|
expect(callback_msgAckWarp).toHaveBeenCalled();
|
|
736
736
|
expect(eventLog.arr_events.length).toBe(2);
|
|
737
737
|
expect(eventLog.arr_events[1].type).toBe('2PhW[JSON_MsgAckWarp]');
|
|
@@ -809,7 +809,7 @@ describe('2PhW protocol', () => {
|
|
|
809
809
|
expect(Date.now()-T0).toBe(4999);
|
|
810
810
|
expect(callback_msg_sent_expired).not.toHaveBeenCalled();
|
|
811
811
|
|
|
812
|
-
twoPhW.
|
|
812
|
+
twoPhW.handler_json_msgAck(str_msgAckEngage_dst, json_msgAckEngage_dst);
|
|
813
813
|
expect(eventLog.arr_events.length).toBe(4);
|
|
814
814
|
expect(eventLog.arr_events[3].type).toBe('2PhW[JSON_MsgAckEngage]');
|
|
815
815
|
expect(eventLog.arr_events[3].data).toEqual({ msg: str_msgAckEngage_dst, json_msg : json_msgAckEngage_dst });
|
|
@@ -827,7 +827,7 @@ describe('2PhW protocol', () => {
|
|
|
827
827
|
expect(callback_msg_sent_expired).toHaveBeenCalled();
|
|
828
828
|
|
|
829
829
|
// test to make sure a late ack isn't a valid AckEngage state
|
|
830
|
-
twoPhW.
|
|
830
|
+
twoPhW.handler_json_msgAck(str_msgAckEngage_dst, json_msgAckEngage_dst);
|
|
831
831
|
expect(eventLog.arr_events.length).toBe(3);
|
|
832
832
|
expect((twoPhW.eventLog.traverse_State(json_msgWarp_src)).state instanceof StateEngage).toBe(true);
|
|
833
833
|
});
|
package/dist/EventLog-mem.d.mts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export class EventLog {
|
|
2
|
-
arr_events: any[];
|
|
3
|
-
map_index_id: Map<any, any>;
|
|
4
|
-
map_timeouts: Map<any, any>;
|
|
5
|
-
addEvent(type: any, id: any, data: any, timeout_ms?: any, timeout_callback?: any): {
|
|
6
|
-
idx: number;
|
|
7
|
-
evt: {
|
|
8
|
-
type: any;
|
|
9
|
-
dt: number;
|
|
10
|
-
id: any;
|
|
11
|
-
data: any;
|
|
12
|
-
timeout_id: any;
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
cancelTimeout(idx: any): boolean;
|
|
16
|
-
cancelAllTimeouts(): void;
|
|
17
|
-
getEventsById(id: any, lookback_ms?: any): {
|
|
18
|
-
idx: any;
|
|
19
|
-
evt: any;
|
|
20
|
-
}[];
|
|
21
|
-
getEvents(lookback_ms: any, predicate: any): {
|
|
22
|
-
idx: number;
|
|
23
|
-
evt: any;
|
|
24
|
-
}[];
|
|
25
|
-
clear(): void;
|
|
26
|
-
}
|
|
27
|
-
declare const logr_: any;
|
|
28
|
-
export { logr_ as logr };
|
package/src/EventLog-mem.mjs
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { LOGR, l_array } from '@knev/bitlogr';
|
|
3
|
-
|
|
4
|
-
//-------------------------------------------------------------------------------------------------
|
|
5
|
-
|
|
6
|
-
let LOGR_= LOGR.get_instance();
|
|
7
|
-
const logr_= LOGR_.create({ labels: l_array(['LOG_EVENTS', 'TIMEOUTS']) });
|
|
8
|
-
const l_= logr_.l;
|
|
9
|
-
|
|
10
|
-
//-------------------------------------------------------------------------------------------------
|
|
11
|
-
|
|
12
|
-
class EventLog {
|
|
13
|
-
constructor() {
|
|
14
|
-
this.arr_events = []; // In-memory event store
|
|
15
|
-
this.map_index_id = new Map(); // Map of id -> array of indices into arr_events; arr_events[idx]
|
|
16
|
-
this.map_timeouts = new Map(); // Store timeout references: idx -> timeout_id
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
addEvent(type, id, data, timeout_ms = null, timeout_callback = null) {
|
|
20
|
-
const evt = {
|
|
21
|
-
type,
|
|
22
|
-
dt: Date.now(),
|
|
23
|
-
id,
|
|
24
|
-
data,
|
|
25
|
-
timeout_id: null // Will store timeout reference if set
|
|
26
|
-
};
|
|
27
|
-
const idx = this.arr_events.push(evt) - 1; // More efficient than .length before push
|
|
28
|
-
|
|
29
|
-
let arr_indices = this.map_index_id.get(id);
|
|
30
|
-
if (! arr_indices) {
|
|
31
|
-
arr_indices = [];
|
|
32
|
-
this.map_index_id.set(id, arr_indices);
|
|
33
|
-
}
|
|
34
|
-
arr_indices.push(idx);
|
|
35
|
-
|
|
36
|
-
// Set timeout if specified
|
|
37
|
-
if (timeout_ms && timeout_callback) {
|
|
38
|
-
evt.timeout_id = setTimeout(() => {
|
|
39
|
-
if (this.map_timeouts.delete(idx)) {
|
|
40
|
-
evt.timeout_id = null;
|
|
41
|
-
timeout_callback({idx, evt});
|
|
42
|
-
logr_.log(l_.LOG_EVENTS, () => [`Event DELETED[${idx}]: ${type}`, evt]);
|
|
43
|
-
}
|
|
44
|
-
}, timeout_ms);
|
|
45
|
-
|
|
46
|
-
this.map_timeouts.set(idx, evt.timeout_id);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// the error "toISOString is not a function" occurs because Date(evt.dt) is not doing what you expect.
|
|
50
|
-
// When you pass a number to the Date constructor without using new, it doesn’t return a Date object—it returns a string. Specifically, it’s equivalent to new Date(evt.dt).toString().
|
|
51
|
-
// Date(evt.dt).toISOString() -> new Date(evt.dt).toISOString()
|
|
52
|
-
logr_.log(l_.LOG_EVENTS, () => [`Event added[${idx}] @${new Date(evt.dt).toISOString()}: ${type}`, evt]);
|
|
53
|
-
return { idx, evt };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
cancelTimeout(idx) {
|
|
57
|
-
const timeout_id = this.map_timeouts.get(idx);
|
|
58
|
-
if (timeout_id) {
|
|
59
|
-
clearTimeout(timeout_id);
|
|
60
|
-
this.map_timeouts.delete(idx);
|
|
61
|
-
if (this.arr_events[idx])
|
|
62
|
-
this.arr_events[idx].timeout_id = null;
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
cancelAllTimeouts() {
|
|
69
|
-
for (const [idx, timeout_id] of this.map_timeouts) {
|
|
70
|
-
clearTimeout(timeout_id);
|
|
71
|
-
if (this.arr_events[idx])
|
|
72
|
-
this.arr_events[idx].timeout_id = null;
|
|
73
|
-
}
|
|
74
|
-
this.map_timeouts.clear();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
getEventsById(id, lookback_ms = null) {
|
|
78
|
-
const arr_indices = this.map_index_id.get(id);
|
|
79
|
-
if (!arr_indices?.length) return [];
|
|
80
|
-
|
|
81
|
-
const result = [];
|
|
82
|
-
const cutoff = lookback_ms !== null ? Date.now() - lookback_ms : -Infinity;
|
|
83
|
-
|
|
84
|
-
// Single loop with early cutoff
|
|
85
|
-
for (let i = arr_indices.length - 1; i >= 0; i--) {
|
|
86
|
-
const idx = arr_indices[i];
|
|
87
|
-
const evt = this.arr_events[idx];
|
|
88
|
-
if (evt.dt < cutoff)
|
|
89
|
-
break;
|
|
90
|
-
result.push({ idx, evt });
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return result; // Newest to oldest order
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// getRecentEvents(id, lookbackMs) {
|
|
97
|
-
// const cutoff = Date.now() - lookbackMs;
|
|
98
|
-
// return this.events.filter(e => e.id === id && e.dt >= cutoff);
|
|
99
|
-
// }
|
|
100
|
-
|
|
101
|
-
getEvents(lookback_ms, predicate) {
|
|
102
|
-
const cutoff = Date.now() - lookback_ms;
|
|
103
|
-
const result = [];
|
|
104
|
-
|
|
105
|
-
// Iterate backwards from the end
|
|
106
|
-
for (let idx = this.arr_events.length - 1; idx >= 0; idx--) {
|
|
107
|
-
const evt = this.arr_events[idx];
|
|
108
|
-
if (evt.dt < cutoff) {
|
|
109
|
-
break; // Stop once we pass the cutoff
|
|
110
|
-
}
|
|
111
|
-
if (predicate(evt)) {
|
|
112
|
-
result.push({ idx, evt });
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return result; // Newest to oldest order
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// setLogr(obj_logr) {
|
|
120
|
-
// this.logr.toggled = obj_logr;
|
|
121
|
-
// }
|
|
122
|
-
|
|
123
|
-
clear() {
|
|
124
|
-
this.cancelAllTimeouts(); // Clear all timeouts before clearing events
|
|
125
|
-
this.arr_events.length = 0;
|
|
126
|
-
this.map_index_id.clear();
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export {
|
|
131
|
-
EventLog,
|
|
132
|
-
logr_ as logr
|
|
133
|
-
};
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { EventLog, logr as logr_ } from '../src/EventLog-mem.mjs';
|
|
3
|
-
import { LOGR } from '@knev/bitlogr';
|
|
4
|
-
|
|
5
|
-
//-------------------------------------------------------------------------------------------------
|
|
6
|
-
|
|
7
|
-
const LOGR_= LOGR.get_instance();
|
|
8
|
-
const l_= logr_.l;
|
|
9
|
-
|
|
10
|
-
//-------------------------------------------------------------------------------------------------
|
|
11
|
-
|
|
12
|
-
// Helper to mock Date.now for consistent timestamps
|
|
13
|
-
function mockDateNow(fixedTimestamp) {
|
|
14
|
-
spyOn(Date, 'now').and.returnValue(fixedTimestamp);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
describe('EventLog', () => {
|
|
18
|
-
let eventLog;
|
|
19
|
-
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
eventLog = new EventLog();
|
|
22
|
-
// Mock BitLogr.log to avoid actual logging during tests
|
|
23
|
-
// spyOn(eventLog.logr, 'log');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
afterEach(() => {
|
|
27
|
-
// Reset Date.now mock if used
|
|
28
|
-
jasmine.clock().uninstall();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe('constructor', () => {
|
|
32
|
-
it('should initialize with an empty events array', () => {
|
|
33
|
-
expect(eventLog.arr_events).toEqual([]);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe('addEvent;', () => {
|
|
39
|
-
beforeEach(() => {
|
|
40
|
-
// Use Jasmine's clock to control time
|
|
41
|
-
jasmine.clock().install();
|
|
42
|
-
jasmine.clock().mockDate(new Date(1000000)); // Fixed base time: 1,000,000 ms
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('add 1 event', () => {
|
|
46
|
-
const { idx, evt } = eventLog.addEvent('TEST_EVENT', 'msg1', { key: 'value' });
|
|
47
|
-
expect(typeof evt.dt).toBe('number');
|
|
48
|
-
expect(eventLog.arr_events.length).toBe(1);
|
|
49
|
-
expect(eventLog.arr_events[0]).toBe(evt);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('add 3 events', () => {
|
|
53
|
-
const { idx: idx0, evt: evt0 } = eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
|
|
54
|
-
jasmine.clock().tick(1000); // Advance 1 second
|
|
55
|
-
const { idx: idx1, evt: evt1 } = eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,001,000
|
|
56
|
-
jasmine.clock().tick(1000); // Advance another second
|
|
57
|
-
const { idx: idx2, evt: evt2 } = eventLog.addEvent('EVENT3', 'msg2', { data: 3 }); // t=1,002,000
|
|
58
|
-
|
|
59
|
-
expect(eventLog.arr_events.length).toBe(3);
|
|
60
|
-
expect(eventLog.arr_events[0]).toBe(evt0);
|
|
61
|
-
expect(eventLog.arr_events[1]).toBe(evt1);
|
|
62
|
-
expect(eventLog.arr_events[2]).toBe(evt2);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should return the added event', () => {
|
|
66
|
-
const { idx, evt } = eventLog.addEvent('TEST_EVENT', 'msg1', {});
|
|
67
|
-
expect(evt).toBeDefined();
|
|
68
|
-
expect(evt).toBe(eventLog.arr_events[0]);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
describe('getRecentEvents;', () => {
|
|
73
|
-
beforeEach(() => {
|
|
74
|
-
// Use Jasmine's clock to control time
|
|
75
|
-
jasmine.clock().install();
|
|
76
|
-
jasmine.clock().mockDate(new Date(1000000)); // Fixed base time: 1,000,000 ms
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('should return events matching msg_id predicate within lookback_ms', () => {
|
|
80
|
-
eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
|
|
81
|
-
jasmine.clock().tick(1000);
|
|
82
|
-
eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,001,000
|
|
83
|
-
jasmine.clock().tick(1000);
|
|
84
|
-
eventLog.addEvent('EVENT3', 'msg2', { data: 3 }); // t=1,002,000
|
|
85
|
-
|
|
86
|
-
let arr_evts;
|
|
87
|
-
|
|
88
|
-
arr_evts = eventLog.getEvents(1500, event => event.id === 'msg1');
|
|
89
|
-
expect(arr_evts.length).toBe(1); // Only one event within 1500 ms
|
|
90
|
-
expect(arr_evts[0].evt.dt).toBe(1001000); // Newest (and only) match
|
|
91
|
-
expect(arr_evts[0].evt.data).toEqual({ data: 2 });
|
|
92
|
-
|
|
93
|
-
arr_evts = eventLog.getEventsById('msg1', 1500);
|
|
94
|
-
expect(arr_evts.length).toBe(1); // Only one event within 1500 ms
|
|
95
|
-
expect(arr_evts[0].evt.dt).toBe(1001000); // Newest (and only) match
|
|
96
|
-
expect(arr_evts[0].evt.data).toEqual({ data: 2 });
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
it('should return events matching msg_id within lookback period', () => {
|
|
101
|
-
eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
|
|
102
|
-
jasmine.clock().tick(1000); // Advance 1 second
|
|
103
|
-
eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,001,000
|
|
104
|
-
jasmine.clock().tick(1000); // Advance another second
|
|
105
|
-
eventLog.addEvent('EVENT3', 'msg2', { data: 3 }); // t=1,002,000
|
|
106
|
-
|
|
107
|
-
const arr_evts = eventLog.getEvents(1500, event => event.type === 'EVENT3'); // Look back 1.5s from t=1,002,000
|
|
108
|
-
expect(arr_evts.length).toBe(1);
|
|
109
|
-
expect(arr_evts[0].evt.type).toBe('EVENT3');
|
|
110
|
-
// expect(recent[1].evt.type).toBe('EVENT2');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
/*
|
|
114
|
-
it('should exclude events outside lookback period', () => {
|
|
115
|
-
eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
|
|
116
|
-
jasmine.clock().tick(2000); // Advance 2 seconds
|
|
117
|
-
eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,002,000
|
|
118
|
-
|
|
119
|
-
const recent = eventLog.getRecentEvents('msg1', 1000); // Look back 1s from t=1,002,000
|
|
120
|
-
expect(recent.length).toBe(1);
|
|
121
|
-
expect(recent[0].type).toBe('EVENT2');
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('should return empty array if no matching events', () => {
|
|
125
|
-
eventLog.addEvent('EVENT1', 'msg2', { data: 1 });
|
|
126
|
-
const recent = eventLog.getRecentEvents('msg1', 1000);
|
|
127
|
-
expect(recent).toEqual([]);
|
|
128
|
-
});
|
|
129
|
-
*/
|
|
130
|
-
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
describe('addEvent = timeouts;', () => {
|
|
134
|
-
let eventLog;
|
|
135
|
-
|
|
136
|
-
beforeEach(() => {
|
|
137
|
-
eventLog = new EventLog();
|
|
138
|
-
jasmine.clock().install(); // Mock the clock for timeout testing
|
|
139
|
-
jasmine.clock().mockDate(new Date(1000000)); // Fixed base time: 1,000,000 ms
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
afterEach(() => {
|
|
143
|
-
jasmine.clock().uninstall();
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('should add event without timeout', () => {
|
|
147
|
-
const { idx, evt } = eventLog.addEvent('test', 'id1', { value: 1 });
|
|
148
|
-
expect(eventLog.arr_events.length).toBe(1);
|
|
149
|
-
expect(evt.type).toBe('test');
|
|
150
|
-
expect(evt.id).toBe('id1');
|
|
151
|
-
expect(evt.data).toEqual({ value: 1 });
|
|
152
|
-
expect(evt.timeout_id).toBeNull();
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('should add event with timeout', () => {
|
|
156
|
-
const timeout_callback = jasmine.createSpy('timeout_callback');
|
|
157
|
-
const { idx, evt } = eventLog.addEvent('test', 'id1', { value: 1 }, 5000, timeout_callback);
|
|
158
|
-
|
|
159
|
-
expect(eventLog.arr_events.length).toBe(1);
|
|
160
|
-
expect(evt.timeout_id).not.toBeNull();
|
|
161
|
-
expect(eventLog.map_timeouts.size).toBe(1);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
it('should trigger timeout callback after specified time', () => {
|
|
166
|
-
const timeout_callback = jasmine.createSpy('timeout_callback');
|
|
167
|
-
const { idx, evt } = eventLog.addEvent('test', 'id1', { value: 1 }, 5000, timeout_callback);
|
|
168
|
-
|
|
169
|
-
jasmine.clock().tick(5001);
|
|
170
|
-
|
|
171
|
-
expect(timeout_callback).toHaveBeenCalledWith({ idx, evt});
|
|
172
|
-
expect(eventLog.map_timeouts.size).toBe(0); // Timeout should be cleaned up
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('should trigger all timeouts after specified time', () => {
|
|
176
|
-
LOGR_.toggle(l_, {
|
|
177
|
-
EVENTS : true
|
|
178
|
-
});
|
|
179
|
-
const cb1 = jasmine.createSpy('callback1');
|
|
180
|
-
const cb2 = jasmine.createSpy('callback2');
|
|
181
|
-
eventLog.addEvent('test1', 'id1', { value: 1 }, 5000, cb1);
|
|
182
|
-
eventLog.addEvent('test2', 'id2', { value: 2 }, 3000, cb2);
|
|
183
|
-
|
|
184
|
-
jasmine.clock().tick(2999);
|
|
185
|
-
expect(cb1).not.toHaveBeenCalled();
|
|
186
|
-
expect(cb2).not.toHaveBeenCalled();
|
|
187
|
-
|
|
188
|
-
jasmine.clock().tick(2); // 3001
|
|
189
|
-
expect(cb1).not.toHaveBeenCalled();
|
|
190
|
-
expect(cb2).toHaveBeenCalled();
|
|
191
|
-
expect(eventLog.map_timeouts.size).toBe(1);
|
|
192
|
-
|
|
193
|
-
jasmine.clock().tick(1998); // 4999
|
|
194
|
-
expect(cb2).toHaveBeenCalled();
|
|
195
|
-
|
|
196
|
-
jasmine.clock().tick(2000); // 5001
|
|
197
|
-
expect(cb1).toHaveBeenCalled();
|
|
198
|
-
expect(cb2).toHaveBeenCalled();
|
|
199
|
-
expect(eventLog.map_timeouts.size).toBe(0);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it('should cancel all timeouts', () => {
|
|
203
|
-
const cb1 = jasmine.createSpy('callback1');
|
|
204
|
-
const cb2 = jasmine.createSpy('callback2');
|
|
205
|
-
eventLog.addEvent('test1', 'id1', { value: 1 }, 5000, cb1);
|
|
206
|
-
eventLog.addEvent('test2', 'id2', { value: 2 }, 5000, cb2);
|
|
207
|
-
|
|
208
|
-
eventLog.cancelAllTimeouts();
|
|
209
|
-
jasmine.clock().tick(5001);
|
|
210
|
-
|
|
211
|
-
expect(cb1).not.toHaveBeenCalled();
|
|
212
|
-
expect(cb2).not.toHaveBeenCalled();
|
|
213
|
-
expect(eventLog.map_timeouts.size).toBe(0);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
/*
|
|
219
|
-
describe('clear', () => {
|
|
220
|
-
it('should remove all events from the log', () => {
|
|
221
|
-
eventLog.addEvent('EVENT1', 'msg1', { data: 1 });
|
|
222
|
-
eventLog.addEvent('EVENT2', 'msg1', { data: 2 });
|
|
223
|
-
expect(eventLog.events.length).toBe(2);
|
|
224
|
-
|
|
225
|
-
eventLog.clear();
|
|
226
|
-
expect(eventLog.events.length).toBe(0);
|
|
227
|
-
expect(eventLog.events).toEqual([]);
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
it('should not affect logr configuration', () => {
|
|
231
|
-
const mockLogrConfig = { enabled: true };
|
|
232
|
-
eventLog.setLogr(mockLogrConfig);
|
|
233
|
-
eventLog.addEvent('EVENT1', 'msg1', {});
|
|
234
|
-
eventLog.clear();
|
|
235
|
-
expect(eventLog.logr.toggled).toBe(mockLogrConfig);
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
*/
|
|
239
|
-
});
|