@kubun/hlc 0.9.0 → 0.10.1

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.
package/lib/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export type HLCTimestamp = {
5
5
  };
6
6
  type HLCParams = {
7
7
  nodeID: string;
8
+ counterCeiling?: number;
8
9
  };
9
10
  export declare class HLC {
10
11
  #private;
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- export class HLC{#t;#e=0;#l=0;constructor(t){this.#t=t.nodeID}get nodeID(){return this.#t}now(){let t=Date.now();return t>this.#e?(this.#e=t,this.#l=0):this.#l++,{wallTime:this.#e,counter:this.#l,nodeID:this.#t}}receive(t){let e=Date.now();return e>this.#e&&e>t.wallTime?(this.#e=e,this.#l=0):t.wallTime>this.#e?(this.#e=t.wallTime,this.#l=t.counter+1):this.#e>t.wallTime?this.#l++:this.#l=Math.max(this.#l,t.counter)+1,{wallTime:this.#e,counter:this.#l,nodeID:this.#t}}static serialize(t){let e=new Date(t.wallTime).toISOString(),l=t.counter.toString().padStart(4,"0");return`${e}:${l}:${t.nodeID}`}static parse(t){let e=t.indexOf("Z"),l=new Date(t.slice(0,e+1)).getTime(),i=t.slice(e+2),a=i.indexOf(":");return{wallTime:l,counter:Number.parseInt(i.slice(0,a),10),nodeID:i.slice(a+1)}}static compare(t,e){return t.wallTime!==e.wallTime?t.wallTime-e.wallTime:t.counter!==e.counter?t.counter-e.counter:t.nodeID<e.nodeID?-1:+(t.nodeID>e.nodeID)}}
1
+ export class HLC{#t;#e=0;#l=0;#i;constructor(t){this.#t=t.nodeID,this.#i=Math.min(t.counterCeiling??1e8,1e8)}get nodeID(){return this.#t}#n(t){t>=this.#i?(this.#e+=1,this.#l=0):this.#l=t}now(){let t=Date.now();return t>this.#e?(this.#e=t,this.#l=0):this.#n(this.#l+1),{wallTime:this.#e,counter:this.#l,nodeID:this.#t}}receive(t){let e=Date.now();return e>this.#e&&e>t.wallTime?(this.#e=e,this.#l=0):t.wallTime>this.#e?(this.#e=t.wallTime,this.#n(t.counter+1)):this.#e>t.wallTime?this.#n(this.#l+1):this.#n(Math.max(this.#l,t.counter)+1),{wallTime:this.#e,counter:this.#l,nodeID:this.#t}}static serialize(t){let e=new Date(t.wallTime).toISOString(),l=t.counter.toString().padStart(8,"0");return`${e}:${l}:${t.nodeID}`}static parse(t){let e=t.indexOf("Z"),l=new Date(t.slice(0,e+1)).getTime(),i=t.slice(e+2),n=i.indexOf(":");return{wallTime:l,counter:Number.parseInt(i.slice(0,n),10),nodeID:i.slice(n+1)}}static compare(t,e){return t.wallTime!==e.wallTime?t.wallTime-e.wallTime:t.counter!==e.counter?t.counter-e.counter:t.nodeID<e.nodeID?-1:+(t.nodeID>e.nodeID)}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/hlc",
3
- "version": "0.9.0",
3
+ "version": "0.10.1",
4
4
  "license": "see LICENSE.md",
5
5
  "keywords": [],
6
6
  "type": "module",