Skip to content

Commit 7ea700c

Browse files
committed
[optimize] add Generic Types for MobX adapter
[add] WebCellFunction type
1 parent fc751b8 commit 7ea700c

3 files changed

Lines changed: 25 additions & 16 deletions

File tree

MobX/package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-web-cell",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"license": "LGPL-3.0",
55
"author": "shiy2008@gmail.com",
66
"description": "MobX adaptor for WebCell v2",
@@ -12,6 +12,7 @@
1212
"TypeScript",
1313
"decorator"
1414
],
15+
"homepage": "https://github.com/EasyWebApp/WebCell/tree/v2/MobX",
1516
"source": "source/index.ts",
1617
"types": "dist/index.d.ts",
1718
"main": "dist/index.umd.js",
@@ -22,16 +23,16 @@
2223
"prepublishOnly": "npm test && rm -rf .rts2_cache_* dist/ && npm run build"
2324
},
2425
"dependencies": {
25-
"mobx": "^5.15.4",
26-
"web-cell": "^2.1.0"
26+
"mobx": "^5.15.6",
27+
"web-cell": "^2.2.0"
2728
},
2829
"devDependencies": {
29-
"@types/jest": "^26.0.3",
30-
"@webcomponents/webcomponentsjs": "^2.4.3",
31-
"jest": "^26.1.0",
32-
"microbundle": "^0.12.2",
33-
"ts-jest": "^26.1.1",
34-
"typescript": "^3.9.5"
30+
"@types/jest": "^26.0.10",
31+
"@webcomponents/webcomponentsjs": "^2.4.4",
32+
"jest": "^26.4.2",
33+
"microbundle": "^0.12.3",
34+
"ts-jest": "^26.3.0",
35+
"typescript": "^4.0.2"
3536
},
3637
"jest": {
3738
"preset": "ts-jest"

MobX/source/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { WebCellProps, WebCellClass, VNode, patch } from 'web-cell';
22
import { autorun } from 'mobx';
33

4-
export type FunctionComponent = (props?: WebCellProps) => VNode;
4+
export type FunctionComponent<P extends WebCellProps = WebCellProps> = (
5+
props?: P
6+
) => VNode;
57

6-
function wrapFunction(func: FunctionComponent) {
7-
return function (props?: WebCellProps) {
8+
function wrapFunction<P>(func: FunctionComponent<P>) {
9+
return function (props?: P) {
810
var vTree: VNode;
911

1012
autorun(
@@ -14,17 +16,19 @@ function wrapFunction(func: FunctionComponent) {
1416
};
1517
}
1618

17-
function wrapClass(Class: WebCellClass) {
19+
function wrapClass<P, S>(Class: WebCellClass<P, S>) {
1820
const { update } = Class.prototype;
1921

2022
Class.prototype.update = function () {
2123
autorun(() => update.call(this));
2224
};
2325
}
2426

25-
export function observer(Class: FunctionComponent | WebCellClass): any {
27+
export function observer<P, S>(
28+
Class: FunctionComponent<P> | WebCellClass<P, S>
29+
): any {
2630
if (Object.getPrototypeOf(Class) === Function.prototype)
27-
return wrapFunction(Class as FunctionComponent);
31+
return wrapFunction(Class as FunctionComponent<P>);
2832

29-
wrapClass(Class as WebCellClass);
33+
wrapClass(Class as WebCellClass<P, S>);
3034
}

source/WebCell.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
import { ComponentMeta, watch, DOMEventDelegater } from './decorator';
1010
import { VNodeChildElement, VNode, createCell, render } from './renderer';
1111

12+
export type WebCellFunction<P extends WebCellProps = WebCellProps> = (
13+
props?: P
14+
) => WebCellElement;
15+
1216
export interface WebCellComponent<P extends WebCellProps = WebCellProps, S = {}>
1317
extends HTMLElement {
1418
/**

0 commit comments

Comments
 (0)