Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 2x 2x 2x 2x 2x 11x 1x 10x 10x 10x 10x 10x 10x 2x 2x 2x 2x 1x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 6x 9x 9x 9x 2x 12x 12x 12x 12x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 4x 4x 4x 4x 4x 2x 4x 10x 2x 2x 2x 17x 17x 2x 1x 1x 1x 1x 2x 5x 1x 4x 3x 3x 1x 2x 3x 5x 5x 3x 3x 2x 5x 2x 4x 2x 11x | import {DevToolsEventData,DevToolsExtension,DevToolsInstance} from "./rdtTypes";
import { RootState } from "./RootState";
import { DataElement, DataElementCtor, DataElementMetaData } from "./DataElement";
import { StateChange, StateChangeMetaData } from "@domx/statechange";
export { applyDataElementRdtLogging }
/**
* Redux Dev Tools Middleware
*
* Docs:
* https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/API/Methods.md
*
* Set logChangeEvents to false if using StateChange and
* do not want the duplicate state entry.
* @param options {RdtLoggingOptions}
*/
const applyDataElementRdtLogging = (options:RdtLoggingOptions = {logChangeEvents:true, exclude:[]}) => {
if (isApplied || hasDevTools() === false) {
return;
}
isApplied = true;
logChangeEvents = options.logChangeEvents ? true : false;
excludeActions = options.exclude ? excludeActions.concat(options.exclude) : excludeActions;
DataElement.applyMiddlware(connectedCallback, disconnectedCallback);
StateChange.applyNextMiddleware(stateChangeNext);
window.addEventListener("rootstate-snapshot", sendSnapshot as EventListener);
};
let isApplied = false;
let logChangeEvents = true;
let excludeActions:Array<string> = ["domx-"];
interface RdtLoggingOptions {
logChangeEvents?: boolean,
exclude?: Array<string>
}
/**
* Used to keep track of all the connected elements in the DOM
* so we can update their state from dev tools
* The key is the statePath.
**/
interface ConnectedElements {
[key:string]: Array<ConnectedDataElement>
}
interface ConnectedDataElement {
element: DataElement,
property: string,
changeEvent: string
};
const sendSnapshot = (event:CustomEvent) => {
getDevToolsInstance().send(event.detail.name, event.detail.state);
};
const connectedElements:ConnectedElements = {};
const connectedCallback = (metaData:DataElementMetaData) => (next:Function) => () => {
const el = metaData.element;
Object.keys(metaData.dataProperties).forEach((propertyName) => {
const dp = metaData.dataProperties[propertyName];
const { statePath, changeEvent } = dp;
// update the connected elements
connectedElements[statePath] = connectedElements[statePath] || [];
connectedElements[statePath].push({
element: el,
changeEvent,
property: propertyName
});
sendStateToDevTools(el, propertyName, statePath, changeEvent);
const rdtListener = ((event:CustomEvent) => {
!event.detail?.isFromDevTools &&
sendStateToDevTools(el, propertyName, statePath, changeEvent);
}) as EventListener;
logChangeEvents && el.addEventListener(changeEvent, rdtListener);
//@ts-ignore TS2339 dynamic property
dp.rdtListener = rdtListener;
});
next();
};
const sendStateToDevTools = (el:DataElement, propertyName:string, statePath:string, changeEvent:string) => {
// @ts-ignore TS7053 getting indexed property
const nextState = el[propertyName] as object;
const action = `${(el.constructor as DataElementCtor).__elementName}@${changeEvent}`;
if (!excludeActions.find(a => action.indexOf(a) === 0)) {
getDevToolsInstance().send(action, RootState.draft(statePath, nextState));
}
};
const disconnectedCallback = (metaData:DataElementMetaData) => (next:Function) => () => {
const el = metaData.element;
Object.keys(metaData.dataProperties).forEach((propertyName) => {
// update the connected elements
const dp = metaData.dataProperties[propertyName];
const {statePath, changeEvent} = dp;
const elIndex = connectedElements[statePath].findIndex(cde => cde.element === el);
connectedElements[statePath].splice(elIndex, 1);
//@ts-ignore TS2339 dynamic property
el.removeEventListener(changeEvent, dp.rdtLitener);
//@ts-ignore TS2339 dynamic property
delete dp.rdtListener;
});
next();
};
const stateChangeNext = (stateChange:StateChange) => (next:Function) => (state:any) => {
const result = next(state);
const meta = stateChange.meta;
const dpmd = (meta.el as DataElement).__dataPropertyMetaData;
const statePath = dpmd[meta.property].statePath;
getDevToolsInstance().send(getFnName(meta), RootState.draft(statePath, result));
return result;
};
const getFnName = ({className, tapName, nextName}:StateChangeMetaData) =>
`${className}.${tapName ? `${tapName}(${nextName})` : `${nextName}()`}`;
/**
* True if the redux dev tools extension is installed
* @returns {boolean}
*/
const hasDevTools = ():boolean => (window as any).__REDUX_DEVTOOLS_EXTENSION__ !== undefined;
/**
* Returns the redux dev tools extension
* @returns {DevToolsExtension}
*/
const getDevTools = ():DevToolsExtension => (window as any).__REDUX_DEVTOOLS_EXTENSION__;
/**
* Pulls the connected dev tools instance from the HTML Element.
* Creates it if it does not exist.
* @param stateChange {StateChange}
* @returns {DevToolsInstance}
*/
let __rdt:DevToolsInstance|null = null;
const getDevToolsInstance = ():DevToolsInstance => {
__rdt = __rdt || setupDevToolsInstance();
return __rdt as DevToolsInstance;
};
/**
* Creates the dev tools istance and sets up the
* listener for dev tools interactions.
* @returns DevToolsInstance
*/
const setupDevToolsInstance = ():DevToolsInstance => {
const dt = getDevTools().connect();
dt.init(RootState.current);
dt.subscribe(updateFromDevTools(dt));
return dt;
};
const updateFromDevTools = (dt: DevToolsInstance) => (data:DevToolsEventData) => {
if (isInit(data)) {
return;
}
if (canHandleUpdate(data)) {
RootState.init(JSON.parse(data.state));
updateConnectedElements();
} else {
dt.error(`DataElement RDT logging does not support payload type: ${data.type}:${data.payload.type}`);
}
};
/**
* Loops through connected elements and updates
* their state properties and dispatches the sync change
*/
const updateConnectedElements = () => {
Object.keys(connectedElements).forEach((statePath:string) => {
const stateAtPath = RootState.get(statePath);
connectedElements[statePath].forEach(({ property, changeEvent, element }) => {
// @ts-ignore TS7053 setting indexed property
element[property] = stateAtPath;
element.dispatchEvent(new CustomEvent(changeEvent, {
detail: {isSyncUpdate: true, isFromDevTools: true}
}));
});
});
};
/**
* Returns true if the listener data is for initializing dev tools state.
* @param data {DevToolsEventData}
* @returns {boolean}
*/
const isInit = (data:DevToolsEventData):boolean =>
data.type === "START" || data.payload.type === "IMPORT_STATE";
/**
* Returns true if this middleware can handle the listener data.
* @param data {DevToolsEventData}
* @returns {boolean}
*/
const canHandleUpdate = (data:DevToolsEventData):boolean =>
data.type === "DISPATCH" && (
data.payload.type === "JUMP_TO_ACTION" ||
data.payload.type === "JUMP_TO_STATE"
);
/**
* Exposed for testing
*/
applyDataElementRdtLogging.reset = () => {
isApplied = false;
}; |