@@ -12,6 +12,7 @@ import { ClassRegistry } from 'data/model/literals';
1212import { MutableContentEvents } from 'data/model/mutable/MutableObject' ;
1313import { MultiMap } from 'util/multimap' ;
1414import { BaseCollection , Collection , CollectionConfig , CollectionOp } from './Collection' ;
15+ import { Identity } from 'data/identity' ;
1516
1617// a simple mutable list with a single writer
1718
@@ -24,11 +25,14 @@ class InsertOp<T> extends CollectionOp<T> {
2425 element ?: T ;
2526 ordinal ?: Ordinal ;
2627
27- constructor ( target ?: MutableArray < T > , element ?: T , ordinal ?: Ordinal ) {
28+ constructor ( target ?: MutableArray < T > , element ?: T , ordinal ?: Ordinal , author ?: Identity ) {
2829 super ( target ) ;
2930
3031 this . element = element ;
3132 this . ordinal = ordinal ;
33+ if ( author !== undefined ) {
34+ this . setAuthor ( author ) ;
35+ }
3236 }
3337
3438 getClassName ( ) : string {
@@ -69,7 +73,7 @@ class DeleteOp<T> extends CollectionOp<T> {
6973 elementHash ?: Hash ;
7074 deletedOps ?: HashedSet < HashReference < InsertOp < T > > > ;
7175
72- constructor ( target ?: MutableArray < T > , elementHash ?: Hash , insertOps ?: IterableIterator < HashReference < InsertOp < T > > > ) {
76+ constructor ( target ?: MutableArray < T > , elementHash ?: Hash , insertOps ?: IterableIterator < HashReference < InsertOp < T > > > , author ?: Identity ) {
7377 super ( target ) ;
7478
7579 this . elementHash = elementHash ;
@@ -85,6 +89,10 @@ class DeleteOp<T> extends CollectionOp<T> {
8589 this . deletedOps . add ( insertOp ) ;
8690 }
8791 }
92+
93+ if ( author !== undefined ) {
94+ this . setAuthor ( author ) ;
95+ }
8896 }
8997
9098 init ( ) {
@@ -208,11 +216,11 @@ class MutableArray<T> extends BaseCollection<T> implements Collection<T> {
208216 this . _ordinals = [ ] ;
209217 }
210218
211- async insertAt ( element : T , idx : number ) {
212- await this . insertManyAt ( [ element ] , idx ) ;
219+ async insertAt ( element : T , idx : number , author ?: Identity ) {
220+ await this . insertManyAt ( [ element ] , idx , author ) ;
213221 }
214222
215- async insertManyAt ( elements : T [ ] , idx : number ) {
223+ async insertManyAt ( elements : T [ ] , idx : number , author ?: Identity ) {
216224 this . rebuild ( ) ;
217225
218226 // In the "no duplicates" case, any items we insert will disappear from their old positions. So
@@ -262,7 +270,7 @@ class MutableArray<T> extends BaseCollection<T> implements Collection<T> {
262270 oldInsertionOps = this . _currentInsertOpRefs . get ( elementHash ) ;
263271 }
264272
265- const insertOp = new InsertOp ( this , element , ordinal ) ;
273+ const insertOp = new InsertOp ( this , element , ordinal , author ) ;
266274 await this . applyNewOp ( insertOp ) ;
267275
268276 // Note: in the "no duplicates" case, the delete -if necessary- has to come after the
@@ -280,52 +288,52 @@ class MutableArray<T> extends BaseCollection<T> implements Collection<T> {
280288 }
281289 }
282290
283- async deleteAt ( idx : number ) {
284- await this . deleteManyAt ( idx , 1 ) ;
291+ async deleteAt ( idx : number , author ?: Identity ) {
292+ await this . deleteManyAt ( idx , 1 , author ) ;
285293 }
286294
287- async deleteManyAt ( idx : number , count : number ) {
295+ async deleteManyAt ( idx : number , count : number , author ?: Identity ) {
288296 this . rebuild ( ) ;
289297
290298 while ( idx < this . _contents . length && count > 0 ) {
291299 let hash = this . _hashes [ idx ] ;
292300
293301 if ( this . duplicates ) {
294- await this . delete ( hash , this . _ordinals [ idx ] ) ;
302+ await this . delete ( hash , this . _ordinals [ idx ] , author ) ;
295303 } else {
296- await this . delete ( hash ) ;
304+ await this . delete ( hash , undefined , author ) ;
297305 }
298306
299307 idx = idx + 1 ;
300308 count = count - 1 ;
301309 }
302310 }
303311
304- async deleteElement ( element : T ) {
305- this . deleteElementByHash ( HashedObject . hashElement ( element ) ) ;
312+ async deleteElement ( element : T , author ?: Identity ) {
313+ this . deleteElementByHash ( HashedObject . hashElement ( element ) , author ) ;
306314 }
307315
308- async deleteElementByHash ( hash : Hash ) {
316+ async deleteElementByHash ( hash : Hash , author ?: Identity ) {
309317 this . rebuild ( ) ;
310- this . delete ( hash ) ;
318+ this . delete ( hash , undefined , author ) ;
311319 }
312320
313- async push ( element : T ) {
314- await this . insertAt ( element , this . _contents . length ) ;
321+ async push ( element : T , author ?: Identity ) {
322+ await this . insertAt ( element , this . _contents . length , author ) ;
315323 }
316324
317- async pop ( ) : Promise < T > {
325+ async pop ( author ?: Identity ) : Promise < T > {
318326 this . rebuild ( ) ;
319327 const lastIdx = this . _contents . length - 1 ;
320328 const last = this . _contents [ lastIdx ] ;
321- await this . deleteAt ( lastIdx ) ;
329+ await this . deleteAt ( lastIdx , author ) ;
322330
323331 return last ;
324332 }
325333
326- async concat ( elements : T [ ] ) {
334+ async concat ( elements : T [ ] , author ?: Identity ) {
327335 this . rebuild ( ) ;
328- await this . insertManyAt ( elements , this . _contents . length ) ;
336+ await this . insertManyAt ( elements , this . _contents . length , author ) ;
329337 }
330338
331339 contents ( ) {
@@ -369,15 +377,15 @@ class MutableArray<T> extends BaseCollection<T> implements Collection<T> {
369377 return this . _contents [ idx ] ;
370378 }
371379
372- private async delete ( hash : Hash , ordinal ?: Ordinal ) {
380+ private async delete ( hash : Hash , ordinal ?: Ordinal , author ?: Identity ) {
373381
374382 let deleteOp : DeleteOp < T > | undefined = undefined ;
375383 const insertOpRefs = this . _currentInsertOpRefs . get ( hash ) ;
376384
377385 if ( ordinal !== undefined ) {
378386 for ( const insertOpRef of insertOpRefs . values ( ) ) {
379387 if ( this . _currentInsertOpOrds . get ( insertOpRef . hash ) === ordinal ) {
380- deleteOp = new DeleteOp ( this , hash , [ insertOpRef ] . values ( ) ) ;
388+ deleteOp = new DeleteOp ( this , hash , [ insertOpRef ] . values ( ) , author ) ;
381389 break ;
382390 }
383391 }
0 commit comments