-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
RemoteAuxChannel.ts
117 lines (111 loc) · 3.44 KB
/
RemoteAuxChannel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
LocalActions,
auxCausalTreeFactory,
AuxCausalTree,
GLOBALS_BOT_ID,
tagsOnBot,
ON_ACTION_ACTION_NAME,
BotTags,
} from '@casual-simulation/aux-common';
import {
CausalTreeManager,
SocketManager,
} from '@casual-simulation/causal-tree-client-socketio';
import {
AuxConfig,
BaseAuxChannel,
AuxUser,
AuxChannelOptions,
CausalTreePartitionConfig,
createMemoryPartition,
createAuxPartition,
createCausalRepoPartition,
PartitionConfig,
AuxPartition,
iteratePartitions,
filterAtomFactory,
createCausalRepoClientPartition,
} from '@casual-simulation/aux-vm';
import {
SyncedRealtimeCausalTree,
RemoteAction,
RealtimeCausalTreeOptions,
} from '@casual-simulation/causal-trees';
import { SigningCryptoImpl } from '@casual-simulation/crypto';
import { CausalTreeStore } from '@casual-simulation/causal-trees';
import {
createRemoteCausalTreePartitionFactory,
RemoteCausalTreePartitionOptions,
RemoteCausalTreePartitionImpl,
} from '../partitions/RemoteCausalTreePartition';
import { createRemoteCausalRepoPartition } from '../partitions/RemoteCausalRepoPartitionFactory';
export interface RemoteAuxChannelOptions extends AuxChannelOptions {
partitionOptions?: RemoteCausalTreePartitionOptions;
}
export class RemoteAuxChannel extends BaseAuxChannel {
protected _treeManager: CausalTreeManager;
protected _socketManager: SocketManager;
protected _partitionOptions: RemoteCausalTreePartitionOptions;
constructor(
user: AuxUser,
config: AuxConfig,
options: RemoteAuxChannelOptions
) {
super(user, config, options);
this._partitionOptions = {
...(options.partitionOptions || {
defaultHost: null,
}),
treeOptions: {
filter: filterAtomFactory(() => this.helper),
},
};
// {
// defaultHost: defaultHost,
// store: options.store,
// crypto: options.crypto,
// treeOptions: {
// filter: filterAtomFactory(() => this.helper),
// },
// };
}
protected async _createPartition(
config: PartitionConfig
): Promise<AuxPartition> {
return await createAuxPartition(
config,
createRemoteCausalTreePartitionFactory(
this._partitionOptions,
this.user
),
createMemoryPartition,
config => createCausalRepoPartition(config, this.user),
config => createRemoteCausalRepoPartition(config, this.user),
config => createCausalRepoClientPartition(config, this.user)
);
}
protected _handleError(error: any) {
if (error instanceof Error) {
super._handleError({
type: 'general',
message: error.toString(),
});
} else {
super._handleError(error);
}
}
protected _handleLocalEvents(e: LocalActions[]) {
for (let event of e) {
if (event.type === 'set_offline_state') {
for (let [key, partition] of iteratePartitions(
this._partitions
)) {
if ('forcedOffline' in partition) {
partition.forcedOffline = event.offline;
}
}
}
}
super._handleLocalEvents(e);
}
}