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 LOCALE_DB: string = 'LOCALE_DB'
const REMOTE: string = 'https://juliencalixte.ddns.net/database' 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 { class CouchService {
public remote: PouchDB.Database | null = null
public db: PouchDB.Database | null = new PouchDb(LOCALE_DB) public db: PouchDB.Database | null = new PouchDb(LOCALE_DB)
public remoteUserDb: PouchDB.Database | null = null public remoteUserDb: PouchDB.Database | null = null
public userDb: PouchDB.Database | null = null public userDb: PouchDB.Database | null = null
private innerRemote: PouchDB.Database | null = null
private user: IUser | null = null private user: IUser | null = null
private sync: PouchDB.Replication.Sync<{}> | null = null private sync: PouchDB.Replication.Sync<{}> | null = null
private userSync: PouchDB.Replication.Sync<{}> | null = null private userSync: PouchDB.Replication.Sync<{}> | null = null
private eventListened: boolean = false private eventListened: boolean = false
public get remote(): PouchDB.Database {
if (!this.innerRemote) {
this.innerRemote = new PouchDb(`${REMOTE}/vaquant`, remoteConfig)
}
return this.innerRemote
}
constructor() { constructor() {
if (!this.eventListened) { if (!this.eventListened) {
this.eventListened = true this.eventListened = true
@@ -60,13 +57,6 @@ class CouchService {
this.cancelSync() this.cancelSync()
}) })
} }
this.initRemote()
}
public async initRemote() {
if (!this.remote) {
this.remote = await initRemote()
}
} }
public async initLive(): Promise<boolean> { public async initLive(): Promise<boolean> {
@@ -90,11 +80,6 @@ class CouchService {
}) })
} }
if (!this.remote) {
await this.initRemote()
return false
}
this.sync = this.db this.sync = this.db
.sync(this.remote, { .sync(this.remote, {
live: true, live: true,

View File

@@ -4,12 +4,6 @@ import IResponse from '@/models/IResponse'
class UserService { class UserService {
public async signup(user: IUser, password: string): Promise<IResponse> { public async signup(user: IUser, password: string): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try { try {
const response = await couchService.remote.signUp(user.userId, password, { const response = await couchService.remote.signUp(user.userId, password, {
metadata: user metadata: user
@@ -30,12 +24,6 @@ class UserService {
} }
public async login(userId: string, password: string): Promise<IResponse> { public async login(userId: string, password: string): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try { try {
const response = await couchService.remote.logIn(userId, password) const response = await couchService.remote.logIn(userId, password)
return { return {
@@ -57,12 +45,6 @@ class UserService {
} }
public async logout(): Promise<IResponse> { public async logout(): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try { try {
const { ok } = await couchService.remote.logOut() const { ok } = await couchService.remote.logOut()
return { return {
@@ -77,12 +59,6 @@ class UserService {
} }
public async deleteUser(userId: string): Promise<IResponse> { public async deleteUser(userId: string): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try { try {
const { ok } = await couchService.remote.deleteUser(userId) const { ok } = await couchService.remote.deleteUser(userId)
return { return {
@@ -96,9 +72,6 @@ class UserService {
} }
public async getUser(current: IUser | null): Promise<IUser | null> { public async getUser(current: IUser | null): Promise<IUser | null> {
if (!couchService.remote) {
return null
}
try { try {
const session = await couchService.remote.getSession() const session = await couchService.remote.getSession()
if (session.ok && session.userCtx && session.userCtx.name) { if (session.ok && session.userCtx && session.userCtx.name) {
@@ -118,9 +91,6 @@ class UserService {
} }
public async updateUser(user: IUser): Promise<boolean> { public async updateUser(user: IUser): Promise<boolean> {
if (!couchService.remote) {
return false
}
try { try {
const result = await couchService.remote.putUser(user.userId, { const result = await couchService.remote.putUser(user.userId, {
metadata: user metadata: user