@itwin/core-i18n 4.8.0-dev.32 → 4.8.0-dev.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18476,6 +18476,11 @@ var Id64;
18476
18476
  }
18477
18477
  Id64.Uint32Map = Uint32Map;
18478
18478
  })(Id64 || (Id64 = {}));
18479
+ function validateLocalId(num) {
18480
+ if (num < 0 || Math.round(num) !== num) {
18481
+ throw new Error("Local Id must be a non-negative integer");
18482
+ }
18483
+ }
18479
18484
  /**
18480
18485
  * Generates unique [[Id64String]] values in sequence, which are guaranteed not to conflict with Ids associated with persistent elements or models.
18481
18486
  * This is useful for associating stable, non-persistent identifiers with things like [Decorator]($frontend)s.
@@ -18483,8 +18488,20 @@ var Id64;
18483
18488
  * @public
18484
18489
  */
18485
18490
  class TransientIdSequence {
18486
- constructor() {
18487
- this._localId = 0;
18491
+ /** Constructor.
18492
+ * @param initialLocalId The starting local Id. The local Id of the first [[Id64String]] generated by [[getNext]] will be `initialLocalId + 1`.
18493
+ */
18494
+ constructor(initialLocalId = 0) {
18495
+ validateLocalId(initialLocalId);
18496
+ this.initialLocalId = initialLocalId;
18497
+ this._localId = initialLocalId;
18498
+ }
18499
+ /** The maximum local Id generated by the sequence thus far. It is never less than [[initialLocalId]]. If it is equal to [[initialLocalId]], then the sequence has
18500
+ * not yet generated any Ids.
18501
+ * Each call to [[getNext]] increments this by 1 and uses it as the local Id of the generated [[Id64String]].
18502
+ */
18503
+ get currentLocalId() {
18504
+ return this._localId;
18488
18505
  }
18489
18506
  /** Generate and return the next transient Id64String in the sequence.
18490
18507
  * @deprecated in 3.x. Use [[getNext]].
@@ -18502,6 +18519,55 @@ class TransientIdSequence {
18502
18519
  peekNext() {
18503
18520
  return Id64.fromLocalAndBriefcaseIds(this._localId + 1, 0xffffff);
18504
18521
  }
18522
+ /** Convert this sequence to its JSON representation. */
18523
+ toJSON() {
18524
+ return {
18525
+ initialLocalId: this.initialLocalId,
18526
+ currentLocalId: this.currentLocalId,
18527
+ };
18528
+ }
18529
+ /** Create a sequence from its JSON representation. */
18530
+ static fromJSON(props) {
18531
+ validateLocalId(props.currentLocalId);
18532
+ const sequence = new TransientIdSequence(props.initialLocalId);
18533
+ sequence._localId = props.currentLocalId;
18534
+ return sequence;
18535
+ }
18536
+ /** Obtain the JSON representation of a new sequence that diverges from this sequence, with its [[initialLocalId]] set to this sequence's [[currentLocalId]].
18537
+ * The two sequences can generate Ids independently. Later, you can [[merge]] the sequences, resolving conflicts where the two sequences generated identical Ids.
18538
+ * This is chiefly useful when generating transient Ids on a [Worker](https://developer.mozilla.org/en-US/docs/Web/API/Worker).
18539
+ */
18540
+ fork() {
18541
+ return {
18542
+ initialLocalId: this.currentLocalId,
18543
+ currentLocalId: this.currentLocalId,
18544
+ };
18545
+ }
18546
+ /** Integrate the Ids generated by a [[fork]] of this sequence. All of the Ids generated by `source` will be remapped to Ids at the end of this sequence.
18547
+ * This is chiefly useful when generating transient Ids on a [Worker](https://developer.mozilla.org/en-US/docs/Web/API/Worker).
18548
+ * @param source The JSON representation of the [[fork]]ed sequence to be merged with this one.
18549
+ * @returns a function that permits you to remap the local Ids generated by `source` into the corresponding local Ids assigned by this sequence.
18550
+ * @throws Error if `source` is not a fork of this sequence or is malformed (e.g., contains negative and/or non-integer local Ids).
18551
+ */
18552
+ merge(source) {
18553
+ const { initialLocalId, currentLocalId } = source;
18554
+ validateLocalId(initialLocalId);
18555
+ validateLocalId(currentLocalId);
18556
+ if (initialLocalId > this.currentLocalId) {
18557
+ throw new Error("Transient Id sequences do not intersect");
18558
+ }
18559
+ if (initialLocalId > currentLocalId) {
18560
+ throw new Error("Current local Id cannot be less than initial local Id");
18561
+ }
18562
+ const delta = this.currentLocalId - initialLocalId;
18563
+ this._localId += currentLocalId - initialLocalId;
18564
+ return (sourceLocalId) => {
18565
+ if (sourceLocalId > initialLocalId && sourceLocalId <= currentLocalId) {
18566
+ return sourceLocalId + delta;
18567
+ }
18568
+ return sourceLocalId;
18569
+ };
18570
+ }
18505
18571
  }
18506
18572
  /**
18507
18573
  * The Guid namespace provides facilities for working with GUID strings using the "8-4-4-4-12" pattern.