@peers-app/peers-sdk 0.7.5 → 0.7.6
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.
|
@@ -6,6 +6,7 @@ import { Observable } from "../observable";
|
|
|
6
6
|
export declare class UserContext {
|
|
7
7
|
readonly userId: string;
|
|
8
8
|
readonly dataSourceFactory: DataSourceFactory;
|
|
9
|
+
readonly ephemeral?: boolean | undefined;
|
|
9
10
|
readonly deviceId: Observable<string>;
|
|
10
11
|
readonly currentlyActiveGroupId: Observable<string | undefined>;
|
|
11
12
|
readonly reloadPackagesOnPageRefresh: Observable<boolean>;
|
|
@@ -14,7 +15,7 @@ export declare class UserContext {
|
|
|
14
15
|
readonly groupDataContexts: Map<string, DataContext>;
|
|
15
16
|
readonly defaultDataContext: Observable<DataContext>;
|
|
16
17
|
readonly loadingPromise: Promise<UserContext>;
|
|
17
|
-
constructor(userId: string, dataSourceFactory: DataSourceFactory);
|
|
18
|
+
constructor(userId: string, dataSourceFactory: DataSourceFactory, ephemeral?: boolean | undefined);
|
|
18
19
|
private init;
|
|
19
20
|
/**
|
|
20
21
|
* We want to load all packages as part of user context initialization
|
|
@@ -11,6 +11,7 @@ const utils_1 = require("../utils");
|
|
|
11
11
|
class UserContext {
|
|
12
12
|
userId;
|
|
13
13
|
dataSourceFactory;
|
|
14
|
+
ephemeral;
|
|
14
15
|
deviceId = (0, observable_1.observable)('');
|
|
15
16
|
currentlyActiveGroupId = (0, observable_1.observable)();
|
|
16
17
|
reloadPackagesOnPageRefresh = (0, observable_1.observable)(false);
|
|
@@ -19,19 +20,20 @@ class UserContext {
|
|
|
19
20
|
groupDataContexts = new Map();
|
|
20
21
|
defaultDataContext;
|
|
21
22
|
loadingPromise;
|
|
22
|
-
constructor(userId, dataSourceFactory) {
|
|
23
|
+
constructor(userId, dataSourceFactory, ephemeral) {
|
|
23
24
|
this.userId = userId;
|
|
24
25
|
this.dataSourceFactory = dataSourceFactory;
|
|
26
|
+
this.ephemeral = ephemeral;
|
|
27
|
+
if (ephemeral === undefined && typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
|
|
28
|
+
this.ephemeral = true;
|
|
29
|
+
}
|
|
25
30
|
this.userDataContext = new data_context_1.DataContext(this);
|
|
26
31
|
this.defaultDataContext = (0, observable_1.observable)(this.userDataContext);
|
|
27
32
|
this.defaultDataContext(this.userDataContext);
|
|
28
33
|
this.loadingPromise = this.init();
|
|
29
34
|
}
|
|
30
35
|
async init() {
|
|
31
|
-
if (
|
|
32
|
-
console.log(`not setting up user-context pvars during tests because they will interfere with other UserContext instances`);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
36
|
+
if (!this.ephemeral) {
|
|
35
37
|
await this.loadUserContextObservablesFromDB();
|
|
36
38
|
}
|
|
37
39
|
await this.loadGroupContexts();
|
|
@@ -49,7 +49,10 @@ class MockPeerDevice {
|
|
|
49
49
|
deviceId: this.deviceId,
|
|
50
50
|
timestampLastApplied: Date.now(),
|
|
51
51
|
connections: this.connections,
|
|
52
|
-
preferredDeviceIds: []
|
|
52
|
+
preferredDeviceIds: [],
|
|
53
|
+
cpuPercent: 10,
|
|
54
|
+
memPercent: 10,
|
|
55
|
+
connectionSlotsAvailable: 10,
|
|
53
56
|
};
|
|
54
57
|
}
|
|
55
58
|
async listChanges(query) {
|
|
@@ -81,10 +81,12 @@ function sortConnections({ myDeviceId, connections, unconnectedDeviceIds }) {
|
|
|
81
81
|
const MAX_CONNECTIONS = peer_device_1.PeerDeviceConsts.MAX_CONNECTIONS;
|
|
82
82
|
// deprioritize devices that are close to max connections
|
|
83
83
|
connections.sort((a, b) => {
|
|
84
|
-
const aConnCnt = a.preferredByDeviceIds.length + a.preferredDeviceIds.length;
|
|
85
|
-
const bConnCnt = b.preferredByDeviceIds.length + b.preferredDeviceIds.length;
|
|
86
|
-
const nearingMaxA = aConnCnt >= MAX_CONNECTIONS * 0.9;
|
|
87
|
-
const nearingMaxB = bConnCnt >= MAX_CONNECTIONS * 0.9;
|
|
84
|
+
// const aConnCnt = a.preferredByDeviceIds.length + a.preferredDeviceIds.length;
|
|
85
|
+
// const bConnCnt = b.preferredByDeviceIds.length + b.preferredDeviceIds.length;
|
|
86
|
+
// const nearingMaxA = aConnCnt >= MAX_CONNECTIONS * 0.9;
|
|
87
|
+
// const nearingMaxB = bConnCnt >= MAX_CONNECTIONS * 0.9;
|
|
88
|
+
const nearingMaxA = a.connectionSlotsAvailable <= 3;
|
|
89
|
+
const nearingMaxB = b.connectionSlotsAvailable <= 3;
|
|
88
90
|
if (nearingMaxA && !nearingMaxB)
|
|
89
91
|
return 1;
|
|
90
92
|
if (!nearingMaxA && nearingMaxB)
|
|
@@ -17,12 +17,16 @@ export interface IDeviceMessage {
|
|
|
17
17
|
ttl: number;
|
|
18
18
|
payload: any;
|
|
19
19
|
signature: string;
|
|
20
|
+
hops?: string[];
|
|
20
21
|
}
|
|
21
22
|
export interface INetworkInfo {
|
|
22
23
|
deviceId: string;
|
|
23
24
|
timestampLastApplied: number;
|
|
24
25
|
connections: IDeviceConnection[];
|
|
25
26
|
preferredDeviceIds: string[];
|
|
27
|
+
cpuPercent: number;
|
|
28
|
+
memPercent: number;
|
|
29
|
+
connectionSlotsAvailable: number;
|
|
26
30
|
}
|
|
27
31
|
export interface IDeviceConnection {
|
|
28
32
|
deviceId: string;
|