{"version":3,"file":"index.cjs","sources":["../src/constants.ts","../src/env.ts","../src/id.ts","../src/now.ts"],"sourcesContent":["/**\n * The `UNINITIALIZED` symbol represents a special internal value that can be used to differentiate\n * between any user-supplied value and the state of being uninitialized.\n *\n * You do not **need** to import `@starbeam/shared` to get this symbol, as it is specified using\n * `Symbol.for`.\n */\nconst UNINITIALIZED = Symbol.for(\"starbeam.UNINITIALIZED\");\ntype UNINITIALIZED = typeof UNINITIALIZED;\n\n/**\n * The `REACTIVE` symbol is the protocol entry point for reactive values. Implementations of\n * the `ReactiveProtocol` interface specify their reactive behavior under this symbol.\n */\nconst REACTIVE: unique symbol = Symbol.for(\"starbeam.REACTIVE\");\ntype REACTIVE = typeof REACTIVE;\n\n/**\n * The `NOW` symbol is the name on `globalThis` that is used to store the current timestamp.\n */\nconst COORDINATION: unique symbol = Symbol.for(\"starbeam.COORDINATION\");\ntype COORDINATION = typeof COORDINATION;\n\nexport { COORDINATION, REACTIVE, UNINITIALIZED };\n","import { COORDINATION } from \"./constants.js\";\n\nexport interface Clock {\n  timestamp: number;\n}\n\nexport interface StarbeamCoordination {\n  now: {\n    timestamp: number;\n  };\n  id: {\n    get: () => string | number;\n  };\n}\n\nexport interface GlobalWithStarbeam {\n  [COORDINATION]: StarbeamCoordination;\n}\n\nexport function getCoordination(): Partial<StarbeamCoordination> {\n  let coordination = (globalThis as unknown as Partial<GlobalWithStarbeam>)[\n    COORDINATION\n  ];\n\n  if (!coordination) {\n    (globalThis as unknown as GlobalWithStarbeam)[COORDINATION] = coordination =\n      {} as GlobalWithStarbeam[typeof COORDINATION];\n  }\n\n  return coordination;\n}\n","import { getCoordination } from \"./env.js\";\n\nconst COORDINATION = getCoordination();\n\n/**\n * This code provides a guaranteed-unique ID, even if there are multiple copies of `@starbeam/shared`.\n *\n * This code intentionally does not use a UUID to make it easier to use these IDs in debugging\n * context, which is their primary purpose.\n */\nlet idGenerator = COORDINATION.id;\n\nif (!idGenerator) {\n  let CURRENT_ID = 1;\n\n  idGenerator = COORDINATION.id = {\n    get() {\n      return CURRENT_ID++;\n    },\n  };\n}\n\nconst ID_GENERATOR = idGenerator;\n\n/**\n * Get a fresh unique ID.\n */\nexport function getID(): string | number {\n  return ID_GENERATOR.get();\n}\n","import { getCoordination } from \"./env.js\";\n\n/**\n * The `CLOCK` constant is a universal monotonically increasing clock. The `Timestamp` class is used\n * in `@starbeam/timeline` and `@starbeam/universal`, but `Timestamp` defers to this constant. This means\n * that multiple copies of `@starbeam/timeline` will still see the same monotonically increasing clock.\n *\n * The term \"timestamp\" is used in this context to refer to a monotonically increasing number, where\n * each number represents a different moment in time.\n */\nconst coordination = getCoordination();\n\nlet clock = coordination.now;\n\nif (!clock) {\n  clock = coordination.now = {\n    timestamp: 0,\n  };\n}\n\nconst CLOCK = clock;\nconst TICK = 1;\n\n/**\n * Get the current timestamp.\n */\nexport function now(): number {\n  return CLOCK.timestamp;\n}\n\n/**\n * Increment the current timestamp, and return the new one.\n */\nexport function bump(): number {\n  CLOCK.timestamp = CLOCK.timestamp + TICK;\n  return now();\n}\n"],"names":["UNINITIALIZED","Symbol","for","REACTIVE","COORDINATION","getCoordination","coordination","globalThis","idGenerator","id","CURRENT_ID","get","ID_GENERATOR","getID","clock","now","timestamp","CLOCK","TICK","bump"],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,aAAa,GAAGC,MAAM,CAACC,GAAG,CAAC,wBAAwB,EAAC;AAG1D;AACA;AACA;AACA;AACA,MAAMC,QAAuB,GAAGF,MAAM,CAACC,GAAG,CAAC,mBAAmB,EAAC;AAG/D;AACA;AACA;AACA,MAAME,cAA2B,GAAGH,MAAM,CAACC,GAAG,CAAC,uBAAuB;;ACD/D,SAASG,eAAe,GAAkC;AAC/D,EAAA,IAAIC,YAAY,GAAIC,UAAU,CAC5BH,cAAY,CACb,CAAA;EAED,IAAI,CAACE,YAAY,EAAE;AAChBC,IAAAA,UAAU,CAAmCH,cAAY,CAAC,GAAGE,Y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