* Initiate a sync connection with the server
(session: SyncSession | undefined)
| 789 | * Initiate a sync connection with the server |
| 790 | */ |
| 791 | createConnection(session: SyncSession | undefined) { |
| 792 | // Validate that there is enough information to connect |
| 793 | if (!this.validateSessionWithWarning(session)) return; |
| 794 | // If we are creating a connection for a session that is not the current session, we should not proceed |
| 795 | if (this.currentSession !== session) return; |
| 796 | // If we are already connected, we should not proceed |
| 797 | if (session.status === 'OPEN') return; |
| 798 | if (session.status === 'CONNECTING') { |
| 799 | console.warn('Already connecting, ignoring connect call'); |
| 800 | return; |
| 801 | } |
| 802 | session.status = 'CONNECTING'; |
| 803 | this.fireConnectionChangeHandlers(session); |
| 804 | // if (isOpeningConnection) { |
| 805 | // console.log('OPENING CONNECTION'); |
| 806 | |
| 807 | // // this.lastParamsHash = undefined; // reset lastParamsHash |
| 808 | // } |
| 809 | |
| 810 | // // TODO: we are sort of double checking this |
| 811 | // const paramsHash = hashObject({ |
| 812 | // token: this.currentSession.token, |
| 813 | // server: this.currentSession.serverUrl, |
| 814 | // }); |
| 815 | // console.log(paramsHash, this.lastParamsHash); |
| 816 | // // We can get stuck CONNECTING here in reconnect loop |
| 817 | // // Dont reconnect with the same parameters |
| 818 | // // if (this.lastParamsHash === paramsHash) return; |
| 819 | |
| 820 | // Setup connection |
| 821 | this.transport.connect({ |
| 822 | token: session.token, |
| 823 | server: session.serverUrl, |
| 824 | syncSchema: false, |
| 825 | schema: undefined, |
| 826 | }); |
| 827 | |
| 828 | // Setup listeners |
| 829 | // There is still probably too much "global" state that we should continue to refactor |
| 830 | // To prevent confusion, we are binding the handlers to the current session so they only update that session |
| 831 | this.transport.onMessage(this.onMessageHandler(session).bind(this)); |
| 832 | this.transport.onOpen(this.onOpenHandler(session).bind(this)); |
| 833 | this.transport.onClose(this.onCloseHandler(session).bind(this)); |
| 834 | this.transport.onError(this.onErrorHandler(session).bind(this)); |
| 835 | } |
| 836 | |
| 837 | private async initializeSync() { |
| 838 | const syncStatus = await this.syncWrites(); |
no test coverage detected