-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from cuixiaorui/master
refactor: use devtools realize emit
- Loading branch information
Showing
7 changed files
with
67 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { setDevtoolsHook, devtools } from 'vue' | ||
|
||
const enum DevtoolsHooks { | ||
COMPONENT_EMIT = 'component:emit' | ||
} | ||
|
||
let events: Record<string, unknown[]> | ||
|
||
export function emitted<T = unknown>( | ||
eventName?: string | ||
): T[] | Record<string, T[]> { | ||
if (eventName) { | ||
const emitted = (events as Record<string, T[]>)[eventName] | ||
return emitted | ||
} | ||
|
||
return events as Record<string, T[]> | ||
} | ||
|
||
export const attachEmitListener = () => { | ||
events = {} | ||
// use devtools to capture this "emit" | ||
setDevtoolsHook(createDevTools(events)) | ||
} | ||
|
||
function createDevTools(events): any { | ||
const devTools: Partial<typeof devtools> = { | ||
emit(eventType, ...payload) { | ||
if (eventType !== DevtoolsHooks.COMPONENT_EMIT) return | ||
|
||
// The first argument is root component | ||
// The second argument is vm | ||
// The third argument is event | ||
// The fourth argument is args of event | ||
recordEvent(events, payload[2], payload[3]) | ||
} | ||
} | ||
|
||
return devTools | ||
} | ||
|
||
function recordEvent(events, event, args) { | ||
// Record the event message sent by the emit | ||
events[event] | ||
? (events[event] = [...events[event], [...args]]) | ||
: (events[event] = [[...args]]) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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