Forráskód Böngészése

chore: update weex flow type annotations (#7322)

Hanks 8 éve
szülő
commit
f319bef532

+ 2 - 1
.eslintrc

@@ -8,6 +8,7 @@
     "plugin:flowtype/recommended"
   ],
   "globals": {
-    "__WEEX__": true
+    "__WEEX__": true,
+    "WXEnvironment": true
   }
 }

+ 45 - 7
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<any>, 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;

+ 0 - 1
src/core/util/env.js

@@ -1,5 +1,4 @@
 /* @flow */
-declare var WXEnvironment: any;
 
 // can we use __proto__?
 export const hasProto = '__proto__' in {}

+ 21 - 15
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)
 }

+ 1 - 1
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'