@foony/chat 0.0.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/LICENSE +215 -0
- package/README.md +67 -0
- package/lib/chatClient.d.ts +22 -0
- package/lib/chatClient.d.ts.map +1 -0
- package/lib/chatClient.js +28 -0
- package/lib/chatClient.js.map +1 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +18 -0
- package/lib/index.js.map +1 -0
- package/lib/messages.d.ts +45 -0
- package/lib/messages.d.ts.map +1 -0
- package/lib/messages.js +146 -0
- package/lib/messages.js.map +1 -0
- package/lib/occupancy.d.ts +30 -0
- package/lib/occupancy.d.ts.map +1 -0
- package/lib/occupancy.js +65 -0
- package/lib/occupancy.js.map +1 -0
- package/lib/presence.d.ts +0 -0
- package/lib/presence.d.ts.map +1 -0
- package/lib/presence.js +0 -0
- package/lib/presence.js.map +1 -0
- package/lib/protocol.d.ts +52 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +72 -0
- package/lib/protocol.js.map +1 -0
- package/lib/reactions.d.ts +26 -0
- package/lib/reactions.d.ts.map +1 -0
- package/lib/reactions.js +56 -0
- package/lib/reactions.js.map +1 -0
- package/lib/reconciler.d.ts +29 -0
- package/lib/reconciler.d.ts.map +1 -0
- package/lib/reconciler.js +149 -0
- package/lib/reconciler.js.map +1 -0
- package/lib/room.d.ts +53 -0
- package/lib/room.d.ts.map +1 -0
- package/lib/room.js +81 -0
- package/lib/room.js.map +1 -0
- package/lib/rooms.d.ts +20 -0
- package/lib/rooms.d.ts.map +1 -0
- package/lib/rooms.js +38 -0
- package/lib/rooms.js.map +1 -0
- package/lib/types.d.ts +139 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +9 -0
- package/lib/types.js.map +1 -0
- package/lib/typing.d.ts +43 -0
- package/lib/typing.d.ts.map +1 -0
- package/lib/typing.js +108 -0
- package/lib/typing.js.map +1 -0
- package/lib/util.d.ts +9 -0
- package/lib/util.d.ts.map +1 -0
- package/lib/util.js +12 -0
- package/lib/util.js.map +1 -0
- package/lib-cjs/chatClient.js +32 -0
- package/lib-cjs/chatClient.js.map +1 -0
- package/lib-cjs/index.js +30 -0
- package/lib-cjs/index.js.map +1 -0
- package/lib-cjs/messages.js +150 -0
- package/lib-cjs/messages.js.map +1 -0
- package/lib-cjs/occupancy.js +69 -0
- package/lib-cjs/occupancy.js.map +1 -0
- package/lib-cjs/package.json +3 -0
- package/lib-cjs/presence.js +0 -0
- package/lib-cjs/presence.js.map +1 -0
- package/lib-cjs/protocol.js +79 -0
- package/lib-cjs/protocol.js.map +1 -0
- package/lib-cjs/reactions.js +60 -0
- package/lib-cjs/reactions.js.map +1 -0
- package/lib-cjs/reconciler.js +153 -0
- package/lib-cjs/reconciler.js.map +1 -0
- package/lib-cjs/room.js +85 -0
- package/lib-cjs/room.js.map +1 -0
- package/lib-cjs/rooms.js +42 -0
- package/lib-cjs/rooms.js.map +1 -0
- package/lib-cjs/types.js +10 -0
- package/lib-cjs/types.js.map +1 -0
- package/lib-cjs/typing.js +112 -0
- package/lib-cjs/typing.js.map +1 -0
- package/lib-cjs/util.js +15 -0
- package/lib-cjs/util.js.map +1 -0
- package/package.json +56 -0
- package/src/chatClient.ts +31 -0
- package/src/index.ts +32 -0
- package/src/messages.ts +159 -0
- package/src/occupancy.ts +78 -0
- package/src/presence.ts +0 -0
- package/src/protocol.ts +90 -0
- package/src/reactions.ts +64 -0
- package/src/reconciler.ts +169 -0
- package/src/room.ts +97 -0
- package/src/rooms.ts +42 -0
- package/src/types.ts +141 -0
- package/src/typing.ts +123 -0
- package/src/util.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work, excluding
|
|
103
|
+
those notices that do not pertain to any part of the Derivative
|
|
104
|
+
Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one of
|
|
111
|
+
the following places: within a NOTICE text file distributed as
|
|
112
|
+
part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and do
|
|
117
|
+
not modify the License. You may add Your own attribution notices
|
|
118
|
+
within Derivative Works that You distribute, alongside or as an
|
|
119
|
+
addendum to the NOTICE text from the Work, provided that such
|
|
120
|
+
additional attribution notices cannot be construed as modifying
|
|
121
|
+
the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
|
203
|
+
Copyright 2026 Foony Limited
|
|
204
|
+
|
|
205
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
206
|
+
you may not use this file except in compliance with the License.
|
|
207
|
+
You may obtain a copy of the License at
|
|
208
|
+
|
|
209
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
210
|
+
|
|
211
|
+
Unless required by applicable law or agreed to in writing, software
|
|
212
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
213
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
214
|
+
See the License for the specific language governing permissions and
|
|
215
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# @foony/chat
|
|
2
|
+
|
|
3
|
+
A chat API — rooms, messages (with edit/delete), typing indicators, room
|
|
4
|
+
reactions, presence, occupancy, and history — built on top of
|
|
5
|
+
[`@foony/realtime`](../realtime-js). It adds no new transport; it rides the
|
|
6
|
+
existing realtime connection and maps each chat feature onto pub/sub event
|
|
7
|
+
names on a per-room channel (`chat:<roomName>`).
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Realtime } from '@foony/realtime';
|
|
11
|
+
import { ChatClient } from '@foony/chat';
|
|
12
|
+
|
|
13
|
+
const realtime = new Realtime({ authCallback });
|
|
14
|
+
const chat = new ChatClient(realtime);
|
|
15
|
+
|
|
16
|
+
const room = chat.rooms.get('general');
|
|
17
|
+
await room.attach();
|
|
18
|
+
|
|
19
|
+
room.messages.subscribe((event) => {
|
|
20
|
+
// event.type is 'created' | 'updated' | 'deleted'
|
|
21
|
+
console.log(event.message.text);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const message = await room.messages.send({ text: 'Hello!' });
|
|
25
|
+
await room.messages.update(message.id, { text: 'Hello, world!' });
|
|
26
|
+
await room.messages.delete(message.id);
|
|
27
|
+
|
|
28
|
+
// Backfill recent history (materialized through the same reconciler).
|
|
29
|
+
const page = await room.messages.history({ limit: 50 });
|
|
30
|
+
|
|
31
|
+
// Presence, typing, reactions, occupancy
|
|
32
|
+
await room.presence.enter({ name: 'Alice' });
|
|
33
|
+
await room.typing.keystroke();
|
|
34
|
+
await room.reactions.send({ name: '🎉' });
|
|
35
|
+
room.occupancy.subscribe(({ connections, presenceMembers }) => {});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Design notes
|
|
39
|
+
|
|
40
|
+
- **Messages** are keyed by a stable, sender-assigned `id`, so `update(id, …)`
|
|
41
|
+
and `delete(id, …)` reference a message directly. Edits/deletes are published
|
|
42
|
+
as new frames and reconciled client-side by `id`; the same reconciler runs
|
|
43
|
+
over the live stream and over `history()`, so both converge on identical
|
|
44
|
+
state regardless of delivery order or duplicates.
|
|
45
|
+
- **Typing** is ephemeral: a heartbeat is throttled to `heartbeatThrottleMs`
|
|
46
|
+
(default 10s, must be uniform across clients), and receivers auto-expire a
|
|
47
|
+
typer ~2s after the last heartbeat.
|
|
48
|
+
- **Occupancy** is derived entirely from the presence set — `presenceMembers`
|
|
49
|
+
counts distinct client ids, `connections` counts distinct
|
|
50
|
+
(clientId, connectionId) pairs. It counts only members who entered presence.
|
|
51
|
+
- **React** bindings are intentionally out of this core package and planned as
|
|
52
|
+
a separate `@foony/chat/react` subpath.
|
|
53
|
+
|
|
54
|
+
## Examples
|
|
55
|
+
|
|
56
|
+
Runnable browser + Node examples that exercise every feature live against the prod
|
|
57
|
+
edge live in [`examples/`](./examples) (`npm run client` for the React playground,
|
|
58
|
+
`npm run bot` for a Node participant). See [examples/README.md](./examples/README.md).
|
|
59
|
+
|
|
60
|
+
## Scripts
|
|
61
|
+
|
|
62
|
+
- `npm run build` — dual ESM (`lib/`) + CJS (`lib-cjs/`) build.
|
|
63
|
+
- `npm test` — vitest unit tests.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
[Apache-2.0](./LICENSE) © Foony Limited
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChatClient is the top-level entry point: wrap a connected (or connecting)
|
|
3
|
+
* {@link Realtime} client and use `chat.rooms.get(name)` to start chatting.
|
|
4
|
+
* The chat layer adds no new transport — it rides the existing connection.
|
|
5
|
+
*/
|
|
6
|
+
import type { Connection, Realtime } from '@foony/realtime';
|
|
7
|
+
import { Rooms } from './rooms.js';
|
|
8
|
+
/** Chat client built on top of a {@link Realtime} instance. */
|
|
9
|
+
export declare class ChatClient {
|
|
10
|
+
private readonly realtime;
|
|
11
|
+
/** Room registry and factory. */
|
|
12
|
+
readonly rooms: Rooms;
|
|
13
|
+
constructor(realtime: Realtime);
|
|
14
|
+
/** The underlying realtime connection (status, events). */
|
|
15
|
+
get connection(): Connection;
|
|
16
|
+
/**
|
|
17
|
+
* The resolved client id for this connection, or null until the auth
|
|
18
|
+
* handshake completes. Used to attribute messages and detect self.
|
|
19
|
+
*/
|
|
20
|
+
get clientId(): string | null;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=chatClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chatClient.d.ts","sourceRoot":"","sources":["../src/chatClient.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,+DAA+D;AAC/D,qBAAa,UAAU;IAIT,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAHrC,iCAAiC;IACjC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;gBAEO,QAAQ,EAAE,QAAQ;IAI/C,2DAA2D;IAC3D,IAAI,UAAU,IAAI,UAAU,CAE3B;IAED;;;OAGG;IACH,IAAI,QAAQ,IAAI,MAAM,GAAG,IAAI,CAE5B;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChatClient is the top-level entry point: wrap a connected (or connecting)
|
|
3
|
+
* {@link Realtime} client and use `chat.rooms.get(name)` to start chatting.
|
|
4
|
+
* The chat layer adds no new transport — it rides the existing connection.
|
|
5
|
+
*/
|
|
6
|
+
import { Rooms } from './rooms.js';
|
|
7
|
+
/** Chat client built on top of a {@link Realtime} instance. */
|
|
8
|
+
export class ChatClient {
|
|
9
|
+
realtime;
|
|
10
|
+
/** Room registry and factory. */
|
|
11
|
+
rooms;
|
|
12
|
+
constructor(realtime) {
|
|
13
|
+
this.realtime = realtime;
|
|
14
|
+
this.rooms = new Rooms(realtime, () => this.clientId);
|
|
15
|
+
}
|
|
16
|
+
/** The underlying realtime connection (status, events). */
|
|
17
|
+
get connection() {
|
|
18
|
+
return this.realtime.connection;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The resolved client id for this connection, or null until the auth
|
|
22
|
+
* handshake completes. Used to attribute messages and detect self.
|
|
23
|
+
*/
|
|
24
|
+
get clientId() {
|
|
25
|
+
return this.realtime.getClientId();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=chatClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chatClient.js","sourceRoot":"","sources":["../src/chatClient.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,+DAA+D;AAC/D,MAAM,OAAO,UAAU;IAIQ;IAH7B,iCAAiC;IACxB,KAAK,CAAQ;IAEtB,YAA6B,QAAkB;QAAlB,aAAQ,GAAR,QAAQ,CAAU;QAC7C,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACxD,CAAC;IAED,2DAA2D;IAC3D,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;IACrC,CAAC;CACF"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for @foony/chat.
|
|
3
|
+
*
|
|
4
|
+
* A chat layer over @foony/realtime: construct a `ChatClient` from a `Realtime`
|
|
5
|
+
* instance, then `chat.rooms.get(name)` for messages, presence, typing,
|
|
6
|
+
* reactions and occupancy. React bindings are intentionally out of this core
|
|
7
|
+
* package (planned as a separate `@foony/chat/react` subpath).
|
|
8
|
+
*/
|
|
9
|
+
export { ChatClient } from './chatClient.js';
|
|
10
|
+
export { Rooms } from './rooms.js';
|
|
11
|
+
export { Room, type RoomStatus, type DiscontinuityListener } from './room.js';
|
|
12
|
+
export { Messages, type MessageListener } from './messages.js';
|
|
13
|
+
export { Presence, type PresenceListener } from './presence.js';
|
|
14
|
+
export { Typing, type TypingListener } from './typing.js';
|
|
15
|
+
export { Reactions, type ReactionListener } from './reactions.js';
|
|
16
|
+
export { Occupancy, type OccupancyListener } from './occupancy.js';
|
|
17
|
+
export { MessageReconciler } from './reconciler.js';
|
|
18
|
+
export type { Message, MessageAction, ChatMessageEvent, SendMessageParams, UpdateMessageParams, MessagePage, PresenceMember, PresenceEvent, TypingEvent, RoomReaction, Occupancy as OccupancyData, RoomOptions, } from './types.js';
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,KAAK,UAAU,EAAE,KAAK,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,KAAK,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,YAAY,EACV,OAAO,EACP,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,WAAW,EACX,cAAc,EACd,aAAa,EACb,WAAW,EACX,YAAY,EACZ,SAAS,IAAI,aAAa,EAC1B,WAAW,GACZ,MAAM,YAAY,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for @foony/chat.
|
|
3
|
+
*
|
|
4
|
+
* A chat layer over @foony/realtime: construct a `ChatClient` from a `Realtime`
|
|
5
|
+
* instance, then `chat.rooms.get(name)` for messages, presence, typing,
|
|
6
|
+
* reactions and occupancy. React bindings are intentionally out of this core
|
|
7
|
+
* package (planned as a separate `@foony/chat/react` subpath).
|
|
8
|
+
*/
|
|
9
|
+
export { ChatClient } from './chatClient.js';
|
|
10
|
+
export { Rooms } from './rooms.js';
|
|
11
|
+
export { Room } from './room.js';
|
|
12
|
+
export { Messages } from './messages.js';
|
|
13
|
+
export { Presence } from './presence.js';
|
|
14
|
+
export { Typing } from './typing.js';
|
|
15
|
+
export { Reactions } from './reactions.js';
|
|
16
|
+
export { Occupancy } from './occupancy.js';
|
|
17
|
+
export { MessageReconciler } from './reconciler.js';
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAA+C,MAAM,WAAW,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAwB,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAyB,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,MAAM,EAAuB,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAyB,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,SAAS,EAA0B,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-room message API: send, edit, delete, subscribe, and history.
|
|
3
|
+
*
|
|
4
|
+
* One {@link MessageReconciler} is shared across every subscriber and the
|
|
5
|
+
* history backfill, so live and replayed frames converge on identical state.
|
|
6
|
+
* Subscribers see only live frames; history is returned as a separate page
|
|
7
|
+
* (the app renders backfill itself, matching Ably's historyBeforeSubscribe).
|
|
8
|
+
*/
|
|
9
|
+
import type { Channel, UnsubscribeFn } from '@foony/realtime';
|
|
10
|
+
import type { ChatMessageEvent, Message, MessagePage, SendMessageParams, UpdateMessageParams } from './types.js';
|
|
11
|
+
/** Listener invoked for every materialized message change on the room. */
|
|
12
|
+
export type MessageListener = (event: ChatMessageEvent) => void;
|
|
13
|
+
/** The message feature of a {@link Room}. */
|
|
14
|
+
export declare class Messages {
|
|
15
|
+
private readonly channel;
|
|
16
|
+
private readonly roomName;
|
|
17
|
+
private readonly getClientId;
|
|
18
|
+
private readonly reconciler;
|
|
19
|
+
private readonly listeners;
|
|
20
|
+
private channelUnsubscribe;
|
|
21
|
+
constructor(channel: Channel, roomName: string, getClientId: () => string | null);
|
|
22
|
+
/** Send a new message. Returns the message optimistically; its `id` is stable. */
|
|
23
|
+
send(params: SendMessageParams): Promise<Message>;
|
|
24
|
+
/** Edit a message by id. Replaces text/metadata/headers (omitted fields clear). */
|
|
25
|
+
update(id: string, params: UpdateMessageParams): Promise<void>;
|
|
26
|
+
/** Delete a message by id. */
|
|
27
|
+
delete(id: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Subscribe to live message changes. The first subscriber attaches the
|
|
30
|
+
* underlying channel subscription; the last to unsubscribe removes it.
|
|
31
|
+
*/
|
|
32
|
+
subscribe(listener: MessageListener): UnsubscribeFn;
|
|
33
|
+
/**
|
|
34
|
+
* Fetch a page of past messages, oldest-first, materialized through the same
|
|
35
|
+
* reconciler as the live stream. Pass `cursor` (a previous page's
|
|
36
|
+
* `nextCursor`) to page further back.
|
|
37
|
+
*/
|
|
38
|
+
history(params?: {
|
|
39
|
+
limit?: number;
|
|
40
|
+
cursor?: string;
|
|
41
|
+
}): Promise<MessagePage>;
|
|
42
|
+
/** Lazily attach the single channel subscription that feeds the reconciler. */
|
|
43
|
+
private ensureChannelSubscription;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAgB,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAG5E,OAAO,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAGjH,0EAA0E;AAC1E,MAAM,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAC;AAUhE,6CAA6C;AAC7C,qBAAa,QAAQ;IAMjB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAP9B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAC/C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8B;IACxD,OAAO,CAAC,kBAAkB,CAA8B;gBAGrC,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,WAAW,EAAE,MAAM,MAAM,GAAG,IAAI;IAKnD,kFAAkF;IAC5E,IAAI,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,OAAO,CAAC;IA0BvD,mFAAmF;IAC7E,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAYpE,8BAA8B;IACxB,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKvC;;;OAGG;IACH,SAAS,CAAC,QAAQ,EAAE,eAAe,GAAG,aAAa;IAYnD;;;;OAIG;IACG,OAAO,CAAC,MAAM,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,WAAW,CAAC;IA0BjF,+EAA+E;IAC/E,OAAO,CAAC,yBAAyB;CAclC"}
|
package/lib/messages.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-room message API: send, edit, delete, subscribe, and history.
|
|
3
|
+
*
|
|
4
|
+
* One {@link MessageReconciler} is shared across every subscriber and the
|
|
5
|
+
* history backfill, so live and replayed frames converge on identical state.
|
|
6
|
+
* Subscribers see only live frames; history is returned as a separate page
|
|
7
|
+
* (the app renders backfill itself, matching Ably's historyBeforeSubscribe).
|
|
8
|
+
*/
|
|
9
|
+
import { MESSAGE_EVENT, PAYLOAD_VERSION } from './protocol.js';
|
|
10
|
+
import { MessageReconciler } from './reconciler.js';
|
|
11
|
+
import { newMessageId } from './util.js';
|
|
12
|
+
/**
|
|
13
|
+
* Retention requested for chat messages: the maximum the platform offers (1
|
|
14
|
+
* year). The edge clamps this down to the app's plan ceiling, so a chat message
|
|
15
|
+
* persists as long as the plan allows — versus typing/reactions, which are left
|
|
16
|
+
* at the short ephemeral default. One year in milliseconds.
|
|
17
|
+
*/
|
|
18
|
+
const MESSAGE_TTL_MS = 365 * 24 * 60 * 60 * 1000;
|
|
19
|
+
/** The message feature of a {@link Room}. */
|
|
20
|
+
export class Messages {
|
|
21
|
+
channel;
|
|
22
|
+
roomName;
|
|
23
|
+
getClientId;
|
|
24
|
+
reconciler;
|
|
25
|
+
listeners = new Set();
|
|
26
|
+
channelUnsubscribe = null;
|
|
27
|
+
constructor(channel, roomName, getClientId) {
|
|
28
|
+
this.channel = channel;
|
|
29
|
+
this.roomName = roomName;
|
|
30
|
+
this.getClientId = getClientId;
|
|
31
|
+
this.reconciler = new MessageReconciler(roomName);
|
|
32
|
+
}
|
|
33
|
+
/** Send a new message. Returns the message optimistically; its `id` is stable. */
|
|
34
|
+
async send(params) {
|
|
35
|
+
const id = newMessageId();
|
|
36
|
+
const payload = {
|
|
37
|
+
v: PAYLOAD_VERSION,
|
|
38
|
+
action: 'create',
|
|
39
|
+
id,
|
|
40
|
+
text: params.text,
|
|
41
|
+
...(params.metadata === undefined ? {} : { metadata: params.metadata }),
|
|
42
|
+
...(params.headers === undefined ? {} : { headers: params.headers }),
|
|
43
|
+
};
|
|
44
|
+
await this.channel.publish(MESSAGE_EVENT, payload, { ttlMs: MESSAGE_TTL_MS });
|
|
45
|
+
const now = new Date();
|
|
46
|
+
return {
|
|
47
|
+
id,
|
|
48
|
+
clientId: this.getClientId() ?? '',
|
|
49
|
+
roomName: this.roomName,
|
|
50
|
+
text: params.text,
|
|
51
|
+
metadata: params.metadata ?? {},
|
|
52
|
+
headers: params.headers ?? {},
|
|
53
|
+
createdAt: now,
|
|
54
|
+
updatedAt: now,
|
|
55
|
+
action: 'create',
|
|
56
|
+
deleted: false,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Edit a message by id. Replaces text/metadata/headers (omitted fields clear). */
|
|
60
|
+
async update(id, params) {
|
|
61
|
+
const payload = {
|
|
62
|
+
v: PAYLOAD_VERSION,
|
|
63
|
+
action: 'update',
|
|
64
|
+
id,
|
|
65
|
+
text: params.text,
|
|
66
|
+
...(params.metadata === undefined ? {} : { metadata: params.metadata }),
|
|
67
|
+
...(params.headers === undefined ? {} : { headers: params.headers }),
|
|
68
|
+
};
|
|
69
|
+
await this.channel.publish(MESSAGE_EVENT, payload, { ttlMs: MESSAGE_TTL_MS });
|
|
70
|
+
}
|
|
71
|
+
/** Delete a message by id. */
|
|
72
|
+
async delete(id) {
|
|
73
|
+
const payload = { v: PAYLOAD_VERSION, action: 'delete', id };
|
|
74
|
+
await this.channel.publish(MESSAGE_EVENT, payload, { ttlMs: MESSAGE_TTL_MS });
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Subscribe to live message changes. The first subscriber attaches the
|
|
78
|
+
* underlying channel subscription; the last to unsubscribe removes it.
|
|
79
|
+
*/
|
|
80
|
+
subscribe(listener) {
|
|
81
|
+
this.listeners.add(listener);
|
|
82
|
+
this.ensureChannelSubscription();
|
|
83
|
+
return () => {
|
|
84
|
+
this.listeners.delete(listener);
|
|
85
|
+
if (this.listeners.size === 0 && this.channelUnsubscribe) {
|
|
86
|
+
this.channelUnsubscribe();
|
|
87
|
+
this.channelUnsubscribe = null;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Fetch a page of past messages, oldest-first, materialized through the same
|
|
93
|
+
* reconciler as the live stream. Pass `cursor` (a previous page's
|
|
94
|
+
* `nextCursor`) to page further back.
|
|
95
|
+
*/
|
|
96
|
+
async history(params) {
|
|
97
|
+
const { messages: frames, more } = await this.channel.history({
|
|
98
|
+
...(params?.limit === undefined ? {} : { limit: params.limit }),
|
|
99
|
+
...(params?.cursor === undefined ? {} : { start: params.cursor }),
|
|
100
|
+
});
|
|
101
|
+
const touched = [];
|
|
102
|
+
for (const frame of frames) {
|
|
103
|
+
if (frame.name !== MESSAGE_EVENT) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
this.reconciler.apply(frame);
|
|
107
|
+
const id = messageIdOf(frame);
|
|
108
|
+
if (id !== null && !touched.includes(id)) {
|
|
109
|
+
touched.push(id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const messages = touched
|
|
113
|
+
.map((id) => this.reconciler.get(id))
|
|
114
|
+
.filter((message) => message !== undefined);
|
|
115
|
+
return {
|
|
116
|
+
messages,
|
|
117
|
+
hasMore: more,
|
|
118
|
+
...(frames.length > 0 && frames[0] ? { nextCursor: frames[0].messageId } : {}),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Lazily attach the single channel subscription that feeds the reconciler. */
|
|
122
|
+
ensureChannelSubscription() {
|
|
123
|
+
if (this.channelUnsubscribe) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
this.channelUnsubscribe = this.channel.subscribe(MESSAGE_EVENT, (frame) => {
|
|
127
|
+
const event = this.reconciler.apply(frame);
|
|
128
|
+
if (event === null) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
for (const listener of [...this.listeners]) {
|
|
132
|
+
listener(event);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** The chat message id a frame refers to (payload `id`), or null if unparseable. */
|
|
138
|
+
function messageIdOf(frame) {
|
|
139
|
+
const data = frame.data;
|
|
140
|
+
if (typeof data !== 'object' || data === null) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const id = data['id'];
|
|
144
|
+
return typeof id === 'string' && id !== '' ? id : null;
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,aAAa,EAAE,eAAe,EAAuB,MAAM,eAAe,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAKzC;;;;;GAKG;AACH,MAAM,cAAc,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEjD,6CAA6C;AAC7C,MAAM,OAAO,QAAQ;IAMA;IACA;IACA;IAPF,UAAU,CAAoB;IAC9B,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;IAChD,kBAAkB,GAAyB,IAAI,CAAC;IAExD,YACmB,OAAgB,EAChB,QAAgB,EAChB,WAAgC;QAFhC,YAAO,GAAP,OAAO,CAAS;QAChB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,gBAAW,GAAX,WAAW,CAAqB;QAEjD,IAAI,CAAC,UAAU,GAAG,IAAI,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;IAED,kFAAkF;IAClF,KAAK,CAAC,IAAI,CAAC,MAAyB;QAClC,MAAM,EAAE,GAAG,YAAY,EAAE,CAAC;QAC1B,MAAM,OAAO,GAAmB;YAC9B,CAAC,EAAE,eAAe;YAClB,MAAM,EAAE,QAAQ;YAChB,EAAE;YACF,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,GAAG,CAAC,MAAM,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAA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AAK,KAAK,IAAI,EAAE,CAAC;gBACnB,OAAO;YACT,CAAC;YACD,KAAK,MAAM,QAAQ,IAAI,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC3C,QAAQ,CAAC,KAAK,CAAC,CAAC;YAClB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,oFAAoF;AACpF,SAAS,WAAW,CAAC,KAAmB;IACtC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACxB,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAC9C,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,EAAE,GAAI,IAAgC,CAAC,IAAI,CAAC,CAAC;IACnD,OAAO,OAAO,EAAE,KAAK,QAAQ,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-room occupancy, derived entirely from the presence set — no edge call.
|
|
3
|
+
*
|
|
4
|
+
* `connections` counts distinct (clientId, connectionId) pairs present (a user
|
|
5
|
+
* on two devices = two connections); `presenceMembers` counts distinct client
|
|
6
|
+
* ids. This reuses presence's TTL-based ghost-leave detection, so counts
|
|
7
|
+
* self-heal after a crash. Limitation: it only counts members who entered
|
|
8
|
+
* presence — always the case in a chat room, where joining enters presence.
|
|
9
|
+
*/
|
|
10
|
+
import type { UnsubscribeFn } from '@foony/realtime';
|
|
11
|
+
import type { Presence } from './presence.js';
|
|
12
|
+
import type { Occupancy as OccupancyData } from './types.js';
|
|
13
|
+
/** Listener invoked when occupancy counts change. */
|
|
14
|
+
export type OccupancyListener = (occupancy: OccupancyData) => void;
|
|
15
|
+
/** The occupancy feature of a {@link Room}. */
|
|
16
|
+
export declare class Occupancy {
|
|
17
|
+
private readonly presence;
|
|
18
|
+
private readonly listeners;
|
|
19
|
+
private readonly debounceMs;
|
|
20
|
+
private debounceTimer;
|
|
21
|
+
private lastEmitted;
|
|
22
|
+
private membersChangedUnsubscribe;
|
|
23
|
+
constructor(presence: Presence, debounceMs?: number);
|
|
24
|
+
/** Current occupancy snapshot, computed from the local presence member set. */
|
|
25
|
+
get(): OccupancyData;
|
|
26
|
+
/** Subscribe to occupancy changes (debounced, deduplicated). */
|
|
27
|
+
subscribe(listener: OccupancyListener): UnsubscribeFn;
|
|
28
|
+
private scheduleEmit;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=occupancy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"occupancy.d.ts","sourceRoot":"","sources":["../src/occupancy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,YAAY,CAAC;AAE7D,qDAAqD;AACrD,MAAM,MAAM,iBAAiB,GAAG,CAAC,SAAS,EAAE,aAAa,KAAK,IAAI,CAAC;AAKnE,+CAA+C;AAC/C,qBAAa,SAAS;IAQlB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAP3B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgC;IAC1D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,aAAa,CAA8C;IACnE,OAAO,CAAC,WAAW,CAA8B;IACjD,OAAO,CAAC,yBAAyB,CAA8B;gBAG5C,QAAQ,EAAE,QAAQ,EACnC,UAAU,CAAC,EAAE,MAAM;IAKrB,+EAA+E;IAC/E,GAAG,IAAI,aAAa;IAMpB,gEAAgE;IAChE,SAAS,CAAC,QAAQ,EAAE,iBAAiB,GAAG,aAAa;IAkBrD,OAAO,CAAC,YAAY;CAgBrB"}
|