mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-02 00:02:49 +00:00
Add a method to send messages on the network using pubsub (#293)
* Add a method to send messages using pubsub on a topic * Update version to 0.2.22
This commit is contained in:
parent
55884a8ecd
commit
38daa31029
@ -2,7 +2,7 @@
|
|||||||
"packages": [
|
"packages": [
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"npmClient": "yarn",
|
"npmClient": "yarn",
|
||||||
"useWorkspaces": true,
|
"useWorkspaces": true,
|
||||||
"command": {
|
"command": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/address-watcher",
|
"name": "@cerc-io/address-watcher",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "Address Watcher",
|
"description": "Address Watcher",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -26,11 +26,11 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cache": "^0.2.21",
|
"@cerc-io/cache": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/tracing-client": "^0.2.21",
|
"@cerc-io/tracing-client": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@types/lodash": "^4.14.168",
|
"@types/lodash": "^4.14.168",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
"ethers": "^5.4.4",
|
"ethers": "^5.4.4",
|
||||||
|
2
packages/cache/package.json
vendored
2
packages/cache/package.json
vendored
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/cache",
|
"name": "@cerc-io/cache",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "Generic object cache",
|
"description": "Generic object cache",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/cli",
|
"name": "@cerc-io/cli",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -11,8 +11,8 @@
|
|||||||
"chat": "node dist/chat.js"
|
"chat": "node dist/chat.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/peer": "^0.2.21",
|
"@cerc-io/peer": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"@graphql-tools/utils": "^9.1.1",
|
"@graphql-tools/utils": "^9.1.1",
|
||||||
"@ipld/dag-cbor": "^8.0.0",
|
"@ipld/dag-cbor": "^8.0.0",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/codegen",
|
"name": "@cerc-io/codegen",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "Code generator",
|
"description": "Code generator",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
@ -20,7 +20,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@graphql-tools/load-files": "^6.5.2",
|
"@graphql-tools/load-files": "^6.5.2",
|
||||||
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
|
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
|
||||||
"@solidity-parser/parser": "^0.13.2",
|
"@solidity-parser/parser": "^0.13.2",
|
||||||
|
@ -41,12 +41,12 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"@cerc-io/cli": "^0.2.21",
|
"@cerc-io/cli": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
{{#if (subgraphPath)}}
|
{{#if (subgraphPath)}}
|
||||||
"@cerc-io/graph-node": "^0.2.21",
|
"@cerc-io/graph-node": "^0.2.22",
|
||||||
{{/if}}
|
{{/if}}
|
||||||
"apollo-type-bigint": "^0.1.3",
|
"apollo-type-bigint": "^0.1.3",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/eden-watcher",
|
"name": "@cerc-io/eden-watcher",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "eden-watcher",
|
"description": "eden-watcher",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -38,11 +38,11 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cli": "^0.2.21",
|
"@cerc-io/cli": "^0.2.22",
|
||||||
"@cerc-io/graph-node": "^0.2.21",
|
"@cerc-io/graph-node": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"apollo-type-bigint": "^0.1.3",
|
"apollo-type-bigint": "^0.1.3",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/erc20-watcher",
|
"name": "@cerc-io/erc20-watcher",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "ERC20 Watcher",
|
"description": "ERC20 Watcher",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -42,10 +42,10 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cli": "^0.2.21",
|
"@cerc-io/cli": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"apollo-type-bigint": "^0.1.3",
|
"apollo-type-bigint": "^0.1.3",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/erc721-watcher",
|
"name": "@cerc-io/erc721-watcher",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "erc721-watcher",
|
"description": "erc721-watcher",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -47,10 +47,10 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cli": "^0.2.21",
|
"@cerc-io/cli": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"apollo-type-bigint": "^0.1.3",
|
"apollo-type-bigint": "^0.1.3",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/graph-node",
|
"name": "@cerc-io/graph-node",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"@graphprotocol/graph-ts": "^0.22.0",
|
"@graphprotocol/graph-ts": "^0.22.0",
|
||||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||||
@ -50,9 +50,9 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
|
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
|
||||||
"@cerc-io/cache": "^0.2.21",
|
"@cerc-io/cache": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@types/json-diff": "^0.5.2",
|
"@types/json-diff": "^0.5.2",
|
||||||
"bn.js": "^4.11.9",
|
"bn.js": "^4.11.9",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/graph-test-watcher",
|
"name": "@cerc-io/graph-test-watcher",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "graph-test-watcher",
|
"description": "graph-test-watcher",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -38,11 +38,11 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cli": "^0.2.21",
|
"@cerc-io/cli": "^0.2.22",
|
||||||
"@cerc-io/graph-node": "^0.2.21",
|
"@cerc-io/graph-node": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"apollo-type-bigint": "^0.1.3",
|
"apollo-type-bigint": "^0.1.3",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/ipld-eth-client",
|
"name": "@cerc-io/ipld-eth-client",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "IPLD ETH Client",
|
"description": "IPLD ETH Client",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -20,7 +20,7 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.7.1",
|
"@apollo/client": "^3.7.1",
|
||||||
"@cerc-io/cache": "^0.2.21",
|
"@cerc-io/cache": "^0.2.22",
|
||||||
"cross-fetch": "^3.1.4",
|
"cross-fetch": "^3.1.4",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
"ethers": "^5.4.4",
|
"ethers": "^5.4.4",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/mobymask-watcher",
|
"name": "@cerc-io/mobymask-watcher",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "mobymask-watcher",
|
"description": "mobymask-watcher",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -37,10 +37,10 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cli": "^0.2.21",
|
"@cerc-io/cli": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@cerc-io/util": "^0.2.21",
|
"@cerc-io/util": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"apollo-type-bigint": "^0.1.3",
|
"apollo-type-bigint": "^0.1.3",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/peer-test-app",
|
"name": "@cerc-io/peer-test-app",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/peer": "^0.2.21",
|
"@cerc-io/peer": "^0.2.22",
|
||||||
"@cerc-io/react-peer": "^0.2.21",
|
"@cerc-io/react-peer": "^0.2.22",
|
||||||
"@emotion/react": "^11.10.5",
|
"@emotion/react": "^11.10.5",
|
||||||
"@emotion/styled": "^11.10.5",
|
"@emotion/styled": "^11.10.5",
|
||||||
"@mui/material": "^5.11.3",
|
"@mui/material": "^5.11.3",
|
||||||
|
@ -8,8 +8,13 @@ import { AppBar, Box, CssBaseline, Paper, Table, TableBody, TableCell, TableCont
|
|||||||
import './App.css';
|
import './App.css';
|
||||||
import { useForceUpdate } from './hooks/forceUpdate';
|
import { useForceUpdate } from './hooks/forceUpdate';
|
||||||
|
|
||||||
|
const TEST_TOPIC = 'test';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window { broadcast: (message: string) => void; }
|
interface Window {
|
||||||
|
broadcast: (message: string) => void;
|
||||||
|
flood: (message: string) => void;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const theme = createTheme();
|
const theme = createTheme();
|
||||||
@ -33,6 +38,14 @@ function App() {
|
|||||||
peer.broadcastMessage(message)
|
peer.broadcastMessage(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const unsubscribeTopic = peer.subscribeTopic(TEST_TOPIC, (data) => {
|
||||||
|
console.log(`> ${data}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
window.flood = (message: string) => {
|
||||||
|
peer.floodMessage(TEST_TOPIC, message)
|
||||||
|
}
|
||||||
|
|
||||||
peer.node.peerStore.addEventListener('change:multiaddrs', forceUpdate)
|
peer.node.peerStore.addEventListener('change:multiaddrs', forceUpdate)
|
||||||
peer.node.connectionManager.addEventListener('peer:connect', forceUpdate)
|
peer.node.connectionManager.addEventListener('peer:connect', forceUpdate)
|
||||||
|
|
||||||
@ -50,6 +63,7 @@ function App() {
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
unsubscribeMessage()
|
unsubscribeMessage()
|
||||||
|
unsubscribeTopic()
|
||||||
peer.node?.peerStore.removeEventListener('change:multiaddrs', forceUpdate)
|
peer.node?.peerStore.removeEventListener('change:multiaddrs', forceUpdate)
|
||||||
peer.node?.connectionManager.removeEventListener('peer:connect', forceUpdate)
|
peer.node?.connectionManager.removeEventListener('peer:connect', forceUpdate)
|
||||||
peer.node?.connectionManager.removeEventListener('peer:disconnect', disconnectHandler)
|
peer.node?.connectionManager.removeEventListener('peer:disconnect', disconnectHandler)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/peer",
|
"name": "@cerc-io/peer",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "libp2p module",
|
"description": "libp2p module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"exports": "./dist/index.js",
|
"exports": "./dist/index.js",
|
||||||
|
@ -18,6 +18,7 @@ import { noise } from '@chainsafe/libp2p-noise';
|
|||||||
import { mplex } from '@libp2p/mplex';
|
import { mplex } from '@libp2p/mplex';
|
||||||
import type { Stream as P2PStream, Connection } from '@libp2p/interface-connection';
|
import type { Stream as P2PStream, Connection } from '@libp2p/interface-connection';
|
||||||
import type { PeerInfo } from '@libp2p/interface-peer-info';
|
import type { PeerInfo } from '@libp2p/interface-peer-info';
|
||||||
|
import type { Message } from '@libp2p/interface-pubsub';
|
||||||
import { PeerId } from '@libp2p/interface-peer-id';
|
import { PeerId } from '@libp2p/interface-peer-id';
|
||||||
import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
|
import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
|
||||||
import { floodsub } from '@libp2p/floodsub';
|
import { floodsub } from '@libp2p/floodsub';
|
||||||
@ -36,6 +37,7 @@ export class Peer {
|
|||||||
_remotePeerIds: PeerId[] = []
|
_remotePeerIds: PeerId[] = []
|
||||||
_peerStreamMap: Map<string, Pushable<any>> = new Map()
|
_peerStreamMap: Map<string, Pushable<any>> = new Map()
|
||||||
_messageHandlers: Array<(peerId: PeerId, message: any) => void> = []
|
_messageHandlers: Array<(peerId: PeerId, message: any) => void> = []
|
||||||
|
_topicHandlers: Map<string, Array<(data: any) => void>> = new Map()
|
||||||
|
|
||||||
constructor (nodejs?: boolean) {
|
constructor (nodejs?: boolean) {
|
||||||
// Instantiation in nodejs.
|
// Instantiation in nodejs.
|
||||||
@ -145,6 +147,11 @@ export class Peer {
|
|||||||
await this._node.handle(CHAT_PROTOCOL, async ({ stream, connection }) => {
|
await this._node.handle(CHAT_PROTOCOL, async ({ stream, connection }) => {
|
||||||
this._handleStream(connection.remotePeer, stream);
|
this._handleStream(connection.remotePeer, stream);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Listen for pubsub messages
|
||||||
|
this._node.pubsub.addEventListener('message', (evt) => {
|
||||||
|
this._handleMessage(evt.detail);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async close (): Promise<void> {
|
async close (): Promise<void> {
|
||||||
@ -153,6 +160,7 @@ export class Peer {
|
|||||||
this._node.removeEventListener('peer:discovery');
|
this._node.removeEventListener('peer:discovery');
|
||||||
this._node.connectionManager.removeEventListener('peer:connect');
|
this._node.connectionManager.removeEventListener('peer:connect');
|
||||||
this._node.connectionManager.removeEventListener('peer:disconnect');
|
this._node.connectionManager.removeEventListener('peer:disconnect');
|
||||||
|
this._node.pubsub.removeEventListener('message');
|
||||||
|
|
||||||
await this._node.unhandle(CHAT_PROTOCOL);
|
await this._node.unhandle(CHAT_PROTOCOL);
|
||||||
const hangUpPromises = this._remotePeerIds.map(async peerId => this._node?.hangUp(peerId));
|
const hangUpPromises = this._remotePeerIds.map(async peerId => this._node?.hangUp(peerId));
|
||||||
@ -165,6 +173,11 @@ export class Peer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
floodMessage (topic: string, msg: any): void {
|
||||||
|
assert(this._node);
|
||||||
|
this._node.pubsub.publish(topic, uint8ArrayFromString(JSON.stringify(msg)));
|
||||||
|
}
|
||||||
|
|
||||||
subscribeMessage (handler: (peerId: PeerId, message: any) => void) : () => void {
|
subscribeMessage (handler: (peerId: PeerId, message: any) => void) : () => void {
|
||||||
this._messageHandlers.push(handler);
|
this._messageHandlers.push(handler);
|
||||||
|
|
||||||
@ -176,6 +189,37 @@ export class Peer {
|
|||||||
return unsubscribe;
|
return unsubscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subscribeTopic (topic: string, handler: (data: any) => void): () => void {
|
||||||
|
assert(this._node);
|
||||||
|
|
||||||
|
// Subscribe node to the topic
|
||||||
|
this._node.pubsub.subscribe(topic);
|
||||||
|
|
||||||
|
// Register provided handler for the topic
|
||||||
|
if (!this._topicHandlers.has(topic)) {
|
||||||
|
this._topicHandlers.set(topic, [handler]);
|
||||||
|
} else {
|
||||||
|
this._topicHandlers.get(topic)?.push(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a unsubscribe callback
|
||||||
|
const unsubscribe = () => {
|
||||||
|
// Remove handler from registered handlers for the topic
|
||||||
|
const filteredTopicHandlers = this._topicHandlers.get(topic)
|
||||||
|
?.filter(registeredHandler => registeredHandler !== handler);
|
||||||
|
|
||||||
|
if (filteredTopicHandlers?.length) {
|
||||||
|
this._topicHandlers.set(topic, filteredTopicHandlers);
|
||||||
|
} else {
|
||||||
|
// Remove topic from map and unsubscribe node from the topic if no handlers left
|
||||||
|
this._topicHandlers.delete(topic);
|
||||||
|
this._node?.pubsub.unsubscribe(topic);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
}
|
||||||
|
|
||||||
_handleDiscovery (peer: PeerInfo): void {
|
_handleDiscovery (peer: PeerInfo): void {
|
||||||
// Check connected peers as they are discovered repeatedly.
|
// Check connected peers as they are discovered repeatedly.
|
||||||
if (!this._remotePeerIds.some(remotePeerId => remotePeerId.toString() === peer.id.toString())) {
|
if (!this._remotePeerIds.some(remotePeerId => remotePeerId.toString() === peer.id.toString())) {
|
||||||
@ -273,4 +317,12 @@ export class Peer {
|
|||||||
// TODO: Check if stream already exists for peer id
|
// TODO: Check if stream already exists for peer id
|
||||||
this._peerStreamMap.set(peerId.toString(), messageStream);
|
this._peerStreamMap.set(peerId.toString(), messageStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_handleMessage (msg: Message): void {
|
||||||
|
// Send msg data to registered topic handlers
|
||||||
|
this._topicHandlers.get(msg.topic)?.forEach(handler => {
|
||||||
|
const dataObj = JSON.parse(uint8ArrayToString(msg.data));
|
||||||
|
handler(dataObj);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/react-peer",
|
"name": "@cerc-io/react-peer",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "react-peer React component",
|
"description": "react-peer React component",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"files": [
|
"files": [
|
||||||
@ -18,7 +18,7 @@
|
|||||||
"test:watch": "nwb test-react --server"
|
"test:watch": "nwb test-react --server"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/peer": "^0.2.21"
|
"@cerc-io/peer": "^0.2.22"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "^18.2.0"
|
"react": "^18.2.0"
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/solidity-mapper",
|
"name": "@cerc-io/solidity-mapper",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@ethersproject/abi": "^5.3.0",
|
"@ethersproject/abi": "^5.3.0",
|
||||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/test",
|
"name": "@cerc-io/test",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/tracing-client",
|
"name": "@cerc-io/tracing-client",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"description": "ETH VM tracing client",
|
"description": "ETH VM tracing client",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/util",
|
"name": "@cerc-io/util",
|
||||||
"version": "0.2.21",
|
"version": "0.2.22",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/utils.keyvaluecache": "^1.0.1",
|
"@apollo/utils.keyvaluecache": "^1.0.1",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.21",
|
"@cerc-io/solidity-mapper": "^0.2.22",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"@graphql-tools/schema": "^9.0.10",
|
"@graphql-tools/schema": "^9.0.10",
|
||||||
"@graphql-tools/utils": "^9.1.1",
|
"@graphql-tools/utils": "^9.1.1",
|
||||||
@ -37,8 +37,8 @@
|
|||||||
"yargs": "^17.0.1"
|
"yargs": "^17.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cerc-io/cache": "^0.2.21",
|
"@cerc-io/cache": "^0.2.22",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.21",
|
"@cerc-io/ipld-eth-client": "^0.2.22",
|
||||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||||
"@types/express": "^4.17.14",
|
"@types/express": "^4.17.14",
|
||||||
"@types/fs-extra": "^9.0.11",
|
"@types/fs-extra": "^9.0.11",
|
||||||
|
Loading…
Reference in New Issue
Block a user