-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
271 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/act-master/src/vue/plugins/__tests__/devtools.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
packages/act-master/src/vue/plugins/lib/monkey-watch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { CustomInspectorNode, StateBase } from '@vue/devtools-api'; | ||
import { getArguments, getCurrentTime } from './utils'; | ||
import { type ActMaster, type ActMasterAction } from '../../..'; | ||
|
||
const eventMonkeyState = new Map<string, StateBase[]>(); | ||
const eventMonkeyStateSort = new Set<string>(); | ||
const config = { | ||
isShowOnlyCalls: false, | ||
}; | ||
|
||
export function watchOnEventsByMonkey(actMaster: ActMaster) { | ||
// @ts-ignore | ||
if (actMaster._old_emit) { | ||
return; | ||
} | ||
|
||
//@ts-ignore | ||
actMaster._old_emit = actMaster.emit.bind(actMaster); | ||
|
||
//@ts-ignore | ||
actMaster.emit = (...args: any[]) => { | ||
const [name, ...props] = args; | ||
const time = getCurrentTime(); | ||
|
||
addStateByMonkey(name, { | ||
//@ts-ignore | ||
_isArgument: true, // path to define argument | ||
key: `Arguments ${time}`, | ||
value: props, | ||
}); | ||
|
||
//@ts-ignore | ||
return actMaster._old_emit(...args).then((result) => { | ||
addStateByMonkey(name, { | ||
key: `Result ${time}`, | ||
value: result, | ||
}); | ||
|
||
return result; | ||
}); | ||
}; | ||
} | ||
|
||
export function addStateByMonkey(name: string, state: StateBase): void { | ||
let items: StateBase[] = eventMonkeyState.get(name) || []; | ||
|
||
if (items.length > 20) { | ||
items = items.slice(-10); | ||
} | ||
|
||
items.push(state); | ||
eventMonkeyState.set(name, items); | ||
if (eventMonkeyStateSort.has(name)) { | ||
eventMonkeyStateSort.delete(name); | ||
} | ||
eventMonkeyStateSort.add(name); | ||
} | ||
|
||
export function getActEventsByMonkeyWatcher( | ||
actName: string, | ||
act: ActMasterAction | null | ||
): StateBase[] { | ||
const list = eventMonkeyState.get(actName) || []; | ||
|
||
const argumentNames = getArguments(act?.exec); | ||
|
||
return list.map((stateBase) => { | ||
//@ts-ignore | ||
if (!stateBase._isArgument) { | ||
return stateBase; | ||
} | ||
|
||
let value: Record<string, any> = {}; | ||
|
||
stateBase.value.forEach((v: any, i: number) => { | ||
value[argumentNames[i]] = v; | ||
}); | ||
|
||
return { | ||
...stateBase, | ||
value, | ||
}; | ||
}); | ||
} | ||
|
||
export function hasMonkeyState(name: string): boolean { | ||
return eventMonkeyState.has(name); | ||
} | ||
|
||
export function resetMonkeyWatcherState() { | ||
eventMonkeyState.clear(); | ||
eventMonkeyStateSort.clear(); | ||
} | ||
|
||
// | ||
export function isShowActionByConfig(name: string): boolean { | ||
if (config.isShowOnlyCalls) { | ||
return hasMonkeyState(name); | ||
} | ||
return true; | ||
} | ||
|
||
export function toggleSettingsShowCall() { | ||
return (config.isShowOnlyCalls = !config.isShowOnlyCalls); | ||
} | ||
|
||
export function useSettings(conf: typeof config | any) { | ||
Object.assign(config, conf || {}); | ||
return config; | ||
} | ||
|
||
export function sortActInspectorTree(list: CustomInspectorNode[]) { | ||
if (config.isShowOnlyCalls) { | ||
const orderNames = [...eventMonkeyStateSort]; | ||
const orderIndexes = new Map<string, number>( | ||
orderNames.map((item, index) => [item, index]) | ||
); | ||
|
||
return list.sort((a, b) => { | ||
const indexOfA = orderIndexes.get(a.label) || -1; | ||
const indexOfB = orderNames.indexOf(b.label) || -1; | ||
|
||
return indexOfA - indexOfB; | ||
}); | ||
} | ||
|
||
return list.sort((a, b) => (a.id > b.id ? 1 : -1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export function getArguments(fn?: Function): string[] { | ||
const argRegExp = /[^\(]*\(([^\)]*)\)/; | ||
const match = fn?.toString().match(argRegExp); | ||
|
||
if (match && match[1]) { | ||
return match[1] | ||
.split(',') | ||
.map((arg) => arg.trim()) | ||
.filter((arg) => arg.length > 0); | ||
} | ||
return []; | ||
} | ||
|
||
export function getCurrentTime() { | ||
const now = new Date(); | ||
return `${now.getHours().toString().padStart(2, '0')}:${now | ||
.getMinutes() | ||
.toString() | ||
.padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}.${now | ||
.getMilliseconds() | ||
.toString() | ||
.padStart(3, '0')}`; | ||
} | ||
|
||
// #region [ debounce ] | ||
let debounceTimer: Record<any, any> = {}; | ||
|
||
/** | ||
* | ||
* @param {*} wait | ||
* @param {*} func | ||
* @param {*} args | ||
* @example | ||
* debounce(200, (v) => { | ||
* this.$store.commit('api/search', v); | ||
* }, val); | ||
*/ | ||
export function debounce(wait: number, func: Function, args?: any) { | ||
debounceTimer = debounceTimer || {}; | ||
|
||
if (debounceTimer[func.toString()]) { | ||
clearTimeout(debounceTimer[func.toString()]); | ||
} | ||
|
||
debounceTimer[func.toString()] = setTimeout(() => { | ||
func(args); | ||
debounceTimer[func.toString()] = null; | ||
}, wait); | ||
} | ||
|
||
// #endregion | ||
|
||
|
||
export function logSettings(type: 'CALL_FILTER', value: any) { | ||
if (type === 'CALL_FILTER') { | ||
const title = value ? 'Acts filtered by call' : 'Show all Acts'; | ||
console.log( | ||
`%c ActMaster 🥷 %c "${title}"\n%c Don\'t forget to update the list %c ⟳ `, | ||
'background:#44bd90; color:#fff; border-radius: 3px; padding: 3px;', | ||
value ? 'background:transparent; color:#e56e17;' : 'background:transparent; color:#44bd90;', | ||
'background:transparent; color:#44bd90;', | ||
'background:#ffc759; color:#fff; border-radius: 3px; padding: 3px;' | ||
); | ||
} | ||
} |