@hadoku/rooms-protocol 0.45.120 → 0.45.121
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/avatars.d.ts +10 -2
- package/dist/avatars.d.ts.map +1 -1
- package/dist/avatars.js +34 -2
- package/dist/session.d.ts +37 -1
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +89 -1
- package/package.json +2 -2
package/dist/avatars.d.ts
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
* is fan art of third-party characters, and the names stay out of every repo.
|
|
8
8
|
* Portraits live on watchparty's media as `square_<id>.png`, and every app
|
|
9
9
|
* draws them, so their URL is room protocol too: `avatarPortraitPath`, with the
|
|
10
|
-
* art revisions it carries.
|
|
11
|
-
*
|
|
10
|
+
* art revisions it carries. So is each character's colour (`AVATAR_COLORS`):
|
|
11
|
+
* a name in watchparty's chat and the same player's tint on a game's board are
|
|
12
|
+
* one colour, and a second table of them drifts. Only the grouping (which
|
|
13
|
+
* characters share a picker page) is each app's presentation.
|
|
12
14
|
*
|
|
13
15
|
* Append a new character, never insert one: apps draw the cast in this order
|
|
14
16
|
* (watchparty's picker, its colour table) and their tables are checked
|
|
@@ -16,6 +18,12 @@
|
|
|
16
18
|
*/
|
|
17
19
|
export declare const AVATAR_POOL: readonly ["sage", "herald", "cleric", "knight", "mage", "brawler", "wanderer", "rogue", "gunner", "chief", "walker", "operative", "idol", "recluse", "phantom", "harlequin", "doll", "imp", "apprentice", "prodigy", "dreamer", "sunny", "mentor"];
|
|
18
20
|
export type AvatarId = (typeof AVATAR_POOL)[number];
|
|
21
|
+
/**
|
|
22
|
+
* Each character's colour: their chat name, roster dot and in-game tint in every
|
|
23
|
+
* app, taken from the character's hair. A game written in another language
|
|
24
|
+
* (bomberman's Rust crate) keeps its own copy, held to this one by a test.
|
|
25
|
+
*/
|
|
26
|
+
export declare const AVATAR_COLORS: Readonly<Record<AvatarId, string>>;
|
|
19
27
|
/**
|
|
20
28
|
* A portrait's revision, for art redrawn in place: bump it when
|
|
21
29
|
* `media/sprites/square_<id>.png` is replaced. The sprites are served immutable
|
package/dist/avatars.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"avatars.d.ts","sourceRoot":"","sources":["../src/avatars.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"avatars.d.ts","sourceRoot":"","sources":["../src/avatars.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,WAAW,oPAKd,CAAC;AAEX,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AAEpD;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAwB5D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAGtE,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAGrD;AAED,yEAAyE;AACzE,wBAAgB,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,EAAE,IAAI,QAAQ,CAExE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,QAAQ,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EACzC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,EACzB,IAAI,GAAE,SAAS,MAAM,EAAgB,EACrC,IAAI,GAAE,MAAM,MAAoB,GAC/B,MAAM,GAAG,SAAS,CAMpB"}
|
package/dist/avatars.js
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
* is fan art of third-party characters, and the names stay out of every repo.
|
|
8
8
|
* Portraits live on watchparty's media as `square_<id>.png`, and every app
|
|
9
9
|
* draws them, so their URL is room protocol too: `avatarPortraitPath`, with the
|
|
10
|
-
* art revisions it carries.
|
|
11
|
-
*
|
|
10
|
+
* art revisions it carries. So is each character's colour (`AVATAR_COLORS`):
|
|
11
|
+
* a name in watchparty's chat and the same player's tint on a game's board are
|
|
12
|
+
* one colour, and a second table of them drifts. Only the grouping (which
|
|
13
|
+
* characters share a picker page) is each app's presentation.
|
|
12
14
|
*
|
|
13
15
|
* Append a new character, never insert one: apps draw the cast in this order
|
|
14
16
|
* (watchparty's picker, its colour table) and their tables are checked
|
|
@@ -20,6 +22,36 @@ export const AVATAR_POOL = [
|
|
|
20
22
|
'idol', 'recluse', 'phantom', 'harlequin', 'doll', 'imp',
|
|
21
23
|
'apprentice', 'prodigy', 'dreamer', 'sunny', 'mentor',
|
|
22
24
|
];
|
|
25
|
+
/**
|
|
26
|
+
* Each character's colour: their chat name, roster dot and in-game tint in every
|
|
27
|
+
* app, taken from the character's hair. A game written in another language
|
|
28
|
+
* (bomberman's Rust crate) keeps its own copy, held to this one by a test.
|
|
29
|
+
*/
|
|
30
|
+
export const AVATAR_COLORS = {
|
|
31
|
+
sage: '#7f7c84',
|
|
32
|
+
herald: '#bddaf9',
|
|
33
|
+
cleric: '#78855e',
|
|
34
|
+
knight: '#cfccc0',
|
|
35
|
+
mage: '#794983',
|
|
36
|
+
brawler: '#af4a33',
|
|
37
|
+
wanderer: '#936f42',
|
|
38
|
+
rogue: '#667240',
|
|
39
|
+
gunner: '#b6a472',
|
|
40
|
+
chief: '#a47e4d',
|
|
41
|
+
walker: '#e0443a',
|
|
42
|
+
operative: '#347cae',
|
|
43
|
+
idol: '#eb82bf',
|
|
44
|
+
recluse: '#6d6880',
|
|
45
|
+
phantom: '#a4a3ae',
|
|
46
|
+
harlequin: '#e5d5c5',
|
|
47
|
+
doll: '#a02633',
|
|
48
|
+
imp: '#b398cb',
|
|
49
|
+
apprentice: '#c6c15b',
|
|
50
|
+
prodigy: '#5b4a6a',
|
|
51
|
+
dreamer: '#97d0e4',
|
|
52
|
+
sunny: '#ee989b',
|
|
53
|
+
mentor: '#e1e0dc',
|
|
54
|
+
};
|
|
23
55
|
/**
|
|
24
56
|
* A portrait's revision, for art redrawn in place: bump it when
|
|
25
57
|
* `media/sprites/square_<id>.png` is replaced. The sprites are served immutable
|
package/dist/session.d.ts
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
*
|
|
32
32
|
* ## A tab's id
|
|
33
33
|
*
|
|
34
|
-
* Kept in `sessionStorage`, so a reload keeps it and never asks. A duplicated
|
|
34
|
+
* Kept in `sessionStorage`, so a reload keeps it and never asks (`createTabIdentity`). A duplicated
|
|
35
35
|
* tab starts with a COPY of the original's `sessionStorage`, so the stored id
|
|
36
36
|
* alone would make two live tabs one; `settleTabId` asks every other tab of
|
|
37
37
|
* the origin on a `BroadcastChannel` whether the id is in use, and mints a
|
|
@@ -110,4 +110,40 @@ export interface SettleTabIdOptions {
|
|
|
110
110
|
export declare function settleTabId(opts: SettleTabIdOptions): Promise<string>;
|
|
111
111
|
/** A real `BroadcastChannel` as a `TabIdChannel`, or null where the host has none. */
|
|
112
112
|
export declare function browserTabChannel(name?: string): TabIdChannel | null;
|
|
113
|
+
/** The storage and channel a tab identity reads, injectable for a test. */
|
|
114
|
+
export interface TabIdentityOptions {
|
|
115
|
+
/** The sessionStorage key the id is kept under, per app (`cardtable:tab-id`). Read at first use. */
|
|
116
|
+
storageKey: string | (() => string);
|
|
117
|
+
/** Default: the page's `sessionStorage`, or none where the accessor throws (a sandboxed frame). */
|
|
118
|
+
storage?: () => Pick<Storage, 'getItem' | 'setItem'> | null;
|
|
119
|
+
/** How the page was loaded (`reload`, `navigate`, …). Default: the navigation timing entry. */
|
|
120
|
+
navigation?: () => string;
|
|
121
|
+
/** Default: `browserTabChannel()`. */
|
|
122
|
+
channel?: () => TabIdChannel | null;
|
|
123
|
+
mint?: () => string;
|
|
124
|
+
waitMs?: number;
|
|
125
|
+
}
|
|
126
|
+
export interface TabIdentity {
|
|
127
|
+
/** This tab's id once no other live tab claims it: what every claim and join carries. */
|
|
128
|
+
settledTabId(): Promise<string>;
|
|
129
|
+
/** Run `use` with the settled id: now when it is known, else once the probe has answered. */
|
|
130
|
+
withSettledTabId(use: (id: string) => void): void;
|
|
131
|
+
/** The settled id, or null while the probe is out. */
|
|
132
|
+
currentTabId(): string | null;
|
|
133
|
+
}
|
|
134
|
+
/** The stored id a page may keep, given how it was loaded: only a reload's. */
|
|
135
|
+
export declare function storedTabCandidate(navigation: string, stored: string | null): string | null;
|
|
136
|
+
/**
|
|
137
|
+
* This browser tab's identity, one per page load: every socket the page opens,
|
|
138
|
+
* every reconnect, carries the same id. A reload keeps it, read back from
|
|
139
|
+
* sessionStorage. Only a reload: a duplicate, a restored tab, a new tab and a
|
|
140
|
+
* link navigate as something other than `reload`, so they mint their own. And
|
|
141
|
+
* because a duplicated tab starts with a COPY of the original's
|
|
142
|
+
* sessionStorage, a kept id is probed (`settleTabId`) and replaced if another
|
|
143
|
+
* live tab answers that it uses it. Every id handed out is the settled one: an
|
|
144
|
+
* id given before the probe answered could be the duplicate's.
|
|
145
|
+
*
|
|
146
|
+
* Create one per page (module level), with the app's own storage key.
|
|
147
|
+
*/
|
|
148
|
+
export declare function createTabIdentity(opts: TabIdentityOptions): TabIdentity;
|
|
113
149
|
//# sourceMappingURL=session.d.ts.map
|
package/dist/session.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,0BAA0B;AAC1B,eAAO,MAAM,cAAc;IACzB,iGAAiG;;IAEjG,sGAAsG;;IAEtG,8FAA8F;;CAEtF,CAAC;AAEX,mEAAmE;AACnE,eAAO,MAAM,cAAc,QAA0B,CAAC;AAEtD,wBAAg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|
|
31
31
|
*
|
|
32
32
|
* ## A tab's id
|
|
33
33
|
*
|
|
34
|
-
* Kept in `sessionStorage`, so a reload keeps it and never asks. A duplicated
|
|
34
|
+
* Kept in `sessionStorage`, so a reload keeps it and never asks (`createTabIdentity`). A duplicated
|
|
35
35
|
* tab starts with a COPY of the original's `sessionStorage`, so the stored id
|
|
36
36
|
* alone would make two live tabs one; `settleTabId` asks every other tab of
|
|
37
37
|
* the origin on a `BroadcastChannel` whether the id is in use, and mints a
|
|
@@ -158,3 +158,91 @@ export function browserTabChannel(name = TAB_ID_CHANNEL) {
|
|
|
158
158
|
},
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
|
+
/** The stored id a page may keep, given how it was loaded: only a reload's. */
|
|
162
|
+
export function storedTabCandidate(navigation, stored) {
|
|
163
|
+
return navigation === 'reload' && isTabId(stored) ? stored : null;
|
|
164
|
+
}
|
|
165
|
+
function pageNavigation() {
|
|
166
|
+
const entry = typeof performance === 'undefined' ? undefined : performance.getEntriesByType?.('navigation')[0];
|
|
167
|
+
return entry !== undefined && 'type' in entry ? String(entry.type) : 'unknown';
|
|
168
|
+
}
|
|
169
|
+
function pageStorage() {
|
|
170
|
+
try {
|
|
171
|
+
return typeof sessionStorage === 'undefined' ? null : sessionStorage;
|
|
172
|
+
}
|
|
173
|
+
catch (err) {
|
|
174
|
+
// A sandboxed frame throws on the accessor: the id then lives for this page only.
|
|
175
|
+
console.warn('[tab-id] no sessionStorage', err);
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* This browser tab's identity, one per page load: every socket the page opens,
|
|
181
|
+
* every reconnect, carries the same id. A reload keeps it, read back from
|
|
182
|
+
* sessionStorage. Only a reload: a duplicate, a restored tab, a new tab and a
|
|
183
|
+
* link navigate as something other than `reload`, so they mint their own. And
|
|
184
|
+
* because a duplicated tab starts with a COPY of the original's
|
|
185
|
+
* sessionStorage, a kept id is probed (`settleTabId`) and replaced if another
|
|
186
|
+
* live tab answers that it uses it. Every id handed out is the settled one: an
|
|
187
|
+
* id given before the probe answered could be the duplicate's.
|
|
188
|
+
*
|
|
189
|
+
* Create one per page (module level), with the app's own storage key.
|
|
190
|
+
*/
|
|
191
|
+
export function createTabIdentity(opts) {
|
|
192
|
+
let provisional = null;
|
|
193
|
+
let settled = null;
|
|
194
|
+
let settling = null;
|
|
195
|
+
const begin = () => {
|
|
196
|
+
if (settling !== null)
|
|
197
|
+
return settling;
|
|
198
|
+
const key = typeof opts.storageKey === 'function' ? opts.storageKey() : opts.storageKey;
|
|
199
|
+
const store = (opts.storage ?? pageStorage)();
|
|
200
|
+
let stored = null;
|
|
201
|
+
try {
|
|
202
|
+
stored = store?.getItem(key) ?? null;
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
console.warn('[tab-id] could not read the stored id', err);
|
|
206
|
+
}
|
|
207
|
+
const mint = opts.mint ?? mintTabId;
|
|
208
|
+
const candidate = storedTabCandidate((opts.navigation ?? pageNavigation)(), stored);
|
|
209
|
+
provisional = candidate ?? mint();
|
|
210
|
+
let channel = null;
|
|
211
|
+
try {
|
|
212
|
+
channel = (opts.channel ?? (() => browserTabChannel()))();
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
console.warn('[tab-id] no BroadcastChannel', err);
|
|
216
|
+
}
|
|
217
|
+
if (channel !== null)
|
|
218
|
+
answerTabProbes(channel, () => settled ?? provisional);
|
|
219
|
+
// A freshly minted id is nobody else's: only a kept one is probed.
|
|
220
|
+
settling = settleTabId({
|
|
221
|
+
candidate: provisional,
|
|
222
|
+
channel: candidate === null ? null : channel,
|
|
223
|
+
mint,
|
|
224
|
+
...(opts.waitMs === undefined ? {} : { waitMs: opts.waitMs }),
|
|
225
|
+
}).then((id) => {
|
|
226
|
+
settled = id;
|
|
227
|
+
try {
|
|
228
|
+
store?.setItem(key, id);
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
console.warn('[tab-id] could not store the id', err);
|
|
232
|
+
}
|
|
233
|
+
return id;
|
|
234
|
+
});
|
|
235
|
+
return settling;
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
settledTabId: begin,
|
|
239
|
+
withSettledTabId(use) {
|
|
240
|
+
const pending = begin();
|
|
241
|
+
if (settled !== null)
|
|
242
|
+
use(settled);
|
|
243
|
+
else
|
|
244
|
+
void pending.then(use);
|
|
245
|
+
},
|
|
246
|
+
currentTabId: () => settled,
|
|
247
|
+
};
|
|
248
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/rooms-protocol",
|
|
3
|
-
"version": "0.45.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.45.121",
|
|
4
|
+
"gitHead": "8734dfd290533cfa5011a48821ba690ec1c01add",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Room, identity and seating protocol — the wire contract shared by rooms-server and rooms-client.",
|