Skip to content

Commit 06bb8f5

Browse files
committed
Small fixes, mostly to enable first multi-project app, p2p-chat.
1 parent e4142b2 commit 06bb8f5

File tree

12 files changed

+57
-25
lines changed

12 files changed

+57
-25
lines changed

examples/chat/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
2-
3-
4-
//$Env:NODE_PATH="dist-chat/src;dist-chat/examples/chat"
5-
6-
71
import '@hyper-hyper-space/node-env';
82
import { Identity } from 'data/identity';
93
import { RSAKeyPair } from 'data/identity';
@@ -132,6 +126,9 @@ async function main() {
132126
console.log(m.getAuthor()?.info?.name + ': ' + m.text);
133127
});
134128

129+
room.participants?.loadAndWatchForChanges();
130+
room.messages?.loadAndWatchForChanges();
131+
135132
console.log('Type and press return to send a message!')
136133
console.log();
137134

examples/chat/model/ChatRoom.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ class ChatRoom extends HashedObject implements SpaceEntryPoint {
8787

8888
this._node.broadcast(this);
8989
this._node.sync(this);
90-
91-
this.participants?.loadAndWatchForChanges();
92-
this.messages?.loadAndWatchForChanges();
9390
}
9491

9592
async stopSync(): Promise<void> {

scripts/beacon.bat

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
setlocal
12
@ECHO OFF
2-
setx NODE_PATH "dist-examples/src;dist-examples/examples/randomness-beacon" > NUL
3+
set NODE_PATH=dist-examples/src;dist-examples/examples/chat
34
@ECHO ON
4-
node ./dist-examples/examples/randomness-beacon/index.js
5+
6+
node ./dist-examples/examples/randomness-beacon/index.js
7+
endlocal

scripts/chat.bat

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
setlocal
12
@ECHO OFF
2-
setx NODE_PATH "dist-examples/src;dist-examples/examples/chat" > NUL
3+
set NODE_PATH=dist-examples/src;dist-examples/examples/chat
34
@ECHO ON
45

5-
node ./dist-examples/examples/chat/index.js
6+
node ./dist-examples/examples/chat/index.js
7+
8+
endlocal

src/data/containers/ImmutableReference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ImmutableReference<T extends MutableObject> extends HashedObject {
2323
}
2424

2525
const ref = references.get(this.value.hash);
26-
const knownClass = HashedObject.knownClasses.get(this.value.className);
26+
const knownClass = HashedObject.lookupClass(this.value.className);
2727

2828
if (ref === undefined || knownClass === undefined) {
2929
return false;

src/data/model.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ export { HashReference } from './model/HashReference';
66
export { HashedSet } from './model/HashedSet';
77
export { HashedLiteral } from './model/HashedLiteral';
88
export { Hashing, Hash } from './model/Hashing';
9-
export { ReversibleObject } from './model/ReversibleObject';
10-
export { ReversibleOp } from './model/ReversibleOp';
9+
10+
// commenting out because these imports trigger a weird error:
11+
12+
// export { ReversibleObject } from './model/ReversibleObject';
13+
// export { ReversibleOp } from './model/ReversibleOp';
14+
15+
// TypeError: Object prototype may only be an Object or null: undefined
16+
// see @oleersoy's hypothesis here:
17+
// https://github.com/Microsoft/TypeScript/issues/28314
18+
1119
export { UndoOp } from './model/UndoOp';
1220
export { Serialization } from './model/Serialization';
1321
export { Namespace } from './model/Namespace';

src/data/model/HashedObject.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,21 @@ const BITS_FOR_ID = 128;
3636
and which objects should be preloaded when loading operations that mutate
3737
this object and its subobjects. */
3838

39+
3940
abstract class HashedObject {
4041

42+
4143
static knownClasses = new Map<string, new () => HashedObject>();
44+
4245
static registerClass(name: string, clazz: new () => HashedObject) {
43-
this.knownClasses.set(name, clazz);
46+
HashedObject.knownClasses.set(name, clazz);
4447
}
4548

49+
static lookupClass(name: string): (new () => HashedObject) | undefined {
50+
return HashedObject.knownClasses.get(name);
51+
}
52+
53+
4654
private id? : string;
4755
private author? : Identity;
4856

@@ -511,12 +519,12 @@ abstract class HashedObject {
511519
throw new Error("Missing 'hashed_object' type signature while attempting to deliteralize " + literal.hash);
512520
}
513521

514-
let constr = HashedObject.knownClasses.get(value['_class']);
522+
let constr = HashedObject.lookupClass(value['_class']);
515523

516524
if (constr === undefined) {
517525
throw new Error("A local implementation of class '" + value['_class'] + "' is necessary to deliteralize " + literal.hash);
518526
} else {
519-
hashedObject = new constr();
527+
hashedObject = new constr() as HashedObject;
520528
}
521529

522530
for (const [fieldName, fieldValue] of Object.entries(value['_fields'])) {
@@ -691,7 +699,7 @@ abstract class HashedObject {
691699
something = tab;
692700
let contents;
693701
if (value['_type'] === 'hashed_object') {
694-
let constr = HashedObject.knownClasses.get(value['_class']);
702+
let constr = HashedObject.lookupClass(value['_class']);
695703
if (constr === undefined) {
696704
something = something + 'HashedObject: ';
697705
} else {

src/mesh/agents/peer/PeerGroupAgent.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class PeerGroupAgent implements Agent {
194194
}
195195

196196
ready(pod: AgentPod): void {
197-
this.controlLog.debug('Started PeerControlAgent on ' + this.localPeer.endpoint + ' for id ' + this.localPeer.identityHash);
197+
this.controlLog.debug('Started PeerControlAgent on local ' + this.localPeer.endpoint + ' (id=' + this.localPeer.identityHash + ') for peerGroupId ' + this.peerGroupId);
198198
this.pod = pod;
199199
this.init();
200200
}
@@ -351,8 +351,10 @@ class PeerGroupAgent implements Agent {
351351
}
352352

353353
private async queryForOnlinePeers() {
354-
if (this.connectionsPerEndpoint.size < this.params.minPeers) {
355354

355+
PeerGroupAgent.controlLog.trace("Consdidering querying for peers on " + this.peerGroupId);
356+
357+
if (this.connectionsPerEndpoint.size < this.params.minPeers) {
356358
let candidates = await this.peerSource.getPeers(this.params.minPeers * 5);
357359
let endpoints = new Array<Endpoint>();
358360
let fallbackEndpoints = new Array<Endpoint>();
@@ -415,6 +417,8 @@ class PeerGroupAgent implements Agent {
415417
}
416418

417419

420+
} else {
421+
PeerGroupAgent.controlLog.trace("Skipping querying for peers on " + this.peerGroupId);
418422
}
419423
}
420424

src/mesh/agents/peer/sources/ObjectDiscoveryPeerSource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ class ObjectDiscoveryPeerSource implements PeerSource {
6161
}
6262

6363
while (found.length < count && now < limit) {
64-
64+
now = Date.now();
65+
6566
try {
6667
const reply = await this.replyStream.next(limit - now)
6768
const peerInfo = await this.parseEndpoint(reply.source);
69+
6870
if (peerInfo !== undefined && !unique.has(peerInfo.endpoint)) {
6971
found.push(peerInfo);
7072
unique.add(peerInfo.endpoint);

src/spaces/Resources.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Resources {
6565
} else {
6666
let key = RSAKeyPair.generate(1024);
6767
localId = Identity.fromKeyPair({name: 'auto-generated id ' + + new RNGImpl().randomHexString(64)}, key);
68+
this.config.id = localId;
6869
}
6970

7071
this.config.peersForDiscovery = [(new IdentityPeer(this.config.linkupServers[0], localId.hash(), localId)).asPeerIfReady()];

0 commit comments

Comments
 (0)