@automerge/automerge-repo 2.5.2-alpha.0 → 2.5.2-alpha.1
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/dist/helpers/array.d.ts +2 -0
- package/dist/helpers/array.d.ts.map +1 -0
- package/dist/helpers/array.js +3 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/presence/PeerPresenceInfo.d.ts +50 -0
- package/dist/presence/PeerPresenceInfo.d.ts.map +1 -0
- package/dist/presence/PeerPresenceInfo.js +83 -0
- package/dist/presence/PeerStateView.d.ts +69 -0
- package/dist/presence/PeerStateView.d.ts.map +1 -0
- package/dist/presence/PeerStateView.js +131 -0
- package/dist/presence/Presence.d.ts +81 -0
- package/dist/presence/Presence.d.ts.map +1 -0
- package/dist/presence/Presence.js +245 -0
- package/dist/presence/constants.d.ts +4 -0
- package/dist/presence/constants.d.ts.map +1 -0
- package/dist/presence/constants.js +3 -0
- package/dist/presence/types.d.ts +75 -0
- package/dist/presence/types.d.ts.map +1 -0
- package/dist/presence/types.js +1 -0
- package/package.json +2 -2
- package/src/helpers/array.ts +3 -0
- package/src/index.ts +6 -4
- package/src/presence/PeerPresenceInfo.ts +108 -0
- package/src/presence/PeerStateView.ts +154 -0
- package/src/presence/Presence.ts +311 -0
- package/src/presence/constants.ts +3 -0
- package/src/presence/types.ts +94 -0
- package/test/Presence.test.ts +20 -26
- package/dist/Presence.d.ts +0 -245
- package/dist/Presence.d.ts.map +0 -1
- package/dist/Presence.js +0 -526
- package/src/Presence.ts +0 -722
package/dist/Presence.d.ts
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
import { DocHandle } from "./DocHandle.js";
|
|
2
|
-
import { PeerId } from "./types.js";
|
|
3
|
-
import { EventEmitter } from "eventemitter3";
|
|
4
|
-
export type UserId = unknown;
|
|
5
|
-
export type DeviceId = unknown;
|
|
6
|
-
export declare const PRESENCE_MESSAGE_MARKER = "__presence";
|
|
7
|
-
export type PeerState<State> = {
|
|
8
|
-
peerId: PeerId;
|
|
9
|
-
deviceId?: DeviceId;
|
|
10
|
-
userId?: UserId;
|
|
11
|
-
value: State;
|
|
12
|
-
};
|
|
13
|
-
type PresenceMessageBase = {
|
|
14
|
-
deviceId?: DeviceId;
|
|
15
|
-
userId?: UserId;
|
|
16
|
-
};
|
|
17
|
-
type PresenceMessageUpdate = PresenceMessageBase & {
|
|
18
|
-
type: "update";
|
|
19
|
-
channel: string;
|
|
20
|
-
value: any;
|
|
21
|
-
};
|
|
22
|
-
type PresenceMessageSnapshot = PresenceMessageBase & {
|
|
23
|
-
type: "snapshot";
|
|
24
|
-
state: any;
|
|
25
|
-
};
|
|
26
|
-
type PresenceMessageHeartbeat = PresenceMessageBase & {
|
|
27
|
-
type: "heartbeat";
|
|
28
|
-
};
|
|
29
|
-
type PresenceMessageGoodbye = PresenceMessageBase & {
|
|
30
|
-
type: "goodbye";
|
|
31
|
-
};
|
|
32
|
-
type WithPeerId = {
|
|
33
|
-
peerId: PeerId;
|
|
34
|
-
};
|
|
35
|
-
export type PresenceEventUpdate = PresenceMessageUpdate & WithPeerId;
|
|
36
|
-
export type PresenceEventSnapshot = PresenceMessageSnapshot & WithPeerId;
|
|
37
|
-
export type PresenceEventHeartbeat = PresenceMessageHeartbeat & WithPeerId;
|
|
38
|
-
export type PresenceEventGoodbye = PresenceMessageGoodbye & WithPeerId;
|
|
39
|
-
/**
|
|
40
|
-
* Events emitted by Presence when ephemeral messages are received from peers.
|
|
41
|
-
*/
|
|
42
|
-
export type PresenceEvents = {
|
|
43
|
-
/**
|
|
44
|
-
* Handle a state update broadcast by a peer.
|
|
45
|
-
*/
|
|
46
|
-
update: (msg: PresenceEventUpdate) => void;
|
|
47
|
-
/**
|
|
48
|
-
* Handle a full state snapshot broadcast by a peer.
|
|
49
|
-
*/
|
|
50
|
-
snapshot: (msg: PresenceEventSnapshot) => void;
|
|
51
|
-
/**
|
|
52
|
-
* Handle a heartbeat broadcast by a peer.
|
|
53
|
-
*/
|
|
54
|
-
heartbeat: (msg: PresenceEventHeartbeat) => void;
|
|
55
|
-
/**
|
|
56
|
-
* Handle a disconnection broadcast by a peer.
|
|
57
|
-
*/
|
|
58
|
-
goodbye: (msg: PresenceEventGoodbye) => void;
|
|
59
|
-
};
|
|
60
|
-
export declare const DEFAULT_HEARTBEAT_INTERVAL_MS = 15000;
|
|
61
|
-
export declare const DEFAULT_PEER_TTL_MS: number;
|
|
62
|
-
export type PresenceConfig<State> = {
|
|
63
|
-
/** The full initial state to broadcast to peers */
|
|
64
|
-
initialState: State;
|
|
65
|
-
/** How frequently to send heartbeats (default {@link DEFAULT_HEARTBEAT_INTERVAL_MS}) */
|
|
66
|
-
heartbeatMs?: number;
|
|
67
|
-
/** How long to wait until forgetting peers with no activity (default {@link DEFAULT_PEER_TTL_MS}) */
|
|
68
|
-
peerTtlMs?: number;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Presence encapsulates ephemeral state communication for a specific doc
|
|
72
|
-
* handle. It tracks caller-provided local state and broadcasts that state to
|
|
73
|
-
* all peers. It sends periodic heartbeats when there are no state updates.
|
|
74
|
-
*
|
|
75
|
-
* It also tracks ephemeral state broadcast by peers and emits events when peers
|
|
76
|
-
* send ephemeral state updates (see {@link PresenceEvents}).
|
|
77
|
-
*
|
|
78
|
-
* Presence starts out in an inactive state. Call {@link start} and {@link stop}
|
|
79
|
-
* to activate and deactivate it.
|
|
80
|
-
*/
|
|
81
|
-
export declare class Presence<State extends Record<string, any>, DocType = any> extends EventEmitter<PresenceEvents> {
|
|
82
|
-
#private;
|
|
83
|
-
readonly deviceId?: DeviceId;
|
|
84
|
-
readonly userId?: UserId;
|
|
85
|
-
/**
|
|
86
|
-
* Create a new Presence to share ephemeral state with peers.
|
|
87
|
-
*
|
|
88
|
-
* @param config see {@link PresenceConfig}
|
|
89
|
-
* @returns
|
|
90
|
-
*/
|
|
91
|
-
constructor({ handle, deviceId, userId, }: {
|
|
92
|
-
handle: DocHandle<DocType>;
|
|
93
|
-
/** Our device id (like userId, this is unverified; peers can send anything) */
|
|
94
|
-
deviceId?: DeviceId;
|
|
95
|
-
/** Our user id (this is unverified; peers can send anything) */
|
|
96
|
-
userId?: UserId;
|
|
97
|
-
});
|
|
98
|
-
/**
|
|
99
|
-
* Start listening to ephemeral messages on the handle, broadcast initial
|
|
100
|
-
* state to peers, and start sending heartbeats.
|
|
101
|
-
*/
|
|
102
|
-
start({ initialState, heartbeatMs, peerTtlMs }: PresenceConfig<State>): void;
|
|
103
|
-
/**
|
|
104
|
-
* Return a view of current peer states.
|
|
105
|
-
*/
|
|
106
|
-
getPeerStates(): PeerPresenceView<State>;
|
|
107
|
-
/**
|
|
108
|
-
* Return a view of current local state.
|
|
109
|
-
*/
|
|
110
|
-
getLocalState(): State | undefined;
|
|
111
|
-
/**
|
|
112
|
-
* Update state for the specific channel, and broadcast new state to all
|
|
113
|
-
* peers.
|
|
114
|
-
*
|
|
115
|
-
* @param channel
|
|
116
|
-
* @param value
|
|
117
|
-
*/
|
|
118
|
-
broadcast<Channel extends keyof State>(channel: Channel, value: State[Channel]): void;
|
|
119
|
-
/**
|
|
120
|
-
* Whether this Presence is currently active. See
|
|
121
|
-
* {@link start} and {@link stop}.
|
|
122
|
-
*/
|
|
123
|
-
get running(): boolean;
|
|
124
|
-
/**
|
|
125
|
-
* Stop this Presence: broadcast a "goodbye" message (when received, other
|
|
126
|
-
* peers will immediately forget the sender), stop sending heartbeats, and
|
|
127
|
-
* stop listening to ephemeral-messages broadcast from peers.
|
|
128
|
-
*
|
|
129
|
-
* This can be used with browser events like
|
|
130
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event | "pagehide"}
|
|
131
|
-
* or
|
|
132
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event | "visibilitychange"}
|
|
133
|
-
* to stop sending and receiving updates when not active.
|
|
134
|
-
*/
|
|
135
|
-
stop(): void;
|
|
136
|
-
private announce;
|
|
137
|
-
private broadcastLocalState;
|
|
138
|
-
private broadcastChannelState;
|
|
139
|
-
private resetHeartbeats;
|
|
140
|
-
private sendHeartbeat;
|
|
141
|
-
private doBroadcast;
|
|
142
|
-
private startHeartbeats;
|
|
143
|
-
private stopHeartbeats;
|
|
144
|
-
private startPruningPeers;
|
|
145
|
-
private stopPruningPeers;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* A summary of the latest Presence information for the set of peers who have
|
|
149
|
-
* reported a Presence status to us.
|
|
150
|
-
*/
|
|
151
|
-
export declare class PeerPresenceView<State> {
|
|
152
|
-
#private;
|
|
153
|
-
/** @hidden */
|
|
154
|
-
constructor(peersLastSeen: Map<PeerId, number>, peerStates: Map<PeerId, PeerState<State>>, userPeers: Map<UserId, Set<PeerId>>, devicePeers: Map<DeviceId, Set<PeerId>>);
|
|
155
|
-
/**
|
|
156
|
-
* Check if peer is currently present.
|
|
157
|
-
*
|
|
158
|
-
* @param peerId
|
|
159
|
-
* @returns true if the peer has been seen recently
|
|
160
|
-
*/
|
|
161
|
-
has(peerId: PeerId): boolean;
|
|
162
|
-
/**
|
|
163
|
-
* Check when the peer was last seen.
|
|
164
|
-
*
|
|
165
|
-
* @param peerId
|
|
166
|
-
* @returns last seen UNIX timestamp, or undefined for unknown peers
|
|
167
|
-
*/
|
|
168
|
-
getLastSeen(peerId: PeerId): number | undefined;
|
|
169
|
-
/**
|
|
170
|
-
* Get all recently-seen peers.
|
|
171
|
-
*
|
|
172
|
-
* @returns Array of peer ids
|
|
173
|
-
*/
|
|
174
|
-
getPeers(): PeerId[];
|
|
175
|
-
/**
|
|
176
|
-
* Get all recently-seen users.
|
|
177
|
-
*
|
|
178
|
-
* @returns Array of user ids
|
|
179
|
-
*/
|
|
180
|
-
getUsers(): unknown[];
|
|
181
|
-
/**
|
|
182
|
-
* Get all recently-seen devices.
|
|
183
|
-
*
|
|
184
|
-
* @returns Array of device ids
|
|
185
|
-
*/
|
|
186
|
-
getDevices(): unknown[];
|
|
187
|
-
/**
|
|
188
|
-
* Get all recently-seen peers for this user.
|
|
189
|
-
*
|
|
190
|
-
* @param userId
|
|
191
|
-
* @returns Array of peer ids for this user
|
|
192
|
-
*/
|
|
193
|
-
getUserPeers(userId: UserId): PeerId[] | undefined;
|
|
194
|
-
/**
|
|
195
|
-
* Get all recently-seen peers for this device.
|
|
196
|
-
*
|
|
197
|
-
* @param deviceId
|
|
198
|
-
* @returns Array of peer ids for this device
|
|
199
|
-
*/
|
|
200
|
-
getDevicePeers(deviceId: DeviceId): PeerId[] | undefined;
|
|
201
|
-
/**
|
|
202
|
-
* Get most-recently-seen peer from this group.
|
|
203
|
-
*
|
|
204
|
-
* @param peers
|
|
205
|
-
* @returns id of most recently seen peer
|
|
206
|
-
*/
|
|
207
|
-
getFreshestPeer(peers: Set<PeerId>): PeerId | undefined;
|
|
208
|
-
/**
|
|
209
|
-
* Get current @type PeerState for given peer.
|
|
210
|
-
*
|
|
211
|
-
* @param peerId
|
|
212
|
-
* @returns details for the peer
|
|
213
|
-
*/
|
|
214
|
-
getPeerInfo(peerId: PeerId): PeerState<State> | undefined;
|
|
215
|
-
/**
|
|
216
|
-
* Get current ephemeral state value for this peer. If a channel is specified,
|
|
217
|
-
* only returns the ephemeral state for that specific channel. Otherwise,
|
|
218
|
-
* returns the full ephemeral state.
|
|
219
|
-
*
|
|
220
|
-
* @param peerId
|
|
221
|
-
* @param channel
|
|
222
|
-
* @returns latest ephemeral state received
|
|
223
|
-
*/
|
|
224
|
-
getPeerState<Channel extends keyof State>(peerId: PeerId, channel?: Channel): State | NonNullable<State>[Channel] | undefined;
|
|
225
|
-
/**
|
|
226
|
-
* Get current ephemeral state value for this user's most-recently-active
|
|
227
|
-
* peer. See {@link getPeerState}.
|
|
228
|
-
*
|
|
229
|
-
* @param userId
|
|
230
|
-
* @param channel
|
|
231
|
-
* @returns
|
|
232
|
-
*/
|
|
233
|
-
getUserState<Channel extends keyof State>(userId: UserId, channel?: Channel): State | NonNullable<State>[Channel] | undefined;
|
|
234
|
-
/**
|
|
235
|
-
* Get current ephemeral state value for this device's most-recently-active
|
|
236
|
-
* peer. See {@link getPeerState}.
|
|
237
|
-
*
|
|
238
|
-
* @param userId
|
|
239
|
-
* @param channel
|
|
240
|
-
* @returns
|
|
241
|
-
*/
|
|
242
|
-
getDeviceState<Channel extends keyof State>(deviceId: UserId, channel?: Channel): State | NonNullable<State>[Channel] | undefined;
|
|
243
|
-
}
|
|
244
|
-
export {};
|
|
245
|
-
//# sourceMappingURL=Presence.d.ts.map
|
package/dist/Presence.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Presence.d.ts","sourceRoot":"","sources":["../src/Presence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAoC,MAAM,gBAAgB,CAAA;AAC5E,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAE5C,MAAM,MAAM,MAAM,GAAG,OAAO,CAAA;AAC5B,MAAM,MAAM,QAAQ,GAAG,OAAO,CAAA;AAE9B,eAAO,MAAM,uBAAuB,eAAe,CAAA;AAEnD,MAAM,MAAM,SAAS,CAAC,KAAK,IAAI;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED,KAAK,mBAAmB,GAAG;IACzB,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,KAAK,qBAAqB,GAAG,mBAAmB,GAAG;IACjD,IAAI,EAAE,QAAQ,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,GAAG,CAAA;CACX,CAAA;AAED,KAAK,uBAA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