@nocobase/client 0.12.0-alpha.5 → 0.13.0-alpha.2
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.
- package/es/acl/ACLProvider.js +4 -2
- package/es/api-client/APIClient.d.ts +2 -0
- package/es/api-client/APIClient.js +15 -5
- package/es/appInfo/CurrentAppInfoProvider.js +4 -2
- package/es/application/Application.d.ts +12 -6
- package/es/application/Application.js +62 -9
- package/es/application/PluginManager.js +2 -2
- package/es/application/WebSocketClient.d.ts +32 -0
- package/es/application/WebSocketClient.js +269 -0
- package/es/application/components/AppComponent.js +17 -12
- package/es/application/context.d.ts +1 -1
- package/es/application/hooks/useAppPluginLoad.js +3 -0
- package/es/application/hooks/useAppSpin.d.ts +4 -0
- package/es/application/hooks/useAppSpin.js +12 -0
- package/es/application/utils/remotePlugins.d.ts +2 -3
- package/es/application/utils/remotePlugins.js +44 -40
- package/es/collection-manager/CollectionHistoryProvider.js +4 -2
- package/es/collection-manager/CollectionManagerProvider.js +4 -2
- package/es/collection-manager/Configuration/EditFieldAction.js +3 -0
- package/es/collection-manager/Configuration/ViewInheritedField.js +3 -0
- package/es/collection-manager/Configuration/components/CollectionCategory.js +1 -0
- package/es/collection-manager/interfaces/index.d.ts +1 -0
- package/es/collection-manager/interfaces/index.js +2 -1
- package/es/collection-manager/interfaces/username.d.ts +2 -0
- package/es/collection-manager/interfaces/username.js +44 -0
- package/es/collection-manager/templates/components/PreviewFields.js +7 -4
- package/es/collection-manager/templates/components/PreviewTable.js +22 -13
- package/es/locale/en_US.d.ts +2 -0
- package/es/locale/en_US.js +3 -1
- package/es/locale/zh_CN.d.ts +4 -2
- package/es/locale/zh_CN.js +4 -2
- package/es/nocobase-buildin-plugin/index.js +151 -10
- package/es/nocobase-buildin-plugin/plugins/LocalePlugin.js +3 -3
- package/es/pm/Card.js +13 -16
- package/es/route-switch/antd/admin-layout/index.js +8 -3
- package/es/schema-component/antd/association-field/InternalTag.js +1 -3
- package/es/schema-component/antd/association-field/InternalViewer.js +2 -1
- package/es/schema-component/antd/form-item/FormItem.js +4 -1
- package/es/schema-component/antd/gantt/helpers/other-helper.d.ts +1 -1
- package/es/schema-component/antd/page/Page.js +5 -2
- package/es/schema-settings/SchemaSettings.js +4 -4
- package/es/schema-templates/SchemaTemplateManagerProvider.js +4 -2
- package/es/system-settings/SystemSettingsProvider.js +5 -3
- package/es/user/CurrentUser.js +7 -12
- package/es/user/CurrentUserProvider.js +4 -2
- package/es/user/EditProfile.js +11 -4
- package/lib/acl/ACLProvider.js +4 -2
- package/lib/api-client/APIClient.d.ts +2 -0
- package/lib/api-client/APIClient.js +15 -5
- package/lib/appInfo/CurrentAppInfoProvider.js +4 -2
- package/lib/application/Application.d.ts +12 -6
- package/lib/application/Application.js +62 -9
- package/lib/application/PluginManager.js +2 -2
- package/lib/application/WebSocketClient.d.ts +32 -0
- package/lib/application/WebSocketClient.js +277 -0
- package/lib/application/components/AppComponent.js +16 -11
- package/lib/application/context.d.ts +1 -1
- package/lib/application/hooks/useAppPluginLoad.js +3 -0
- package/lib/application/hooks/useAppSpin.d.ts +4 -0
- package/lib/application/hooks/useAppSpin.js +20 -0
- package/lib/application/utils/remotePlugins.d.ts +2 -3
- package/lib/application/utils/remotePlugins.js +44 -40
- package/lib/collection-manager/CollectionHistoryProvider.js +4 -2
- package/lib/collection-manager/CollectionManagerProvider.js +4 -2
- package/lib/collection-manager/Configuration/EditFieldAction.js +3 -0
- package/lib/collection-manager/Configuration/ViewInheritedField.js +3 -0
- package/lib/collection-manager/Configuration/components/CollectionCategory.js +1 -0
- package/lib/collection-manager/interfaces/index.d.ts +1 -0
- package/lib/collection-manager/interfaces/index.js +11 -0
- package/lib/collection-manager/interfaces/username.d.ts +2 -0
- package/lib/collection-manager/interfaces/username.js +51 -0
- package/lib/collection-manager/templates/components/PreviewFields.js +7 -4
- package/lib/collection-manager/templates/components/PreviewTable.js +22 -13
- package/lib/locale/en_US.d.ts +2 -0
- package/lib/locale/en_US.js +3 -1
- package/lib/locale/zh_CN.d.ts +4 -2
- package/lib/locale/zh_CN.js +4 -2
- package/lib/nocobase-buildin-plugin/index.js +150 -9
- package/lib/nocobase-buildin-plugin/plugins/LocalePlugin.js +3 -3
- package/lib/pm/Card.js +13 -16
- package/lib/route-switch/antd/admin-layout/index.js +7 -2
- package/lib/schema-component/antd/association-field/InternalTag.js +2 -4
- package/lib/schema-component/antd/association-field/InternalViewer.js +2 -1
- package/lib/schema-component/antd/form-item/FormItem.js +4 -1
- package/lib/schema-component/antd/gantt/helpers/other-helper.d.ts +1 -1
- package/lib/schema-component/antd/page/Page.js +4 -1
- package/lib/schema-settings/SchemaSettings.js +1 -1
- package/lib/schema-templates/SchemaTemplateManagerProvider.js +4 -2
- package/lib/system-settings/SystemSettingsProvider.js +6 -4
- package/lib/user/CurrentUser.js +7 -12
- package/lib/user/CurrentUserProvider.js +4 -2
- package/lib/user/EditProfile.js +11 -4
- package/package.json +5 -5
package/es/acl/ACLProvider.js
CHANGED
|
@@ -11,10 +11,10 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|
|
11
11
|
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
12
12
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
13
13
|
import { useField, useFieldSchema } from '@formily/react';
|
|
14
|
-
import { Spin } from 'antd';
|
|
15
14
|
import React, { createContext, useContext, useEffect } from 'react';
|
|
16
15
|
import { Navigate } from 'react-router-dom';
|
|
17
16
|
import { useAPIClient, useRequest } from '../api-client';
|
|
17
|
+
import { useAppSpin } from '../application/hooks/useAppSpin';
|
|
18
18
|
import { useBlockRequestContext } from '../block-provider/BlockProvider';
|
|
19
19
|
import { useCollection } from '../collection-manager';
|
|
20
20
|
import { useResourceActionContext } from '../collection-manager/ResourceActionProvider';
|
|
@@ -43,6 +43,8 @@ export var ACLRolesCheckProvider = function ACLRolesCheckProvider(props) {
|
|
|
43
43
|
var route = getRouteUrl(props.children.props);
|
|
44
44
|
var _useDesignable = useDesignable(),
|
|
45
45
|
setDesignable = _useDesignable.setDesignable;
|
|
46
|
+
var _useAppSpin = useAppSpin(),
|
|
47
|
+
render = _useAppSpin.render;
|
|
46
48
|
var api = useAPIClient();
|
|
47
49
|
var result = useRequest({
|
|
48
50
|
url: 'roles:check'
|
|
@@ -59,7 +61,7 @@ export var ACLRolesCheckProvider = function ACLRolesCheckProvider(props) {
|
|
|
59
61
|
}
|
|
60
62
|
});
|
|
61
63
|
if (result.loading) {
|
|
62
|
-
return
|
|
64
|
+
return render();
|
|
63
65
|
}
|
|
64
66
|
if (result.error) {
|
|
65
67
|
return /*#__PURE__*/React.createElement(Navigate, {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { APIClient as APIClientSDK } from '@nocobase/sdk';
|
|
2
2
|
import { Result } from 'ahooks/es/useRequest/src/types';
|
|
3
|
+
import { Application } from '../application';
|
|
3
4
|
export declare class APIClient extends APIClientSDK {
|
|
4
5
|
services: Record<string, Result<any, any>>;
|
|
5
6
|
silence: boolean;
|
|
7
|
+
app: Application;
|
|
6
8
|
/** 该值会在 AntdAppProvider 中被重新赋值 */
|
|
7
9
|
notification: any;
|
|
8
10
|
service(uid: string): Result<any, any>;
|
|
@@ -24,9 +24,7 @@ var handleErrorMessage = function handleErrorMessage(error, notification) {
|
|
|
24
24
|
var _JSON$parse$errors, _JSON$parse$errors$ma;
|
|
25
25
|
notification.error({
|
|
26
26
|
message: (_JSON$parse$errors = JSON.parse(reader.result).errors) === null || _JSON$parse$errors === void 0 ? void 0 : (_JSON$parse$errors$ma = _JSON$parse$errors.map) === null || _JSON$parse$errors$ma === void 0 ? void 0 : _JSON$parse$errors$ma.call(_JSON$parse$errors, function (error) {
|
|
27
|
-
return /*#__PURE__*/React.createElement('div', {
|
|
28
|
-
children: error.message
|
|
29
|
-
});
|
|
27
|
+
return /*#__PURE__*/React.createElement('div', {}, error.message);
|
|
30
28
|
})
|
|
31
29
|
});
|
|
32
30
|
};
|
|
@@ -44,6 +42,7 @@ export var APIClient = /*#__PURE__*/function (_APIClientSDK) {
|
|
|
44
42
|
_this = _super.call.apply(_super, [this].concat(args));
|
|
45
43
|
_this.services = {};
|
|
46
44
|
_this.silence = false;
|
|
45
|
+
_this.app = void 0;
|
|
47
46
|
/** 该值会在 AntdAppProvider 中被重新赋值 */
|
|
48
47
|
_this.notification = notification;
|
|
49
48
|
return _this;
|
|
@@ -85,11 +84,22 @@ export var APIClient = /*#__PURE__*/function (_APIClientSDK) {
|
|
|
85
84
|
if ((error === null || error === void 0 ? void 0 : (_error$response3 = error.response) === null || _error$response3 === void 0 ? void 0 : (_error$response3$data = _error$response3.data) === null || _error$response3$data === void 0 ? void 0 : _error$response3$data.type) === 'application/json') {
|
|
86
85
|
handleErrorMessage(error, _this2.notification);
|
|
87
86
|
} else {
|
|
88
|
-
var _error$response4, _error$response4$data, _errs, _errs$map;
|
|
87
|
+
var _error$response4, _error$response4$data, _error$response4$data2, _error$response6, _error$response6$data, _errs, _errs$map;
|
|
89
88
|
if (errorCache.size > 10) {
|
|
90
89
|
errorCache.clear();
|
|
91
90
|
}
|
|
92
|
-
var
|
|
91
|
+
var maintaining = !!(error !== null && error !== void 0 && (_error$response4 = error.response) !== null && _error$response4 !== void 0 && (_error$response4$data = _error$response4.data) !== null && _error$response4$data !== void 0 && (_error$response4$data2 = _error$response4$data.error) !== null && _error$response4$data2 !== void 0 && _error$response4$data2.maintaining);
|
|
92
|
+
if (_this2.app.maintaining !== maintaining) {
|
|
93
|
+
_this2.app.maintaining = maintaining;
|
|
94
|
+
}
|
|
95
|
+
if (_this2.app.maintaining) {
|
|
96
|
+
var _error$response5, _error$response5$data;
|
|
97
|
+
_this2.app.error = error === null || error === void 0 ? void 0 : (_error$response5 = error.response) === null || _error$response5 === void 0 ? void 0 : (_error$response5$data = _error$response5.data) === null || _error$response5$data === void 0 ? void 0 : _error$response5$data.error;
|
|
98
|
+
return;
|
|
99
|
+
} else if (_this2.app.error) {
|
|
100
|
+
_this2.app.error = null;
|
|
101
|
+
}
|
|
102
|
+
var errs = (error === null || error === void 0 ? void 0 : (_error$response6 = error.response) === null || _error$response6 === void 0 ? void 0 : (_error$response6$data = _error$response6.data) === null || _error$response6$data === void 0 ? void 0 : _error$response6$data.errors) || [{
|
|
93
103
|
message: 'Server error'
|
|
94
104
|
}];
|
|
95
105
|
errs = errs.filter(function (error) {
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { Spin } from 'antd';
|
|
2
1
|
import React, { createContext, useContext } from 'react';
|
|
3
2
|
import { useRequest } from '../api-client';
|
|
3
|
+
import { useAppSpin } from '../application/hooks/useAppSpin';
|
|
4
4
|
export var CurrentAppInfoContext = /*#__PURE__*/createContext(null);
|
|
5
5
|
export var useCurrentAppInfo = function useCurrentAppInfo() {
|
|
6
6
|
return useContext(CurrentAppInfoContext);
|
|
7
7
|
};
|
|
8
8
|
export var CurrentAppInfoProvider = function CurrentAppInfoProvider(props) {
|
|
9
|
+
var _useAppSpin = useAppSpin(),
|
|
10
|
+
render = _useAppSpin.render;
|
|
9
11
|
var result = useRequest({
|
|
10
12
|
url: 'app:getInfo'
|
|
11
13
|
});
|
|
12
14
|
if (result.loading) {
|
|
13
|
-
return
|
|
15
|
+
return render();
|
|
14
16
|
}
|
|
15
17
|
return /*#__PURE__*/React.createElement(CurrentAppInfoContext.Provider, {
|
|
16
18
|
value: result.data
|
|
@@ -5,24 +5,27 @@ import { APIClient } from '../api-client';
|
|
|
5
5
|
import type { Plugin } from './Plugin';
|
|
6
6
|
import { PluginManager, PluginType } from './PluginManager';
|
|
7
7
|
import { ComponentTypeAndString, RouterManager, RouterOptions } from './RouterManager';
|
|
8
|
+
import { WebSocketClient, WebSocketClientOptions } from './WebSocketClient';
|
|
8
9
|
import type { RequireJS } from './utils/requirejs';
|
|
9
10
|
declare global {
|
|
10
11
|
interface Window {
|
|
11
12
|
define: RequireJS['define'];
|
|
12
13
|
}
|
|
13
14
|
}
|
|
15
|
+
export type DevDynamicImport = (packageName: string) => Promise<{
|
|
16
|
+
default: typeof Plugin;
|
|
17
|
+
}>;
|
|
14
18
|
export type ComponentAndProps<T = any> = [ComponentType, T];
|
|
15
19
|
export interface ApplicationOptions {
|
|
16
20
|
apiClient?: APIClientOptions;
|
|
21
|
+
ws?: WebSocketClientOptions | boolean;
|
|
17
22
|
i18n?: i18next;
|
|
18
23
|
providers?: (ComponentType | ComponentAndProps)[];
|
|
19
24
|
plugins?: PluginType[];
|
|
20
25
|
components?: Record<string, ComponentType>;
|
|
21
26
|
scopes?: Record<string, any>;
|
|
22
27
|
router?: RouterOptions;
|
|
23
|
-
|
|
24
|
-
default: typeof Plugin;
|
|
25
|
-
}>>;
|
|
28
|
+
devDynamicImport?: DevDynamicImport;
|
|
26
29
|
}
|
|
27
30
|
export declare class Application {
|
|
28
31
|
protected options: ApplicationOptions;
|
|
@@ -30,13 +33,16 @@ export declare class Application {
|
|
|
30
33
|
router: RouterManager;
|
|
31
34
|
scopes: Record<string, any>;
|
|
32
35
|
i18n: i18next;
|
|
36
|
+
ws: WebSocketClient;
|
|
33
37
|
apiClient: APIClient;
|
|
34
38
|
components: Record<string, ComponentType>;
|
|
35
39
|
pm: PluginManager;
|
|
36
|
-
|
|
37
|
-
default: typeof Plugin;
|
|
38
|
-
}>>;
|
|
40
|
+
devDynamicImport: DevDynamicImport;
|
|
39
41
|
requirejs: RequireJS;
|
|
42
|
+
loading: boolean;
|
|
43
|
+
maintained: boolean;
|
|
44
|
+
maintaining: boolean;
|
|
45
|
+
error: any;
|
|
40
46
|
constructor(options?: ApplicationOptions);
|
|
41
47
|
private initRequireJs;
|
|
42
48
|
private addDefaultProviders;
|
|
@@ -16,6 +16,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
|
|
|
16
16
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
17
17
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
18
18
|
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
19
|
+
import { define, observable } from '@formily/reactive';
|
|
19
20
|
import get from 'lodash/get';
|
|
20
21
|
import merge from 'lodash/merge';
|
|
21
22
|
import set from 'lodash/set';
|
|
@@ -27,6 +28,7 @@ import { APIClient, APIClientProvider } from '../api-client';
|
|
|
27
28
|
import { i18n } from '../i18n';
|
|
28
29
|
import { PluginManager } from './PluginManager';
|
|
29
30
|
import { RouterManager } from './RouterManager';
|
|
31
|
+
import { WebSocketClient } from './WebSocketClient';
|
|
30
32
|
import { AppComponent, BlankComponent, defaultAppComponents } from './components';
|
|
31
33
|
import { compose, normalizeContainer } from './utils';
|
|
32
34
|
import { defineGlobalDeps } from './utils/globalDeps';
|
|
@@ -40,17 +42,29 @@ export var Application = /*#__PURE__*/function () {
|
|
|
40
42
|
this.router = void 0;
|
|
41
43
|
this.scopes = {};
|
|
42
44
|
this.i18n = void 0;
|
|
45
|
+
this.ws = void 0;
|
|
43
46
|
this.apiClient = void 0;
|
|
44
47
|
this.components = _objectSpread({}, defaultAppComponents);
|
|
45
48
|
this.pm = void 0;
|
|
46
|
-
this.
|
|
49
|
+
this.devDynamicImport = void 0;
|
|
47
50
|
this.requirejs = void 0;
|
|
51
|
+
this.loading = true;
|
|
52
|
+
this.maintained = false;
|
|
53
|
+
this.maintaining = false;
|
|
54
|
+
this.error = null;
|
|
48
55
|
this.options = options;
|
|
49
56
|
this.initRequireJs();
|
|
50
|
-
this
|
|
57
|
+
define(this, {
|
|
58
|
+
maintained: observable.ref,
|
|
59
|
+
loading: observable.ref,
|
|
60
|
+
maintaining: observable.ref,
|
|
61
|
+
error: observable.ref
|
|
62
|
+
});
|
|
63
|
+
this.devDynamicImport = options.devDynamicImport;
|
|
51
64
|
this.scopes = merge(this.scopes, options.scopes);
|
|
52
65
|
this.components = merge(this.components, options.components);
|
|
53
66
|
this.apiClient = new APIClient(options.apiClient);
|
|
67
|
+
this.apiClient.app = this;
|
|
54
68
|
this.i18n = options.i18n || i18n;
|
|
55
69
|
this.router = new RouterManager(_objectSpread(_objectSpread({}, options.router), {}, {
|
|
56
70
|
renderComponent: this.renderComponent.bind(this)
|
|
@@ -59,6 +73,7 @@ export var Application = /*#__PURE__*/function () {
|
|
|
59
73
|
this.addDefaultProviders();
|
|
60
74
|
this.addReactRouterComponents();
|
|
61
75
|
this.addProviders(options.providers || []);
|
|
76
|
+
this.ws = new WebSocketClient(options.ws);
|
|
62
77
|
}
|
|
63
78
|
_createClass(Application, [{
|
|
64
79
|
key: "initRequireJs",
|
|
@@ -119,15 +134,53 @@ export var Application = /*#__PURE__*/function () {
|
|
|
119
134
|
key: "load",
|
|
120
135
|
value: function () {
|
|
121
136
|
var _load = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
137
|
+
var _this2 = this;
|
|
122
138
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
123
139
|
while (1) switch (_context.prev = _context.next) {
|
|
124
140
|
case 0:
|
|
125
|
-
|
|
126
|
-
|
|
141
|
+
this.ws.on('message', function (event) {
|
|
142
|
+
var data = JSON.parse(event.data);
|
|
143
|
+
console.log(data.payload);
|
|
144
|
+
var maintaining = data.type === 'maintaining' && data.payload.code !== 'APP_RUNNING';
|
|
145
|
+
if (maintaining) {
|
|
146
|
+
_this2.maintaining = true;
|
|
147
|
+
_this2.error = data.payload;
|
|
148
|
+
} else if (_this2.maintaining) {
|
|
149
|
+
// && !this.maintained
|
|
150
|
+
window.location.reload();
|
|
151
|
+
return;
|
|
152
|
+
} else {
|
|
153
|
+
_this2.maintaining = false;
|
|
154
|
+
_this2.maintained = true;
|
|
155
|
+
_this2.error = null;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
this.ws.on('serverDown', function () {
|
|
159
|
+
_this2.maintaining = true;
|
|
160
|
+
});
|
|
161
|
+
this.ws.connect();
|
|
162
|
+
_context.prev = 3;
|
|
163
|
+
this.loading = true;
|
|
164
|
+
_context.next = 7;
|
|
165
|
+
return this.pm.load();
|
|
166
|
+
case 7:
|
|
167
|
+
_context.next = 13;
|
|
168
|
+
break;
|
|
169
|
+
case 9:
|
|
170
|
+
_context.prev = 9;
|
|
171
|
+
_context.t0 = _context["catch"](3);
|
|
172
|
+
this.error = {
|
|
173
|
+
code: 'LOAD_ERROR',
|
|
174
|
+
message: _context.t0.message
|
|
175
|
+
};
|
|
176
|
+
console.error(_context.t0);
|
|
177
|
+
case 13:
|
|
178
|
+
this.loading = false;
|
|
179
|
+
case 14:
|
|
127
180
|
case "end":
|
|
128
181
|
return _context.stop();
|
|
129
182
|
}
|
|
130
|
-
}, _callee, this);
|
|
183
|
+
}, _callee, this, [[3, 9]]);
|
|
131
184
|
}));
|
|
132
185
|
function load() {
|
|
133
186
|
return _load.apply(this, arguments);
|
|
@@ -177,9 +230,9 @@ export var Application = /*#__PURE__*/function () {
|
|
|
177
230
|
}, {
|
|
178
231
|
key: "addComponents",
|
|
179
232
|
value: function addComponents(components) {
|
|
180
|
-
var
|
|
233
|
+
var _this3 = this;
|
|
181
234
|
Object.keys(components).forEach(function (name) {
|
|
182
|
-
|
|
235
|
+
_this3.addComponent(components[name], name);
|
|
183
236
|
});
|
|
184
237
|
}
|
|
185
238
|
}, {
|
|
@@ -190,10 +243,10 @@ export var Application = /*#__PURE__*/function () {
|
|
|
190
243
|
}, {
|
|
191
244
|
key: "getRootComponent",
|
|
192
245
|
value: function getRootComponent() {
|
|
193
|
-
var
|
|
246
|
+
var _this4 = this;
|
|
194
247
|
var Root = function Root() {
|
|
195
248
|
return /*#__PURE__*/React.createElement(AppComponent, {
|
|
196
|
-
app:
|
|
249
|
+
app: _this4
|
|
197
250
|
});
|
|
198
251
|
};
|
|
199
252
|
return Root;
|
|
@@ -142,13 +142,13 @@ export var PluginManager = /*#__PURE__*/function () {
|
|
|
142
142
|
});
|
|
143
143
|
case 3:
|
|
144
144
|
res = _context3.sent;
|
|
145
|
-
pluginList = ((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.data) || [];
|
|
145
|
+
pluginList = (res === null || res === void 0 ? void 0 : (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.data) || [];
|
|
146
146
|
_context3.next = 7;
|
|
147
147
|
return getPlugins({
|
|
148
148
|
requirejs: this.app.requirejs,
|
|
149
149
|
pluginData: pluginList,
|
|
150
150
|
baseURL: (_this$app$apiClient$a = this.app.apiClient.axios) === null || _this$app$apiClient$a === void 0 ? void 0 : (_this$app$apiClient$a2 = _this$app$apiClient$a.defaults) === null || _this$app$apiClient$a2 === void 0 ? void 0 : _this$app$apiClient$a2.baseURL,
|
|
151
|
-
|
|
151
|
+
devDynamicImport: this.app.devDynamicImport
|
|
152
152
|
});
|
|
153
153
|
case 7:
|
|
154
154
|
plugins = _context3.sent;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare const getWebSocketURL: () => string;
|
|
2
|
+
export type WebSocketClientOptions = {
|
|
3
|
+
reconnectInterval?: number;
|
|
4
|
+
reconnectAttempts?: number;
|
|
5
|
+
pingInterval?: number;
|
|
6
|
+
url?: string;
|
|
7
|
+
protocols?: string | string[];
|
|
8
|
+
onServerDown?: any;
|
|
9
|
+
};
|
|
10
|
+
export declare class WebSocketClient {
|
|
11
|
+
protected _ws: WebSocket;
|
|
12
|
+
protected _reconnectTimes: number;
|
|
13
|
+
protected events: any[];
|
|
14
|
+
protected options: WebSocketClientOptions;
|
|
15
|
+
protected enabled: boolean;
|
|
16
|
+
connected: boolean;
|
|
17
|
+
serverDown: boolean;
|
|
18
|
+
lastMessage: {};
|
|
19
|
+
constructor(options: WebSocketClientOptions | boolean);
|
|
20
|
+
get reconnectAttempts(): number;
|
|
21
|
+
get reconnectInterval(): number;
|
|
22
|
+
get pingInterval(): number;
|
|
23
|
+
get readyState(): number;
|
|
24
|
+
connect(): void;
|
|
25
|
+
reconnect(): void;
|
|
26
|
+
close(): void;
|
|
27
|
+
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
|
|
28
|
+
on(type: string, listener: any, options?: boolean | AddEventListenerOptions): void;
|
|
29
|
+
emit(type: string, args: any): void;
|
|
30
|
+
off(type: string, listener: any, options?: boolean | EventListenerOptions): void;
|
|
31
|
+
removeAllListeners(): void;
|
|
32
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
6
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
7
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
8
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
10
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
11
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
12
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
13
|
+
import { define, observable } from '@formily/reactive';
|
|
14
|
+
import { getSubAppName } from '@nocobase/sdk';
|
|
15
|
+
export var getWebSocketURL = function getWebSocketURL() {
|
|
16
|
+
if (!process.env.API_BASE_URL) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
var subApp = getSubAppName();
|
|
20
|
+
var queryString = subApp ? "?__appName=".concat(subApp) : '';
|
|
21
|
+
console.log(queryString);
|
|
22
|
+
if (process.env.WEBSOCKET_URL) {
|
|
23
|
+
return "".concat(process.env.WEBSOCKET_URL).concat(queryString);
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
var url = new URL(process.env.API_BASE_URL);
|
|
27
|
+
return "".concat(url.protocol === 'https:' ? 'wss' : 'ws', "://").concat(url.host, "/ws").concat(queryString);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return "".concat(location.protocol === 'https:' ? 'wss' : 'ws', "://").concat(location.host, "/ws").concat(queryString);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export var WebSocketClient = /*#__PURE__*/function () {
|
|
33
|
+
function WebSocketClient(options) {
|
|
34
|
+
_classCallCheck(this, WebSocketClient);
|
|
35
|
+
this._ws = void 0;
|
|
36
|
+
this._reconnectTimes = 0;
|
|
37
|
+
this.events = [];
|
|
38
|
+
this.options = void 0;
|
|
39
|
+
this.enabled = void 0;
|
|
40
|
+
this.connected = false;
|
|
41
|
+
this.serverDown = false;
|
|
42
|
+
this.lastMessage = {};
|
|
43
|
+
if (!options) {
|
|
44
|
+
this.enabled = false;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.options = options === true ? {} : options;
|
|
48
|
+
this.enabled = true;
|
|
49
|
+
define(this, {
|
|
50
|
+
serverDown: observable.ref,
|
|
51
|
+
connected: observable.ref,
|
|
52
|
+
lastMessage: observable.ref
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
_createClass(WebSocketClient, [{
|
|
56
|
+
key: "reconnectAttempts",
|
|
57
|
+
get: function get() {
|
|
58
|
+
var _this$options;
|
|
59
|
+
return ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.reconnectAttempts) || 30;
|
|
60
|
+
}
|
|
61
|
+
}, {
|
|
62
|
+
key: "reconnectInterval",
|
|
63
|
+
get: function get() {
|
|
64
|
+
var _this$options2;
|
|
65
|
+
return ((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.reconnectInterval) || 1000;
|
|
66
|
+
}
|
|
67
|
+
}, {
|
|
68
|
+
key: "pingInterval",
|
|
69
|
+
get: function get() {
|
|
70
|
+
var _this$options3;
|
|
71
|
+
return ((_this$options3 = this.options) === null || _this$options3 === void 0 ? void 0 : _this$options3.pingInterval) || 30000;
|
|
72
|
+
}
|
|
73
|
+
}, {
|
|
74
|
+
key: "readyState",
|
|
75
|
+
get: function get() {
|
|
76
|
+
if (!this._ws) {
|
|
77
|
+
return -1;
|
|
78
|
+
}
|
|
79
|
+
return this._ws.readyState;
|
|
80
|
+
}
|
|
81
|
+
}, {
|
|
82
|
+
key: "connect",
|
|
83
|
+
value: function connect() {
|
|
84
|
+
var _this = this;
|
|
85
|
+
if (!this.enabled) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (this._reconnectTimes === 0) {
|
|
89
|
+
console.log('[nocobase-ws]: connecting...');
|
|
90
|
+
}
|
|
91
|
+
if (this._reconnectTimes >= this.reconnectAttempts) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (this.readyState === WebSocket.OPEN) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this._reconnectTimes++;
|
|
98
|
+
var ws = new WebSocket(this.options.url || getWebSocketURL(), this.options.protocols);
|
|
99
|
+
var pingIntervalTimer;
|
|
100
|
+
ws.onopen = function () {
|
|
101
|
+
console.log('[nocobase-ws]: connected.');
|
|
102
|
+
_this.serverDown = false;
|
|
103
|
+
if (_this._ws) {
|
|
104
|
+
_this.removeAllListeners();
|
|
105
|
+
}
|
|
106
|
+
_this._reconnectTimes = 0;
|
|
107
|
+
_this._ws = ws;
|
|
108
|
+
var _iterator = _createForOfIteratorHelper(_this.events),
|
|
109
|
+
_step;
|
|
110
|
+
try {
|
|
111
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
112
|
+
var _step$value = _step.value,
|
|
113
|
+
type = _step$value.type,
|
|
114
|
+
listener = _step$value.listener,
|
|
115
|
+
options = _step$value.options;
|
|
116
|
+
_this._ws.addEventListener(type, listener, options);
|
|
117
|
+
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
_iterator.e(err);
|
|
120
|
+
} finally {
|
|
121
|
+
_iterator.f();
|
|
122
|
+
}
|
|
123
|
+
pingIntervalTimer = setInterval(function () {
|
|
124
|
+
return _this.send('ping');
|
|
125
|
+
}, _this.pingInterval);
|
|
126
|
+
_this.connected = true;
|
|
127
|
+
};
|
|
128
|
+
ws.onerror = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
129
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
130
|
+
while (1) switch (_context.prev = _context.next) {
|
|
131
|
+
case 0:
|
|
132
|
+
// setTimeout(() => this.connect(), this.reconnectInterval);
|
|
133
|
+
console.log('onerror', _this.readyState, _this._reconnectTimes);
|
|
134
|
+
case 1:
|
|
135
|
+
case "end":
|
|
136
|
+
return _context.stop();
|
|
137
|
+
}
|
|
138
|
+
}, _callee);
|
|
139
|
+
}));
|
|
140
|
+
ws.onclose = /*#__PURE__*/function () {
|
|
141
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(event) {
|
|
142
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
143
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
144
|
+
case 0:
|
|
145
|
+
setTimeout(function () {
|
|
146
|
+
return _this.connect();
|
|
147
|
+
}, _this.reconnectInterval);
|
|
148
|
+
console.log('onclose', _this.readyState, _this._reconnectTimes, _this.serverDown);
|
|
149
|
+
_this.connected = false;
|
|
150
|
+
clearInterval(pingIntervalTimer);
|
|
151
|
+
if (_this._reconnectTimes >= Math.min(_this.reconnectAttempts, 5)) {
|
|
152
|
+
_this.serverDown = true;
|
|
153
|
+
_this.emit('serverDown', event);
|
|
154
|
+
}
|
|
155
|
+
case 5:
|
|
156
|
+
case "end":
|
|
157
|
+
return _context2.stop();
|
|
158
|
+
}
|
|
159
|
+
}, _callee2);
|
|
160
|
+
}));
|
|
161
|
+
return function (_x) {
|
|
162
|
+
return _ref2.apply(this, arguments);
|
|
163
|
+
};
|
|
164
|
+
}();
|
|
165
|
+
}
|
|
166
|
+
}, {
|
|
167
|
+
key: "reconnect",
|
|
168
|
+
value: function reconnect() {
|
|
169
|
+
this._reconnectTimes = 0;
|
|
170
|
+
this.connect();
|
|
171
|
+
}
|
|
172
|
+
}, {
|
|
173
|
+
key: "close",
|
|
174
|
+
value: function close() {
|
|
175
|
+
if (!this._ws) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
this._reconnectTimes = this.reconnectAttempts;
|
|
179
|
+
return this._ws.close();
|
|
180
|
+
}
|
|
181
|
+
}, {
|
|
182
|
+
key: "send",
|
|
183
|
+
value: function send(data) {
|
|
184
|
+
if (!this._ws) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
return this._ws.send(data);
|
|
188
|
+
}
|
|
189
|
+
}, {
|
|
190
|
+
key: "on",
|
|
191
|
+
value: function on(type, listener, options) {
|
|
192
|
+
this.events.push({
|
|
193
|
+
type: type,
|
|
194
|
+
listener: listener,
|
|
195
|
+
options: options
|
|
196
|
+
});
|
|
197
|
+
if (!this._ws) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
this._ws.addEventListener(type, listener, options);
|
|
201
|
+
}
|
|
202
|
+
}, {
|
|
203
|
+
key: "emit",
|
|
204
|
+
value: function emit(type, args) {
|
|
205
|
+
var _iterator2 = _createForOfIteratorHelper(this.events),
|
|
206
|
+
_step2;
|
|
207
|
+
try {
|
|
208
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
209
|
+
var event = _step2.value;
|
|
210
|
+
if (event.type === type) {
|
|
211
|
+
event.listener(args);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
} catch (err) {
|
|
215
|
+
_iterator2.e(err);
|
|
216
|
+
} finally {
|
|
217
|
+
_iterator2.f();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}, {
|
|
221
|
+
key: "off",
|
|
222
|
+
value: function off(type, listener, options) {
|
|
223
|
+
var index = 0;
|
|
224
|
+
var _iterator3 = _createForOfIteratorHelper(this.events),
|
|
225
|
+
_step3;
|
|
226
|
+
try {
|
|
227
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
228
|
+
var event = _step3.value;
|
|
229
|
+
if (event.type === type && event.listener === listener) {
|
|
230
|
+
this.events.splice(index, 1);
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
index++;
|
|
234
|
+
}
|
|
235
|
+
} catch (err) {
|
|
236
|
+
_iterator3.e(err);
|
|
237
|
+
} finally {
|
|
238
|
+
_iterator3.f();
|
|
239
|
+
}
|
|
240
|
+
if (!this._ws) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
this._ws.removeEventListener(type, listener, options);
|
|
244
|
+
}
|
|
245
|
+
}, {
|
|
246
|
+
key: "removeAllListeners",
|
|
247
|
+
value: function removeAllListeners() {
|
|
248
|
+
if (!this._ws) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
var _iterator4 = _createForOfIteratorHelper(this.events),
|
|
252
|
+
_step4;
|
|
253
|
+
try {
|
|
254
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
255
|
+
var _step4$value = _step4.value,
|
|
256
|
+
type = _step4$value.type,
|
|
257
|
+
listener = _step4$value.listener,
|
|
258
|
+
options = _step4$value.options;
|
|
259
|
+
this._ws.removeEventListener(type, listener, options);
|
|
260
|
+
}
|
|
261
|
+
} catch (err) {
|
|
262
|
+
_iterator4.e(err);
|
|
263
|
+
} finally {
|
|
264
|
+
_iterator4.f();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}]);
|
|
268
|
+
return WebSocketClient;
|
|
269
|
+
}();
|