convergencelabs / convergence-project

The project used for Convergence Project Management and Issue Reporting
https://convergence.io
42 stars 5 forks source link

A splice method to RealTimeString #237

Closed mmacfadden closed 3 years ago

mmacfadden commented 3 years ago

Versions

Description The RealTimeString class will be augmented with a splice method that will allow for easy, atomic, replacements of segments of a string.

public splice(index: number, deleteCount?: number, insertValue?: string): void

A few examples illustrating the feature:


rtString.value("ABCDEFGH");
rtString.splice(2, 3);        
console.log(rtString.value());  // ABFGH

rtString.value("ABCDEFGH");
rtString.splice(2, 0, "XY");  
console.log(rtString.value());  // ABXYCDEFGH

rtString.value("ABCDEFGH");
rtString.splice(2, 3, "XY");  
console.log(rtString.value());  // ABXYFGH

A new Event will be added to the RealTimeString class.

export class StringSpliceEvent implements IValueChangedEvent {
  public static readonly NAME = "splice";

  public readonly element: ObservableString;
  public readonly user: DomainUser;
  public readonly sessionId: string;
  public readonly local: boolean;
  public readonly index: number;
  public readonly deleteCount: number;
  public readonly insertValue: string;
}

The current insert and remove events will ultimately be updated to use the underlying splice mechanism, and those two previous methods will simply be convenience methods.

However, the current StringInsertEvent and StringRemove events will sill be kept. The following rules will be used to determine which event is emitted:

  1. If the splice has a deleteCount == 0 and non-empty insertValue emit a StringInsertEvent
  2. If the splice has a deleteCount > 0 and no insertValue emit a StringRemoveEvent
  3. If the splice has a deleteCount > 0 and non-empty insertValue emit a StringSpliceEvent

Tasks

Is there a workaround in the absence of this feature? At present time the user must do a batch operation consisting of a remove and an insert.

realTimeString.model().startBatch();
realTimeString.remove(4, 2);
realTimeString.insert(4, "hello");
realTimeString.model().completeBatch();
mmacfadden commented 3 years ago

This has been implemented.