ronds-metadata 1.2.93 → 1.2.94

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ import Provider from './utils/Provider';
2
+ import Context from './utils/Context';
3
+ type ReturnTypes<T extends any> = T extends (...args: any) => infer R ? R : any;
4
+ type Model<T extends keyof U, U> = {
5
+ [key in keyof U]: ReturnTypes<U[T]>;
6
+ };
7
+ type UseModelFn = <T extends keyof U, U>(namespace: T) => Model<T, U>[T];
8
+ declare const useModel: UseModelFn;
9
+ export { useModel, Provider, Context, Model };
@@ -0,0 +1,50 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ /*
3
+ * @Author: wangxian
4
+ * @Date: 2023-08-01 19:30:25
5
+ * @LastEditTime: 2023-08-01 19:30:35
6
+ */
7
+ import { useEffect, useContext, useRef, useState } from 'react';
8
+ import Provider from './utils/Provider';
9
+ import Context from './utils/Context';
10
+ var useModel = function useModel(namespace) {
11
+ var dispatcher = useContext(Context);
12
+ // 判断组件是否挂载
13
+ var isMount = useRef(false);
14
+ var _useState = useState(dispatcher.data[namespace]),
15
+ _useState2 = _slicedToArray(_useState, 2),
16
+ state = _useState2[0],
17
+ setState = _useState2[1];
18
+ useEffect(function () {
19
+ isMount.current = true;
20
+ return function () {
21
+ isMount.current = false;
22
+ };
23
+ }, []);
24
+ useEffect(function () {
25
+ var handler = function handler(value) {
26
+ if (!isMount.current) {
27
+ // 如果函数执行中,组件被卸载,则强制更新全局 data
28
+ setTimeout(function () {
29
+ dispatcher.data[namespace] = value;
30
+ dispatcher.update(namespace);
31
+ });
32
+ } else {
33
+ setState(value);
34
+ }
35
+ };
36
+ try {
37
+ dispatcher.callBacks[namespace].add(handler);
38
+ dispatcher.update(namespace);
39
+ } catch (e) {
40
+ dispatcher.callBacks[namespace] = new Set();
41
+ dispatcher.callBacks[namespace].add(handler);
42
+ dispatcher.update(namespace);
43
+ }
44
+ return function () {
45
+ dispatcher.callBacks[namespace].delete(handler);
46
+ };
47
+ }, [namespace]);
48
+ return state;
49
+ };
50
+ export { useModel, Provider, Context };
@@ -0,0 +1,3 @@
1
+ /// <reference types="react" />
2
+ declare const _default: import("react").Context<{}>;
3
+ export default _default;
@@ -0,0 +1,2 @@
1
+ import { createContext } from 'react';
2
+ export default /*#__PURE__*/createContext({});
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ type ExecutorProps = {
3
+ hook: () => void;
4
+ namespace: string;
5
+ onUpdate: (value: any) => void;
6
+ };
7
+ declare const Executor: React.FC<ExecutorProps>;
8
+ export default Executor;
@@ -0,0 +1,33 @@
1
+ import React, { useEffect, useMemo, useRef } from 'react';
2
+ var Executor = function Executor(_ref) {
3
+ var hook = _ref.hook,
4
+ namespace = _ref.namespace,
5
+ onUpdate = _ref.onUpdate;
6
+ var updateRef = useRef(onUpdate);
7
+ updateRef.current = onUpdate;
8
+ var initRef = useRef(false);
9
+ var data;
10
+ try {
11
+ if (typeof hook === 'function') {
12
+ data = hook();
13
+ } else {
14
+ data = null;
15
+ }
16
+ } catch (e) {
17
+ throw new TypeError(e);
18
+ }
19
+ // 初始化时执行一次
20
+ useMemo(function () {
21
+ updateRef.current(data);
22
+ initRef.current = false;
23
+ }, []);
24
+ useEffect(function () {
25
+ if (initRef.current) {
26
+ updateRef.current(data);
27
+ } else {
28
+ initRef.current = true;
29
+ }
30
+ });
31
+ return /*#__PURE__*/React.createElement(React.Fragment, null);
32
+ };
33
+ export default Executor;
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const Provider: React.FC<{
3
+ models: any;
4
+ }>;
5
+ export default Provider;
@@ -0,0 +1,30 @@
1
+ /*
2
+ * @Author: wangxian
3
+ * @Date: 2023-08-01 19:31:08
4
+ * @LastEditTime: 2023-08-01 19:32:27
5
+ */
6
+ import React from 'react';
7
+ import Dispatcher from './dispatcher';
8
+ import Executor from './Executor';
9
+ import Context from './Context';
10
+ var dispatcher = new Dispatcher();
11
+ var Provider = function Provider(_ref) {
12
+ var children = _ref.children,
13
+ _ref$models = _ref.models,
14
+ models = _ref$models === void 0 ? {} : _ref$models;
15
+ var Exe = Executor;
16
+ return /*#__PURE__*/React.createElement(Context.Provider, {
17
+ value: dispatcher
18
+ }, Object.keys(models).map(function (item) {
19
+ return /*#__PURE__*/React.createElement(Exe, {
20
+ key: item,
21
+ namespace: item,
22
+ hook: models[item],
23
+ onUpdate: function onUpdate(value) {
24
+ dispatcher.data[item] = value;
25
+ dispatcher.update(item);
26
+ }
27
+ });
28
+ }), children);
29
+ };
30
+ export default Provider;
@@ -0,0 +1,5 @@
1
+ export default class Dispatcher {
2
+ data: any;
3
+ callBacks: any;
4
+ update: (namespace: string) => void;
5
+ }
@@ -0,0 +1,24 @@
1
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ /*
4
+ * @Author: wangxian
5
+ * @Date: 2023-08-01 19:31:09
6
+ * @LastEditTime: 2023-08-01 19:32:07
7
+ */
8
+ var Dispatcher = /*#__PURE__*/_createClass(function Dispatcher() {
9
+ var _this = this;
10
+ _classCallCheck(this, Dispatcher);
11
+ this.data = {};
12
+ this.callBacks = {};
13
+ this.update = function (namespace) {
14
+ (_this.callBacks[namespace] || []).forEach(function (callback) {
15
+ try {
16
+ var data = _this.data[namespace];
17
+ callback(data);
18
+ } catch (e) {
19
+ callback(undefined);
20
+ }
21
+ });
22
+ };
23
+ });
24
+ export { Dispatcher as default };
package/es/index.d.ts CHANGED
@@ -19,6 +19,7 @@ export * from './framework/metadata/index';
19
19
  export { default as http, addInterceptor } from './framework/http/index';
20
20
  export { default as useMemoSubject } from './framework/rxjs-hooks/useMemoSubject';
21
21
  export { default as useObservable } from './framework/rxjs-hooks/useObservable';
22
+ export { useModel, Provider, Context, Model } from './framework/hooks/useModel';
22
23
  export { default as useSyncScroll, useSyncScrollByRefs } from './framework/hooks/use-sync-scroll';
23
24
  export { registerMetadataAPI } from './framework/metadata/MetadataService';
24
25
  export { FormRobot } from './framework/fg/index';
package/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author:wangxian
3
3
  * @Date: 2021-09-18 14:15:04
4
- * @LastEditTime: 2023-04-27 19:08:54
4
+ * @LastEditTime: 2023-08-02 08:35:33
5
5
  */
6
6
  import './theme.less';
7
7
  export { default as MetadataEdit } from './comps/MetadataEdit';
@@ -24,6 +24,7 @@ export * from './framework/metadata/index';
24
24
  export { default as http, addInterceptor } from './framework/http/index';
25
25
  export { default as useMemoSubject } from './framework/rxjs-hooks/useMemoSubject';
26
26
  export { default as useObservable } from './framework/rxjs-hooks/useObservable';
27
+ export { useModel, Provider, Context } from './framework/hooks/useModel';
27
28
  export { default as useSyncScroll, useSyncScrollByRefs } from './framework/hooks/use-sync-scroll';
28
29
  export { registerMetadataAPI } from './framework/metadata/MetadataService';
29
30
  export { FormRobot } from './framework/fg/index';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "ronds-metadata",
4
- "version": "1.2.93",
4
+ "version": "1.2.94",
5
5
  "scripts": {
6
6
  "start": "dumi dev",
7
7
  "docs:build": "dumi build",