Skip to content

Commit

Permalink
feat(weex): update weex utils (#7115)
Browse files Browse the repository at this point in the history
Add flow type annotations. Add the "registerComponentHook" and the "updateComponentData" api.
  • Loading branch information
Hanks10100 authored and yyx990803 committed Dec 19, 2017
1 parent e5da1da commit 3b32652
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/platforms/weex/util/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* globals document */
/* @flow */
declare var document: Object;

import { makeMap } from 'shared/util'
import { warn } from 'core/util/index'
Expand Down Expand Up @@ -30,25 +31,50 @@ export const isUnaryTag = makeMap(
true
)

export function mustUseProp () { /* console.log('mustUseProp') */ }
export function getTagNamespace () { /* console.log('getTagNamespace') */ }
export function isUnknownElement () { /* console.log('isUnknownElement') */ }
export function mustUseProp (tag: string, type: ?string, name: string): boolean {
return false
}

export function getTagNamespace (tag?: string): string | void { }

export function isUnknownElement (tag?: string): boolean {
return false
}

export function query (el, document) {
export function query (el: string | Element, document: Object) {
// document is injected by weex factory wrapper
const placeholder = document.createComment('root')
placeholder.hasAttribute = placeholder.removeAttribute = function () {} // hack for patch
document.documentElement.appendChild(placeholder)
return placeholder
}

export function registerHook (cid, type, hook, fn) {
// Register the component hook to weex native render engine.
// The hook will be triggered by native, not javascript.
export function registerComponentHook (
componentId: string,
type: string, // hook type, could be "lifecycle" or "instance"
hook: string, // hook name
fn: Function
) {
if (!document || !document.taskCenter) {
warn(`Can't find available "document" or "taskCenter".`)
return
}
if (typeof document.taskCenter.registerHook === 'function') {
return document.taskCenter.registerHook(cid, type, hook, fn)
return document.taskCenter.registerHook(componentId, type, hook, fn)
}
warn(`Failed to register component hook "${type}@${hook}#${componentId}".`)
}

// Updates the state of the component to weex native render engine.
export function updateComponentData (componentId: string, newData: Object) {
if (!document || !document.taskCenter) {
warn(`Can't find available "document" or "taskCenter".`)
return
}
if (typeof document.taskCenter.updateData === 'function') {
return document.taskCenter.updateData(componentId, newData)
}
warn(`Not support to register component hook "${type}@${hook}#${cid}".`)
warn(`Failed to update component data (${componentId}).`)
}

0 comments on commit 3b32652

Please sign in to comment.