Skip to content

Commit cf43d49

Browse files
committed
feat: v0.1.0
1 parent dc67b88 commit cf43d49

20 files changed

+3555
-292
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
tab_width = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintrc.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
module.exports = {
2-
extends: ['prettier'],
3-
plugins: ['prettier'],
2+
extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
3+
plugins: ['@typescript-eslint', 'prettier'],
44
rules: {
5-
'prettier/prettier': ['error'],
5+
'prettier/prettier': 'error',
6+
'prefer-const': 0,
7+
'@typescript-eslint/explicit-module-boundary-types': 0,
8+
'@typescript-eslint/no-explicit-any': 0,
9+
'@typescript-eslint/no-empty-function': 0,
10+
'@typescript-eslint/ban-types': 0,
611
},
712
}

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
node_modules
44
*.log
55
.idea
6-
.vscode
76
package-lock.json
87
npm-debug.log
98

.vscode/settings.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"editor.detectIndentation": false,
4+
"editor.formatOnSave": true,
5+
"editor.formatOnType": true,
6+
"editor.codeActionsOnSave": {
7+
"source.fixAll": true
8+
},
9+
"typescript.tsdk": "node_modules/typescript/lib",
10+
"cSpell.words": ["Registrator", "Unmount", "libundler", "surmon", "veact"]
11+
}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Veact
22

3-
![veact](https://img.shields.io/badge/MADE%20WITH-VEACT-42a97a?style=for-the-badge&labelColor=35495d)
3+
[![veact](https://img.shields.io/badge/MADE%20WITH-VEACT-42a97a?style=for-the-badge&labelColor=35495d)](https://github.com/veactjs/veact)
44
[![GitHub stars](https://img.shields.io/github/stars/veactjs/veact.svg?style=for-the-badge)](https://github.com/veactjs/veact/stargazers)
55
[![GitHub issues](https://img.shields.io/github/issues-raw/veactjs/veact.svg?style=for-the-badge)](https://github.com/veactjs/veact/issues)
66
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/veactjs/veact/Deploy?label=deploy&style=for-the-badge)](https://github.com/veactjs/veact/actions?query=workflow:%22Deploy%22)
77
[![GitHub license](https://img.shields.io/github/license/veactjs/veact.svg?style=for-the-badge)](https://github.com/veactjs/veact/blob/master/LICENSE)
88

9-
> Mutable state enhancer library for [`React`](https://github.com/facebook/react) by [`@vuejs/reactivity`](https://github.com/vuejs/vue-next).
9+
> Mutable state enhancer library for [`React`](https://github.com/facebook/react) by [`@vue/reactivity`](https://github.com/vuejs/vue-next).
1010
11-
> 适用于 [`React`](https://github.com/facebook/react) 的可变式状态管理库,基于 [`@vuejs/reactivity`](https://github.com/vuejs/vue-next) 进行开发。
11+
> 适用于 [`React`](https://github.com/facebook/react) 的可变式状态管理库,基于 [`@vue/reactivity`](https://github.com/vuejs/vue-next) 进行开发。
1212
1313
**🔥 Who is using this library**
1414

libundler.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @type {import('@surmon-china/libundler/lib/interface').LibundlerConfigObject} */
2+
module.exports = {
3+
libName: 'veact',
4+
outFileName: 'veact',
5+
entry: './src/index.ts',
6+
outDir: './dist',
7+
eslint: false,
8+
external: ['react', 'react-dom', '@vue/reactivity'],
9+
globals: {
10+
react: 'React',
11+
'react-dom': 'ReactDOM',
12+
'@vue/reactivity': 'Vue',
13+
},
14+
}

package.json

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
{
22
"name": "veact",
3-
"description": "Vue Composition API for React",
4-
"version": "0.1.0",
5-
"license": "MIT",
6-
"author": {
7-
"name": "Surmon",
8-
"email": "[email protected]",
9-
"url": "https://surmon.me"
10-
},
3+
"description": "Mutable state enhancer library for @react by @vue/reactivity",
114
"keywords": [
125
"React mutable state lib",
136
"React vue state",
147
"React composition API",
158
"React state library"
169
],
10+
"version": "0.1.0",
11+
"license": "MIT",
12+
"author": "Surmon",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/veactjs/veact.git"
16+
},
1717
"files": [
1818
"dist",
1919
"src"
2020
],
21+
"sideEffects": false,
22+
"main": "dist/veact.umd.js",
23+
"module": "dist/veact.esm.js",
24+
"browser": "dist/veact.umd.js",
25+
"types": "dist/veact.esm.d.ts",
2126
"scripts": {
2227
"lint": "eslint --ext .ts src/**",
2328
"format": "prettier --write --parser typescript \"src/**/*.ts\"",
2429
"build": "libundler"
2530
},
2631
"peerDependencies": {
27-
"react": "*",
28-
"react-dom": "*"
32+
"react": "^16.8",
33+
"react-dom": "^16.8"
2934
},
3035
"dependencies": {
3136
"@vue/reactivity": "^3.2.0-beta.5"
3237
},
3338
"devDependencies": {
39+
"@surmon-china/libundler": "../../NPM/libundler",
3440
"@types/react": "^17.0.14",
3541
"@types/react-dom": "^17.0.9",
42+
"@typescript-eslint/eslint-plugin": "^4.28.4",
43+
"@typescript-eslint/parser": "^4.28.4",
3644
"eslint": "^7.31.0",
3745
"eslint-config-prettier": "^8.3.0",
3846
"eslint-plugin-prettier": "^3.4.0",

src/enhancer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* @author Surmon <https://github.com/surmon-china>
44
*/
55

6-
import { unstable_batchedUpdates } from 'react-dom';
6+
import { unstable_batchedUpdates } from 'react-dom'
77

8-
export const batchedUpdates = unstable_batchedUpdates;
8+
export const batchedUpdates = unstable_batchedUpdates

src/hooks.ts

+29-30
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,62 @@
33
* @author Surmon <https://github.com/surmon-china>
44
*/
55

6-
import { useState as useReactState } from 'react';
7-
import { useWatch } from './watch';
8-
import { useForceUpdate } from './utils';
6+
import { useState as useReactState } from 'react'
7+
import { useWatch } from './watch'
8+
import { useForceUpdate } from './utils'
99
import {
1010
ref as vRef,
1111
shallowRef as vShallowRef,
1212
reactive as vReactive,
1313
shallowReactive as vShallowReactive,
1414
computed as vComputed,
1515
ComputedGetter,
16-
} from './reactivity';
16+
} from './reactivity'
1717

1818
/** ref hook */
1919
export function useRef<T>(initValue: T) {
20-
const [value] = useReactState(() => vRef(initValue));
21-
const forceUpdate = useForceUpdate();
22-
useWatch(value, forceUpdate);
23-
return value;
20+
const [value] = useReactState(() => vRef(initValue))
21+
const forceUpdate = useForceUpdate()
22+
useWatch(value, forceUpdate)
23+
return value
2424
}
2525

2626
/** shallowRef hook */
2727
export function useShallowRef<T extends object>(initValue: T) {
28-
const [value] = useReactState(() => vShallowRef(initValue));
29-
const forceUpdate = useForceUpdate();
30-
useWatch(value, forceUpdate);
31-
return value;
28+
const [value] = useReactState(() => vShallowRef(initValue))
29+
const forceUpdate = useForceUpdate()
30+
useWatch(value, forceUpdate)
31+
return value
3232
}
3333

3434
/** reactive hook */
3535
export function useReactive<T extends object>(target: T) {
36-
const [value] = useReactState(() => vReactive(target));
37-
const forceUpdate = useForceUpdate();
38-
useWatch(value, forceUpdate);
39-
return value;
36+
const [value] = useReactState(() => vReactive(target))
37+
const forceUpdate = useForceUpdate()
38+
useWatch(value, forceUpdate)
39+
return value
4040
}
4141

4242
/** shallowReactive hook */
4343
export function useShallowReactive<T extends object>(target: T) {
44-
const [value] = useReactState(() => vShallowReactive(target));
45-
const forceUpdate = useForceUpdate();
46-
useWatch(value, forceUpdate);
47-
return value;
44+
const [value] = useReactState(() => vShallowReactive(target))
45+
const forceUpdate = useForceUpdate()
46+
useWatch(value, forceUpdate)
47+
return value
4848
}
4949

5050
/** computed hook */
5151
export function useComputed<T>(getter: ComputedGetter<T>) {
52-
const [value] = useReactState(() => vComputed(getter));
53-
const forceUpdate = useForceUpdate();
54-
useWatch(value, forceUpdate);
55-
return value;
52+
const [value] = useReactState(() => vComputed(getter))
53+
const forceUpdate = useForceUpdate()
54+
useWatch(value, forceUpdate)
55+
return value
5656
}
5757

5858
/** any data to reactivity */
59-
type ReactivityHookFn<T> = () => T;
60-
export function useReactivity<T = any>(getter: ReactivityHookFn<T>) {
61-
const forceUpdate = useForceUpdate();
62-
// deep > watch > traverse(getter()) > ref | array | set | map | plain object(reactive) > forch update
63-
useWatch(() => getter(), forceUpdate, { deep: true });
64-
return getter();
59+
export function useReactivity<T = any>(getter: () => T) {
60+
const forceUpdate = useForceUpdate()
61+
// deep > watch > traverse(getter()) > ref | array | set | map | plain object(reactive) > force update
62+
useWatch(() => getter(), forceUpdate, { deep: true })
63+
return getter()
6564
}

src/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/**
2-
* @module veact
3-
* @author Surmon <https://github.com/surmon-china>
4-
*/
5-
6-
export * from './reactivity';
7-
export * from './watch';
8-
export * from './hooks';
9-
export * from './enhancer';
10-
export * from './lifecycle';
1+
/**
2+
* @module veact
3+
* @author Surmon <https://github.com/surmon-china>
4+
*/
5+
6+
export * from './reactivity'
7+
export * from './watch'
8+
export * from './hooks'
9+
export * from './enhancer'
10+
export * from './lifecycle'

src/lifecycle.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
* @author Surmon <https://github.com/surmon-china>
44
*/
55

6-
/* eslint-disable react-hooks/rules-of-hooks */
7-
import { useEffect } from 'react';
6+
import { useEffect } from 'react'
87

98
export function onMounted(callback: () => any) {
109
useEffect(() => {
11-
callback();
12-
}, []);
10+
callback()
11+
}, [])
1312
}
1413

1514
export function onBeforeUnmount(callback: () => void) {
1615
useEffect(() => {
1716
return () => {
18-
callback();
19-
};
20-
}, []);
17+
callback()
18+
}
19+
}, [])
2120
}
2221

2322
export function onUpdated(callback: () => void) {
2423
useEffect(() => {
25-
callback();
26-
});
24+
callback()
25+
})
2726
}

src/logger.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@
33
* @author Surmon <https://github.com/surmon-china>
44
*/
55

6-
import { isFunction, isPromise } from './utils';
6+
import { isFunction, isPromise } from './utils'
77

8-
const VEACT_NAME = 'veact';
9-
const LOGGER_PREFIX = `[${VEACT_NAME}]`;
8+
const VEACT_NAME = 'veact'
9+
const LOGGER_PREFIX = `[${VEACT_NAME}]`
1010

1111
export const logger: Console = {
1212
...console,
1313
log(...args: any) {
14-
console.log(LOGGER_PREFIX, ...args);
14+
console.log(LOGGER_PREFIX, ...args)
1515
},
1616
warn(...args: any) {
17-
console.warn(LOGGER_PREFIX, ...args);
17+
console.warn(LOGGER_PREFIX, ...args)
1818
},
1919
error(...args: any) {
20-
console.error(LOGGER_PREFIX, ...args);
20+
console.error(LOGGER_PREFIX, ...args)
2121
},
22-
};
22+
}
2323

2424
// fork from: https://github.com/vuejs/vue-next/blob/master/packages/runtime-core/src/errorHandling.ts
2525
export function callWithErrorHandling(
2626
fn: Function,
2727
errorString: string,
2828
args?: unknown[]
2929
) {
30-
let result;
30+
let result
3131
try {
32-
result = args ? fn(...args) : fn();
32+
result = args ? fn(...args) : fn()
3333
} catch (error) {
34-
logger.error(error, errorString);
34+
logger.error(error, errorString)
3535
}
36-
return result;
36+
return result
3737
}
3838

3939
export function callWithAsyncErrorHandling(
@@ -42,18 +42,18 @@ export function callWithAsyncErrorHandling(
4242
args?: unknown[]
4343
): any[] {
4444
if (isFunction(fn)) {
45-
const result = callWithErrorHandling(fn, errorString, args);
45+
const result = callWithErrorHandling(fn, errorString, args)
4646
if (result && isPromise(result)) {
4747
result.catch((error) => {
48-
logger.error(error, errorString);
49-
});
48+
logger.error(error, errorString)
49+
})
5050
}
51-
return result;
51+
return result
5252
}
5353

54-
const values = [];
54+
const values = []
5555
for (let i = 0; i < fn.length; i++) {
56-
values.push(callWithAsyncErrorHandling(fn[i], errorString, args));
56+
values.push(callWithAsyncErrorHandling(fn[i], errorString, args))
5757
}
58-
return values;
58+
return values
5959
}

src/reactivity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export {
3232
triggerRef,
3333
unref,
3434
ReactiveEffect,
35-
} from '@vue/reactivity';
35+
} from '@vue/reactivity'
3636

3737
export type {
3838
EffectScheduler,
@@ -50,4 +50,4 @@ export type {
5050
UnwrapRef,
5151
WritableComputedOptions,
5252
WritableComputedRef,
53-
} from '@vue/reactivity';
53+
} from '@vue/reactivity'

0 commit comments

Comments
 (0)