Fix: loading backend from master desktop (#69)

This commit is contained in:
Anthony Laibe 2021-09-29 17:09:38 +02:00 committed by GitHub
parent b2ecbda9e7
commit 62a7c02153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 41 additions and 108 deletions

View File

@ -1,31 +1,16 @@
import backend_type
from ./types import Backend, StatusGoBackend, MockBackend
export Backend, StatusGoBackend, MockBackend
import backend_wrapper
export backend_wrapper
from statusgo/types as statusgo_types import StatusGoBackend
from mock/types as mock_types import MockBackend
export StatusGoBackend
export MockBackend
import base/bookmarks
import statusgo/statusgo_instance
export newStatusGoBackendInstance
import mock/mock_instance
export newMockBackendInstance
import statusgo/bookmark as statusgo_bookmark
import mock/bookmark as mock_bookmark
from bookmarks as bookmarks_methods import storeBookmark, updateBookmark, getBookmarks, deleteBookmark
from base/bookmarks as bookmarks_methods import storeBookmark, updateBookmark, getBookmarks, deleteBookmark
export storeBookmark, updateBookmark, getBookmarks, deleteBookmark
method loadBackend*(self: BackendWrapper, name: string) =
if name == "statusgo":
self.backend = newStatusGoBackendInstance()
if name == "mock":
self.backend = newMockBackendInstance()
else:
raise newException(ValueError, "unknown backend")
import statusgo/bookmarks as statusgo_bookmarks
import mock/bookmarks as mock_bookmarks
proc newBackend*(name: string): Backend =
if name == "statusgo":
result = StatusGoBackend()
elif name == "mock":
result = MockBackend()
else:
raise newException(ValueError, "unknown backend")

View File

@ -1 +0,0 @@
type Backend* = ref object of RootObj

View File

@ -1,7 +0,0 @@
import backend_type
type BackendWrapper* = ref object
backend*: Backend
proc newBackendWrapperInstance*(): BackendWrapper =
result = BackendWrapper()

View File

@ -1,14 +1,14 @@
import ../../types/[bookmark]
import ../backend_type
import ../types
method storeBookmark*(self: Backend, bookmark: Bookmark): Bookmark =
raise newException(ValueError, "No implementation available")
raise newException(ValueError, "No implementation available")
method updateBookmark*(self: Backend, originalUrl: string, bookmark: Bookmark) =
raise newException(ValueError, "No implementation available")
raise newException(ValueError, "No implementation available")
method getBookmarks*(self: Backend): seq[Bookmark] =
raise newException(ValueError, "No implementation available")
raise newException(ValueError, "No implementation available")
method deleteBookmark*(self: Backend, url: string) =
raise newException(ValueError, "No implementation available")
raise newException(ValueError, "No implementation available")

View File

@ -1,2 +0,0 @@
import bookmark
export bookmark

View File

@ -1,15 +0,0 @@
import backend_type, backend_wrapper
import base/bookmarks
import ../types/[bookmark]
proc storeBookmark*(self: BackendWrapper, bookmark: Bookmark): Bookmark =
self.backend.storeBookmark(bookmark)
proc updateBookmark*(self: BackendWrapper, originalUrl: string, bookmark: Bookmark) =
self.backend.updateBookmark(originalUrl, bookmark)
proc getBookmarks*(self: BackendWrapper): seq[Bookmark] =
self.backend.getBookmarks()
proc deleteBookmark*(self: BackendWrapper, url: string) =
self.backend.deleteBookmark(url)

View File

@ -1,7 +1,7 @@
import tables, sequtils
import types
import ../../types/[bookmark], json, chronicles
import ../backend_type
import tables, sequtils, json, chronicles
import ../types
import ../../types/[bookmark]
var bookmarks_storage_mock = initTable[string, Bookmark]()

View File

@ -1,8 +0,0 @@
import types
proc newMockBackendInstance*(): MockBackend =
result = MockBackend()
export types
# import bookmark
# export bookmark

View File

@ -1,5 +0,0 @@
import types
proc newMockBackendInstance*(): MockBackend =
result = MockBackend()

View File

@ -1,3 +0,0 @@
import ../backend_type
type MockBackend* = ref object of Backend

View File

@ -1,8 +1,10 @@
import types
import core, ../../types/[bookmark], json, chronicles
import ../backend_type
import json, chronicles
proc storeBookmark*(bookmark: Bookmark): Bookmark =
import ../../types/[bookmark]
import ../types
import ./core
method storeBookmark*(self: StatusGoBackend, bookmark: Bookmark): Bookmark =
let payload = %* [{"url": bookmark.url, "name": bookmark.name}]
try:
let resp = callPrivateRPC("browsers_storeBookmark", payload).parseJson["result"]
@ -12,7 +14,7 @@ proc storeBookmark*(bookmark: Bookmark): Bookmark =
error "Error updating bookmark", msg = e.msg
discard
proc updateBookmark*(originalUrl: string, bookmark: Bookmark) =
method updateBookmark*(self: StatusGoBackend, originalUrl: string, bookmark: Bookmark) =
let payload = %* [originalUrl, {"url": bookmark.url, "name": bookmark.name}]
try:
discard callPrivateRPC("browsers_updateBookmark", payload)
@ -20,7 +22,7 @@ proc updateBookmark*(originalUrl: string, bookmark: Bookmark) =
error "Error updating bookmark", msg = e.msg
discard
proc getBookmarks*(): seq[Bookmark] =
method getBookmarks*(self: StatusGoBackend): seq[Bookmark] =
let payload = %* []
try:
let responseResult = callPrivateRPC("browsers_getBookmarks", payload).parseJson["result"]
@ -30,6 +32,6 @@ proc getBookmarks*(): seq[Bookmark] =
except:
discard
proc deleteBookmark*(url: string) =
method deleteBookmark*(self: StatusGoBackend, url: string) =
let payload = %* [url]
discard callPrivateRPC("browsers_deleteBookmark", payload)

View File

@ -1,8 +0,0 @@
import types
proc newStatusGoBackendInstance*(): StatusGoBackend =
result = StatusGoBackend()
export types
import bookmark
export bookmark

View File

@ -1,4 +0,0 @@
import types
proc newStatusGoBackendInstance*(): StatusGoBackend =
result = StatusGoBackend()

View File

@ -1,3 +0,0 @@
import ../backend_type
type StatusGoBackend* = ref object of Backend

3
backends/types.nim Normal file
View File

@ -0,0 +1,3 @@
type Backend* = ref object of RootObj
type StatusGoBackend* = ref object of Backend
type MockBackend* = ref object of Backend

View File

@ -6,11 +6,11 @@ import ../types/[bookmark]
import ../backends/backend
type
BrowserModel* = ref object
events*: EventEmitter
backend*: BackendWrapper
BrowserModel* = ref object
events*: EventEmitter
backend*: Backend
proc newBrowserModel*(events: EventEmitter, backend: BackendWrapper): BrowserModel =
proc newBrowserModel*(events: EventEmitter, backend: Backend): BrowserModel =
result = BrowserModel()
result.events = events
result.backend = backend

View File

@ -12,7 +12,7 @@ import ../backends/backend
export chat, accounts, node, messages, contacts, profile, network, permissions, fleet, eventemitter
type Status* = ref object
backend*: BackendWrapper
backend*: Backend
events*: EventEmitter
fleet*: FleetModel
chat*: ChatModel
@ -33,10 +33,9 @@ type Status* = ref object
provider*: ProviderModel
osnotifications*: OsNotifications
proc newStatusInstance*(fleetConfig: string, backendName:string = "statusgo"): Status =
proc newStatusInstance*(fleetConfig: string, backendName: string = "statusgo"): Status =
result = Status()
result.backend = newBackendWrapperInstance()
result.backend.loadBackend(backendName)
result.backend = newBackend(backendName)
result.events = createEventEmitter()
result.fleet = fleet.newFleetModel(fleetConfig)
result.chat = chat.newChatModel(result.events)