Skip to content

chore: update weex flow type annotations #7322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"plugin:flowtype/recommended"
],
"globals": {
"__WEEX__": true
"__WEEX__": true,
"WXEnvironment": true
}
}
52 changes: 45 additions & 7 deletions flow/weex.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
// 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;
isRegisteredModule: (name: string, method?: string) => boolean;
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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/core/util/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* @flow */
declare var WXEnvironment: any;

// can we use __proto__?
export const hasProto = '__proto__' in {}
Expand Down
36 changes: 21 additions & 15 deletions src/platforms/weex/runtime/node-ops.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -36,15 +40,15 @@ 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
}
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)
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion src/platforms/weex/util/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow */
declare var document: Object;
declare var document: WeexDocument;

import { warn } from 'core/util/index'

Expand Down