hermes-io 2.2.36 → 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.
@@ -1 +1 @@
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
+ 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)};
@@ -1 +1 @@
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{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])};
@@ -1 +1 @@
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 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 +1 @@
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"),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.36",
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",