Skip to content

Commit 6e18a93

Browse files
authored
Merge pull request #5 from Huddle-01/dev
Updated package with Null safety enabled.
2 parents 7af1444 + b735c09 commit 6e18a93

13 files changed

+507
-518
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22
--------------------------------------------
3+
[0.3.1]
4+
5+
* Updated package using https://github.com/ZackMitkin/dart-protoo-client as reference and added null-safety
6+
37
[0.2.0] - 2019.12.18
48

59
* Add support for Flutter Web.

lib/src/EnhancedEventEmitter.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:async';
2+
3+
import 'package:events2/events2.dart';
4+
5+
import 'Logger.dart';
6+
7+
Logger _logger = Logger('EnhancedEventEmitter');
8+
9+
class EnhancedEventEmitter extends EventEmitter {
10+
EnhancedEventEmitter() : super();
11+
void safeEmit(String event, [Map<String, dynamic>? args]) {
12+
try {
13+
emit(event, args);
14+
} catch (error) {
15+
_logger.error(
16+
'safeEmit() event listener threw an error [event:$event]:$error',
17+
);
18+
}
19+
}
20+
21+
Future<dynamic> safeEmitAsFuture(String event,
22+
[Map<String, dynamic>? args]) async {
23+
try {
24+
return emitAsFuture(event, args);
25+
} catch (error) {
26+
_logger.error(
27+
'safeEmitAsFuture() event listener threw an error [event:$event]:$error',
28+
);
29+
}
30+
}
31+
}

lib/src/message.dart

Lines changed: 100 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,119 @@
1-
import 'utils.dart' as utils;
2-
import 'logger.dart';
31
import 'dart:convert';
42

5-
const APP_NAME = 'protoo-client';
6-
var logger = new Logger(APP_NAME);
3+
import 'Logger.dart';
4+
import 'Utils.dart' as utils;
75

8-
class Message
9-
{
6+
final logger = new Logger('Message');
7+
8+
class Message {
109
static JsonEncoder encoder = new JsonEncoder();
1110
static JsonDecoder decoder = new JsonDecoder();
11+
static Map<String, dynamic>? parse(dynamic raw) {
12+
var object;
13+
final message = Map<String, dynamic>();
14+
15+
try {
16+
object = decoder.convert(raw);
17+
} catch (error) {
18+
logger.error('parse() | invalid JSON: %s' + error.toString());
19+
20+
return null;
21+
}
22+
23+
// Request.
24+
if (object['request'] != null) {
25+
message['request'] = true;
1226

13-
static Future<String> parse(raw)
14-
{
15-
var object;
16-
var message;
17-
18-
try
19-
{
20-
object = decoder.convert(raw);
21-
}
22-
catch (error)
23-
{
24-
logger.failure('parse() | invalid JSON: ' + error);
25-
}
26-
27-
// if (typeof object !== 'object' || Array.isArray(object))
28-
// {
29-
// logger.error('parse() | not an object');
30-
// return;
31-
// }
32-
33-
// Request.
34-
if (object['request'])
35-
{
36-
message['request'] = true;
37-
38-
if(!(object['method'] is String)){
27+
if (!(object['method'] is String)) {
3928
logger.failure('parse() | missing/invalid method field');
4029
}
4130

42-
if(!(object['id'] is num)){
31+
if (!(object['id'] is num)) {
4332
logger.failure('parse() | missing/invalid id field');
4433
}
4534

46-
message['id'] = object['id'];
47-
message['method'] = object['method'];
48-
message['data'] = object['data'] ?? {};
49-
}
50-
// Response.
51-
else if (object['response'])
52-
{
53-
message['response'] = true;
54-
if(!(object['id'] is num)){
35+
message['id'] = object['id'];
36+
message['method'] = object['method'];
37+
message['data'] = object['data'] ?? {};
38+
}
39+
// Response.
40+
else if (object['response'] != null) {
41+
message['response'] = true;
42+
if (!(object['id'] is num)) {
5543
logger.failure('parse() | missing/invalid id field');
5644
}
5745

58-
message['id'] = object['id'];
59-
60-
// Success.
61-
if (object['ok'])
62-
{
63-
message['ok'] = true;
64-
message['data'] = object['data'] ?? {};
65-
}
66-
// Error.
67-
else
68-
{
69-
message['errorCode'] = object['errorCode'];
70-
message['errorReason'] = object['errorReason'];
71-
}
72-
}
73-
// Notification.
74-
else if (object['notification'])
75-
{
76-
message['notification'] = true;
77-
if(!(object['method'] is String)){
46+
message['id'] = object['id'];
47+
48+
// Success.
49+
if (object['ok']) {
50+
message['ok'] = true;
51+
message['data'] = object['data'] ?? {};
52+
}
53+
// Error.
54+
else {
55+
message['errorCode'] = object['errorCode'];
56+
message['errorReason'] = object['errorReason'];
57+
}
58+
}
59+
// Notification.
60+
else if (object['notification'] != null) {
61+
message['notification'] = true;
62+
if (!(object['method'] is String)) {
7863
logger.failure('parse() | missing/invalid method field');
7964
}
8065

81-
message['method'] = object['method'];
82-
message['data'] = object['data'] ?? {};
83-
}else {
84-
logger.failure('parse() | missing request/response field');
85-
}
86-
87-
return message;
88-
}
89-
90-
static requestFactory(method, data)
91-
{
92-
var requestObj =
93-
{
94-
'request' : true,
95-
'id' : utils.randomNumber,
96-
'method' : method,
97-
'data' : data ?? {}
98-
};
99-
100-
return requestObj;
101-
}
102-
103-
static successResponseFactory(request, data)
104-
{
105-
var responseObj =
106-
{
107-
'response' : true,
108-
'id' : request['id'],
109-
'ok' : true,
110-
'data' : data ?? {}
111-
};
112-
113-
return responseObj;
114-
}
115-
116-
static errorResponseFactory(request, errorCode, errorReason)
117-
{
118-
var responseObj =
119-
{
120-
'response' : true,
121-
'id' : request['id'],
122-
'errorCode' : errorCode,
123-
'errorReason' : errorReason
124-
};
125-
126-
return responseObj;
127-
}
128-
129-
static notificationFactory(method, data)
130-
{
131-
var notificationObj =
132-
{
133-
'notification' : true,
134-
'method' : method,
135-
'data ' : data ?? {},
136-
};
137-
138-
return notificationObj;
139-
}
66+
message['method'] = object['method'];
67+
message['data'] = object['data'] ?? {};
68+
}
69+
// Invalid.
70+
else {
71+
logger.failure('parse() | missing request/response field');
72+
return null;
73+
}
74+
75+
return message;
76+
}
77+
78+
static createRequest(method, data) {
79+
var requestObj = {
80+
'request': true,
81+
'id': utils.randomNumber,
82+
'method': method,
83+
'data': data ?? {}
84+
};
85+
return requestObj;
86+
}
87+
88+
static createSuccessResponse(request, data) {
89+
var responseObj = {
90+
'response': true,
91+
'id': request['id'],
92+
'ok': true,
93+
'data': data ?? {}
94+
};
95+
96+
return responseObj;
97+
}
98+
99+
static createErrorResponse(request, errorCode, errorReason) {
100+
var responseObj = {
101+
'response': true,
102+
'id': request['id'],
103+
'errorCode': errorCode,
104+
'errorReason': errorReason
105+
};
106+
107+
return responseObj;
108+
}
109+
110+
static createNotification(method, data) {
111+
var notificationObj = {
112+
'notification': true,
113+
'method': method,
114+
'data ': data ?? {},
115+
};
116+
117+
return notificationObj;
118+
}
140119
}

0 commit comments

Comments
 (0)