-
Notifications
You must be signed in to change notification settings - Fork 8
/
Doc.cs
433 lines (385 loc) · 17.3 KB
/
Doc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using YDotNet.Document.Events;
using YDotNet.Document.Options;
using YDotNet.Document.Transactions;
using YDotNet.Document.Types.Maps;
using YDotNet.Document.Types.Texts;
using YDotNet.Document.Types.XmlElements;
using YDotNet.Document.Types.XmlTexts;
using YDotNet.Document.UndoManagers;
using YDotNet.Infrastructure;
using YDotNet.Native.Document;
using YDotNet.Native.Document.Events;
using Array = YDotNet.Document.Types.Arrays.Array;
namespace YDotNet.Document;
/// <summary>
/// A Yrs document type.
/// </summary>
/// <remarks>
/// <para>
/// The documents are the most important units of collaborative resources management.
/// </para>
/// <para>
/// All shared collections live within the scope of their corresponding documents. All updates are
/// generated on per document basis (rather than generated per shared type basis). All operations on
/// shared collections happen via <see cref="Transaction" />, whose lifetime is also bound to a document.
/// </para>
/// <para>
/// Document manages the so-called root types, which are top-level shared types definitions (as opposed
/// to recursively nested types).
/// </para>
/// </remarks>
public class Doc : IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="Doc" /> class.
/// </summary>
/// <remarks>
/// The default encoding used for <see cref="Doc" /> instances is see <see cref="DocEncoding.Utf16" />
/// to match default encoding used by C#.
/// </remarks>
public Doc()
: this(DocOptions.Default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Doc" /> class with the specified <paramref name="options" />.
/// </summary>
/// <param name="options">The options to be used when initializing this document.</param>
public Doc(DocOptions options)
{
Handle = DocChannel.NewWithOptions(DocOptionsNative.From(options));
}
/// <summary>
/// Initializes a new instance of the <see cref="Doc" /> class with the specified <see cref="Handle" />.
/// </summary>
/// <param name="handle">The pointer to be used by this document to manage the native resource.</param>
internal Doc(nint handle)
{
Handle = handle;
}
/// <summary>
/// Gets the unique client identifier of this <see cref="Doc" /> instance.
/// </summary>
public ulong Id => DocChannel.Id(Handle);
/// <summary>
/// Gets the unique document identifier of this <see cref="Doc" /> instance.
/// </summary>
public string Guid => MemoryReader.ReadUtf8String(DocChannel.Guid(Handle));
/// <summary>
/// Gets the collection identifier of this <see cref="Doc" /> instance.
/// </summary>
/// <remarks>
/// If none was defined, a <c>null</c> will be returned.
/// </remarks>
public string? CollectionId
{
get
{
MemoryReader.TryReadUtf8String(DocChannel.CollectionId(Handle), out var result);
return result;
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="Doc" /> instance requested a data load.
/// </summary>
/// <remarks>
/// This flag is often used by the parent <see cref="Doc" /> instance to check if this <see cref="Doc" />
/// instance requested a data load.
/// </remarks>
public bool ShouldLoad => DocChannel.ShouldLoad(Handle);
/// <summary>
/// Gets a value indicating whether this <see cref="Doc" /> instance will auto load.
/// </summary>
/// <remarks>
/// Auto loaded nested <see cref="Doc" /> instances automatically send a load request to their parent
/// <see cref="Doc" /> instances.
/// </remarks>
public bool AutoLoad => DocChannel.AutoLoad(Handle);
/// <summary>
/// Gets the handle to the native resource.
/// </summary>
internal nint Handle { get; }
/// <inheritdoc />
public void Dispose()
{
DocChannel.Destroy(Handle);
}
/// <summary>
/// Creates a copy of the current <see cref="Doc" /> instance.
/// </summary>
/// <remarks>The instance returned will not be the same, but they will both control the same document.</remarks>
/// <returns>A new <see cref="Doc" /> instance that controls the same document.</returns>
public Doc? Clone()
{
return ReferenceAccessor.Access(new Doc(DocChannel.Clone(Handle)));
}
/// <summary>
/// Gets or creates a new shared <see cref="YDotNet.Document.Types.Texts.Text" /> data type instance as a root-level
/// type in this document.
/// </summary>
/// <remarks>
/// This structure can later be accessed using its <c>name</c>.
/// </remarks>
/// <param name="name">The name of the <see cref="YDotNet.Document.Types.Texts.Text" /> instance to get.</param>
/// <returns>
/// The <see cref="YDotNet.Document.Types.Texts.Text" /> instance related to the <c>name</c> provided or
/// <c>null</c> if failed.
/// </returns>
public Text? Text(string name)
{
var nameHandle = MemoryWriter.WriteUtf8String(name);
var result = ReferenceAccessor.Access(new Text(DocChannel.Text(Handle, nameHandle)));
MemoryWriter.Release(nameHandle);
return result;
}
/// <summary>
/// Gets or creates a new shared <see cref="YDotNet.Document.Types.Maps.Map" /> data type instance as a root-level type
/// in this document.
/// </summary>
/// <remarks>
/// This structure can later be accessed using its <c>name</c>.
/// </remarks>
/// <param name="name">The name of the <see cref="YDotNet.Document.Types.Maps.Map" /> instance to get.</param>
/// <returns>
/// The <see cref="YDotNet.Document.Types.Maps.Map" /> instance related to the <c>name</c> provided or <c>null</c>
/// if failed.
/// </returns>
public Map? Map(string name)
{
var nameHandle = MemoryWriter.WriteUtf8String(name);
var result = ReferenceAccessor.Access(new Map(DocChannel.Map(Handle, nameHandle)));
MemoryWriter.Release(nameHandle);
return result;
}
/// <summary>
/// Gets or creates a new shared <see cref="YDotNet.Document.Types.Arrays.Array" /> data type instance as a root-level
/// type in this document.
/// </summary>
/// <remarks>
/// This structure can later be accessed using its <c>name</c>.
/// </remarks>
/// <param name="name">The name of the <see cref="YDotNet.Document.Types.Arrays.Array" /> instance to get.</param>
/// <returns>
/// The <see cref="YDotNet.Document.Types.Arrays.Array" /> instance related to the <c>name</c> provided or
/// <c>null</c> if failed.
/// </returns>
public Array? Array(string name)
{
var nameHandle = MemoryWriter.WriteUtf8String(name);
var result = ReferenceAccessor.Access(new Array(DocChannel.Array(Handle, nameHandle)));
MemoryWriter.Release(nameHandle);
return result;
}
/// <summary>
/// Gets or creates a new shared <see cref="YDotNet.Document.Types.XmlElements.XmlElement" /> data type instance as a
/// root-level type in this document.
/// </summary>
/// <remarks>
/// This structure can later be accessed using its <c>name</c>.
/// </remarks>
/// <param name="name">The name of the <see cref="YDotNet.Document.Types.XmlElements.XmlElement" /> instance to get.</param>
/// <returns>
/// The <see cref="YDotNet.Document.Types.XmlElements.XmlElement" /> instance related to the <c>name</c> provided
/// or <c>null</c> if failed.
/// </returns>
public XmlElement? XmlElement(string name)
{
// TODO [LSViana] Wrap the XmlElement with an XmlFragment before returning the value.
var nameHandle = MemoryWriter.WriteUtf8String(name);
var result = ReferenceAccessor.Access(new XmlElement(DocChannel.XmlElement(Handle, nameHandle)));
MemoryWriter.Release(nameHandle);
return result;
}
/// <summary>
/// Gets or creates a new shared <see cref="YDotNet.Document.Types.XmlTexts.XmlText" /> data type instance as a
/// root-level type in this document.
/// </summary>
/// <remarks>
/// This structure can later be accessed using its <c>name</c>.
/// </remarks>
/// <param name="name">The name of the <see cref="YDotNet.Document.Types.XmlTexts.XmlText" /> instance to get.</param>
/// <returns>
/// The <see cref="YDotNet.Document.Types.XmlTexts.XmlText" /> instance related to the <c>name</c> provided
/// or <c>null</c> if failed.
/// </returns>
public XmlText? XmlText(string name)
{
// TODO [LSViana] Wrap the XmlText with an XmlFragment before returning the value.
var nameHandle = MemoryWriter.WriteUtf8String(name);
var result = ReferenceAccessor.Access(new XmlText(DocChannel.XmlText(Handle, nameHandle)));
MemoryWriter.Release(nameHandle);
return result;
}
/// <summary>
/// Starts a new read-write <see cref="Transaction" /> on this document.
/// </summary>
/// <param name="origin">
/// Optional byte marker to indicate the source of changes to be applied by this transaction.
/// This value is used by <see cref="UndoManager" />.
/// </param>
/// <returns>
/// <para>The <see cref="Transaction" /> to perform operations in the document or <c>null</c>.</para>
/// <para>
/// Returns <c>null</c> if the <see cref="Transaction" /> could not be created because, for example, another
/// read-write <see cref="Transaction" /> already exists and was not committed yet.
/// </para>
/// </returns>
public Transaction? WriteTransaction(byte[]? origin = null)
{
var length = (uint) (origin?.Length ?? 0);
return ReferenceAccessor.Access(
new Transaction(DocChannel.WriteTransaction(Handle, length, origin)));
}
/// <summary>
/// Starts a new read-only <see cref="Transaction" /> on this document.
/// </summary>
/// <returns>
/// <para>The <see cref="Transaction" /> to perform operations in the document or <c>null</c>.</para>
/// <para>
/// Returns <c>null</c> if the <see cref="Transaction" /> could not be created because, for example, another
/// read-write <see cref="Transaction" /> already exists and was not committed yet.
/// </para>
/// </returns>
public Transaction? ReadTransaction()
{
return ReferenceAccessor.Access(new Transaction(DocChannel.ReadTransaction(Handle)));
}
/// <summary>
/// Destroys the current document, sending a <c>destroy</c> event and
/// clearing up all the registered callbacks.
/// </summary>
public void Clear()
{
DocChannel.Clear(Handle);
}
/// <summary>
/// Manually send a load request to the parent document of this sub-document.
/// </summary>
/// <remarks>
/// Works only if current document is a sub-document of an another document.
/// </remarks>
/// <param name="transaction">A read-only <see cref="Transaction" /> of the parent document.</param>
public void Load(Transaction transaction)
{
DocChannel.Load(Handle, transaction.Handle);
}
/// <summary>
/// Subscribes a callback function to be called when the <see cref="Clear" /> method is called.
/// </summary>
/// <param name="action">The callback function.</param>
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveClear(Action<ClearEvent> action)
{
var subscriptionId = DocChannel.ObserveClear(
Handle,
nint.Zero,
(_, doc) => action(ClearEventNative.From(new Doc(doc)).ToClearEvent()));
return new EventSubscription(subscriptionId);
}
/// <summary>
/// Unsubscribes a callback function, represented by an <see cref="EventSubscription" /> instance,
/// for the <see cref="Clear" /> method.
/// </summary>
/// <param name="subscription">The subscription that represents the callback function to be unobserved.</param>
public void UnobserveClear(EventSubscription subscription)
{
DocChannel.UnobserveClear(Handle, subscription.Id);
}
/// <summary>
/// Subscribes a callback function for changes performed within <see cref="Transaction" /> scope.
/// </summary>
/// <remarks>
/// The updates are encoded using <c>lib0</c> V1 encoding and they can be passed to remote peers right away.
/// </remarks>
/// <param name="action">The callback to be executed when a <see cref="Transaction" /> is committed.</param>
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveUpdatesV1(Action<UpdateEvent> action)
{
var subscriptionId = DocChannel.ObserveUpdatesV1(
Handle,
nint.Zero,
(_, length, data) => action(UpdateEventNative.From(length, data).ToUpdateEvent()));
return new EventSubscription(subscriptionId);
}
/// <summary>
/// Unsubscribes a callback function, represented by an <see cref="EventSubscription" /> instance, for changes
/// performed within <see cref="Transaction" /> scope.
/// </summary>
/// <param name="subscription">The subscription that represents the callback function to be unobserved.</param>
public void UnobserveUpdatesV1(EventSubscription subscription)
{
DocChannel.UnobserveUpdatesV1(Handle, subscription.Id);
}
/// <summary>
/// Subscribes a callback function for changes performed within <see cref="Transaction" /> scope.
/// </summary>
/// <remarks>
/// The updates are encoded using <c>lib0</c> V1 encoding and they can be passed to remote peers right away.
/// </remarks>
/// <param name="action">The callback to be executed when a <see cref="Transaction" /> is committed.</param>
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveUpdatesV2(Action<UpdateEvent> action)
{
var subscriptionId = DocChannel.ObserveUpdatesV2(
Handle,
nint.Zero,
(_, length, data) => action(UpdateEventNative.From(length, data).ToUpdateEvent()));
return new EventSubscription(subscriptionId);
}
/// <summary>
/// Unsubscribes a callback function, represented by an <see cref="EventSubscription" /> instance, for changes
/// performed within <see cref="Transaction" /> scope.
/// </summary>
/// <param name="subscription">The subscription that represents the callback function to be unobserved.</param>
public void UnobserveUpdatesV2(EventSubscription subscription)
{
DocChannel.UnobserveUpdatesV2(Handle, subscription.Id);
}
/// <summary>
/// Subscribes a callback function for changes performed within <see cref="Transaction" /> scope.
/// </summary>
/// <remarks>
/// The updates are encoded using <c>lib0</c> V1 encoding and they can be passed to remote peers right away.
/// </remarks>
/// <param name="action">The callback to be executed when a <see cref="Transaction" /> is committed.</param>
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveAfterTransaction(Action<AfterTransactionEvent> action)
{
var subscriptionId = DocChannel.ObserveAfterTransaction(
Handle,
nint.Zero,
(_, afterTransactionEvent) => action(afterTransactionEvent.ToAfterTransactionEvent()));
return new EventSubscription(subscriptionId);
}
/// <summary>
/// Unsubscribes a callback function, represented by an <see cref="EventSubscription" /> instance, for changes
/// performed within <see cref="Transaction" /> scope.
/// </summary>
/// <param name="subscription">The subscription that represents the callback function to be unobserved.</param>
public void UnobserveAfterTransaction(EventSubscription subscription)
{
DocChannel.UnobserveAfterTransaction(Handle, subscription.Id);
}
/// <summary>
/// Subscribes a callback function for changes in the sub-documents.
/// </summary>
/// <param name="action">The callback to be executed when a sub-document changes.</param>
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveSubDocs(Action<SubDocsEvent> action)
{
var subscriptionId = DocChannel.ObserveSubDocs(
Handle,
nint.Zero,
(_, subDocsEvent) => action(subDocsEvent.ToSubDocsEvent()));
return new EventSubscription(subscriptionId);
}
/// <summary>
/// Unsubscribes a callback function, represented by an <see cref="EventSubscription" /> instance, for changes
/// performed in the sub-documents.
/// </summary>
/// <param name="subscription">The subscription that represents the callback function to be unobserved.</param>
public void UnobserveSubDocs(EventSubscription subscription)
{
DocChannel.UnobserveSubDocs(Handle, subscription.Id);
}
}