Skip to content

Commit

Permalink
feat(runtime): Basic implementation of the patchEvent() function
Browse files Browse the repository at this point in the history
  • Loading branch information
msaelices committed Mar 15, 2020
1 parent e97cd18 commit 730d416
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
53 changes: 33 additions & 20 deletions apps/test/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ const app = createApp({
col
})

const toggleAttrTest = condition =>
h('Label', {
text: 'Hello World',
ref: 'test',
row: 3,
...(condition
? {
textWrap: true,
fontSize: 60,
padding: 60,
borderWidth: 2,
customProperty: 'TESTING',
'android:borderColor': 'red',
'ios:borderColor': 'blue'
}
: {})
})

const labelsTest = () =>
h(
'GridLayout',
Expand Down Expand Up @@ -91,6 +73,36 @@ const app = createApp({
]
)

const toggleAttrTest = condition =>
h('Label', {
text: 'Hello World',
ref: 'test',
row: 3,
...(condition
? {
textWrap: true,
fontSize: 60,
padding: 60,
borderWidth: 2,
customProperty: 'TESTING',
'android:borderColor': 'red',
'ios:borderColor': 'blue'
}
: {})
})

const buttonsTest = () =>
h(
'Button',
{
row: 4,
onTap: () => {
console.log('Clicked')
}
},
['Click me']
)

return h('Frame', [
h(
'Page',
Expand Down Expand Up @@ -130,12 +142,13 @@ const app = createApp({
h(
'GridLayout',
{
rows: '*, *, auto, auto'
rows: '*, *, *, auto, auto'
},
[
h('ContentView', [labelsTest()]),
textNodesTest(),
toggleAttrTest(this.toggler)
toggleAttrTest(this.toggler),
buttonsTest()
]
)
]
Expand Down
44 changes: 40 additions & 4 deletions packages/runtime/src/modules/events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
import { EMPTY_OBJ } from '@vue/shared'
import { INSVElement } from '../nodes'
import { ComponentInternalInstance } from '@vue/runtime-core'

export function patchEvent(
el: INSVElement,
name: string,
prevValue: any,
nextValue: any,
instance: ComponentInternalInstance | null = null
nextValue: any
) {
// todo implement patchEvent
const prevOptions = prevValue && 'options' in prevValue && prevValue.options
const nextOptions = nextValue && 'options' in nextValue && nextValue.options
const invoker = prevValue && prevValue.invoker
const value =
nextValue && 'handler' in nextValue ? nextValue.handler : nextValue

if (prevOptions || nextOptions) {
const prev = prevOptions || EMPTY_OBJ
const next = nextOptions || EMPTY_OBJ
if (
prev.capture !== next.capture ||
prev.passive !== next.passive ||
prev.once !== next.once
) {
if (invoker) {
el.removeEventListener(name, invoker)
}
if (nextValue && value) {
const invoker = value
nextValue.invoker = invoker
// TODO: use nextOptions here for supporting event options
el.addEventListener(name, invoker)
}
return
}
}

if (nextValue && value) {
if (invoker) {
;(prevValue as any).invoker = null
invoker.value = value
nextValue.invoker = invoker
} else {
el.addEventListener(name, value)
}
} else if (invoker) {
el.removeEventListener(name, invoker)
}
}
6 changes: 6 additions & 0 deletions packages/runtime/src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export interface INSVElement extends INSVNode {

removeEventListener(event: string, handler?: any): void

dispatchEvent(event: string): void

nativeView: (ViewBase | LayoutBase) & { [ELEMENT_REF]: INSVElement }

getAttribute(name: string): unknown
Expand Down Expand Up @@ -144,6 +146,10 @@ export class NSVElement extends NSVNode implements INSVElement {
this.nativeView.removeEventListener(event, handler)
}

dispatchEvent(event: string) {
this.nativeView.notify({ eventName: event, object: this.nativeView })
}

getAttribute(name: string): unknown {
return this.nativeView[name]
}
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function patchProp(
break
default:
if (isOn(key)) {
// console.log('->patchProp+Event')
patchEvent(el, key.substr(2).toLowerCase(), prevValue, nextValue)
} else {
patchAttr(el, key, prevValue, nextValue)
Expand Down

0 comments on commit 730d416

Please sign in to comment.