feffery_utils_components 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4729,6 +4729,132 @@
4729
4729
  }
4730
4730
  }
4731
4731
  },
4732
+ "src/lib/components/listeners/FefferyWebSocket.react.js": {
4733
+ "description": "",
4734
+ "displayName": "FefferyWebSocket",
4735
+ "methods": [],
4736
+ "props": {
4737
+ "id": {
4738
+ "type": {
4739
+ "name": "string"
4740
+ },
4741
+ "required": false,
4742
+ "description": ""
4743
+ },
4744
+ "socketUrl": {
4745
+ "type": {
4746
+ "name": "string"
4747
+ },
4748
+ "required": true,
4749
+ "description": ""
4750
+ },
4751
+ "reconnectLimit": {
4752
+ "type": {
4753
+ "name": "number"
4754
+ },
4755
+ "required": false,
4756
+ "description": ""
4757
+ },
4758
+ "reconnectInterval": {
4759
+ "type": {
4760
+ "name": "number"
4761
+ },
4762
+ "required": false,
4763
+ "description": ""
4764
+ },
4765
+ "operation": {
4766
+ "type": {
4767
+ "name": "enum",
4768
+ "value": [
4769
+ {
4770
+ "value": "'connect'",
4771
+ "computed": false
4772
+ },
4773
+ {
4774
+ "value": "'disconnect'",
4775
+ "computed": false
4776
+ },
4777
+ {
4778
+ "value": "'send'",
4779
+ "computed": false
4780
+ }
4781
+ ]
4782
+ },
4783
+ "required": false,
4784
+ "description": ""
4785
+ },
4786
+ "message": {
4787
+ "type": {
4788
+ "name": "string"
4789
+ },
4790
+ "required": false,
4791
+ "description": ""
4792
+ },
4793
+ "latestMessage": {
4794
+ "type": {
4795
+ "name": "string"
4796
+ },
4797
+ "required": false,
4798
+ "description": ""
4799
+ },
4800
+ "state": {
4801
+ "type": {
4802
+ "name": "enum",
4803
+ "value": [
4804
+ {
4805
+ "value": "'connecting'",
4806
+ "computed": false
4807
+ },
4808
+ {
4809
+ "value": "'open'",
4810
+ "computed": false
4811
+ },
4812
+ {
4813
+ "value": "'closing'",
4814
+ "computed": false
4815
+ },
4816
+ {
4817
+ "value": "'closed'",
4818
+ "computed": false
4819
+ }
4820
+ ]
4821
+ },
4822
+ "required": false,
4823
+ "description": ""
4824
+ },
4825
+ "setProps": {
4826
+ "type": {
4827
+ "name": "func"
4828
+ },
4829
+ "required": false,
4830
+ "description": "Dash-assigned callback that should be called to report property changes\r\nto Dash, to make them available for callbacks."
4831
+ },
4832
+ "loading_state": {
4833
+ "type": {
4834
+ "name": "shape",
4835
+ "value": {
4836
+ "is_loading": {
4837
+ "name": "bool",
4838
+ "description": "Determines if the component is loading or not",
4839
+ "required": false
4840
+ },
4841
+ "prop_name": {
4842
+ "name": "string",
4843
+ "description": "Holds which property is loading",
4844
+ "required": false
4845
+ },
4846
+ "component_name": {
4847
+ "name": "string",
4848
+ "description": "Holds the name of the component that is loading",
4849
+ "required": false
4850
+ }
4851
+ }
4852
+ },
4853
+ "required": false,
4854
+ "description": ""
4855
+ }
4856
+ }
4857
+ },
4732
4858
  "src/lib/components/listeners/FefferyWindowSize.react.js": {
4733
4859
  "description": "",
4734
4860
  "displayName": "FefferyWindowSize",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feffery_utils_components",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Build more utility components for Plotly Dash.",
5
5
  "repository": {
6
6
  "type": "git",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feffery_utils_components",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Build more utility components for Plotly Dash.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,7 +3,7 @@ module FefferyUtilsComponents
3
3
  using Dash
4
4
 
5
5
  const resources_path = realpath(joinpath( @__DIR__, "..", "deps"))
6
- const version = "0.1.7"
6
+ const version = "0.1.8"
7
7
 
8
8
  include("jl/''_fefferyblockcolorpicker.jl")
9
9
  include("jl/''_fefferycirclecolorpicker.jl")
@@ -46,6 +46,7 @@ include("jl/''_fefferyidle.jl")
46
46
  include("jl/''_fefferyinviewport.jl")
47
47
  include("jl/''_fefferykeypress.jl")
48
48
  include("jl/''_fefferyresponsive.jl")
49
+ include("jl/''_fefferywebsocket.jl")
49
50
  include("jl/''_fefferywindowsize.jl")
50
51
  include("jl/''_fefferysortablecontainer.jl")
51
52
  include("jl/''_fefferysortableitem.jl")
@@ -62,14 +63,14 @@ function __init__()
62
63
  [
63
64
  DashBase.Resource(
64
65
  relative_package_path = "feffery_utils_components.min.js",
65
- external_url = "https://unpkg.com/feffery_utils_components@0.1.7/feffery_utils_components/feffery_utils_components.min.js",
66
+ external_url = "https://unpkg.com/feffery_utils_components@0.1.8/feffery_utils_components/feffery_utils_components.min.js",
66
67
  dynamic = nothing,
67
68
  async = nothing,
68
69
  type = :js
69
70
  ),
70
71
  DashBase.Resource(
71
72
  relative_package_path = "feffery_utils_components.min.js.map",
72
- external_url = "https://unpkg.com/feffery_utils_components@0.1.7/feffery_utils_components/feffery_utils_components.min.js.map",
73
+ external_url = "https://unpkg.com/feffery_utils_components@0.1.8/feffery_utils_components/feffery_utils_components.min.js.map",
73
74
  dynamic = true,
74
75
  async = nothing,
75
76
  type = :js
@@ -0,0 +1,30 @@
1
+ # AUTO GENERATED FILE - DO NOT EDIT
2
+
3
+ export ''_fefferywebsocket
4
+
5
+ """
6
+ ''_fefferywebsocket(;kwargs...)
7
+
8
+ A FefferyWebSocket component.
9
+
10
+ Keyword arguments:
11
+ - `id` (String; optional)
12
+ - `latestMessage` (String; optional)
13
+ - `loading_state` (optional): . loading_state has the following type: lists containing elements 'is_loading', 'prop_name', 'component_name'.
14
+ Those elements have the following types:
15
+ - `is_loading` (Bool; optional): Determines if the component is loading or not
16
+ - `prop_name` (String; optional): Holds which property is loading
17
+ - `component_name` (String; optional): Holds the name of the component that is loading
18
+ - `message` (String; optional)
19
+ - `operation` (a value equal to: 'connect', 'disconnect', 'send'; optional)
20
+ - `reconnectInterval` (Real; optional)
21
+ - `reconnectLimit` (Real; optional)
22
+ - `socketUrl` (String; required)
23
+ - `state` (a value equal to: 'connecting', 'open', 'closing', 'closed'; optional)
24
+ """
25
+ function ''_fefferywebsocket(; kwargs...)
26
+ available_props = Symbol[:id, :latestMessage, :loading_state, :message, :operation, :reconnectInterval, :reconnectLimit, :socketUrl, :state]
27
+ wild_props = Symbol[]
28
+ return Component("''_fefferywebsocket", "FefferyWebSocket", "feffery_utils_components", available_props, wild_props; kwargs...)
29
+ end
30
+
@@ -0,0 +1,128 @@
1
+ import { useEffect } from 'react';
2
+ import { useWebSocket } from 'ahooks';
3
+ import PropTypes from 'prop-types';
4
+
5
+ const num2state = new Map([
6
+ [0, 'connecting'],
7
+ [1, 'open'],
8
+ [2, 'closing'],
9
+ [3, 'closed']
10
+ ])
11
+
12
+ // 定义WebSocket通信组件FefferyWebSocket
13
+ const FefferyWebSocket = (props) => {
14
+
15
+ const {
16
+ id,
17
+ socketUrl,
18
+ reconnectLimit,
19
+ reconnectInterval,
20
+ operation,
21
+ message,
22
+ setProps,
23
+ loading_state
24
+ } = props;
25
+
26
+ const { readyState: _readyState, sendMessage, latestMessage: _latestMessage, disconnect, connect } = useWebSocket(
27
+ socketUrl,
28
+ {
29
+ reconnectLimit,
30
+ reconnectInterval
31
+ }
32
+ );
33
+
34
+ // 监听服务器发送来的最新信息
35
+ useEffect(() => {
36
+ if (_latestMessage) {
37
+ setProps({
38
+ latestMessage: _latestMessage.data
39
+ })
40
+ }
41
+ }, [_latestMessage])
42
+
43
+ // 监听当前WebSocket连接的状态
44
+ useEffect(() => {
45
+ setProps({
46
+ state: num2state.get(_readyState)
47
+ })
48
+ }, [_readyState])
49
+
50
+ // 监听operation,从而执行不同操作
51
+ useEffect(() => {
52
+ if (operation) {
53
+ // 断开连接操作
54
+ if (operation === 'connect') {
55
+ connect()
56
+ } else if (operation === 'disconnect') {
57
+ disconnect()
58
+ } else if (operation === 'send') {
59
+ sendMessage(message)
60
+ }
61
+ // 重置operation
62
+ setProps({
63
+ operation: null
64
+ })
65
+ }
66
+ }, [operation])
67
+
68
+ return (<div
69
+ id={id}
70
+ data-dash-is-loading={
71
+ (loading_state && loading_state.is_loading) || undefined
72
+ } />);
73
+ }
74
+
75
+ // 定义参数或属性
76
+ FefferyWebSocket.propTypes = {
77
+ // 部件id
78
+ id: PropTypes.string,
79
+
80
+ // 设置要建立连接的WebSocket服务url
81
+ socketUrl: PropTypes.string.isRequired,
82
+
83
+ // 设置连接重试次数,默认为3
84
+ reconnectLimit: PropTypes.number,
85
+
86
+ // 设置连接重试间隔,默认为3000,单位:毫秒
87
+ reconnectInterval: PropTypes.number,
88
+
89
+ // 用于执行连接/断开连接/发送信息操作,可选项有'connect'、'disconnect'、'send'
90
+ // 每次新操作执行后,operation参数都会被重置为null
91
+ operation: PropTypes.oneOf(['connect', 'disconnect', 'send']),
92
+
93
+ // 当operation更新为'send'时,用于设置要通过WebSocket服务向服务器发送的数据
94
+ message: PropTypes.string,
95
+
96
+ // 监听从服务器传来的最新信息
97
+ latestMessage: PropTypes.string,
98
+
99
+ // 用于监听当前此连接的状态,有'connecting'、'open'、'closing'、'closed'四种状态
100
+ state: PropTypes.oneOf(['connecting', 'open', 'closing', 'closed']),
101
+
102
+ /**
103
+ * Dash-assigned callback that should be called to report property changes
104
+ * to Dash, to make them available for callbacks.
105
+ */
106
+ setProps: PropTypes.func,
107
+
108
+ loading_state: PropTypes.shape({
109
+ /**
110
+ * Determines if the component is loading or not
111
+ */
112
+ is_loading: PropTypes.bool,
113
+ /**
114
+ * Holds which property is loading
115
+ */
116
+ prop_name: PropTypes.string,
117
+ /**
118
+ * Holds the name of the component that is loading
119
+ */
120
+ component_name: PropTypes.string
121
+ })
122
+ };
123
+
124
+ // 设置默认参数
125
+ FefferyWebSocket.defaultProps = {
126
+ }
127
+
128
+ export default React.memo(FefferyWebSocket);
package/src/lib/index.js CHANGED
@@ -46,6 +46,7 @@ import FefferyEyeDropper from "./components/colorPickers/FefferyEyeDropper.react
46
46
  import FefferySticky from "./components/FefferySticky.react";
47
47
  import FefferyLazyLoadImage from "./components/FefferyLazyLoadImage.react";
48
48
  import FefferySessionStorage from "./components/store/FefferySessionStorage";
49
+ import FefferyWebSocket from "./components/listeners/FefferyWebSocket.react";
49
50
 
50
51
  /*
51
52
  忽略部分设计React中规范的console警告信息
@@ -126,5 +127,6 @@ export {
126
127
  FefferyEyeDropper,
127
128
  FefferySticky,
128
129
  FefferyLazyLoadImage,
129
- FefferySessionStorage
130
+ FefferySessionStorage,
131
+ FefferyWebSocket
130
132
  };