lazy link to remote

This commit is contained in:
2019-11-28 20:03:48 +01:00
parent cb85217751
commit 8f01f90932
2 changed files with 8 additions and 53 deletions

View File

@@ -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<PouchDB.Database>((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<boolean> {
@@ -90,11 +80,6 @@ class CouchService {
})
}
if (!this.remote) {
await this.initRemote()
return false
}
this.sync = this.db
.sync(this.remote, {
live: true,

View File

@@ -4,12 +4,6 @@ import IResponse from '@/models/IResponse'
class UserService {
public async signup(user: IUser, password: string): Promise<IResponse> {
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<IResponse> {
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<IResponse> {
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<IResponse> {
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<IUser | null> {
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<boolean> {
if (!couchService.remote) {
return false
}
try {
const result = await couchService.remote.putUser(user.userId, {
metadata: user