fizen-evm-networks 1.0.1 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/package.json +1 -1
- package/lib/lib/pubsub.d.ts +0 -24
- package/lib/lib/pubsub.js +0 -46
- package/lib/store/actions.d.ts +0 -5
- package/lib/store/actions.js +0 -10
- package/lib/store/index.d.ts +0 -3
- package/lib/store/index.js +0 -14
- package/lib/store/mutations.d.ts +0 -5
- package/lib/store/mutations.js +0 -12
- package/lib/store/state.d.ts +0 -4
- package/lib/store/state.js +0 -5
- package/lib/store/store.d.ts +0 -35
- package/lib/store/store.js +0 -102
package/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# fizen-evm-networks
|
2
2
|
|
3
|
+
> This library support EVM networks, it will auto fetch the rpcs every 5 seconds and response the latency & height. User can use this information to find the fastest rpc of the chain
|
4
|
+
|
5
|
+
## How to use?
|
6
|
+
|
3
7
|
```
|
4
8
|
const chainIds = [80001, 1, 56, 97]
|
5
9
|
const evmNetworks = new FizenEvmNetworks(chainIds);
|
6
|
-
|
7
10
|
const rpc = evmNetworks.getLowestLatencyChain(1);
|
8
11
|
```
|
package/package.json
CHANGED
package/lib/lib/pubsub.d.ts
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
export default class PubSub {
|
2
|
-
private events;
|
3
|
-
constructor();
|
4
|
-
/**
|
5
|
-
* Either create a new event instance for passed `event` name
|
6
|
-
* or push a new callback into the existing collection
|
7
|
-
*
|
8
|
-
* @param {string} event
|
9
|
-
* @param {function} callback
|
10
|
-
* @returns {number} A count of callbacks for this event
|
11
|
-
* @memberof PubSub
|
12
|
-
*/
|
13
|
-
subscribe(event: string, callback: Function): any;
|
14
|
-
/**
|
15
|
-
* If the passed event has callbacks attached to it, loop through each one
|
16
|
-
* and call it
|
17
|
-
*
|
18
|
-
* @param {string} event
|
19
|
-
* @param {object} [data={}]
|
20
|
-
* @returns {array} The callbacks for this event, or an empty array if no event exits
|
21
|
-
* @memberof PubSub
|
22
|
-
*/
|
23
|
-
publish(event: string, data?: {}): any;
|
24
|
-
}
|
package/lib/lib/pubsub.js
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
class PubSub {
|
4
|
-
constructor() {
|
5
|
-
this.events = {};
|
6
|
-
}
|
7
|
-
/**
|
8
|
-
* Either create a new event instance for passed `event` name
|
9
|
-
* or push a new callback into the existing collection
|
10
|
-
*
|
11
|
-
* @param {string} event
|
12
|
-
* @param {function} callback
|
13
|
-
* @returns {number} A count of callbacks for this event
|
14
|
-
* @memberof PubSub
|
15
|
-
*/
|
16
|
-
subscribe(event, callback) {
|
17
|
-
let self = this;
|
18
|
-
// If there's not already an event with this name set in our collection
|
19
|
-
// go ahead and create a new one and set it with an empty array, so we don't
|
20
|
-
// have to type check it later down-the-line
|
21
|
-
if (!self.events.hasOwnProperty(event)) {
|
22
|
-
self.events[event] = [];
|
23
|
-
}
|
24
|
-
// We know we've got an array for this event, so push our callback in there with no fuss
|
25
|
-
return self.events[event].push(callback);
|
26
|
-
}
|
27
|
-
/**
|
28
|
-
* If the passed event has callbacks attached to it, loop through each one
|
29
|
-
* and call it
|
30
|
-
*
|
31
|
-
* @param {string} event
|
32
|
-
* @param {object} [data={}]
|
33
|
-
* @returns {array} The callbacks for this event, or an empty array if no event exits
|
34
|
-
* @memberof PubSub
|
35
|
-
*/
|
36
|
-
publish(event, data = {}) {
|
37
|
-
let self = this;
|
38
|
-
// There's no event to publish to, so bail out
|
39
|
-
if (!self.events.hasOwnProperty(event)) {
|
40
|
-
return [];
|
41
|
-
}
|
42
|
-
// Get each subscription and call its callback with the passed data
|
43
|
-
return self.events[event].map((callback) => callback(data));
|
44
|
-
}
|
45
|
-
}
|
46
|
-
exports.default = PubSub;
|
package/lib/store/actions.d.ts
DELETED
package/lib/store/actions.js
DELETED
package/lib/store/index.d.ts
DELETED
package/lib/store/index.js
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
const actions_js_1 = __importDefault(require("./actions.js"));
|
7
|
-
const mutations_js_1 = __importDefault(require("./mutations.js"));
|
8
|
-
const state_js_1 = __importDefault(require("./state.js"));
|
9
|
-
const store_js_1 = __importDefault(require("./store.js"));
|
10
|
-
exports.default = new store_js_1.default({
|
11
|
-
actions: actions_js_1.default,
|
12
|
-
mutations: mutations_js_1.default,
|
13
|
-
state: state_js_1.default,
|
14
|
-
});
|
package/lib/store/mutations.d.ts
DELETED
package/lib/store/mutations.js
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.default = {
|
4
|
-
addItem(state, payload) {
|
5
|
-
state.items.push(payload);
|
6
|
-
return state;
|
7
|
-
},
|
8
|
-
clearItem(state, payload) {
|
9
|
-
state.items.splice(payload.index, 1);
|
10
|
-
return state;
|
11
|
-
},
|
12
|
-
};
|
package/lib/store/state.d.ts
DELETED
package/lib/store/state.js
DELETED
package/lib/store/store.d.ts
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
export default class Store {
|
2
|
-
private actions;
|
3
|
-
private mutations;
|
4
|
-
private state;
|
5
|
-
private status;
|
6
|
-
private events;
|
7
|
-
constructor(params: {
|
8
|
-
state: any;
|
9
|
-
actions: any;
|
10
|
-
mutations: any;
|
11
|
-
plugins?: any;
|
12
|
-
middleware?: any;
|
13
|
-
pubsub?: any;
|
14
|
-
});
|
15
|
-
/**
|
16
|
-
* A dispatcher for actions that looks in the actions
|
17
|
-
* collection and runs the action if it can find it
|
18
|
-
*
|
19
|
-
* @param {string} actionKey
|
20
|
-
* @param {mixed} payload
|
21
|
-
* @returns {boolean}
|
22
|
-
* @memberof Store
|
23
|
-
*/
|
24
|
-
dispatch(actionKey: string, payload: any): boolean;
|
25
|
-
/**
|
26
|
-
* Look for a mutation and modify the state object
|
27
|
-
* if that mutation exists by calling it
|
28
|
-
*
|
29
|
-
* @param {string} mutationKey
|
30
|
-
* @param {mixed} payload
|
31
|
-
* @returns {boolean}
|
32
|
-
* @memberof Store
|
33
|
-
*/
|
34
|
-
commit(mutationKey: string, payload: any): boolean;
|
35
|
-
}
|
package/lib/store/store.js
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
const pubsub_js_1 = __importDefault(require("../lib/pubsub.js"));
|
7
|
-
class Store {
|
8
|
-
constructor(params) {
|
9
|
-
this.status = "resting";
|
10
|
-
this.events = new pubsub_js_1.default();
|
11
|
-
let self = this;
|
12
|
-
// Add some default objects to hold our actions, mutations and state
|
13
|
-
self.actions = {};
|
14
|
-
self.mutations = {};
|
15
|
-
self.state = {};
|
16
|
-
// A status enum to set during actions and mutations
|
17
|
-
self.status = "resting";
|
18
|
-
// Attach our PubSub module as an `events` element
|
19
|
-
self.events = new pubsub_js_1.default();
|
20
|
-
// Look in the passed params object for actions and mutations
|
21
|
-
// that might have been passed in
|
22
|
-
if (params.hasOwnProperty("actions")) {
|
23
|
-
self.actions = params.actions;
|
24
|
-
}
|
25
|
-
if (params.hasOwnProperty("mutations")) {
|
26
|
-
self.mutations = params.mutations;
|
27
|
-
}
|
28
|
-
// Set our state to be a Proxy. We are setting the default state by
|
29
|
-
// checking the params and defaulting to an empty object if no default
|
30
|
-
// state is passed in
|
31
|
-
self.state = new Proxy(params.state || {}, {
|
32
|
-
set: function (state, key, value) {
|
33
|
-
// Set the value as we would normally
|
34
|
-
state[key] = value;
|
35
|
-
// Trace out to the console. This will be grouped by the related action
|
36
|
-
console.log(`stateChange: ${key}: ${value}`);
|
37
|
-
// Publish the change event for the components that are listening
|
38
|
-
self.events.publish("stateChange", self.state);
|
39
|
-
// Give the user a little telling off if they set a value directly
|
40
|
-
if (self.status !== "mutation") {
|
41
|
-
console.warn(`You should use a mutation to set ${key}`);
|
42
|
-
}
|
43
|
-
// Reset the status ready for the next operation
|
44
|
-
self.status = "resting";
|
45
|
-
return true;
|
46
|
-
},
|
47
|
-
});
|
48
|
-
}
|
49
|
-
/**
|
50
|
-
* A dispatcher for actions that looks in the actions
|
51
|
-
* collection and runs the action if it can find it
|
52
|
-
*
|
53
|
-
* @param {string} actionKey
|
54
|
-
* @param {mixed} payload
|
55
|
-
* @returns {boolean}
|
56
|
-
* @memberof Store
|
57
|
-
*/
|
58
|
-
dispatch(actionKey, payload) {
|
59
|
-
let self = this;
|
60
|
-
// Run a quick check to see if the action actually exists
|
61
|
-
// before we try to run it
|
62
|
-
if (typeof self.actions[actionKey] !== "function") {
|
63
|
-
console.error(`Action "${actionKey} doesn't exist.`);
|
64
|
-
return false;
|
65
|
-
}
|
66
|
-
// Create a console group which will contain the logs from our Proxy etc
|
67
|
-
console.groupCollapsed(`ACTION: ${actionKey}`);
|
68
|
-
// Let anything that's watching the status know that we're dispatching an action
|
69
|
-
self.status = "action";
|
70
|
-
// Actually call the action and pass it the Store context and whatever payload was passed
|
71
|
-
self.actions[actionKey](self, payload);
|
72
|
-
// Close our console group to keep things nice and neat
|
73
|
-
console.groupEnd();
|
74
|
-
return true;
|
75
|
-
}
|
76
|
-
/**
|
77
|
-
* Look for a mutation and modify the state object
|
78
|
-
* if that mutation exists by calling it
|
79
|
-
*
|
80
|
-
* @param {string} mutationKey
|
81
|
-
* @param {mixed} payload
|
82
|
-
* @returns {boolean}
|
83
|
-
* @memberof Store
|
84
|
-
*/
|
85
|
-
commit(mutationKey, payload) {
|
86
|
-
let self = this;
|
87
|
-
// Run a quick check to see if this mutation actually exists
|
88
|
-
// before trying to run it
|
89
|
-
if (typeof self.mutations[mutationKey] !== "function") {
|
90
|
-
console.log(`Mutation "${mutationKey}" doesn't exist`);
|
91
|
-
return false;
|
92
|
-
}
|
93
|
-
// Let anything that's watching the status know that we're mutating state
|
94
|
-
self.status = "mutation";
|
95
|
-
// Get a new version of the state by running the mutation and storing the result of it
|
96
|
-
let newState = self.mutations[mutationKey](self.state, payload);
|
97
|
-
// Merge the old and new together to create a new state and set it
|
98
|
-
self.state = Object.assign(self.state, newState);
|
99
|
-
return true;
|
100
|
-
}
|
101
|
-
}
|
102
|
-
exports.default = Store;
|