From f319bef532dd0d3cae4302e7a6a038d45e7363ba Mon Sep 17 00:00:00 2001 From: Hanks Date: Wed, 27 Dec 2017 10:33:48 +0800 Subject: [PATCH] chore: update weex flow type annotations (#7322) --- .eslintrc | 3 +- flow/weex.js | 52 ++++++++++++++++++++++---- src/core/util/env.js | 1 - src/platforms/weex/runtime/node-ops.js | 36 ++++++++++-------- src/platforms/weex/util/index.js | 2 +- 5 files changed, 69 insertions(+), 25 deletions(-) diff --git a/.eslintrc b/.eslintrc index 523d283e..f5845425 100644 --- a/.eslintrc +++ b/.eslintrc @@ -8,6 +8,7 @@ "plugin:flowtype/recommended" ], "globals": { - "__WEEX__": true + "__WEEX__": true, + "WXEnvironment": true } } diff --git a/flow/weex.js b/flow/weex.js index cef6f0ba..2834313f 100644 --- a/flow/weex.js +++ b/flow/weex.js @@ -1,8 +1,11 @@ // global flag to be compiled away declare var __WEEX__: boolean; +// global object in Weex +declare var WXEnvironment: WeexEnvironment; + declare type Weex = { - config: Object; + config: WeexConfigAPI; document: WeexDocument; requireModule: (name: string) => Object | void; supports: (condition: string) => boolean | void; @@ -10,10 +13,37 @@ declare type Weex = { isRegisteredComponent: (name: string) => boolean; }; +declare type WeexConfigAPI = { + bundleUrl: string; // === weex.document.URL + bundleType: string; + env: WeexEnvironment; // === WXEnvironment +}; + +declare type WeexEnvironment = { + platform: string; // could be "Web", "iOS", "Android" + weexVersion: string; // the version of WeexSDK + + osName: string; // could be "iOS", "Android" or others + osVersion: string; + appName: string; // mobile app name or browser name + appVersion: string; + + // informations of current running device + deviceModel: string; // phone device model + deviceWidth: number; + deviceHeight: number; + scale: number; + + // only available on the web + userAgent?: string; + dpr?: number; + rem?: number; +}; + declare interface WeexDocument { - id: string | number; + id: string; URL: string; - taskCenter: Object; + taskCenter: WeexTaskCenter; open: () => void; close: () => void; @@ -23,11 +53,19 @@ declare interface WeexDocument { destroy: () => void; }; +declare interface WeexTaskCenter { + instanceId: string; + callbackManager: Object; + send: (type: string, params: Object, args: Array, options?: Object) => void; + registerHook: (componentId: string, type: string, hook: string, fn: Function) => void; + updateData: (componentId: string, data: Object | void, callback?: Function) => void; +}; + declare interface WeexElement { nodeType: number; - nodeId: string | number; + nodeId: string; type: string; - ref: string | number; + ref: string; text?: string; parentNode: WeexElement | void; @@ -47,11 +85,11 @@ declare interface WeexElement { removeEvent: (type: string) => void; fireEvent: (type: string) => void; destroy: () => void; -} +}; declare type WeexInstanceOption = { instanceId: string; - config: Object; + config: WeexConfigAPI; document: WeexDocument; Vue?: GlobalAPI; app?: Component; diff --git a/src/core/util/env.js b/src/core/util/env.js index c2e80df6..dc332e28 100644 --- a/src/core/util/env.js +++ b/src/core/util/env.js @@ -1,5 +1,4 @@ /* @flow */ -declare var WXEnvironment: any; // can we use __proto__? export const hasProto = '__proto__' in {} diff --git a/src/platforms/weex/runtime/node-ops.js b/src/platforms/weex/runtime/node-ops.js index 303c8194..0431f7c6 100755 --- a/src/platforms/weex/runtime/node-ops.js +++ b/src/platforms/weex/runtime/node-ops.js @@ -1,27 +1,31 @@ -/* globals document */ -// document is injected by weex factory wrapper +/* @flow */ +declare var document: WeexDocument; import TextNode from 'weex/runtime/text-node' export const namespaceMap = {} -export function createElement (tagName) { +export function createElement (tagName: string): WeexElement { return document.createElement(tagName) } -export function createElementNS (namespace, tagName) { +export function createElementNS (namespace: string, tagName: string): WeexElement { return document.createElement(namespace + ':' + tagName) } -export function createTextNode (text) { +export function createTextNode (text: string) { return new TextNode(text) } -export function createComment (text) { +export function createComment (text: string) { return document.createComment(text) } -export function insertBefore (node, target, before) { +export function insertBefore ( + node: WeexElement, + target: WeexElement, + before: WeexElement +) { if (target.nodeType === 3) { if (node.type === 'text') { node.setAttr('value', target.text) @@ -36,7 +40,7 @@ export function insertBefore (node, target, before) { node.insertBefore(target, before) } -export function removeChild (node, child) { +export function removeChild (node: WeexElement, child: WeexElement) { if (child.nodeType === 3) { node.setAttr('value', '') return @@ -44,7 +48,7 @@ export function removeChild (node, child) { node.removeChild(child) } -export function appendChild (node, child) { +export function appendChild (node: WeexElement, child: WeexElement) { if (child.nodeType === 3) { if (node.type === 'text') { node.setAttr('value', child.text) @@ -60,22 +64,24 @@ export function appendChild (node, child) { node.appendChild(child) } -export function parentNode (node) { +export function parentNode (node: WeexElement): WeexElement | void { return node.parentNode } -export function nextSibling (node) { +export function nextSibling (node: WeexElement): WeexElement | void { return node.nextSibling } -export function tagName (node) { +export function tagName (node: WeexElement): string { return node.type } -export function setTextContent (node, text) { - node.parentNode.setAttr('value', text) +export function setTextContent (node: WeexElement, text: string) { + if (node.parentNode) { + node.parentNode.setAttr('value', text) + } } -export function setAttribute (node, key, val) { +export function setAttribute (node: WeexElement, key: string, val: any) { node.setAttr(key, val) } diff --git a/src/platforms/weex/util/index.js b/src/platforms/weex/util/index.js index 6d90307f..8181d70e 100755 --- a/src/platforms/weex/util/index.js +++ b/src/platforms/weex/util/index.js @@ -1,5 +1,5 @@ /* @flow */ -declare var document: Object; +declare var document: WeexDocument; import { warn } from 'core/util/index'