-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add EOSE class to obtain subscriptionId (#41)
* added EOSE class * added tests * Message.deserialize handle EOSE
- Loading branch information
Showing
5 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'dart:convert'; | ||
|
||
/// Indicates "end of stored events" | ||
/// | ||
/// this class is mostly for getting subscription id | ||
class Eose { | ||
/// default constructor | ||
Eose(this.subscriptionId); | ||
|
||
/// subscription_id is a random string that should be used to represent a subscription. | ||
final String subscriptionId; | ||
|
||
/// Serialize to nostr close message | ||
/// - ["EOSE", subscription_id] | ||
String serialize() { | ||
return jsonEncode(["EOSE", subscriptionId]); | ||
} | ||
|
||
/// Deserialize a nostr close message | ||
/// - ["CLOSE", subscription_id] | ||
factory Eose.deserialize(input) { | ||
if (input is! List<dynamic>) throw 'Invalid type for EOSE message'; | ||
if (input.length != 2) throw 'Invalid length for EOSE message'; | ||
return Eose(input[1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:nostr/nostr.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Eose', () { | ||
test('Constructor', () { | ||
String subscriptionId = generate64RandomHexChars(); | ||
var eose = Eose(subscriptionId); | ||
expect(eose.subscriptionId, subscriptionId); | ||
}); | ||
|
||
test('Eose.serialize', () { | ||
String subscriptionId = generate64RandomHexChars(); | ||
String serialized = '["EOSE","$subscriptionId"]'; | ||
var eose = Eose(subscriptionId); | ||
expect(eose.serialize(), serialized); | ||
}); | ||
|
||
test('Eose.deserialize', () { | ||
String subscriptionId = generate64RandomHexChars(); | ||
var serialized = ["EOSE", subscriptionId]; | ||
var eose = Eose.deserialize(serialized); | ||
expect(eose.subscriptionId, subscriptionId); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters