hermes-io 2.2.35 → 2.2.37
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/lib/constants.js +1 -9
- package/lib/context/context.js +1 -101
- package/lib/hooks/index.js +1 -1
- package/lib/hooks/useObserver.js +1 -26
- package/lib/index.js +1 -3
- package/lib/observer/observer.js +1 -16
- package/lib/setupTests.js +1 -26
- package/package.json +1 -1
package/lib/constants.js
CHANGED
|
@@ -1,9 +1 @@
|
|
|
1
|
-
export default
|
|
2
|
-
CHROME_EXTENSION: "hermes-io-devtools",
|
|
3
|
-
CONTEXT_SNAPSHOT: "CONTEXT_SNAPSHOT",
|
|
4
|
-
START_RECORDING: "START_RECORDING",
|
|
5
|
-
STOP_RECORDING: "STOP_RECORDING",
|
|
6
|
-
SET_CONTEXT: "SET_CONTEXT",
|
|
7
|
-
LOAD_RECORDING: "LOAD_RECORDING",
|
|
8
|
-
RESET_RECORDING: "RESET_RECORDING"
|
|
9
|
-
};
|
|
1
|
+
export default{CHROME_EXTENSION:"hermes-io-devtools",CONTEXT_SNAPSHOT:"CONTEXT_SNAPSHOT",START_RECORDING:"START_RECORDING",STOP_RECORDING:"STOP_RECORDING",SET_CONTEXT:"SET_CONTEXT",LOAD_RECORDING:"LOAD_RECORDING",RESET_RECORDING:"RESET_RECORDING"};
|
package/lib/context/context.js
CHANGED
|
@@ -1,101 +1 @@
|
|
|
1
|
-
|
|
2
|
-
let e = false;
|
|
3
|
-
let s = [];
|
|
4
|
-
export const listenersMap = new Map();
|
|
5
|
-
function n() {
|
|
6
|
-
e = true;
|
|
7
|
-
}
|
|
8
|
-
function i() {
|
|
9
|
-
e = false;
|
|
10
|
-
}
|
|
11
|
-
function a() {
|
|
12
|
-
s = [];
|
|
13
|
-
e = false;
|
|
14
|
-
}
|
|
15
|
-
function r({ id: e = "" }) {
|
|
16
|
-
const n = s.find((t)=>t._internalId === e);
|
|
17
|
-
if (n) {
|
|
18
|
-
if (n.isFromExternalRecording) {
|
|
19
|
-
const t = listenersMap.get(n.listener);
|
|
20
|
-
return t?.({
|
|
21
|
-
value: JSON.parse(n.value)
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
return n.listener(n.value);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function l(e) {
|
|
28
|
-
s = e.recording.map((t = {})=>({
|
|
29
|
-
...t,
|
|
30
|
-
isFromExternalRecording: true,
|
|
31
|
-
_internalId: t.id
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
34
|
-
const o = {
|
|
35
|
-
[t.START_RECORDING]: n,
|
|
36
|
-
[t.STOP_RECORDING]: i,
|
|
37
|
-
[t.RESET_RECORDING]: a,
|
|
38
|
-
[t.SET_CONTEXT]: r,
|
|
39
|
-
[t.LOAD_RECORDING]: l
|
|
40
|
-
};
|
|
41
|
-
const c = (e)=>{
|
|
42
|
-
try {
|
|
43
|
-
const { source: s, payload: n } = e.data;
|
|
44
|
-
if (s === t.CHROME_EXTENSION) {
|
|
45
|
-
if (n?.type) return o[n.type](n);
|
|
46
|
-
o[n]();
|
|
47
|
-
}
|
|
48
|
-
} catch (t) {
|
|
49
|
-
console.error(t);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
window.addEventListener("message", c);
|
|
53
|
-
export class Context {
|
|
54
|
-
constructor(n){
|
|
55
|
-
this.id = null;
|
|
56
|
-
this._internalId = null;
|
|
57
|
-
this.date = null;
|
|
58
|
-
this.value = null;
|
|
59
|
-
this.listener = null;
|
|
60
|
-
this.stackTrace = null;
|
|
61
|
-
this.update = ({ value: t, listener: s })=>{
|
|
62
|
-
this.date = new Date();
|
|
63
|
-
this.value = t;
|
|
64
|
-
this.stackTrace = this.getStackTrace();
|
|
65
|
-
this.listener = s;
|
|
66
|
-
if (e) {
|
|
67
|
-
this.sendSnapshot();
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
this.sendSnapshot = ()=>{
|
|
71
|
-
const e = this.takeSnapshot();
|
|
72
|
-
const { listener: n, stackTrace: i, value: a, date: r, _internalId: l } = e;
|
|
73
|
-
s.push(e);
|
|
74
|
-
window.postMessage({
|
|
75
|
-
type: t.CONTEXT_SNAPSHOT,
|
|
76
|
-
payload: {
|
|
77
|
-
value: JSON.stringify(a.value),
|
|
78
|
-
listener: n.name,
|
|
79
|
-
stackTrace: i,
|
|
80
|
-
date: r,
|
|
81
|
-
id: l
|
|
82
|
-
},
|
|
83
|
-
source: "hermes-io"
|
|
84
|
-
}, "*");
|
|
85
|
-
};
|
|
86
|
-
this.takeSnapshot = ()=>{
|
|
87
|
-
return {
|
|
88
|
-
_internalId: crypto.randomUUID(),
|
|
89
|
-
value: this.value,
|
|
90
|
-
date: this.date,
|
|
91
|
-
listener: this.listener,
|
|
92
|
-
stackTrace: this.stackTrace
|
|
93
|
-
};
|
|
94
|
-
};
|
|
95
|
-
this.getStackTrace = ()=>{
|
|
96
|
-
const t = new Error();
|
|
97
|
-
return t.stack;
|
|
98
|
-
};
|
|
99
|
-
this.id = Symbol(n);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
1
|
+
function _extends(){return(_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}import e from"../constants";var _obj,recording=!1,collection=[];export var listenersMap=new Map;var actions=((_obj={})[e.START_RECORDING]=function(){recording=!0},_obj[e.STOP_RECORDING]=function(){recording=!1},_obj[e.RESET_RECORDING]=function(){collection=[],recording=!1},_obj[e.SET_CONTEXT]=function(t){var n=t.id,r=void 0===n?"":n,a=collection.find(function(e){return e._internalId===r});if(a){if(a.isFromExternalRecording){var i=listenersMap.get(a.listener);return null==i?void 0:i({value:JSON.parse(a.value)})}return a.listener(a.value)}},_obj[e.LOAD_RECORDING]=function(t){collection=t.recording.map(function(e){return void 0===e&&(e={}),_extends({},e,{isFromExternalRecording:!0,_internalId:e.id})})},_obj);window.addEventListener("message",function(t){try{var n=t.data,r=n.source,a=n.payload;if(r===e.CHROME_EXTENSION){if(null==a?void 0:a.type)return actions[a.type](a);actions[a]()}}catch(e){console.error(e)}});export var Context=function(t){var n=this;this.id=null,this._internalId=null,this.date=null,this.value=null,this.listener=null,this.stackTrace=null,this.update=function(e){var t=e.value,r=e.listener;n.date=new Date,n.value=t,n.stackTrace=n.getStackTrace(),n.listener=r,recording&&n.sendSnapshot()},this.sendSnapshot=function(){var t=n.takeSnapshot(),r=t.listener,a=t.stackTrace,i=t.value,o=t.date,s=t._internalId;collection.push(t),window.postMessage({type:e.CONTEXT_SNAPSHOT,payload:{value:JSON.stringify(i.value),listener:r.name,stackTrace:a,date:o,id:s},source:"hermes-io"},"*")},this.takeSnapshot=function(){return{_internalId:crypto.randomUUID(),value:n.value,date:n.date,listener:n.listener,stackTrace:n.stackTrace}},this.getStackTrace=function(){return Error().stack},this.id=Symbol(t)};
|
package/lib/hooks/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export*from"./useObserver";
|
package/lib/hooks/useObserver.js
CHANGED
|
@@ -1,26 +1 @@
|
|
|
1
|
-
import e, {
|
|
2
|
-
import { listenersMap as n } from "../context/context";
|
|
3
|
-
export const useObserver = (r)=>{
|
|
4
|
-
t(()=>{
|
|
5
|
-
const { observer: e, listener: t, contexts: o = [] } = r || {};
|
|
6
|
-
function s(e, n) {
|
|
7
|
-
const r = o.length !== 0;
|
|
8
|
-
const s = r && o.find((t)=>t.id === e?.context?.id);
|
|
9
|
-
if (s) {
|
|
10
|
-
e?.context?.update({
|
|
11
|
-
value: e,
|
|
12
|
-
listener: t
|
|
13
|
-
});
|
|
14
|
-
t?.(e, n);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
e?.subscribe?.(s);
|
|
18
|
-
n.set(r.listener.name, r.listener);
|
|
19
|
-
return ()=>{
|
|
20
|
-
n.delete(r.listener.name);
|
|
21
|
-
e?.unsubscribe?.(s);
|
|
22
|
-
};
|
|
23
|
-
}, [
|
|
24
|
-
r
|
|
25
|
-
]);
|
|
26
|
-
};
|
|
1
|
+
import{useEffect as n}from"react";import{listenersMap as e}from"../context/context";export var useObserver=function(l){n(function(){var n,t=function(n,e){var l;0!==c.length&&c.find(function(e){var l;return e.id===(null==n?void 0:null==(l=n.context)?void 0:l.id)})&&(null==n||null==(l=n.context)||l.update({value:n,listener:i}),null==i||i(n,e))},r=l||{},u=r.observer,i=r.listener,o=r.contexts,c=void 0===o?[]:o;return null==u||null==(n=u.subscribe)||n.call(u,t),e.set(l.listener.name,l.listener),function(){var n;e.delete(l.listener.name),null==u||null==(n=u.unsubscribe)||n.call(u,t)}},[l])};
|
package/lib/index.js
CHANGED
package/lib/observer/observer.js
CHANGED
|
@@ -1,16 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
subscribe(s) {
|
|
3
|
-
this.subscriptors.push(s);
|
|
4
|
-
}
|
|
5
|
-
unsubscribe(s) {
|
|
6
|
-
this.subscriptors.splice(this.subscriptors.findIndex((r)=>r === s), 1);
|
|
7
|
-
}
|
|
8
|
-
notify(s = {}) {
|
|
9
|
-
return new Promise((r)=>{
|
|
10
|
-
this.subscriptors.forEach((i)=>i(s, r));
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
constructor(){
|
|
14
|
-
this.subscriptors = [];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
1
|
+
export var Observer=function(){function r(){this.subscriptors=[]}var s=r.prototype;return s.subscribe=function(r){this.subscriptors.push(r)},s.unsubscribe=function(r){this.subscriptors.splice(this.subscriptors.findIndex(function(s){return s===r}),1)},s.notify=function(r){void 0===r&&(r={});var s=this;return new Promise(function(t){s.subscriptors.forEach(function(s){return s(r,t)})})},r}();
|
package/lib/setupTests.js
CHANGED
|
@@ -1,26 +1 @@
|
|
|
1
|
-
require("@testing-library/jest-dom");
|
|
2
|
-
jest.setTimeout(6000 * 10);
|
|
3
|
-
Object.defineProperty(globalThis, "crypto", {
|
|
4
|
-
value: {
|
|
5
|
-
randomUUID: ()=>{
|
|
6
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(e) {
|
|
7
|
-
var x = Math.random() * 16 | 0, t = e === "x" ? x : x & 0x3 | 0x8;
|
|
8
|
-
return t.toString(16);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
window.SVGPathElement = jest.fn();
|
|
14
|
-
global.navigator = {
|
|
15
|
-
userAgent: "node"
|
|
16
|
-
};
|
|
17
|
-
window.navigator = {
|
|
18
|
-
userAgent: "node"
|
|
19
|
-
};
|
|
20
|
-
window.matchMedia = window.matchMedia || function() {
|
|
21
|
-
return {
|
|
22
|
-
matches: false,
|
|
23
|
-
addListener: function() {},
|
|
24
|
-
removeListener: function() {}
|
|
25
|
-
};
|
|
26
|
-
};
|
|
1
|
+
require("@testing-library/jest-dom"),jest.setTimeout(6e4),Object.defineProperty(globalThis,"crypto",{value:{randomUUID:function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var n=16*Math.random()|0;return("x"===e?n:3&n|8).toString(16)})}}}),window.SVGPathElement=jest.fn(),global.navigator={userAgent:"node"},window.navigator={userAgent:"node"},window.matchMedia=window.matchMedia||function(){return{matches:!1,addListener:function(){},removeListener:function(){}}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hermes-io",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.37",
|
|
4
4
|
"description": "A lightweight javascript library that allows communication between Reactjs components by using the observer pattern and the hook api",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib/index.js",
|