UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

1 lines 2.29 kB
{"version":3,"file":"runtime-reference-identity.cjs","sources":["../../../src/query/runtime-reference-identity.ts"],"sourcesContent":["export type RuntimeReferenceIdentity = [\n `runtimeReference`,\n namespace: string,\n sequence: number,\n]\n\nexport function createRuntimeReferenceIdentityFactory(): (\n value: object,\n) => RuntimeReferenceIdentity {\n const namespace = createRuntimeReferenceNamespace()\n const referenceIds = new WeakMap<object, number>()\n let sequence = 0\n\n return (value) => {\n let referenceId = referenceIds.get(value)\n if (referenceId === undefined) {\n referenceId = ++sequence\n referenceIds.set(value, referenceId)\n }\n return [`runtimeReference`, namespace, referenceId]\n }\n}\n\nexport const getRuntimeReferenceIdentity =\n createRuntimeReferenceIdentityFactory()\n\nfunction createRuntimeReferenceNamespace(): string {\n const randomValues = new Uint32Array(4)\n const runtimeCrypto = Reflect.get(globalThis, `crypto`) as\n | { getRandomValues?: (values: Uint32Array) => Uint32Array }\n | undefined\n if (typeof runtimeCrypto?.getRandomValues === `function`) {\n runtimeCrypto.getRandomValues(randomValues)\n return Array.from(randomValues, (value) => value.toString(36)).join(`-`)\n }\n\n // Reference equality cannot survive a runtime boundary. A per-runtime nonce\n // prevents a persisted key from matching an unrelated reference after a\n // reload, even on platforms without Web Crypto.\n return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}-${Math.random().toString(36).slice(2)}`\n}\n"],"names":[],"mappings":";;AAMO,SAAS,wCAEc;AAC5B,QAAM,YAAY,gCAAA;AAClB,QAAM,mCAAmB,QAAA;AACzB,MAAI,WAAW;AAEf,SAAO,CAAC,UAAU;AAChB,QAAI,cAAc,aAAa,IAAI,KAAK;AACxC,QAAI,gBAAgB,QAAW;AAC7B,oBAAc,EAAE;AAChB,mBAAa,IAAI,OAAO,WAAW;AAAA,IACrC;AACA,WAAO,CAAC,oBAAoB,WAAW,WAAW;AAAA,EACpD;AACF;AAEO,MAAM,8BACX,sCAAA;AAEF,SAAS,kCAA0C;AACjD,QAAM,eAAe,IAAI,YAAY,CAAC;AACtC,QAAM,gBAAgB,QAAQ,IAAI,YAAY,QAAQ;AAGtD,MAAI,OAAO,eAAe,oBAAoB,YAAY;AACxD,kBAAc,gBAAgB,YAAY;AAC1C,WAAO,MAAM,KAAK,cAAc,CAAC,UAAU,MAAM,SAAS,EAAE,CAAC,EAAE,KAAK,GAAG;AAAA,EACzE;AAKA,SAAO,GAAG,KAAK,IAAA,EAAM,SAAS,EAAE,CAAC,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC;AACjH;;;"}