Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SourceState.cs
1using System.Threading.Tasks;
3
5{
9 public class SourceState
10 {
12 private readonly string source;
13 private DocumentType type = DocumentType.Empty;
14 private string firstText = null;
15 private int nrDocuments = 0;
16 private readonly bool isDefault;
17
23 public SourceState(string Source, bool IsDefault)
24 {
25 this.source = Source;
26 this.isDefault = IsDefault;
27 }
28
32 public string Source => this.source;
33
37 public bool IsDefault => this.isDefault;
38
42 public async Task<string> GetFirstText()
43 {
44 if (!(this.firstText is null))
45 return this.firstText;
46
48 if (Doc is null)
49 return string.Empty;
50
51 this.firstText = await Doc.GenerateMarkdown(false);
52
53 return this.firstText;
54 }
55
60 {
61 get
62 {
63 lock (this.documents)
64 {
65 if (this.documents.Count == 0)
66 return null;
67 else
68 return this.documents[0];
69 }
70 }
71 }
72
77 {
78 get
79 {
80 lock (this.documents)
81 {
82 return this.documents.ToArray();
83 }
84 }
85 }
86
93 public async Task<DocumentType> Add(MarkdownDocument Markdown, string Id)
94 {
95 DocumentInformation Info = await DocumentInformation.CreateAsync(Markdown, Id);
96
97 lock (this.documents)
98 {
99 this.documents.Add(Info);
100 this.nrDocuments++;
101
102 if (this.nrDocuments == 1)
103 this.type = Info.Type;
104 else
105 this.type = DocumentType.Complex;
106
107 return this.type;
108 }
109 }
110
117 public async Task<DocumentType> Update(MarkdownDocument Markdown, string Id)
118 {
119 if (string.IsNullOrEmpty(Id))
120 return await this.Add(Markdown, Id);
121
122 DocumentInformation Info = await DocumentInformation.CreateAsync(Markdown, Id);
124 int i, c;
125
126 lock (this.documents)
127 {
128 c = this.documents.Count;
129 for (i = 0; i < c; i++)
130 {
131 Info2 = this.documents[i];
132 if (Info2.Id == Id)
133 {
134 this.documents[i] = Info;
135
136 if (this.nrDocuments == 1)
137 this.type = Info.Type;
138
139 return this.type;
140 }
141 }
142
143 this.documents.Add(Info);
144 this.nrDocuments++;
145
146 if (this.nrDocuments == 1)
147 this.type = Info.Type;
148 else
149 this.type = DocumentType.Complex;
150
151 return this.type;
152 }
153 }
154 }
155}
static async Task< DocumentInformation > CreateAsync(MarkdownDocument Markdown, string Id)
Information about a document.
Maintains the state of one source.
Definition: SourceState.cs:10
DocumentInformation[] Documents
Available documents from source.
Definition: SourceState.cs:77
SourceState(string Source, bool IsDefault)
Maintains the state of one source.
Definition: SourceState.cs:23
async Task< string > GetFirstText()
First document text.
Definition: SourceState.cs:42
DocumentInformation FirstDocument
First document.
Definition: SourceState.cs:60
async Task< DocumentType > Add(MarkdownDocument Markdown, string Id)
Adds a markdown document from the source.
Definition: SourceState.cs:93
async Task< DocumentType > Update(MarkdownDocument Markdown, string Id)
Updates a markdown document from the source. If not found, it is added.
Definition: SourceState.cs:117
bool IsDefault
If the content is default content (true), or reported content (false).
Definition: SourceState.cs:37
Contains a markdown document. This markdown document class supports original markdown,...
Task< string > GenerateMarkdown()
Generates Markdown from the markdown text.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
DocumentType
Type of markdown document.