Skip to content

Commit 0e2e8a0

Browse files
fix 恢复 commonservicebase dataflowservice 事件绑定 review by luox
1 parent 6fe19b3 commit 0e2e8a0

File tree

7 files changed

+140
-21
lines changed

7 files changed

+140
-21
lines changed

src/common/iServer/CommonServiceBase.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* This program are made available under the terms of the Apache License, Version 2.0
33
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
44
import { FetchRequest } from '../util/FetchRequest';
5+
import { Events } from '../commontypes/Events';
56
import { SecurityManager } from '../security/SecurityManager';
67
import { Util } from '../commontypes/Util';
78
import { JSONFormat } from '../format/JSON';
@@ -24,6 +25,12 @@ export class CommonServiceBase {
2425
constructor(url, options) {
2526
let me = this;
2627

28+
this.EVENT_TYPES = [];
29+
30+
this.events = null;
31+
32+
this.eventListeners = null;
33+
2734
this.url = null;
2835

2936
this.urls = null;
@@ -69,6 +76,11 @@ export class CommonServiceBase {
6976

7077
me.isInTheSameDomain = Util.isInTheSameDomain(me.url);
7178

79+
me.events = new Events(me, null, me.EVENT_TYPES, true);
80+
if (me.eventListeners instanceof Object) {
81+
me.events.on(me.eventListeners);
82+
}
83+
7284
this.CLASS_NAME = 'SuperMap.CommonServiceBase';
7385
}
7486

@@ -86,6 +98,14 @@ export class CommonServiceBase {
8698
}
8799
me.url = null;
88100
me.isInTheSameDomain = null;
101+
me.EVENT_TYPES = null;
102+
if (me.events) {
103+
me.events.destroy();
104+
me.events = null;
105+
}
106+
if (me.eventListeners) {
107+
me.eventListeners = null;
108+
}
89109
}
90110

91111
/**
@@ -324,4 +344,5 @@ export class CommonServiceBase {
324344
* @param {Object} serviceResult.result 服务器返回结果。
325345
* @param {Object} serviceResult.object 发布应用程序事件的对象。
326346
* @param {Object} serviceResult.type 事件类型。
347+
* @param {Object} serviceResult.options 请求参数。
327348
*/

src/common/iServer/DataFlowService.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ import {SecurityManager} from '../security/SecurityManager';
2424
export class DataFlowService extends CommonServiceBase {
2525

2626

27-
constructor(url, options, callback) {
27+
constructor(url, options) {
2828
options = options || {};
2929
/*
3030
* @constant EVENT_TYPES
3131
* {Array.<string>}
3232
* 此类支持的事件类型
3333
*/
34+
options.EVENT_TYPES = ["broadcastSocketConnected", "broadcastSocketClosed", "broadcastSocketError", "broadcastFailed", "broadcastSucceeded", "subscribeSocketConnected", "subscribeSocketClosed", "subscribeSocketError", "messageSucceeded", "setFilterParamSucceeded"]
3435
super(url, options);
3536

3637
/**
@@ -51,8 +52,6 @@ export class DataFlowService extends CommonServiceBase {
5152
*/
5253
this.excludeField = null;
5354

54-
this.callback = callback;
55-
5655
Util.extend(this, options);
5756

5857
this.CLASS_NAME = "SuperMap.DataFlowService";
@@ -69,18 +68,18 @@ export class DataFlowService extends CommonServiceBase {
6968
this.broadcastWebSocket.onopen = function (e) {
7069
me.broadcastWebSocket.isOpen = true;
7170
e.eventType = 'broadcastSocketConnected';
72-
me.callback(e);
71+
me.events.triggerEvent('broadcastSocketConnected', e);
7372
};
7473
this.broadcastWebSocket.onclose = function (e) {
7574
if (me.broadcastWebSocket) {
7675
me.broadcastWebSocket.isOpen = false;
7776
}
7877
e.eventType = 'broadcastSocketClosed';
79-
me.callback(e);
78+
me.events.triggerEvent('broadcastSocketClosed', e);
8079
};
8180
this.broadcastWebSocket.onerror = function (e) {
8281
e.eventType = 'broadcastSocketError';
83-
me.callback(e);
82+
me.events.triggerEvent('broadcastSocketError', e);
8483
};
8584
return this;
8685
}
@@ -92,11 +91,11 @@ export class DataFlowService extends CommonServiceBase {
9291
*/
9392
broadcast(geoJSONFeature) {
9493
if (!this.broadcastWebSocket||!this.broadcastWebSocket.isOpen) {
95-
this.callback({ eventType: 'broadcastFailed' });
94+
this.events.triggerEvent('broadcastFailed');
9695
return;
9796
}
9897
this.broadcastWebSocket.send(JSON.stringify(geoJSONFeature));
99-
this.callback({ eventType: 'broadcastSucceeded' });
98+
this.events.triggerEvent('broadcastSucceeded');
10099
}
101100

102101
/**
@@ -110,15 +109,15 @@ export class DataFlowService extends CommonServiceBase {
110109
this.subscribeWebSocket.onopen = function (e) {
111110
me.subscribeWebSocket.send(me._getFilterParams());
112111
e.eventType = 'subscribeSocketConnected';
113-
me.callback(e);
112+
me.events.triggerEvent('subscribeSocketConnected', e);
114113
};
115114
this.subscribeWebSocket.onclose = function (e) {
116115
e.eventType = 'subscribeWebSocketClosed';
117-
me.callback(e);
116+
me.events.triggerEvent('subscribeWebSocketClosed', e);
118117
};
119118
this.subscribeWebSocket.onerror = function (e) {
120119
e.eventType = 'subscribeSocketError';
121-
me.callback(e);
120+
me.events.triggerEvent('subscribeSocketError', e);
122121
};
123122
this.subscribeWebSocket.onmessage = function (e) {
124123
me._onMessage(e);
@@ -135,7 +134,7 @@ export class DataFlowService extends CommonServiceBase {
135134
*/
136135
setExcludeField(excludeField) {
137136
this.excludeField = excludeField;
138-
this.subscribeWebSocket && this.subscribeWebSocket.send(this._getFilterParams());
137+
this.subscribeWebSocket.send(this._getFilterParams());
139138
return this;
140139
}
141140

@@ -147,7 +146,7 @@ export class DataFlowService extends CommonServiceBase {
147146
*/
148147
setGeometry(geometry) {
149148
this.geometry = geometry;
150-
this.subscribeWebSocket && this.subscribeWebSocket.send(this._getFilterParams());
149+
this.subscribeWebSocket.send(this._getFilterParams());
151150
return this;
152151
}
153152

@@ -208,13 +207,13 @@ export class DataFlowService extends CommonServiceBase {
208207
var filterParam = JSON.parse(e.data);
209208
e.filterParam = filterParam;
210209
e.eventType = 'setFilterParamSucceeded';
211-
this.callback(e);
210+
this.events.triggerEvent('setFilterParamSucceeded', e);
212211
return;
213212
}
214213
var feature = JSON.parse(e.data);
215214
e.featureResult = feature;
216215
e.eventType = 'messageSucceeded';
217-
this.callback(e);
216+
this.events.triggerEvent('messageSucceeded', e);
218217
}
219218

220219

src/leaflet/services/DataFlowService.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,50 @@ export var DataFlowService = ServiceBase.extend({
4545
this.options.prjCoordSys = options.projection;
4646
}
4747
ServiceBase.prototype.initialize.call(this, url, options);
48-
this.dataFlow = new DataFlow(url, options, this._defaultEvent.bind(this));
48+
this.dataFlow = new DataFlow(url, options);
49+
/**
50+
* @event DataFlowService#broadcastSocketConnected
51+
* @description broadcast Socket 连接成功。
52+
*/
53+
/**
54+
* @event DataFlowService#broadcastSocketError
55+
* @description broadcast Socket 连接失败。
56+
*/
57+
/**
58+
* @event DataFlowService#broadcastFailed
59+
* @description 广播失败。
60+
*/
61+
/**
62+
* @event DataFlowService#broadcastSucceeded
63+
* @description 广播成功。
64+
*/
65+
/**
66+
* @event DataFlowService#subscribeSocketConnected
67+
* @description 订阅数据连接成功。
68+
*/
69+
/**
70+
* @event DataFlowService#subscribeSocketError
71+
* @description 订阅数据连接失败。
72+
*/
73+
/**
74+
* @event DataFlowService#messageSucceeded
75+
* @description 获取信息成功。
76+
*/
77+
/**
78+
* @event DataFlowService#setFilterParamSucceeded
79+
* @description 设置过滤参数成功。
80+
*/
81+
this.dataFlow.events.on({
82+
"broadcastSocketConnected": this._defaultEvent,
83+
"broadcastSocketError": this._defaultEvent,
84+
"broadcastFailed": this._defaultEvent,
85+
"broadcastSucceeded": this._defaultEvent,
86+
"subscribeSocketConnected": this._defaultEvent,
87+
"subscribeSocketError": this._defaultEvent,
88+
"messageSucceeded": this._defaultEvent,
89+
"setFilterParamSucceeded": this._defaultEvent,
90+
scope: this
91+
})
4992
},
5093

5194
/**

src/maplibregl/services/DataFlowService.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,51 @@ export class DataFlowService extends ServiceBase {
3939
options.prjCoordSys = options.projection;
4040
}
4141
super(url, options);
42-
this.dataFlow = new DataFlow(url, options, this._defaultEvent.bind(this));
42+
this.dataFlow = new DataFlow(url, options);
43+
/**
44+
* @event DataFlowService#broadcastSocketConnected
45+
* @description broadcast Socket 连接成功。
46+
*/
47+
/**
48+
* @event DataFlowService#broadcastSocketError
49+
* @description broadcast Socket 连接失败。
50+
*/
51+
/**
52+
* @event DataFlowService#broadcastFailed
53+
* @description 广播失败。
54+
*/
55+
/**
56+
* @event DataFlowService#broadcastSucceeded
57+
* @description 广播成功。
58+
*/
59+
/**
60+
* @event DataFlowService#subscribeSocketConnected
61+
* @description 订阅数据连接成功。
62+
*/
63+
/**
64+
* @event DataFlowService#subscribeSocketError
65+
* @description 订阅数据连接失败。
66+
*/
67+
/**
68+
* @event DataFlowService#messageSucceeded
69+
* @description 获取信息成功。
70+
*/
71+
/**
72+
* @event DataFlowService#setFilterParamSucceeded
73+
* @description 设置过滤参数成功。
74+
*/
75+
76+
this.dataFlow.events.on({
77+
"broadcastSocketConnected": this._defaultEvent,
78+
"broadcastSocketError": this._defaultEvent,
79+
"broadcastFailed": this._defaultEvent,
80+
"broadcastSucceeded": this._defaultEvent,
81+
"subscribeSocketConnected": this._defaultEvent,
82+
"subscribeSocketError": this._defaultEvent,
83+
"messageSucceeded": this._defaultEvent,
84+
"setFilterParamSucceeded": this._defaultEvent,
85+
scope: this
86+
});
4387
var me = this;
4488
me.on('subscribeSocketConnected', function (e) {
4589
/**

src/openlayers/services/DataFlowService.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ export class DataFlowService extends ServiceBase {
3232
options.prjCoordSys = options.projection;
3333
}
3434
super(url, options);
35-
this.dataFlow = new DataFlow(url, options, this._defaultEvent.bind(this));
35+
this.dataFlow = new DataFlow(url, options);
36+
this.dataFlow.events.on({
37+
"broadcastSocketConnected": this._defaultEvent,
38+
"broadcastSocketError": this._defaultEvent,
39+
"broadcastFailed": this._defaultEvent,
40+
"broadcastSucceeded": this._defaultEvent,
41+
"subscribeSocketConnected": this._defaultEvent,
42+
"subscribeSocketError": this._defaultEvent,
43+
"messageSucceeded": this._defaultEvent,
44+
"setFilterParamSucceeded": this._defaultEvent,
45+
scope: this
46+
});
3647
}
3748

3849
/**

test/leaflet/overlay/DataFlowLayerSpec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ describe('leaflet_DataFlowLayer', () => {
105105
service.on('broadcastSocketConnected', (e) => {
106106
var dataFlow = service.dataFlow;
107107
expect(dataFlow.CLASS_NAME).toBe("SuperMap.DataFlowService");
108+
expect(dataFlow.EVENT_TYPES.length).toEqual(10);
108109
expect(dataFlow.broadcastWebSocket.binaryType).toBe("blob");
109110
timer = window.setInterval(broadcast_Point(service), 1000);
110111
});
@@ -114,7 +115,7 @@ describe('leaflet_DataFlowLayer', () => {
114115
expect(layer.url).toBe(urlDataFlow);
115116
expect(layer.options).not.toBeNull();
116117
expect(service).not.toBeNull();
117-
// expect(service._events.broadcastSocketConnected.length).toEqual(1);
118+
expect(service._events.broadcastSocketConnected.length).toEqual(1);
118119
service.unBroadcast();
119120
done();
120121
}, 0)
@@ -312,7 +313,6 @@ describe('leaflet_DataFlowLayer', () => {
312313
layer.addTo(map);
313314

314315
var e = {
315-
eventType: 'messageSucceeded',
316316
featureResult:
317317
{
318318
"type": "Feature",
@@ -339,7 +339,7 @@ describe('leaflet_DataFlowLayer', () => {
339339
}
340340
});
341341
// done();
342-
layer.dataService.dataFlow.callback(e);
342+
layer.dataService.dataFlow.events.triggerEvent('messageSucceeded', e);
343343
}, 0)
344344

345345
});

test/openlayers/overlay/DataFlowSpec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ describe('ol_DataFlow', () => {
129129
service.on('broadcastSocketConnected', e => {
130130
var dataFlow = service.dataFlow;
131131
expect(dataFlow.CLASS_NAME).toBe('SuperMap.DataFlowService');
132+
expect(dataFlow.EVENT_TYPES.length).toEqual(10);
132133
expect(dataFlow.broadcastWebSocket.binaryType).toBe('blob');
133134
expect(dataFlow.broadcastWebSocket.url).toBe(urlDataFlow + 'broadcast');
134135
broadcast_Point(service);

0 commit comments

Comments
 (0)