Skip to content

Commit

Permalink
Worked on typings
Browse files Browse the repository at this point in the history
  • Loading branch information
nandi95 authored and lmiller1990 committed Feb 12, 2021
1 parent b08cb49 commit 7c8caf7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
26 changes: 14 additions & 12 deletions src/emit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setDevtoolsHook, devtools, ComponentPublicInstance } from 'vue'
import { ComponentInternalInstance } from '@vue/runtime-core'

const enum DevtoolsHooks {
COMPONENT_EMIT = 'component:emit'
Expand All @@ -10,43 +11,44 @@ let events: Record<string, typeof componentEvents>
export function emitted<T = unknown>(
vm: ComponentPublicInstance,
eventName?: string
): T[] | Record<string, T[]> {
): undefined | T[] | Record<string, T[]> {
const cid = vm.$.uid

const vmEvents = (events as Record<string, Record<string, T[]>>)[cid] || {}
if (eventName) {
const emitted = vmEvents
? (vmEvents as Record<string, T[]>)[eventName]
: undefined
return emitted
return vmEvents ? (vmEvents as Record<string, T[]>)[eventName] : undefined
}

return vmEvents as Record<string, T[]>
}
type Events = { [id: number]: Record<string, any> }

export const attachEmitListener = () => {
events = {}
// use devtools to capture this "emit"
setDevtoolsHook(createDevTools(events))
}

function createDevTools(events): any {
const devTools: Partial<typeof devtools> = {
function createDevTools(events: Events): any {
return {
emit(eventType, ...payload) {
if (eventType !== DevtoolsHooks.COMPONENT_EMIT) return

const [rootVM, componentVM, event, eventArgs] = payload
recordEvent(events, componentVM, event, eventArgs)
}
}

return devTools
} as Partial<typeof devtools>
}

function recordEvent(events, vm, event, args): void {
function recordEvent(
events: Events,
vm: ComponentInternalInstance,
event: string,
args: Events[number]
): void {
// Functional component wrapper creates a parent component
let wrapperVm = vm
while (typeof wrapperVm.type === 'function') wrapperVm = wrapperVm.parent
while (typeof wrapperVm?.type === 'function') wrapperVm = wrapperVm.parent!

const cid = wrapperVm.uid
if (!(cid in events)) {
Expand Down
7 changes: 3 additions & 4 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
h,
createApp,
defineComponent,
VNodeNormalizedChildren,
reactive,
FunctionalComponent,
ComponentPublicInstance,
Expand Down Expand Up @@ -230,7 +229,7 @@ export function mount(
options?: MountingOptions<any>
): VueWrapper<any> {
// normalise the incoming component
let component
let component: DefineComponent

if (isFunctionalComponent(originalComponent)) {
component = defineComponent({
Expand Down Expand Up @@ -261,7 +260,7 @@ export function mount(
}

// handle any slots passed via mounting options
const slots: VNodeNormalizedChildren =
const slots =
options?.slots &&
Object.entries(options.slots).reduce(
(
Expand All @@ -270,7 +269,7 @@ export function mount(
): { [key: string]: Function } => {
// case of an SFC getting passed
if (typeof slot === 'object' && 'render' in slot) {
acc[name] = slot.render
acc[name] = slot.render!
return acc
}

Expand Down
8 changes: 5 additions & 3 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ export class VueWrapper<T extends ComponentPublicInstance> {
return true
}

emitted<T = unknown>(): Record<string, T[]>
emitted<T = unknown>(eventName?: string): T[]
emitted<T = unknown>(eventName?: string): T[] | Record<string, T[]> {
emitted<T = unknown>(): undefined | Record<string, T[]>
emitted<T = unknown>(eventName?: string): undefined | T[]
emitted<T = unknown>(
eventName?: string
): undefined | T[] | Record<string, T[]> {
return emitted(this.vm, eventName)
}

Expand Down

0 comments on commit 7c8caf7

Please sign in to comment.