From 8f01f90932208613b33ba94b4899de3f66386ede Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Thu, 28 Nov 2019 20:03:48 +0100 Subject: [PATCH] lazy link to remote --- src/services/CouchService.ts | 31 ++++++++----------------------- src/services/UserService.ts | 30 ------------------------------ 2 files changed, 8 insertions(+), 53 deletions(-) diff --git a/src/services/CouchService.ts b/src/services/CouchService.ts index 60fa625..82e34dd 100644 --- a/src/services/CouchService.ts +++ b/src/services/CouchService.ts @@ -28,26 +28,23 @@ const remoteConfig = { const LOCALE_DB: string = 'LOCALE_DB' const REMOTE: string = 'https://juliencalixte.ddns.net/database' -const initRemote = () => { - return new Promise((resolve, reject) => { - try { - resolve(new PouchDb(`${REMOTE}/vaquant`, remoteConfig)) - } catch (error) { - reject(error) - } - }) -} - class CouchService { - public remote: PouchDB.Database | null = null public db: PouchDB.Database | null = new PouchDb(LOCALE_DB) public remoteUserDb: PouchDB.Database | null = null public userDb: PouchDB.Database | null = null + private innerRemote: PouchDB.Database | null = null private user: IUser | null = null private sync: PouchDB.Replication.Sync<{}> | null = null private userSync: PouchDB.Replication.Sync<{}> | null = null private eventListened: boolean = false + public get remote(): PouchDB.Database { + if (!this.innerRemote) { + this.innerRemote = new PouchDb(`${REMOTE}/vaquant`, remoteConfig) + } + return this.innerRemote + } + constructor() { if (!this.eventListened) { this.eventListened = true @@ -60,13 +57,6 @@ class CouchService { this.cancelSync() }) } - this.initRemote() - } - - public async initRemote() { - if (!this.remote) { - this.remote = await initRemote() - } } public async initLive(): Promise { @@ -90,11 +80,6 @@ class CouchService { }) } - if (!this.remote) { - await this.initRemote() - return false - } - this.sync = this.db .sync(this.remote, { live: true, diff --git a/src/services/UserService.ts b/src/services/UserService.ts index d78d01e..8a5acc8 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -4,12 +4,6 @@ import IResponse from '@/models/IResponse' class UserService { public async signup(user: IUser, password: string): Promise { - if (!couchService.remote) { - return { - ok: false, - message: 'no remote' - } - } try { const response = await couchService.remote.signUp(user.userId, password, { metadata: user @@ -30,12 +24,6 @@ class UserService { } public async login(userId: string, password: string): Promise { - if (!couchService.remote) { - return { - ok: false, - message: 'no remote' - } - } try { const response = await couchService.remote.logIn(userId, password) return { @@ -57,12 +45,6 @@ class UserService { } public async logout(): Promise { - if (!couchService.remote) { - return { - ok: false, - message: 'no remote' - } - } try { const { ok } = await couchService.remote.logOut() return { @@ -77,12 +59,6 @@ class UserService { } public async deleteUser(userId: string): Promise { - if (!couchService.remote) { - return { - ok: false, - message: 'no remote' - } - } try { const { ok } = await couchService.remote.deleteUser(userId) return { @@ -96,9 +72,6 @@ class UserService { } public async getUser(current: IUser | null): Promise { - if (!couchService.remote) { - return null - } try { const session = await couchService.remote.getSession() if (session.ok && session.userCtx && session.userCtx.name) { @@ -118,9 +91,6 @@ class UserService { } public async updateUser(user: IUser): Promise { - if (!couchService.remote) { - return false - } try { const result = await couchService.remote.putUser(user.userId, { metadata: user