-
Notifications
You must be signed in to change notification settings - Fork 124
/
StreamMessage.cs
86 lines (81 loc) · 2.75 KB
/
StreamMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace SqlStreamStore.Streams
{
using System;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Represents a message in a stream.
/// </summary>
public struct StreamMessage
{
public readonly long Position;
public readonly DateTime CreatedUtc;
public readonly Guid MessageId;
public readonly string JsonMetadata;
public readonly int StreamVersion;
public readonly string StreamId;
public readonly string Type;
private readonly Func<CancellationToken, Task<string>> _getJsonData;
public StreamMessage(
string streamId,
Guid messageId,
int streamVersion,
long position,
DateTime createdUtc,
string type,
string jsonMetadata,
string jsonData)
: this(streamId,
messageId,
streamVersion,
position,
createdUtc,
type,
jsonMetadata, _ => Task.FromResult(jsonData))
{}
public StreamMessage(
string streamId,
Guid messageId,
int streamVersion,
long position,
DateTime createdUtc,
string type,
string jsonMetadata,
Func<CancellationToken, Task<string>> getJsonData)
{
MessageId = messageId;
StreamId = streamId;
StreamVersion = streamVersion;
Position = position;
CreatedUtc = createdUtc;
Type = type;
_getJsonData = getJsonData;
JsonMetadata = jsonMetadata;
}
/// <summary>
/// Gets the Json Data of the message. If prefetch is enabled, this will be a fast operation.
/// </summary>
/// <param name="cancellationToken">
/// The cancellation instruction.
/// </param>
/// <returns>
/// The Json Data of the message. If the message has been subsequently deleted since this
/// StreamMessage was created, then it will return null.
/// </returns>
public Task<string> GetJsonData(CancellationToken cancellationToken = default)
{
return _getJsonData(cancellationToken);
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return $"MessageId={MessageId} StreamId={StreamId} StreamVersion={StreamVersion} Position={Position} Type={Type}";
}
}
}