refactor: make event emitter threadsafe

This commit is contained in:
emizzle 2020-10-12 16:21:41 +11:00 committed by Iuri Matias
parent 9ce0ae08a1
commit e58f5b03eb
25 changed files with 91 additions and 28 deletions

3
.gitmodules vendored
View File

@ -40,9 +40,6 @@
[submodule "vendor/isaac"]
path = vendor/isaac
url = https://github.com/pragmagic/isaac.git
[submodule "vendor/eventemitter"]
path = vendor/eventemitter
url = https://github.com/al-bimani/eventemitter.git
[submodule "vendor/nim-libp2p"]
path = vendor/nim-libp2p
url = https://github.com/status-im/nim-libp2p

View File

@ -1,4 +1,4 @@
import NimQml, eventemitter, chronicles, tables
import NimQml, chronicles, tables
import ../../status/chat as chat_model
import ../../status/mailservers as mailserver_model
import ../../status/messages as messages_model
@ -7,6 +7,7 @@ import ../../status/libstatus/types as status_types
import ../../status/libstatus/settings as status_settings
import ../../status/[chat, contacts, status, wallet, stickers]
import view, views/channels_list, views/message_list
import ../../eventemitter
logScope:
topics = "chat-controller"

View File

@ -1,9 +1,10 @@
import NimQml, eventemitter, chronicles, options, std/wrapnils
import NimQml, chronicles, options, std/wrapnils
import ../../status/libstatus/types as status_types
import ../../status/signals/types
import ../../status/status
import view
import ../../status/accounts as status_accounts
import ../../eventemitter
type LoginController* = ref object
status*: Status

View File

@ -2,7 +2,7 @@ import NimQml, chronicles
import ../../status/signals/types
import ../../status/[status, node, network]
import ../../status/libstatus/types as status_types
import eventemitter
import ../../eventemitter
import view
logScope:

View File

@ -1,9 +1,10 @@
import NimQml, eventemitter, chronicles, std/wrapnils
import NimQml, chronicles, std/wrapnils
import ../../status/libstatus/types as status_types
import ../../status/libstatus/accounts as status_accounts
import ../../status/accounts as AccountModel
import ../../status/status
import ../../status/signals/types
import ../../eventemitter
import view
type OnboardingController* = ref object

View File

@ -1,4 +1,4 @@
import NimQml, json, eventemitter, strutils, sugar, sequtils
import NimQml, json, strutils, sugar, sequtils
import json_serialization
import ../../status/libstatus/mailservers as status_mailservers
import ../../status/signals/types
@ -11,6 +11,7 @@ import ../../status/chat as status_chat
import ../../status/devices
import ../../status/chat/chat
import ../../status/wallet
import ../../eventemitter
import view
import views/ens_manager
import chronicles

View File

@ -1,4 +1,4 @@
import NimQml, eventemitter, strformat, strutils, chronicles
import NimQml, strformat, strutils, chronicles
import view
import views/[asset_list, account_list, account_item]
@ -8,6 +8,7 @@ import ../../status/libstatus/types as status_types
import ../../status/signals/types
import ../../status/[status, wallet]
import ../../status/wallet/account as WalletTypes
import ../../eventemitter
type WalletController* = ref object
status: Status

52
src/eventemitter.nim Normal file
View File

@ -0,0 +1,52 @@
import # system libs
tables
import # deps
uuids
type
Args* = ref object of RootObj # ...args
Handler* = proc (args: Args) {.closure.} # callback function type
EventEmitter* = ref object
events: Table[string, Table[UUID, Handler]]
proc createEventEmitter*(): EventEmitter =
result.new
result.events = initTable[string, Table[UUID, Handler]]()
proc on(this: EventEmitter, name: string, handlerId: UUID, handler: Handler): void =
if this.events.hasKey(name):
this.events[name].add handlerId, handler
return
this.events[name] = [(handlerId, handler)].toTable
proc on*(this: EventEmitter, name: string, handler: Handler): void =
var uuid: UUID
this.on(name, uuid, handler)
proc once*(this:EventEmitter, name:string, handler:Handler): void =
var handlerId = genUUID()
this.on(name, handlerId) do(a: Args):
handler(a)
this.events[name].del handlerId
proc emit*(this:EventEmitter, name:string, args:Args): void =
if this.events.hasKey(name):
for (id, handler) in this.events[name].pairs:
handler(args)
when isMainModule:
block:
type ReadyArgs = ref object of Args
text: string
var evts = createEventEmitter()
evts.on("ready") do(a: Args):
var args = ReadyArgs(a)
echo args.text, ": from [1st] handler"
evts.once("ready") do(a: Args):
var args = ReadyArgs(a)
echo args.text, ": from [2nd] handler"
evts.emit("ready", ReadyArgs(text:"Hello, World"))
evts.emit("ready", ReadyArgs(text:"Hello, World"))

View File

@ -1,4 +1,4 @@
import NimQml, eventemitter, chronicles, os, strformat
import NimQml, chronicles, os, strformat
import app/chat/core as chat
import app/wallet/core as wallet
@ -11,6 +11,7 @@ import status/signals/core as signals
import status/libstatus/types
import nim_status
import status/status as statuslib
import ./eventemitter
var signalsQObjPointer: pointer

View File

@ -1,8 +1,9 @@
import eventemitter, options, chronicles, json
import options, chronicles, json
import libstatus/accounts as status_accounts
import libstatus/settings as status_settings
import libstatus/types
import libstatus/utils
import ../eventemitter
type
AccountModel* = ref object

View File

@ -1,10 +1,11 @@
import eventemitter, json, strutils, sequtils, tables, chronicles, times
import json, strutils, sequtils, tables, chronicles, times
import libstatus/chat as status_chat
import libstatus/mailservers as status_mailservers
import libstatus/chatCommands as status_chat_commands
import libstatus/accounts/constants as constants
import libstatus/types
import stickers
import ../eventemitter
import profile/profile
import contacts

View File

@ -1,6 +1,7 @@
import eventemitter, json, sequtils
import json, sequtils
import libstatus/contacts as status_contacts
import profile/profile
import ../eventemitter
const DELETE_CONTACT* = "__deleteThisContact__"

View File

@ -1,6 +1,7 @@
import eventemitter, json, options, typetraits
import json, options, typetraits
import web3/ethtypes, json_serialization, stint
import accounts/constants
import ../../eventemitter
type SignalType* {.pure.} = enum
Message = "messages.new"

View File

@ -1,7 +1,8 @@
import algorithm, json, random, math, os, tables, sets, chronicles, eventemitter, sequtils, locks, sugar
import algorithm, json, random, math, os, tables, sets, chronicles, sequtils, locks, sugar
import libstatus/core as status_core
import libstatus/chat as status_chat
import libstatus/mailservers as status_mailservers
import ../eventemitter
# How do mailserver should work ?

View File

@ -1,5 +1,6 @@
import tables, sets, eventemitter
import tables, sets
import libstatus/chat
import ../eventemitter
type
MessageDetails* = object

View File

@ -1,4 +1,5 @@
import chronicles, eventemitter
import chronicles
import ../eventemitter
logScope:
topics = "network-model"

View File

@ -1,5 +1,5 @@
import eventemitter
import libstatus/core as status
import ../eventemitter
type NodeModel* = ref object
events*: EventEmitter

View File

@ -1,8 +1,9 @@
import json, eventemitter
import json
import libstatus/types
import profile/profile
import libstatus/core as libstatus_core
import libstatus/accounts as status_accounts
import ../eventemitter
type
ProfileModel* = ref object

View File

@ -1,7 +1,8 @@
import NimQml, eventemitter, tables, json, chronicles, strutils, json_serialization
import NimQml, tables, json, chronicles, strutils, json_serialization
import ../libstatus/types as status_types
import types, messages, discovery, whisperFilter, envelopes, expired, wallet
import ../status
import ../../eventemitter
logScope:
topics = "signals"

View File

@ -2,7 +2,7 @@ import json, chronicles, json_serialization, tables
import ../libstatus/types
import ../chat/[chat, message]
import ../profile/[profile, devices]
import eventemitter
import ../../eventemitter
type Signal* = ref object of Args
signalType* {.serializedFieldName("type").}: SignalType

View File

@ -1,10 +1,9 @@
import eventemitter
import libstatus/accounts as libstatus_accounts
import libstatus/core as libstatus_core
import libstatus/settings as libstatus_settings
import libstatus/types as libstatus_types
import chat, accounts, wallet, node, network, mailservers, messages, contacts, profile, stickers
import ../eventemitter
export chat, accounts, node, mailservers, messages, contacts, profile, network

View File

@ -2,12 +2,12 @@ import # global deps
tables, strutils, sequtils
import # project deps
chronicles, web3/[ethtypes, conversions], eventemitter, stint
chronicles, web3/[ethtypes, conversions], stint
import # local deps
libstatus/types, libstatus/eth/contracts as status_contracts,
libstatus/stickers as status_stickers, transactions,
libstatus/wallet
libstatus/wallet, ../eventemitter
from libstatus/utils as libstatus_utils import eth2Wei, gwei2Wei, toUInt64, parseAddress

View File

@ -1,4 +1,4 @@
import eventemitter, json, strformat, strutils, chronicles, sequtils, httpclient, tables
import json, strformat, strutils, chronicles, sequtils, httpclient, tables
import json_serialization, stint
from web3/ethtypes import Address, EthSend, Quantity
from web3/conversions import `$`
@ -14,6 +14,7 @@ from libstatus/types import PendingTransactionType, GeneratedAccount, DerivedAcc
from libstatus/utils as libstatus_utils import eth2Wei, gwei2Wei, first, toUInt64, parseAddress
import wallet/[balance_manager, account, collectibles]
import transactions
import ../eventemitter
export account, collectibles
export Transaction

View File

@ -1,4 +1,4 @@
from eventemitter import Args
from ../../eventemitter import Args
import ../libstatus/types
type CollectibleList* = ref object

1
vendor/eventemitter vendored

@ -1 +0,0 @@
Subproject commit 49cfa2f3135139c3488b68fdd061cc069d31d651