@diffenderfer-games/hub 2.0.0-next.0 → 2.0.0-next.2

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.
Files changed (154) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +21 -21
  3. package/README.md +206 -139
  4. package/dist/auto.d.ts +6 -6
  5. package/dist/auto.js +2 -2
  6. package/dist/doctor/cli.mjs +62 -25
  7. package/dist/doctor/cli.mjs.map +1 -1
  8. package/dist/index.d.ts +6 -6
  9. package/dist/index.js +2 -2
  10. package/dist/mp.d.ts +3 -3
  11. package/dist/race.d.ts +1 -1
  12. package/dist/rooms.d.ts +3 -3
  13. package/dist/shared/bridge.js +9 -3
  14. package/dist/shared/bridge.js.map +1 -1
  15. package/dist/shared/client-version.gen.js +1 -1
  16. package/dist/shared/daily.d.ts +10 -4
  17. package/dist/shared/daily.d.ts.map +1 -1
  18. package/dist/shared/daily.js +12 -2
  19. package/dist/shared/daily.js.map +1 -1
  20. package/dist/shared/globals.js +8 -3
  21. package/dist/shared/globals.js.map +1 -1
  22. package/dist/shared/index.d.ts +314 -35
  23. package/dist/shared/index.d.ts.map +1 -1
  24. package/dist/shared/input.js +7 -2
  25. package/dist/shared/input.js.map +1 -1
  26. package/dist/shared/latest-state.js +1 -1
  27. package/dist/shared/mp.js +10 -2
  28. package/dist/shared/mp.js.map +1 -1
  29. package/dist/shared/multiplayer-types.d.ts +15 -2
  30. package/dist/shared/multiplayer-types.d.ts.map +1 -1
  31. package/dist/shared/race-types.d.ts +1 -1
  32. package/dist/shared/race.js +1 -1
  33. package/dist/shared/room-types.d.ts +28 -3
  34. package/dist/shared/room-types.d.ts.map +1 -1
  35. package/dist/shared/rooms.d.ts.map +1 -1
  36. package/dist/shared/rooms.js +10 -2
  37. package/dist/shared/rooms.js.map +1 -1
  38. package/dist/shared/runtime-opener.js +1 -1
  39. package/dist/shared/social.d.ts +23 -8
  40. package/dist/shared/social.d.ts.map +1 -1
  41. package/dist/shared/social.js +6 -1
  42. package/dist/shared/social.js.map +1 -1
  43. package/dist/shared/src.js +1588 -388
  44. package/dist/shared/src.js.map +1 -1
  45. package/dist/shared/wire-types.d.ts +6 -2
  46. package/dist/shared/wire-types.d.ts.map +1 -1
  47. package/dist/social.d.ts +5 -5
  48. package/dist/social.js +1 -1
  49. package/package.json +89 -89
  50. package/src/auto.ts +39 -39
  51. package/src/core/connection.ts +394 -299
  52. package/src/core/errors.ts +147 -147
  53. package/src/core/game-data.ts +40 -38
  54. package/src/core/hub-economy.ts +216 -179
  55. package/src/core/hub-progress.ts +202 -205
  56. package/src/core/hub.ts +133 -131
  57. package/src/core/menu-control.ts +61 -61
  58. package/src/core/offline/events.ts +50 -50
  59. package/src/core/offline/favorites.ts +21 -21
  60. package/src/core/offline/hub-kinds/daily-kind.ts +68 -0
  61. package/src/core/offline/hub-kinds/economy-kinds.ts +86 -0
  62. package/src/core/offline/hub-kinds/game-fields.ts +2 -0
  63. package/src/core/offline/hub-kinds/index.ts +35 -0
  64. package/src/core/offline/hub-kinds/legacy-ops.ts +37 -0
  65. package/src/core/offline/hub-kinds/progress-kinds.ts +148 -0
  66. package/src/core/offline/kind-adapters.ts +48 -0
  67. package/src/core/offline/local-inventory.ts +64 -0
  68. package/src/core/offline/local-store.ts +259 -261
  69. package/src/core/offline/offline-api.ts +65 -0
  70. package/src/core/offline/outbox-entry.ts +59 -0
  71. package/src/core/offline/outbox-kinds.ts +78 -0
  72. package/src/core/offline/outbox-merge.ts +39 -0
  73. package/src/core/offline/outbox-replayer.ts +141 -0
  74. package/src/core/offline/outbox-store.ts +85 -0
  75. package/src/core/offline/outbox.ts +232 -0
  76. package/src/core/offline/queue.ts +126 -171
  77. package/src/core/offline/replay-lease.ts +87 -0
  78. package/src/core/offline/replay-policy.ts +52 -0
  79. package/src/core/page.ts +74 -74
  80. package/src/core/rest/endpoint-types.ts +22 -22
  81. package/src/core/rest/rest-client.ts +163 -152
  82. package/src/core/rest/routes.gen.ts +108 -87
  83. package/src/core/storage/json-guards.ts +80 -74
  84. package/src/core/storage/key-value.ts +66 -66
  85. package/src/core/ticket.ts +43 -43
  86. package/src/core/types.ts +134 -134
  87. package/src/core/version/client-version.gen.ts +1 -1
  88. package/src/core/version/hub-version.ts +62 -62
  89. package/src/core/version/semver.ts +97 -97
  90. package/src/daily/daily-system.ts +168 -161
  91. package/src/daily/page-daily.ts +36 -36
  92. package/src/daily/types.ts +68 -67
  93. package/src/daily.ts +10 -10
  94. package/src/doctor/checks.ts +86 -76
  95. package/src/doctor/cli.ts +98 -106
  96. package/src/doctor/hub-version-fetch.ts +43 -0
  97. package/src/doctor/tsconfig.json +12 -12
  98. package/src/globals.ts +115 -110
  99. package/src/index.ts +55 -50
  100. package/src/input/device-state.ts +146 -146
  101. package/src/input/edges.ts +55 -55
  102. package/src/input/gamepad.ts +61 -61
  103. package/src/input/input-events.ts +32 -32
  104. package/src/input/input-registry.ts +171 -164
  105. package/src/input/input-system.ts +393 -393
  106. package/src/input/legacy-controls.ts +64 -64
  107. package/src/input/nav-state.ts +191 -191
  108. package/src/input/navigator.ts +171 -171
  109. package/src/input/on-screen-controls.ts +101 -101
  110. package/src/input/page-control.ts +43 -43
  111. package/src/input/page-input.ts +193 -193
  112. package/src/input/player-remaps.ts +77 -77
  113. package/src/input/source-values.ts +116 -116
  114. package/src/input/spatial.ts +47 -47
  115. package/src/input/types.ts +331 -331
  116. package/src/input/zero-config.ts +177 -177
  117. package/src/input.ts +22 -22
  118. package/src/mp.ts +10 -10
  119. package/src/overlay/controls-panel.ts +163 -163
  120. package/src/overlay/dom.ts +46 -46
  121. package/src/overlay/layout-clusters.ts +162 -162
  122. package/src/overlay/layout-geometry.ts +136 -136
  123. package/src/overlay/layout.ts +203 -203
  124. package/src/overlay/listener-scope.ts +23 -23
  125. package/src/overlay/on-screen-keyboard.ts +137 -137
  126. package/src/overlay/overlay.ts +208 -208
  127. package/src/overlay/styles.ts +9 -9
  128. package/src/overlay/virtual-controls.ts +262 -262
  129. package/src/race.ts +10 -10
  130. package/src/realtime/bridge.ts +126 -121
  131. package/src/realtime/cross-bundle.ts +29 -29
  132. package/src/realtime/fallback-notice.ts +48 -48
  133. package/src/realtime/hub-link.ts +65 -65
  134. package/src/realtime/latest-state.ts +33 -33
  135. package/src/realtime/multiplayer-forwarder.ts +40 -28
  136. package/src/realtime/multiplayer-types.ts +66 -52
  137. package/src/realtime/page-runtime.ts +60 -60
  138. package/src/realtime/race-forwarder.ts +59 -59
  139. package/src/realtime/race-types.ts +132 -132
  140. package/src/realtime/room-types.ts +151 -125
  141. package/src/realtime/rooms-forwarder.ts +91 -79
  142. package/src/realtime/runtime-api.ts +60 -49
  143. package/src/realtime/runtime-opener.ts +61 -61
  144. package/src/realtime/social-forwarder.ts +77 -72
  145. package/src/realtime/social-types.ts +86 -78
  146. package/src/realtime/types.ts +53 -53
  147. package/src/realtime/wire-types.ts +79 -72
  148. package/src/rooms.ts +10 -10
  149. package/src/social.ts +18 -18
  150. package/src/types.ts +13 -13
  151. package/src/uistack/page-stack.ts +45 -45
  152. package/src/uistack/play-suspension.ts +82 -82
  153. package/src/uistack/types.ts +76 -76
  154. package/src/uistack/ui-stack.ts +144 -144
@@ -1,79 +1,91 @@
1
- /**
2
- * `hub.rooms`: hub relay rooms, forwarded to the social runtime. Each room is
3
- * wrapped so its rejections use the core client's error classes.
4
- */
5
- import type { Json, RoomInfo } from '@diffenderfer-games/hub-contract';
6
- import { pageBridge, rethrowAsHubError } from './bridge';
7
- import type { HubRoom, HubRoomsApi, RoomEvents, RoomSelf, RoomSendOptions, RoomState, RoomStateOptions } from './room-types';
8
-
9
- /** A runtime room whose rejections are re-created as the core client's errors. */
10
- class ForwardedRoom implements HubRoom {
11
- private readonly room: HubRoom;
12
-
13
- constructor(room: HubRoom) {
14
- this.room = room;
15
- }
16
-
17
- get info(): RoomInfo {
18
- return this.room.info;
19
- }
20
-
21
- get state(): RoomState {
22
- return this.room.state;
23
- }
24
-
25
- get me(): RoomSelf {
26
- return this.room.me;
27
- }
28
-
29
- on<Name extends keyof RoomEvents>(event: Name, listener: (payload: RoomEvents[Name]) => void): () => void {
30
- return this.room.on(event, listener);
31
- }
32
-
33
- ready(isReady: boolean): Promise<void> {
34
- return rethrowAsHubError(this.room.ready(isReady));
35
- }
36
-
37
- seat(seatData: Json): Promise<void> {
38
- return rethrowAsHubError(this.room.seat(seatData));
39
- }
40
-
41
- start(): Promise<void> {
42
- return rethrowAsHubError(this.room.start());
43
- }
44
-
45
- send(message: Json, options?: RoomSendOptions): Promise<void> {
46
- return rethrowAsHubError(this.room.send(message, options));
47
- }
48
-
49
- setState(patch: RoomState, options?: RoomStateOptions): Promise<void> {
50
- return rethrowAsHubError(this.room.setState(patch, options));
51
- }
52
-
53
- chat(text: string): Promise<string> {
54
- return rethrowAsHubError(this.room.chat(text));
55
- }
56
-
57
- quick(phraseId: string): Promise<void> {
58
- return rethrowAsHubError(this.room.quick(phraseId));
59
- }
60
-
61
- kick(userId: number): Promise<void> {
62
- return rethrowAsHubError(this.room.kick(userId));
63
- }
64
-
65
- leave(): Promise<void> {
66
- return rethrowAsHubError(this.room.leave());
67
- }
68
-
69
- end(results?: Json): Promise<void> {
70
- return rethrowAsHubError(this.room.end(results));
71
- }
72
- }
73
-
74
- /** `hub.rooms`: hub relay rooms. */
75
- export const roomsForwarder: HubRoomsApi = {
76
- create: (options) => pageBridge.call(async (realtime) => new ForwardedRoom(await realtime.rooms.create(options))),
77
- join: (codeOrId, options) => pageBridge.call(async (realtime) => new ForwardedRoom(await realtime.rooms.join(codeOrId, options))),
78
- list: (options) => pageBridge.call((realtime) => realtime.rooms.list(options)),
79
- };
1
+ /**
2
+ * `hub.rooms`: hub relay rooms, forwarded to the social runtime. Each room is
3
+ * wrapped so its rejections use the core client's error classes.
4
+ */
5
+ import type { Json, RoomInfo } from '@diffenderfer-games/hub-contract';
6
+ import { outdatedRuntime, pageBridge, rethrowAsHubError } from './bridge';
7
+ import type { HubRoom, HubRoomsApi, RoomEvents, RoomSelf, RoomSendOptions, RoomState, RoomStateOptions } from './room-types';
8
+ import type { RoomSeat } from './wire-types';
9
+
10
+ /** A runtime room whose rejections are re-created as the core client's errors. */
11
+ class ForwardedRoom implements HubRoom {
12
+ private readonly room: HubRoom;
13
+
14
+ constructor(room: HubRoom) {
15
+ this.room = room;
16
+ }
17
+
18
+ get info(): RoomInfo {
19
+ return this.room.info;
20
+ }
21
+
22
+ get state(): RoomState {
23
+ return this.room.state;
24
+ }
25
+
26
+ get me(): RoomSelf {
27
+ return this.room.me;
28
+ }
29
+
30
+ seats(): RoomSeat[] {
31
+ return this.room.info.seats;
32
+ }
33
+
34
+ on<Name extends keyof RoomEvents>(event: Name, listener: (payload: RoomEvents[Name]) => void): () => void {
35
+ return this.room.on(event, listener);
36
+ }
37
+
38
+ ready(isReady: boolean): Promise<void> {
39
+ return rethrowAsHubError(this.room.ready(isReady));
40
+ }
41
+
42
+ seat(seatData: Json): Promise<void> {
43
+ return rethrowAsHubError(this.room.seat(seatData));
44
+ }
45
+
46
+ start(): Promise<void> {
47
+ return rethrowAsHubError(this.room.start());
48
+ }
49
+
50
+ send(message: Json, options?: RoomSendOptions): Promise<void> {
51
+ return rethrowAsHubError(this.room.send(message, options));
52
+ }
53
+
54
+ setState(patch: RoomState, options?: RoomStateOptions): Promise<void> {
55
+ return rethrowAsHubError(this.room.setState(patch, options));
56
+ }
57
+
58
+ chat(text: string): Promise<string> {
59
+ return rethrowAsHubError(this.room.chat(text));
60
+ }
61
+
62
+ quick(phraseId: string): Promise<void> {
63
+ return rethrowAsHubError(this.room.quick(phraseId));
64
+ }
65
+
66
+ kick(userId: number): Promise<void> {
67
+ return rethrowAsHubError(this.room.kick(userId));
68
+ }
69
+
70
+ leave(): Promise<void> {
71
+ return rethrowAsHubError(this.room.leave());
72
+ }
73
+
74
+ end(results?: Json): Promise<void> {
75
+ return rethrowAsHubError(this.room.end(results));
76
+ }
77
+ }
78
+
79
+ /** `hub.rooms`: hub relay rooms. */
80
+ export const roomsForwarder: HubRoomsApi = {
81
+ create: (options) => pageBridge.call(async (realtime) => new ForwardedRoom(await realtime.rooms.create(options))),
82
+ join: (codeOrId, options) => pageBridge.call(async (realtime) => new ForwardedRoom(await realtime.rooms.join(codeOrId, options))),
83
+ list: (options) => pageBridge.call((realtime) => realtime.rooms.list(options)),
84
+ reclaim: () => pageBridge.call(async (realtime) => {
85
+ if (realtime.rooms.reclaim === undefined) {
86
+ throw outdatedRuntime('Taking back a seat');
87
+ }
88
+ const held = await realtime.rooms.reclaim();
89
+ return held === null ? null : new ForwardedRoom(held);
90
+ }),
91
+ };
@@ -1,49 +1,60 @@
1
- /**
2
- * The realtime API the hub's social runtime (`/_hub/social.js`) installs on
3
- * the page, and that `hub.presence`, `hub.social`, `hub.mp`, `hub.rooms`,
4
- * `hub.notify` and `hub.race` forward to.
5
- */
6
- import type { HubMpApi } from './multiplayer-types';
7
- import type { HubRaceApi } from './race-types';
8
- import type { HubRoomsApi } from './room-types';
9
- import type { HubNotifyApi, HubPresenceApi, HubSocialApi } from './social-types';
10
-
11
- /**
12
- * The realtime feature version the runtime reports in {@link RealtimeApi.version}.
13
- * 2 added races; a client feature-detects newer additions with it.
14
- */
15
- export const REALTIME_FEATURE_VERSION = 2;
16
-
17
- /**
18
- * The cross-bundle shape number of the `rt` page singleton (plan §4.4). The
19
- * runtime registers with it through `claimSingleton`; bump it only when the
20
- * shape of {@link RealtimeApi} changes incompatibly.
21
- */
22
- export const REALTIME_SINGLETON_API = 1;
23
-
24
- /** The window event the runtime fires once it has installed itself. */
25
- export const REALTIME_READY_EVENT = 'hub:rt-ready';
26
-
27
- /** The window event the menu fires when the runtime failed to load (its reason is on `window.__HUB_RT_ERROR__`). */
28
- export const REALTIME_FAILED_EVENT = 'hub:rt-failed';
29
-
30
- /**
31
- * Installed by the social runtime as the page's `rt` singleton (and the v1
32
- * alias `window.__HUB_RT__`).
33
- */
34
- export interface RealtimeApi {
35
- /** The runtime's feature version (see {@link REALTIME_FEATURE_VERSION}). */
36
- readonly version: number;
37
- /** Presence reporting. */
38
- presence: HubPresenceApi;
39
- /** Friends and the social dialogs. */
40
- social: HubSocialApi;
41
- /** Invites, launches, the leave guard and tickets. */
42
- mp: HubMpApi;
43
- /** Hub relay rooms. */
44
- rooms: HubRoomsApi;
45
- /** Toast behaviour. */
46
- notify: HubNotifyApi;
47
- /** Races (feature version 2 and later). */
48
- race?: HubRaceApi;
49
- }
1
+ /**
2
+ * The realtime API the hub's social runtime (`/_hub/social.js`) installs on
3
+ * the page, and that `hub.presence`, `hub.social`, `hub.mp`, `hub.rooms`,
4
+ * `hub.notify` and `hub.race` forward to.
5
+ */
6
+ import type { HubMpApi, InviteLinkMethods } from './multiplayer-types';
7
+ import type { HubRaceApi } from './race-types';
8
+ import type { HubRoomsApi, RoomReclaimMethods } from './room-types';
9
+ import type { HubNotifyApi, HubPresenceApi, HubSocialApi, PlayOnlineMethods } from './social-types';
10
+
11
+ /**
12
+ * The realtime feature version the runtime reports in {@link RealtimeApi.version}.
13
+ * 2 added races; 3 added invite links and taking back a held seat; 4 added
14
+ * "Play online" (tournaments). A client feature-detects newer additions with
15
+ * it (or by the optional method).
16
+ */
17
+ export const REALTIME_FEATURE_VERSION = 4;
18
+
19
+ /** `hub.social` as a runtime installs it: version 4's `openPlayOnline` is missing on older runtimes. */
20
+ export type RuntimeSocialApi = Omit<HubSocialApi, keyof PlayOnlineMethods> & Partial<PlayOnlineMethods>;
21
+
22
+ /** `hub.mp` as a runtime installs it: version 3's invite-link methods are missing on older runtimes. */
23
+ export type RuntimeMpApi = Omit<HubMpApi, keyof InviteLinkMethods> & Partial<InviteLinkMethods>;
24
+
25
+ /** `hub.rooms` as a runtime installs it: version 3's `reclaim` is missing on older runtimes. */
26
+ export type RuntimeRoomsApi = Omit<HubRoomsApi, keyof RoomReclaimMethods> & Partial<RoomReclaimMethods>;
27
+
28
+ /**
29
+ * The cross-bundle shape number of the `rt` page singleton (plan §4.4). The
30
+ * runtime registers with it through `claimSingleton`; bump it only when the
31
+ * shape of {@link RealtimeApi} changes incompatibly.
32
+ */
33
+ export const REALTIME_SINGLETON_API = 1;
34
+
35
+ /** The window event the runtime fires once it has installed itself. */
36
+ export const REALTIME_READY_EVENT = 'hub:rt-ready';
37
+
38
+ /** The window event the menu fires when the runtime failed to load (its reason is on `window.__HUB_RT_ERROR__`). */
39
+ export const REALTIME_FAILED_EVENT = 'hub:rt-failed';
40
+
41
+ /**
42
+ * Installed by the social runtime as the page's `rt` singleton (and the v1
43
+ * alias `window.__HUB_RT__`).
44
+ */
45
+ export interface RealtimeApi {
46
+ /** The runtime's feature version (see {@link REALTIME_FEATURE_VERSION}). */
47
+ readonly version: number;
48
+ /** Presence reporting. */
49
+ presence: HubPresenceApi;
50
+ /** Friends and the social dialogs. */
51
+ social: RuntimeSocialApi;
52
+ /** Invites, launches, the leave guard and tickets. */
53
+ mp: RuntimeMpApi;
54
+ /** Hub relay rooms. */
55
+ rooms: RuntimeRoomsApi;
56
+ /** Toast behaviour. */
57
+ notify: HubNotifyApi;
58
+ /** Races (feature version 2 and later). */
59
+ race?: HubRaceApi;
60
+ }
@@ -1,61 +1,61 @@
1
- /**
2
- * Opening the runtime's UI (a player card, the friends drawer, the race
3
- * sheet) from a game button. A tap must never look dead: if the runtime isn't
4
- * there within a moment the player is told it is loading, and if it never
5
- * arrives, how to recover.
6
- */
7
- import type { RealtimeBridge } from './bridge';
8
- import { hideFallbackNotice, showFallbackNotice } from './fallback-notice';
9
- import { readRuntimeFailure } from './page-runtime';
10
- import type { RealtimeApi } from './runtime-api';
11
- import { REALTIME_FAILED_EVENT } from './runtime-api';
12
-
13
- /** How long before "still loading" shows. */
14
- const SLOW_NOTICE_MS = 1500;
15
- /** How long the failure notice stays up. */
16
- const FAILURE_NOTICE_MS = 8000;
17
-
18
- /** Tell the player the social runtime failed, with the reason the menu recorded. */
19
- function showRuntimeFailure(): void {
20
- const reason = readRuntimeFailure();
21
- showFallbackNotice(`Friends couldn't load${reason === '' ? '' : ` (${reason})`}. Reload the page to try again.`, FAILURE_NOTICE_MS);
22
- }
23
-
24
- /** Make an opener: runs a UI call on the runtime once it is up, with the loading and failure notices. */
25
- export function createRuntimeOpener(bridge: RealtimeBridge): (openUi: (realtime: RealtimeApi) => void) => void {
26
- return (openUi) => {
27
- const loaded = bridge.current();
28
- if (loaded !== null) {
29
- openUi(loaded);
30
- return;
31
- }
32
- // The menu already knows the runtime failed.
33
- if (readRuntimeFailure() !== '') {
34
- showRuntimeFailure();
35
- return;
36
- }
37
- const slowTimer = setTimeout(() => {
38
- showFallbackNotice('Friends are still loading…');
39
- }, SLOW_NOTICE_MS);
40
- let isSettled = false;
41
- const onFailed = (): void => {
42
- if (!isSettled) {
43
- isSettled = true;
44
- clearTimeout(slowTimer);
45
- showRuntimeFailure();
46
- }
47
- };
48
- if (typeof window !== 'undefined') {
49
- window.addEventListener(REALTIME_FAILED_EVENT, onFailed, { once: true });
50
- }
51
- bridge.whenReady().then((realtime) => {
52
- if (isSettled) {
53
- return;
54
- }
55
- isSettled = true;
56
- clearTimeout(slowTimer);
57
- hideFallbackNotice();
58
- openUi(realtime);
59
- }, onFailed);
60
- };
61
- }
1
+ /**
2
+ * Opening the runtime's UI (a player card, the friends drawer, the race
3
+ * sheet) from a game button. A tap must never look dead: if the runtime isn't
4
+ * there within a moment the player is told it is loading, and if it never
5
+ * arrives, how to recover.
6
+ */
7
+ import type { RealtimeBridge } from './bridge';
8
+ import { hideFallbackNotice, showFallbackNotice } from './fallback-notice';
9
+ import { readRuntimeFailure } from './page-runtime';
10
+ import type { RealtimeApi } from './runtime-api';
11
+ import { REALTIME_FAILED_EVENT } from './runtime-api';
12
+
13
+ /** How long before "still loading" shows. */
14
+ const SLOW_NOTICE_MS = 1500;
15
+ /** How long the failure notice stays up. */
16
+ const FAILURE_NOTICE_MS = 8000;
17
+
18
+ /** Tell the player the social runtime failed, with the reason the menu recorded. */
19
+ function showRuntimeFailure(): void {
20
+ const reason = readRuntimeFailure();
21
+ showFallbackNotice(`Friends couldn't load${reason === '' ? '' : ` (${reason})`}. Reload the page to try again.`, FAILURE_NOTICE_MS);
22
+ }
23
+
24
+ /** Make an opener: runs a UI call on the runtime once it is up, with the loading and failure notices. */
25
+ export function createRuntimeOpener(bridge: RealtimeBridge): (openUi: (realtime: RealtimeApi) => void) => void {
26
+ return (openUi) => {
27
+ const loaded = bridge.current();
28
+ if (loaded !== null) {
29
+ openUi(loaded);
30
+ return;
31
+ }
32
+ // The menu already knows the runtime failed.
33
+ if (readRuntimeFailure() !== '') {
34
+ showRuntimeFailure();
35
+ return;
36
+ }
37
+ const slowTimer = setTimeout(() => {
38
+ showFallbackNotice('Friends are still loading…');
39
+ }, SLOW_NOTICE_MS);
40
+ let isSettled = false;
41
+ const onFailed = (): void => {
42
+ if (!isSettled) {
43
+ isSettled = true;
44
+ clearTimeout(slowTimer);
45
+ showRuntimeFailure();
46
+ }
47
+ };
48
+ if (typeof window !== 'undefined') {
49
+ window.addEventListener(REALTIME_FAILED_EVENT, onFailed, { once: true });
50
+ }
51
+ bridge.whenReady().then((realtime) => {
52
+ if (isSettled) {
53
+ return;
54
+ }
55
+ isSettled = true;
56
+ clearTimeout(slowTimer);
57
+ hideFallbackNotice();
58
+ openUi(realtime);
59
+ }, onFailed);
60
+ };
61
+ }
@@ -1,72 +1,77 @@
1
- /**
2
- * `hub.presence`, `hub.social` and `hub.notify`: thin forwarders to the
3
- * social runtime. Presence and quiet mode are "latest state" calls,
4
- * remembered and replayed when the runtime loads.
5
- */
6
- import { pageBridge } from './bridge';
7
- import { latestState } from './latest-state';
8
- import { createRuntimeOpener } from './runtime-opener';
9
- import type { HubNotifyApi, HubPresenceApi, HubSocialApi, SocialCounts } from './social-types';
10
-
11
- const openWithRuntime = createRuntimeOpener(pageBridge);
12
-
13
- /** Counts before the runtime is up. */
14
- const NO_COUNTS: SocialCounts = { online: 0, inGame: 0, friendsOnline: 0 };
15
-
16
- /** `hub.presence`: what you're doing, shown to friends. */
17
- export const presenceForwarder: HubPresenceApi = {
18
- set(presence) {
19
- latestState.presence = { ...presence };
20
- pageBridge.current()?.presence.set(latestState.presence);
21
- },
22
- clear() {
23
- latestState.presence = { kind: 'menu' };
24
- pageBridge.current()?.presence.clear();
25
- },
26
- };
27
-
28
- /** `hub.notify`: toast behaviour. */
29
- export const notifyForwarder: HubNotifyApi = {
30
- setQuiet(isQuiet) {
31
- latestState.isQuiet = isQuiet;
32
- pageBridge.current()?.notify.setQuiet(isQuiet);
33
- },
34
- };
35
-
36
- /** `hub.social`: friends and the hub's social dialogs. */
37
- export const socialForwarder: HubSocialApi = {
38
- openPlayer: (ref) => {
39
- openWithRuntime((realtime) => {
40
- realtime.social.openPlayer(ref);
41
- });
42
- },
43
- openFriends: () => {
44
- openWithRuntime((realtime) => {
45
- realtime.social.openFriends();
46
- });
47
- },
48
- openChat: (userId) => {
49
- openWithRuntime((realtime) => {
50
- realtime.social.openChat(userId);
51
- });
52
- },
53
- openInvite: (options) => {
54
- openWithRuntime((realtime) => {
55
- realtime.social.openInvite(options);
56
- });
57
- },
58
- openSuggest: () => {
59
- openWithRuntime((realtime) => {
60
- realtime.social.openSuggest();
61
- });
62
- },
63
- openNotify: (options) => {
64
- openWithRuntime((realtime) => {
65
- realtime.social.openNotify(options);
66
- });
67
- },
68
- friends: () => pageBridge.call((realtime) => realtime.social.friends()),
69
- counts: () => pageBridge.current()?.social.counts() ?? { ...NO_COUNTS },
70
- chatMode: (userIds) => pageBridge.current()?.social.chatMode(userIds) ?? 'off',
71
- on: (event, listener) => pageBridge.subscribe((realtime) => realtime.social.on(event, listener)),
72
- };
1
+ /**
2
+ * `hub.presence`, `hub.social` and `hub.notify`: thin forwarders to the
3
+ * social runtime. Presence and quiet mode are "latest state" calls,
4
+ * remembered and replayed when the runtime loads.
5
+ */
6
+ import { pageBridge } from './bridge';
7
+ import { latestState } from './latest-state';
8
+ import { createRuntimeOpener } from './runtime-opener';
9
+ import type { HubNotifyApi, HubPresenceApi, HubSocialApi, SocialCounts } from './social-types';
10
+
11
+ const openWithRuntime = createRuntimeOpener(pageBridge);
12
+
13
+ /** Counts before the runtime is up. */
14
+ const NO_COUNTS: SocialCounts = { online: 0, inGame: 0, friendsOnline: 0 };
15
+
16
+ /** `hub.presence`: what you're doing, shown to friends. */
17
+ export const presenceForwarder: HubPresenceApi = {
18
+ set(presence) {
19
+ latestState.presence = { ...presence };
20
+ pageBridge.current()?.presence.set(latestState.presence);
21
+ },
22
+ clear() {
23
+ latestState.presence = { kind: 'menu' };
24
+ pageBridge.current()?.presence.clear();
25
+ },
26
+ };
27
+
28
+ /** `hub.notify`: toast behaviour. */
29
+ export const notifyForwarder: HubNotifyApi = {
30
+ setQuiet(isQuiet) {
31
+ latestState.isQuiet = isQuiet;
32
+ pageBridge.current()?.notify.setQuiet(isQuiet);
33
+ },
34
+ };
35
+
36
+ /** `hub.social`: friends and the hub's social dialogs. */
37
+ export const socialForwarder: HubSocialApi = {
38
+ openPlayer: (ref) => {
39
+ openWithRuntime((realtime) => {
40
+ realtime.social.openPlayer(ref);
41
+ });
42
+ },
43
+ openFriends: () => {
44
+ openWithRuntime((realtime) => {
45
+ realtime.social.openFriends();
46
+ });
47
+ },
48
+ openChat: (userId) => {
49
+ openWithRuntime((realtime) => {
50
+ realtime.social.openChat(userId);
51
+ });
52
+ },
53
+ openInvite: (options) => {
54
+ openWithRuntime((realtime) => {
55
+ realtime.social.openInvite(options);
56
+ });
57
+ },
58
+ openSuggest: () => {
59
+ openWithRuntime((realtime) => {
60
+ realtime.social.openSuggest();
61
+ });
62
+ },
63
+ openNotify: (options) => {
64
+ openWithRuntime((realtime) => {
65
+ realtime.social.openNotify(options);
66
+ });
67
+ },
68
+ openPlayOnline: () => {
69
+ openWithRuntime((realtime) => {
70
+ realtime.social.openPlayOnline?.();
71
+ });
72
+ },
73
+ friends: () => pageBridge.call((realtime) => realtime.social.friends()),
74
+ counts: () => pageBridge.current()?.social.counts() ?? { ...NO_COUNTS },
75
+ chatMode: (userIds) => pageBridge.current()?.social.chatMode(userIds) ?? 'off',
76
+ on: (event, listener) => pageBridge.subscribe((realtime) => realtime.social.on(event, listener)),
77
+ };