hermes-io 2.2.32 → 2.2.35

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 CHANGED
@@ -1 +1,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"};
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 +1,101 @@
1
- import CONSTANTS from"../constants";let recording=false;let collection=[];export const listenersMap=new Map;function startRecording(){recording=true}function stopRecording(){recording=false}function resetRecording(){collection=[];recording=false}function setContext({id=""}){const context=collection.find(context=>context._internalId===id);if(context){if(context.isFromExternalRecording){const listener=listenersMap.get(context.listener);return listener?.({value:JSON.parse(context.value)})}return context.listener(context.value)}}function loadRecording(payload){collection=payload.recording.map((item={})=>({...item,isFromExternalRecording:true,_internalId:item.id}))}const actions={[CONSTANTS.START_RECORDING]:startRecording,[CONSTANTS.STOP_RECORDING]:stopRecording,[CONSTANTS.RESET_RECORDING]:resetRecording,[CONSTANTS.SET_CONTEXT]:setContext,[CONSTANTS.LOAD_RECORDING]:loadRecording};const handleMessageFromDevtools=event=>{try{const{source,payload}=event.data;if(source===CONSTANTS.CHROME_EXTENSION){if(payload?.type)return actions[payload.type](payload);actions[payload]()}}catch(error){console.error(error)}};window.addEventListener("message",handleMessageFromDevtools);export class Context{constructor(description){this.id=null;this._internalId=null;this.date=null;this.value=null;this.listener=null;this.stackTrace=null;this.update=({value,listener})=>{this.date=new Date;this.value=value;this.stackTrace=this.getStackTrace();this.listener=listener;if(recording){this.sendSnapshot()}};this.sendSnapshot=()=>{const snapshot=this.takeSnapshot();const{listener,stackTrace,value,date,_internalId}=snapshot;collection.push(snapshot);window.postMessage({type:CONSTANTS.CONTEXT_SNAPSHOT,payload:{value:JSON.stringify(value.value),listener:listener.name,stackTrace,date,id:_internalId},source:"hermes-io"},"*")};this.takeSnapshot=()=>{return{_internalId:crypto.randomUUID(),value:this.value,date:this.date,listener:this.listener,stackTrace:this.stackTrace}};this.getStackTrace=()=>{const err=new Error;return err.stack};this.id=Symbol(description)}}
1
+ import t from "../constants";
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 +1 @@
1
- export*from"./useObserver";
1
+ export * from './useObserver';
@@ -1 +1,26 @@
1
- import React,{useEffect}from"react";import{listenersMap}from"../context/context";export const useObserver=props=>{useEffect(()=>{const{observer,listener,contexts=[]}=props||{};function subscriber(payload,resolve){const hasfromList=contexts.length!==0;const hasValidList=hasfromList&&contexts.find(ctx=>ctx.id===payload?.context?.id);if(hasValidList){payload?.context?.update({value:payload,listener});listener?.(payload,resolve)}}observer?.subscribe?.(subscriber);listenersMap.set(props.listener.name,props.listener);return()=>{listenersMap.delete(props.listener.name);observer?.unsubscribe?.(subscriber)}},[props])};
1
+ import e, { useEffect as t } from "react";
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
+ };
package/lib/index.js CHANGED
@@ -1 +1,3 @@
1
- export*from"./hooks";export*from"./observer/observer";export*from"./context/context";
1
+ export * from "./hooks";
2
+ export * from "./observer/observer";
3
+ export * from "./context/context";
@@ -1 +1,16 @@
1
- export class Observer{subscribe(callback){this.subscriptors.push(callback)}unsubscribe(callback){this.subscriptors.splice(this.subscriptors.findIndex(cb=>cb===callback),1)}notify(args={}){return new Promise(resolve=>{this.subscriptors.forEach(callback=>callback(args,resolve))})}constructor(){this.subscriptors=[]}}
1
+ export class Observer {
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
+ }
package/lib/setupTests.js CHANGED
@@ -1 +1,26 @@
1
- require("@testing-library/jest-dom");jest.setTimeout(6e3*10);Object.defineProperty(globalThis,"crypto",{value:{randomUUID:()=>{return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var r=Math.random()*16|0,v=c==="x"?r:r&3|8;return v.toString(16)})}}});window.SVGPathElement=jest.fn();global.navigator={userAgent:"node"};window.navigator={userAgent:"node"};window.matchMedia=window.matchMedia||function(){return{matches:false,addListener:function(){},removeListener:function(){}}};
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-io",
3
- "version": "2.2.32",
3
+ "version": "2.2.35",
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",
@@ -1 +0,0 @@
1
- import{Context}from"./Context";describe("Context",()=>{let context;beforeEach(()=>{context=new Context("Test Description")});afterEach(()=>{context=null});test("update method updates context properties",()=>{const value="test value";const listener=jest.fn();context.update({value,listener});expect(context.date).toBeInstanceOf(Date);expect(context.value).toBe(value);expect(context.listener).toBe(listener);expect(context.stackTrace).toEqual(expect.any(String))});test("takeSnapshot method returns a snapshot object",()=>{const snapshot=context.takeSnapshot();expect(snapshot._internalId).toEqual(expect.any(String));expect(snapshot.value).toBe(context.value);expect(snapshot.date).toBe(context.date);expect(snapshot.listener).toBe(context.listener);expect(snapshot.stackTrace).toBe(context.stackTrace)});test("getStackTrace method returns stack trace",()=>{const stackTrace=context.getStackTrace();expect(stackTrace).toEqual(expect.any(String))})});
@@ -1 +0,0 @@
1
- import React from"react";import{renderHook,act}from"@testing-library/react-hooks/dom";import{Context}from"../context/context";import{useObserver}from"./useObserver";import{listenersMap}from"../context/context";const mockObserver={subscribe:jest.fn(),unsubscribe:jest.fn()};describe("useObserver",()=>{let context;beforeEach(()=>{context=new Context("Test")});afterEach(()=>{listenersMap.clear();jest.clearAllMocks();context=null});it("should subscribe and unsubscribe to observer",async()=>{const observer=mockObserver;const listener=jest.fn();const contexts=[context];const props={observer,listener,contexts};let hook;await act(()=>{hook=renderHook(()=>useObserver(props))});const{unmount}=hook;expect(observer.subscribe).toHaveBeenCalledTimes(1);expect(observer.subscribe).toHaveBeenCalledWith(expect.any(Function));act(()=>{unmount()});expect(observer.unsubscribe).toHaveBeenCalledTimes(1);expect(observer.unsubscribe).toHaveBeenCalledWith(expect.any(Function))});it("should add and remove listener from listenersMap",async()=>{const observer=mockObserver;const listener=jest.fn();const props={observer,listener,contexts:[context]};await act(()=>{renderHook(()=>useObserver(props))});expect(listenersMap.get(listener.name)).toBe(listener);act(()=>{listenersMap.clear()});expect(listenersMap.size).toBe(0)});it("should call listener and context update when payload matches context id",async()=>{const observer=mockObserver;const listener=jest.fn();const context={id:"contextId",update:jest.fn()};const payload={context};const props={observer,listener,contexts:[context]};await act(()=>{renderHook(()=>useObserver(props))});const subscribeCallback=observer.subscribe.mock.calls[0][0];const resolve=jest.fn();act(()=>{subscribeCallback(payload,resolve)});expect(context.update).toHaveBeenCalledTimes(1);expect(context.update).toHaveBeenCalledWith({value:payload,listener});expect(listener).toHaveBeenCalledTimes(1);expect(listener).toHaveBeenCalledWith(payload,resolve)})});
@@ -1 +0,0 @@
1
- import{Observer}from"./observer";describe("Observer",()=>{let observer;let callback1;let callback2;beforeEach(()=>{observer=new Observer;callback1=jest.fn();callback2=jest.fn()});afterEach(()=>{observer=null;callback1=null;callback2=null});it("subscribe and notify",()=>{observer.subscribe(callback1);observer.subscribe(callback2);const args={data:"test"};observer.notify(args);expect(callback1).toHaveBeenCalledWith(args,expect.any(Function));expect(callback2).toHaveBeenCalledWith(args,expect.any(Function))});it("unsubscribe",async()=>{observer.subscribe(callback1);observer.unsubscribe(callback1);expect(callback1).not.toHaveBeenCalled()})});