@@ -9,10 +9,16 @@ const AV = global.AV || {};
99
1010// All internal configuration items
1111AV . _config = {
12- userAgent,
1312 serverURLs : { } ,
1413 useMasterKey : false ,
1514 production : null ,
15+ realtime : null ,
16+ } ;
17+
18+ // configs shared by all AV instances
19+ AV . _sharedConfig = {
20+ userAgent,
21+ liveQueryRealtime : null ,
1622} ;
1723
1824/**
@@ -43,38 +49,54 @@ AV._getAVPath = function(path) {
4349 return "AV/" + AV . applicationId + "/" + path ;
4450} ;
4551
52+ const hexOctet = ( ) => Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 ) . toString ( 16 ) . substring ( 1 ) ;
53+ const uuid = ( ) => `${ hexOctet ( ) } ${ hexOctet ( ) } -${ hexOctet ( ) } -${ hexOctet ( ) } -${ hexOctet ( ) } -${ hexOctet ( ) } ${ hexOctet ( ) } ${ hexOctet ( ) } ` ;
54+
4655/**
4756 * Returns the unique string for this app on this machine.
4857 * Gets reset when localStorage is cleared.
4958 * @private
5059 */
5160AV . _installationId = null ;
52- AV . _getInstallationId = function ( ) {
61+ AV . _getInstallationId = ( ) => {
5362 // See if it's cached in RAM.
5463 if ( AV . _installationId ) {
5564 return AV . Promise . resolve ( AV . _installationId ) ;
5665 }
5766
5867 // Try to get it from localStorage.
59- var path = AV . _getAVPath ( " installationId" ) ;
60- return AV . localStorage . getItemAsync ( path ) . then ( function ( _installationId ) {
68+ const path = AV . _getAVPath ( ' installationId' ) ;
69+ return AV . localStorage . getItemAsync ( path ) . then ( ( _installationId ) => {
6170 AV . _installationId = _installationId ;
6271 if ( ! AV . _installationId ) {
6372 // It wasn't in localStorage, so create a new one.
64- var hexOctet = function ( ) {
65- return Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 ) . toString ( 16 ) . substring ( 1 ) ;
66- } ;
67- AV . _installationId = (
68- hexOctet ( ) + hexOctet ( ) + "-" +
69- hexOctet ( ) + "-" +
70- hexOctet ( ) + "-" +
71- hexOctet ( ) + "-" +
72- hexOctet ( ) + hexOctet ( ) + hexOctet ( ) ) ;
73- return AV . localStorage . setItemAsync ( path , AV . _installationId ) ;
73+ AV . _installationId = _installationId = uuid ( ) ;
74+ return AV . localStorage . setItemAsync ( path , _installationId ) . then ( ( ) => _installationId ) ;
7475 }
75- else {
76- return _installationId ;
76+ return _installationId ;
77+ } ) ;
78+ } ;
79+
80+ AV . _subscriptionId = null ;
81+ AV . _refreshSubscriptionId = ( path = AV . _getAVPath ( 'subscriptionId' ) ) => {
82+ const subscriptionId = AV . _subscriptionId = uuid ( ) ;
83+ return AV . localStorage . setItemAsync ( path , subscriptionId ) . then ( ( ) => subscriptionId ) ;
84+ } ;
85+ AV . _getSubscriptionId = ( ) => {
86+ // See if it's cached in RAM.
87+ if ( AV . _subscriptionId ) {
88+ return AV . Promise . resolve ( AV . _subscriptionId ) ;
89+ }
90+
91+ // Try to get it from localStorage.
92+ const path = AV . _getAVPath ( 'subscriptionId' ) ;
93+ return AV . localStorage . getItemAsync ( path ) . then ( ( _subscriptionId ) => {
94+ AV . _subscriptionId = _subscriptionId ;
95+ if ( ! AV . _subscriptionId ) {
96+ // It wasn't in localStorage, so create a new one.
97+ _subscriptionId = AV . _refreshSubscriptionId ( path ) ;
7798 }
99+ return _subscriptionId ;
78100 } ) ;
79101} ;
80102
0 commit comments