-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serializing/deserializing methods for Event
instances
#251
Changes from 3 commits
44f9bcc
ce19f12
9a55df9
1846d97
65c353d
03dcdbe
7a7511e
9d2bd10
886b253
9df20b3
f16b37a
9602053
2d58a3d
760d9b7
3977ae8
d71b224
bd2fae1
7fd9071
8cacb0b
1a85335
b412b59
e1d45d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,8 @@ | |
|
||
import 'dart:convert'; | ||
|
||
import 'package:collection/collection.dart'; | ||
|
||
import 'enums.dart'; | ||
|
||
final DeepCollectionEquality _deepCollectionEquality = | ||
const DeepCollectionEquality(); | ||
|
||
final class Event { | ||
final DashEvent eventName; | ||
final Map<String, Object?> eventData; | ||
|
@@ -459,7 +454,6 @@ final class Event { | |
: eventName = DashEvent.hotReloadTime, | ||
eventData = {'timeMs': timeMs}; | ||
|
||
// TODO: eliasyishak, add better dartdocs to explain each param | ||
/// Events to be sent for the Flutter Hot Runner. | ||
Event.hotRunnerInfo({ | ||
required String label, | ||
|
@@ -511,6 +505,7 @@ final class Event { | |
if (reloadVMTimeInMs != null) 'reloadVMTimeInMs': reloadVMTimeInMs, | ||
}; | ||
|
||
// TODO: eliasyishak, add better dartdocs to explain each param | ||
/// Event that is emitted periodically to report the number of times each lint | ||
/// has been enabled. | ||
/// | ||
|
@@ -724,22 +719,9 @@ final class Event { | |
other is Event && | ||
other.runtimeType == runtimeType && | ||
other.eventName == eventName && | ||
_deepCollectionEquality.equals(other.eventData, eventData); | ||
_compareEventData(other.eventData, eventData); | ||
|
||
/// Converts an instance of [Event] to JSON. | ||
/// | ||
/// Example for [Event.timing] converted to JSON below. | ||
/// ```json | ||
/// { | ||
/// "eventName": "timing", | ||
/// "eventData": { | ||
/// "workflow": "my-work-flow", | ||
/// "variableName": "my-variable", | ||
/// "elapsedMilliseconds": 123, | ||
/// "label": "my-label" | ||
/// } | ||
/// } | ||
/// ``` | ||
String toJson() => jsonEncode({ | ||
'eventName': eventName.label, | ||
'eventData': eventData, | ||
|
@@ -748,20 +730,32 @@ final class Event { | |
@override | ||
String toString() => toJson(); | ||
|
||
/// Utility function to take in two maps [a] and [b] and compares them | ||
/// to ensure that they have the same keys and values | ||
bool _compareEventData(Map<String, Object?> a, Map<String, Object?> b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of creating our own, can we use something from package:collection? like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, this commit has it updated to use @christopherfujino, i know we discussed trimming down dependencies for this package, do you think this is a valid reason to add another dep? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following up on offline discussions with @christopherfujino, going to remove the dep on collection and use the same function for comparing two separate maps to reduce dependencies for this package |
||
final keySetA = a.keys.toSet(); | ||
final keySetB = b.keys.toSet(); | ||
final intersection = keySetA.intersection(keySetB); | ||
|
||
// Ensure that the keys are the same for each object | ||
if (intersection.length != keySetA.length || | ||
intersection.length != keySetB.length) { | ||
return false; | ||
} | ||
|
||
// Ensure that each of the key's values are the same | ||
for (final key in a.keys) { | ||
if (a[key] != b[key]) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// Returns a valid instance of [Event] if [json] follows the correct schema. | ||
/// | ||
/// Schema is below with the following notes: | ||
/// - The `eventName` key must have a string value that exists in [DashEvent]. | ||
/// - The `eventData` key must have a nested object as its value. | ||
/// ```json | ||
/// { | ||
/// "eventName": "string-label", | ||
/// "eventData": { | ||
/// "myVar1": "value1", | ||
/// "myVar2": 123 | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Common use case for this static method involves clients of this package | ||
/// that have a client-server setup where the server sends events that the | ||
/// client creates. | ||
static Event? fromJson(String json) { | ||
try { | ||
final jsonMap = jsonDecode(json) as Map<String, Object?>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use the simplified syntax from above for consistency
DashTool.values.where((t) => t.label == label).firstOrNull;