diff --git a/waku/protocol/v2/waku_filter.nim b/waku/protocol/v2/waku_filter.nim index ca332e57d..bb4456a09 100644 --- a/waku/protocol/v2/waku_filter.nim +++ b/waku/protocol/v2/waku_filter.nim @@ -74,25 +74,22 @@ proc init*(T: type MessagePush, buffer: seq[byte]): ProtoResult[T] = ok(push) -proc init*(T: type WakuFilter): T = - var ws = WakuFilter(subscribers: newSeq[Subscriber](0)) - - # From my understanding we need to set up filters, - # then on every message received we need the handle function to send it to the connection - # if the peer subscribed. - +method init*(wf: WakuFilter) = proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} = var message = await conn.readLp(64*1024) var res = FilterRequest.init(message) if res.isErr: return - ws.subscribers.add(Subscriber(connection: conn, filter: res.value)) + wf.subscribers.add(Subscriber(connection: conn, filter: res.value)) # @TODO THIS IS A VERY ROUGH EXPERIMENT - ws.handler = handle - ws.codec = WakuFilterCodec - result = ws + wf.handler = handle + wf.codec = WakuFilterCodec + +proc init*(T: type WakuFilter): T = + new result + result.init() proc subscription*(proto: WakuFilter): MessageNotificationSubscription = ## Returns a Filter for the specific protocol diff --git a/waku/protocol/v2/waku_store.nim b/waku/protocol/v2/waku_store.nim index 35c05f635..b756fe36f 100644 --- a/waku/protocol/v2/waku_store.nim +++ b/waku/protocol/v2/waku_store.nim @@ -61,9 +61,7 @@ proc query(w: WakuStore, query: HistoryQuery): HistoryResponse = if msg.contentTopic in query.topics: result.messages.insert(msg) -proc init*(T: type WakuStore): T = - var ws = WakuStore() - +method init*(ws: WakuStore) = proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} = var message = await conn.readLp(64*1024) var rpc = HistoryQuery.init(message) @@ -78,7 +76,10 @@ proc init*(T: type WakuStore): T = ws.handler = handle ws.codec = WakuStoreCodec - result = ws + +proc init*(T: type WakuStore): T = + new result + result.init() proc subscription*(proto: WakuStore): MessageNotificationSubscription = ## The filter function returns the pubsub filter for the node.