@devvit/public-api 0.10.23-next-2024-07-02-fe7059b4e.0 → 0.10.23-next-2024-07-23-7552e0bd4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. package/apis/AssetsClient/AssetsClient.d.ts +0 -6
  2. package/apis/AssetsClient/AssetsClient.d.ts.map +1 -1
  3. package/apis/AssetsClient/AssetsClient.js +12 -26
  4. package/apis/makeAPIClients.d.ts.map +1 -1
  5. package/apis/makeAPIClients.js +2 -2
  6. package/apis/realtime/RealtimeClient.d.ts +2 -2
  7. package/apis/realtime/RealtimeClient.d.ts.map +1 -1
  8. package/apis/realtime/RealtimeClient.js +2 -2
  9. package/apis/reddit/RedditAPIClient.d.ts +2 -10
  10. package/apis/reddit/RedditAPIClient.d.ts.map +1 -1
  11. package/apis/reddit/RedditAPIClient.js +1 -11
  12. package/apis/reddit/models/Comment.d.ts +0 -2
  13. package/apis/reddit/models/Comment.d.ts.map +1 -1
  14. package/apis/reddit/models/Comment.js +2 -12
  15. package/apis/reddit/models/Post.d.ts +0 -2
  16. package/apis/reddit/models/Post.d.ts.map +1 -1
  17. package/apis/reddit/models/Post.js +2 -12
  18. package/apis/reddit/models/Subreddit.d.ts +1 -17
  19. package/apis/reddit/models/Subreddit.d.ts.map +1 -1
  20. package/apis/reddit/models/Subreddit.js +0 -15
  21. package/apis/reddit/models/User.d.ts.map +1 -1
  22. package/apis/reddit/models/User.js +2 -1
  23. package/apis/scheduler/SchedulerClient.d.ts +1 -1
  24. package/apis/scheduler/SchedulerClient.d.ts.map +1 -1
  25. package/apis/ui/UIClient.d.ts +3 -3
  26. package/apis/ui/UIClient.d.ts.map +1 -1
  27. package/apis/ui/helpers/assertValidFormFields.d.ts +2 -2
  28. package/apis/ui/helpers/assertValidFormFields.d.ts.map +1 -1
  29. package/apis/ui/helpers/getFormValues.d.ts +2 -2
  30. package/apis/ui/helpers/getFormValues.d.ts.map +1 -1
  31. package/apis/ui/helpers/getFormValues.js +1 -3
  32. package/apis/ui/helpers/transformForm.d.ts +1 -1
  33. package/apis/ui/helpers/transformForm.d.ts.map +1 -1
  34. package/devvit/Devvit.d.ts +6 -8
  35. package/devvit/Devvit.d.ts.map +1 -1
  36. package/devvit/Devvit.js +2 -10
  37. package/devvit/internals/blocks/BlocksReconciler.d.ts.map +1 -1
  38. package/devvit/internals/blocks/BlocksReconciler.js +5 -25
  39. package/devvit/internals/blocks/BlocksTransformer.d.ts +2 -2
  40. package/devvit/internals/blocks/BlocksTransformer.d.ts.map +1 -1
  41. package/devvit/internals/blocks/BlocksTransformer.js +3 -3
  42. package/devvit/internals/blocks/handler/BlocksHandler.d.ts +0 -4
  43. package/devvit/internals/blocks/handler/BlocksHandler.d.ts.map +1 -1
  44. package/devvit/internals/blocks/handler/BlocksHandler.js +9 -33
  45. package/devvit/internals/blocks/handler/ContextBuilder.d.ts.map +1 -1
  46. package/devvit/internals/blocks/handler/ContextBuilder.js +8 -3
  47. package/devvit/internals/blocks/handler/RenderContext.d.ts +1 -5
  48. package/devvit/internals/blocks/handler/RenderContext.d.ts.map +1 -1
  49. package/devvit/internals/blocks/handler/RenderContext.js +3 -30
  50. package/devvit/internals/blocks/handler/UIClient.d.ts +4 -4
  51. package/devvit/internals/blocks/handler/UIClient.d.ts.map +1 -1
  52. package/devvit/internals/blocks/handler/test-helpers.d.ts +0 -1
  53. package/devvit/internals/blocks/handler/test-helpers.d.ts.map +1 -1
  54. package/devvit/internals/blocks/handler/test-helpers.js +0 -3
  55. package/devvit/internals/blocks/handler/types.d.ts +9 -15
  56. package/devvit/internals/blocks/handler/types.d.ts.map +1 -1
  57. package/devvit/internals/blocks/handler/useAsync.d.ts.map +1 -1
  58. package/devvit/internals/blocks/handler/useAsync.js +13 -17
  59. package/devvit/internals/blocks/handler/useChannel.d.ts +1 -2
  60. package/devvit/internals/blocks/handler/useChannel.d.ts.map +1 -1
  61. package/devvit/internals/blocks/handler/useChannel.js +13 -49
  62. package/devvit/internals/blocks/handler/useForm.d.ts +5 -18
  63. package/devvit/internals/blocks/handler/useForm.d.ts.map +1 -1
  64. package/devvit/internals/blocks/handler/useForm.js +6 -3
  65. package/devvit/internals/blocks/handler/useState.d.ts +1 -1
  66. package/devvit/internals/blocks/handler/useState.d.ts.map +1 -1
  67. package/devvit/internals/blocks/handler/useState.js +14 -14
  68. package/devvit/internals/blocks/useChannel.d.ts.map +1 -1
  69. package/devvit/internals/blocks/useChannel.js +2 -15
  70. package/devvit/internals/blocks/useForm.d.ts.map +1 -1
  71. package/devvit/internals/context.d.ts.map +1 -1
  72. package/devvit/internals/context.js +13 -16
  73. package/index.d.ts +0 -1
  74. package/index.d.ts.map +1 -1
  75. package/index.js +0 -1
  76. package/meta.json +309 -311
  77. package/meta.min.json +116 -186
  78. package/package.json +8 -8
  79. package/public-api.d.ts +1750 -2042
  80. package/public-api.iife.js +7282 -7007
  81. package/public-api.min.js +5 -5
  82. package/public-api.min.js.map +4 -4
  83. package/types/context.d.ts +3 -5
  84. package/types/context.d.ts.map +1 -1
  85. package/types/data.d.ts +3 -3
  86. package/types/data.d.ts.map +1 -1
  87. package/types/form.d.ts +17 -28
  88. package/types/form.d.ts.map +1 -1
  89. package/types/hooks.d.ts +7 -37
  90. package/types/hooks.d.ts.map +1 -1
  91. package/types/realtime.d.ts +3 -3
  92. package/types/realtime.d.ts.map +1 -1
  93. package/types/scheduler.d.ts +14 -15
  94. package/types/scheduler.d.ts.map +1 -1
  95. package/types/ui-client.d.ts +4 -4
  96. package/types/ui-client.d.ts.map +1 -1
  97. package/devvit/internals/blocks/handler/cache.d.ts +0 -9
  98. package/devvit/internals/blocks/handler/cache.d.ts.map +0 -1
  99. package/devvit/internals/blocks/handler/cache.js +0 -5
  100. package/devvit/internals/blocks/handler/cache.test.d.ts.map +0 -1
  101. package/devvit/internals/blocks/handler/promise_cache.d.ts +0 -67
  102. package/devvit/internals/blocks/handler/promise_cache.d.ts.map +0 -1
  103. package/devvit/internals/blocks/handler/promise_cache.js +0 -239
  104. package/types/context.test.d.ts.map +0 -1
  105. package/version.json +0 -75
@@ -10,10 +10,6 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _RenderContext_state;
13
- /** @internal */
14
- export function _isTombstone(value) {
15
- return typeof value === 'object' && value !== null && '__deleted' in value;
16
- }
17
13
  /**
18
14
  * The RenderContext is a class that holds the state of the rendering process.
19
15
  *
@@ -49,37 +45,16 @@ export class RenderContext {
49
45
  this._undeliveredHandlers = {};
50
46
  this.request = request;
51
47
  this.meta = meta;
52
- __classPrivateFieldSet(this, _RenderContext_state, request.state ?? {
53
- __cache: {},
54
- }, "f");
48
+ __classPrivateFieldSet(this, _RenderContext_state, request.state ?? {}, "f");
55
49
  this._rootProps = request.props ?? {};
56
50
  }
57
51
  /** The state delta new to this render. */
58
52
  get _changedState() {
59
- const changed = {
60
- __cache: __classPrivateFieldGet(this, _RenderContext_state, "f").__cache ?? {},
61
- };
53
+ const changed = {};
62
54
  for (const key in this._changed)
63
55
  changed[key] = this._state[key];
64
- const unmounted = new Set(Object.keys(this._state));
65
- Object.keys(this._hooks).forEach((key) => {
66
- if (key === '__cache') {
67
- return;
68
- }
69
- unmounted.delete(key);
70
- });
71
- unmounted.forEach((key) => {
72
- if (key === '__cache') {
73
- return;
74
- }
75
- const t = { __deleted: true };
76
- this._state[key] = changed[key] = t;
77
- });
78
56
  return changed;
79
57
  }
80
- get hooks() {
81
- return this._hooks;
82
- }
83
58
  /** The complete render state. */
84
59
  get _state() {
85
60
  return __classPrivateFieldGet(this, _RenderContext_state, "f");
@@ -87,7 +62,6 @@ export class RenderContext {
87
62
  /** Replacing state resets the delta for the next render. */
88
63
  set _state(state) {
89
64
  this._changed = {};
90
- this._hooks = {};
91
65
  __classPrivateFieldSet(this, _RenderContext_state, state, "f");
92
66
  }
93
67
  push(options) {
@@ -105,7 +79,6 @@ export class RenderContext {
105
79
  getHook(ref) {
106
80
  return this._hooks[ref.id];
107
81
  }
108
- /** Catches events with no active handler and routes to the corresponding hook to detach/unsubscribe/etc **/
109
82
  static addGlobalUndeliveredEventHandler(id, handler) {
110
83
  RenderContext._staticUndeliveredHandlers[id] = handler;
111
84
  }
@@ -151,7 +124,7 @@ export class RenderContext {
151
124
  const builder = [];
152
125
  /**
153
126
  * We need to build the hook id from the segments in reverse order, because an explicit id
154
- * overrides parent path info.
127
+ * overrrides parent path info.
155
128
  */
156
129
  for (let i = this._segments.length - 1; i >= 0; i--) {
157
130
  const segment = this._segments[i];
@@ -1,14 +1,14 @@
1
- import type { JSONObject } from '@devvit/shared-types/json.js';
2
- import type { FormKey } from '@devvit/shared-types/useForm.js';
3
- import type { Comment, Post, Subreddit, User } from '../../../../apis/reddit/models/index.js';
1
+ import type { Data } from '../../../../types/data.js';
2
+ import type { FormKey } from '../../../../types/form.js';
4
3
  import type { Toast } from '../../../../types/toast.js';
5
4
  import type { UIClient as _UIClient } from '../../../../types/ui-client.js';
5
+ import type { Comment, Post, Subreddit, User } from '../../../../apis/reddit/models/index.js';
6
6
  import type { RenderContext } from './RenderContext.js';
7
7
  export declare function useUI(): _UIClient;
8
8
  export declare class UIClient implements _UIClient {
9
9
  #private;
10
10
  constructor(renderContext: RenderContext);
11
- showForm(formKey: FormKey, data?: JSONObject | undefined): void;
11
+ showForm(formKey: FormKey, data?: Data | undefined): void;
12
12
  showToast(text: string): void;
13
13
  showToast(toast: Toast): void;
14
14
  navigateTo(url: string): void;
@@ -1 +1 @@
1
- {"version":3,"file":"UIClient.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/UIClient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,yCAAyC,CAA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gB,KAAK,IAAI,SAAS,CAMjC;AAED,qBAAa,QAAS,YAAW,SAAS;;gBAG5B,aAAa,EAAE,aAAa;IAIxC,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,SAAS,GAAG,IAAI;IA+BzD,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAC7B,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAwB7B,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAC7B,UAAU,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI;IACtC,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAC5B,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAClC,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;CAiB7B"}
@@ -3,7 +3,6 @@ import type { JSONValue } from '@devvit/shared-types/json.js';
3
3
  import type { BlocksState, HookRef } from './types.js';
4
4
  export declare const findHookId: (ref: HookRef) => string;
5
5
  export declare const findHookState: (ref: HookRef) => JSONValue;
6
- export declare const findHookValue: (ref: HookRef) => JSONValue;
7
6
  export declare const getLatestBlocksState: () => BlocksState;
8
7
  export declare const EmptyRequest: UIRequest;
9
8
  export declare const generatePressRequest: (ref: HookRef) => UIRequest;
@@ -1 +1 @@
1
- {"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/test-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,KAAK,EAAc,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAE1E,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEvD,eAAO,MAAM,UAAU,QAAS,OAAO,KAAG,MAEzC,CAAC;AAEF,eAAO,MAAM,aAAa,QAAS,OAAO,KAAG,SAE5C,CAAC;AAEF,eAAO,MAAM,aAAa,QAAS,OAAO,KAAG,SAE5C,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAO,WAEvC,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,SAA0B,CAAC;AAEtD,eAAO,MAAM,oBAAoB,QAAS,OAAO,KAAG,SAWnD,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAS,OAAO,KAAG,SASnD,CAAC;AAEF,eAAO,MAAM,YAAY;;;;;;;;;;CAUxB,CAAC"}
1
+ {"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/test-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAE9D,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEvD,eAAO,MAAM,UAAU,QAAS,OAAO,KAAG,MAEzC,CAAC;AAEF,eAAO,MAAM,aAAa,QAAS,OAAO,KAAG,SAE5C,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAO,WAEvC,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,SAA0B,CAAC;AAEtD,eAAO,MAAM,oBAAoB,QAAS,OAAO,KAAG,SAWnD,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAS,OAAO,KAAG,SASnD,CAAC;AAEF,eAAO,MAAM,YAAY;;;;;;;;;;CAUxB,CAAC"}
@@ -6,9 +6,6 @@ export const findHookId = (ref) => {
6
6
  export const findHookState = (ref) => {
7
7
  return getLatestBlocksState()[ref.id];
8
8
  };
9
- export const findHookValue = (ref) => {
10
- return findHookState(ref)['value'];
11
- };
12
9
  export const getLatestBlocksState = () => {
13
10
  return _latestBlocksHandler?._latestRenderContext?._state ?? {};
14
11
  };
@@ -1,25 +1,19 @@
1
1
  import type { UIEvent } from '@devvit/protos';
2
2
  import type { JSONValue } from '@devvit/shared-types/json.js';
3
- import type { RenderContext, Tombstone } from './RenderContext.js';
3
+ import type { RenderContext } from './RenderContext.js';
4
4
  export type BlocksState = {
5
- [hookID: string]: JSONValue | Tombstone;
5
+ [hookID: string]: JSONValue;
6
6
  };
7
7
  export type Props = {
8
8
  [key: string]: unknown;
9
9
  };
10
10
  export type HookSegment = {
11
11
  /**
12
- * This is usually the name of the hook: useAsync, useAsyncState,
13
- * useChannel:channelName, useForm, useInterval, useState, the block element,
14
- * or the component name (eg, AppToolbar or FooBar).
15
- *
16
- * Namespaces can be used to encode additional data such as logically shared
17
- * instances that would otherwise have to be gathered from Hook instances.
18
- *
19
- * Dashes (-) and dots (.) delimit hook IDs so don't use those in your
20
- * namespace.
12
+ * This is usually the name of the hook: useAsync, useAsyncState, useChannel,
13
+ * useForm, useInterval, useState, etc, the block element, or the component
14
+ * name (eg, AppToolbar or FooBar).
21
15
  */
22
- namespace: string;
16
+ namespace?: string;
23
17
  /**
24
18
  * If the ordering is known, you can provide it here. Else, the key will be a
25
19
  * generated auto-incrementing number.
@@ -43,13 +37,13 @@ export type EventHandler = (event: UIEvent, context: RenderContext) => Promise<v
43
37
  * Syncs state between client and server, responds to events, provides user
44
38
  * API, and transitions state across renders.
45
39
  */
46
- export type Hook = {
40
+ export interface Hook {
47
41
  /**
48
42
  * State to carry across renders. Hook constructor arguments are recreated on
49
43
  * render but state may be passed in the render request and used to prime that
50
44
  * render's hook before onLoad().
51
45
  */
52
- state: JSONValue | Tombstone;
46
+ state: JSONValue;
53
47
  /**
54
48
  * What event callbacks want to hit.
55
49
  */
@@ -65,7 +59,7 @@ export type Hook = {
65
59
  * 5. state gets serialized.
66
60
  */
67
61
  onStateLoaded?(): void;
68
- };
62
+ }
69
63
  export type HookRef = {
70
64
  id?: string;
71
65
  };
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/types.ts"],"names":[],"mappings":"AAAA,OAAO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AAC;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;CAAG"}
@@ -1 +1 @@
1
- {"version":3,"file":"useAsync.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/useAsync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,KAAK,EAAE,wBAAwB,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAQ3F,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,OAAO,CAAC,EAAE,SAAS,CAAC;IAEpB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AA8GF;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,SAAS,EAC1C,WAAW,EAAE,wBAAwB,CAAC,CAAC,CAAC,EACxC,OAAO,GAAE,YAAiB,GACzB,cAAc,CAAC,CAAC,CAAC,CAUnB"}
1
+ {"version":3,"file":"useAsync.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/useAsync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,KAAK,EAAE,wBAAwB,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAQ3F,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,OAAO,CAAC,EAAE,SAAS,CAAC;IAEpB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAuGF;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,SAAS,EAC1C,WAAW,EAAE,wBAAwB,CAAC,CAAC,CAAC,EACxC,OAAO,GAAE,YAAiB,GACzB,cAAc,CAAC,CAAC,CAAC,CAMnB"}
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _AsyncHook_debug, _AsyncHook_hookId, _AsyncHook_initializer, _AsyncHook_invalidate, _AsyncHook_ctx, _AsyncHook_localDepends;
12
+ var _AsyncHook_debug, _AsyncHook_hookId, _AsyncHook_initializer, _AsyncHook_invalidate, _AsyncHook_ctx, _AsyncHook_localDepends, _AsyncHook_enabled;
13
13
  import { CIRCUIT_BREAKER_MSG } from '@devvit/shared-types/CircuitBreaker.js';
14
14
  import isEqual from 'lodash.isequal';
15
15
  import { registerHook } from './BlocksHandler.js';
@@ -21,32 +21,32 @@ class AsyncHook {
21
21
  _AsyncHook_invalidate.set(this, void 0);
22
22
  _AsyncHook_ctx.set(this, void 0);
23
23
  _AsyncHook_localDepends.set(this, void 0);
24
+ _AsyncHook_enabled.set(this, void 0);
24
25
  __classPrivateFieldSet(this, _AsyncHook_debug, !!params.context.devvitContext.debug.useAsync, "f");
25
26
  if (__classPrivateFieldGet(this, _AsyncHook_debug, "f"))
26
27
  console.debug('[useAsync] v1', options);
27
- this.state = { data: null, load_state: 'initial', error: null, depends: null };
28
+ this.state = { data: null, loading: false, error: null, depends: null };
28
29
  __classPrivateFieldSet(this, _AsyncHook_hookId, params.hookId, "f");
29
30
  __classPrivateFieldSet(this, _AsyncHook_initializer, initializer, "f");
30
31
  __classPrivateFieldSet(this, _AsyncHook_invalidate, params.invalidate, "f");
31
32
  __classPrivateFieldSet(this, _AsyncHook_ctx, params.context, "f");
32
33
  __classPrivateFieldSet(this, _AsyncHook_localDepends, options.depends ?? null, "f");
33
- if (!(options.enabled ?? true)) {
34
- this.state.load_state = 'disabled';
35
- }
34
+ __classPrivateFieldSet(this, _AsyncHook_enabled, options.enabled ?? true, "f");
36
35
  }
37
36
  /**
38
37
  * After we look at our state, we need to decide if we need to dispatch a request to load the data.
39
38
  */
40
39
  onStateLoaded() {
41
- if (this.state.load_state === 'disabled') {
40
+ if (!__classPrivateFieldGet(this, _AsyncHook_enabled, "f")) {
42
41
  return;
43
42
  }
44
43
  if (__classPrivateFieldGet(this, _AsyncHook_debug, "f"))
45
44
  console.debug('[useAsync] async onLoad ', __classPrivateFieldGet(this, _AsyncHook_hookId, "f"), this.state);
46
45
  if (__classPrivateFieldGet(this, _AsyncHook_debug, "f"))
47
46
  console.debug('[useAsync] async onLoad have ', __classPrivateFieldGet(this, _AsyncHook_localDepends, "f"), 'and', this.state.depends);
48
- if (!isEqual(__classPrivateFieldGet(this, _AsyncHook_localDepends, "f"), this.state.depends) || this.state.load_state === 'initial') {
49
- this.state.load_state = 'loading';
47
+ if (!isEqual(__classPrivateFieldGet(this, _AsyncHook_localDepends, "f"), this.state.depends) ||
48
+ (this.state.data === null && this.state.error === null && this.state.loading === false)) {
49
+ this.state.loading = true;
50
50
  this.state.depends = __classPrivateFieldGet(this, _AsyncHook_localDepends, "f");
51
51
  __classPrivateFieldGet(this, _AsyncHook_invalidate, "f").call(this);
52
52
  const requeueEvent = {
@@ -97,12 +97,12 @@ class AsyncHook {
97
97
  else if (event.asyncResponse) {
98
98
  const anticipatedRequestId = __classPrivateFieldGet(this, _AsyncHook_hookId, "f") + '-' + JSON.stringify(this.state.depends);
99
99
  if (event.asyncResponse.requestId === anticipatedRequestId) {
100
- this.state = {
101
- ...this.state,
100
+ const result = {
102
101
  data: event.asyncResponse.data?.value,
102
+ loading: false,
103
103
  error: event.asyncResponse.error ?? null,
104
- load_state: event.asyncResponse.error ? 'error' : 'loaded',
105
104
  };
105
+ this.state = { ...this.state, ...result };
106
106
  __classPrivateFieldGet(this, _AsyncHook_invalidate, "f").call(this);
107
107
  }
108
108
  else {
@@ -115,7 +115,7 @@ class AsyncHook {
115
115
  }
116
116
  }
117
117
  }
118
- _AsyncHook_debug = new WeakMap(), _AsyncHook_hookId = new WeakMap(), _AsyncHook_initializer = new WeakMap(), _AsyncHook_invalidate = new WeakMap(), _AsyncHook_ctx = new WeakMap(), _AsyncHook_localDepends = new WeakMap();
118
+ _AsyncHook_debug = new WeakMap(), _AsyncHook_hookId = new WeakMap(), _AsyncHook_initializer = new WeakMap(), _AsyncHook_invalidate = new WeakMap(), _AsyncHook_ctx = new WeakMap(), _AsyncHook_localDepends = new WeakMap(), _AsyncHook_enabled = new WeakMap();
119
119
  /**
120
120
  * This is the preferred way to handle async state in Devvit.
121
121
  *
@@ -126,9 +126,5 @@ export function useAsync(initializer, options = {}) {
126
126
  const hook = registerHook({ namespace: 'useAsync' }, (params) => {
127
127
  return new AsyncHook(initializer, options, params);
128
128
  });
129
- return {
130
- data: hook.state.data,
131
- error: hook.state.error,
132
- loading: hook.state.load_state === 'loading',
133
- };
129
+ return hook.state;
134
130
  }
@@ -1,5 +1,4 @@
1
- import type { JSONValue } from '@devvit/shared-types/json.js';
2
1
  import type { UseChannelResult } from '../../../../types/hooks.js';
3
2
  import type { ChannelOptions } from '../../../../types/realtime.js';
4
- export declare function useChannel<Message extends JSONValue>(opts: Readonly<ChannelOptions<Message>>): UseChannelResult<Message>;
3
+ export declare function useChannel(opts: Readonly<ChannelOptions>): UseChannelResult;
5
4
  //# sourceMappingURL=useChannel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useChannel.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/useChannel.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AA+HpE,wBAAgB,UAAU,CAAC,OAAO,SAAS,SAAS,EAClD,IAAI,EAAE,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,GACtC,gBAAgB,CAAC,OAAO,CAAC,CAQ3B"}
1
+ {"version":3,"file":"useChannel.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/useChannel.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAyFpE,wBAAgB,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,cAAc,CAAC,GAAG,gBAAgB,CAI3E"}
@@ -9,37 +9,28 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _ChannelHook_instances, _a, _ChannelHook_context, _ChannelHook_debug, _ChannelHook_invalidate, _ChannelHook_opts, _ChannelHook_emitSubscribed;
13
- import { EffectType, RealtimeSubscriptionStatus } from '@devvit/protos';
12
+ var _ChannelHook_context, _ChannelHook_invalidate, _ChannelHook_opts;
13
+ import { RealtimeSubscriptionStatus } from '@devvit/protos';
14
14
  import { Header } from '@devvit/shared-types/Header.js';
15
15
  import { ChannelStatus } from '../../../../types/realtime.js';
16
16
  import { registerHook } from './BlocksHandler.js';
17
17
  class ChannelHook {
18
18
  constructor(opts, params) {
19
- _ChannelHook_instances.add(this);
20
19
  _ChannelHook_context.set(this, void 0);
21
- _ChannelHook_debug.set(this, void 0);
22
20
  /** Record state in BlocksHandler. */
23
21
  _ChannelHook_invalidate.set(this, void 0);
24
22
  _ChannelHook_opts.set(this, void 0);
25
23
  __classPrivateFieldSet(this, _ChannelHook_context, params.context, "f");
26
- __classPrivateFieldSet(this, _ChannelHook_debug, !!params.context._devvitContext?.debug.realtime, "f");
27
24
  __classPrivateFieldSet(this, _ChannelHook_opts, opts, "f");
28
25
  __classPrivateFieldSet(this, _ChannelHook_invalidate, params.invalidate, "f");
29
26
  const appID = params.context.meta[Header.App]?.values[0];
30
27
  if (!appID)
31
- throw Error('useChannel missing app ID metadata');
28
+ throw Error('useChannel() missing app ID metadata');
32
29
  const installID = params.context.meta[Header.Installation]?.values[0];
33
30
  if (!installID)
34
- throw Error('useChannel missing install ID from metadata');
35
- const channel = `${appID}:${installID}:${opts.name}`;
36
- const duplicate = Object.values(__classPrivateFieldGet(this, _ChannelHook_context, "f").hooks)
37
- .filter((hook) => hook instanceof _a)
38
- .some((hook) => hook.state.channel === channel);
39
- if (duplicate)
40
- throw Error(`useChannel channel names must be unique; "${channel}" duplicated`);
31
+ throw Error('useChannel() missing install ID from metadata');
41
32
  this.state = {
42
- channel,
33
+ channel: `${appID}:${installID}:${opts.name}`,
43
34
  connected: false,
44
35
  subscribed: false,
45
36
  };
@@ -50,22 +41,16 @@ class ChannelHook {
50
41
  return;
51
42
  switch (realtime.status) {
52
43
  case RealtimeSubscriptionStatus.REALTIME_SUBSCRIBED:
53
- if (__classPrivateFieldGet(this, _ChannelHook_debug, "f"))
54
- console.debug(`[realtime] "${this.state.channel}" connected`);
55
44
  this.state.connected = true;
56
45
  __classPrivateFieldGet(this, _ChannelHook_invalidate, "f").call(this);
57
46
  await __classPrivateFieldGet(this, _ChannelHook_opts, "f").onSubscribed?.();
58
47
  break;
59
48
  case RealtimeSubscriptionStatus.REALTIME_UNSUBSCRIBED:
60
- if (__classPrivateFieldGet(this, _ChannelHook_debug, "f"))
61
- console.debug(`[realtime] "${this.state.channel}" disconnected`);
62
49
  this.state.connected = false;
63
50
  __classPrivateFieldGet(this, _ChannelHook_invalidate, "f").call(this);
64
51
  await __classPrivateFieldGet(this, _ChannelHook_opts, "f").onUnsubscribed?.();
65
52
  break;
66
53
  default:
67
- if (__classPrivateFieldGet(this, _ChannelHook_debug, "f"))
68
- console.debug(`[realtime] "${this.state.channel}" received message: ${JSON.stringify(ev, undefined, 2)}`);
69
54
  // to-do: define a RealtimeSubscriptionStatus.MESSAGE. this could have
70
55
  // been a oneOf but the current approach allows for status + data
71
56
  // and this default case will break if another new type is added.
@@ -73,14 +58,10 @@ class ChannelHook {
73
58
  break;
74
59
  }
75
60
  }
76
- async send(msg) {
77
- if (__classPrivateFieldGet(this, _ChannelHook_debug, "f"))
78
- console.debug(`[realtime] "${this.state.channel}" send message: ${JSON.stringify(msg, undefined, 2)}`);
79
- if (!this.state.subscribed || !this.state.connected) {
80
- console.debug(`[realtime] "${this.state.channel}" send failed; channel not connected`);
81
- throw Error(`useChannel send failed; "${this.state.channel}" channel not connected`);
82
- }
83
- await __classPrivateFieldGet(this, _ChannelHook_context, "f").devvitContext.realtime.send(this.state.channel, msg);
61
+ async send(data) {
62
+ if (!this.state.subscribed || !this.state.connected)
63
+ throw Error(`send failed; ${this.state.channel} channel not connected`);
64
+ await __classPrivateFieldGet(this, _ChannelHook_context, "f").devvitContext.realtime.send(this.state.channel, data);
84
65
  }
85
66
  get status() {
86
67
  if (this.state.subscribed && this.state.connected)
@@ -94,36 +75,19 @@ class ChannelHook {
94
75
  subscribe() {
95
76
  if (this.state.subscribed)
96
77
  return;
97
- if (__classPrivateFieldGet(this, _ChannelHook_debug, "f"))
98
- console.debug(`[realtime] "${this.state.channel}" subscribed`);
99
78
  this.state.subscribed = true;
100
79
  __classPrivateFieldGet(this, _ChannelHook_invalidate, "f").call(this);
101
- __classPrivateFieldGet(this, _ChannelHook_instances, "m", _ChannelHook_emitSubscribed).call(this);
102
80
  }
103
81
  unsubscribe() {
104
82
  if (!this.state.subscribed)
105
83
  return;
106
- if (__classPrivateFieldGet(this, _ChannelHook_debug, "f"))
107
- console.debug(`[realtime] "${this.state.channel}" unsubscribed`);
108
84
  this.state.subscribed = false;
109
85
  __classPrivateFieldGet(this, _ChannelHook_invalidate, "f").call(this);
110
- __classPrivateFieldGet(this, _ChannelHook_instances, "m", _ChannelHook_emitSubscribed).call(this);
111
86
  }
112
87
  }
113
- _a = ChannelHook, _ChannelHook_context = new WeakMap(), _ChannelHook_debug = new WeakMap(), _ChannelHook_invalidate = new WeakMap(), _ChannelHook_opts = new WeakMap(), _ChannelHook_instances = new WeakSet(), _ChannelHook_emitSubscribed = function _ChannelHook_emitSubscribed() {
114
- const channels = Object.values(__classPrivateFieldGet(this, _ChannelHook_context, "f").hooks)
115
- .filter((hook) => hook instanceof _a && hook.state.subscribed)
116
- .map((hook) => hook.state.channel);
117
- __classPrivateFieldGet(this, _ChannelHook_context, "f").emitEffect(this.state.channel, {
118
- type: EffectType.EFFECT_REALTIME_SUB,
119
- realtimeSubscriptions: { subscriptionIds: channels },
120
- });
121
- };
88
+ _ChannelHook_context = new WeakMap(), _ChannelHook_invalidate = new WeakMap(), _ChannelHook_opts = new WeakMap();
122
89
  export function useChannel(opts) {
123
- if (!opts.name || /[^a-zA-Z0-9]/.test(opts.name))
124
- throw Error('useChannel error: channel names must be nonempty and alphanumeric');
125
- // allow RealtimeEffectHandler to compute hook ID. maintain compatibility with
126
- // realtimeChannelToHookID().
127
- const id = `useChannel:${opts.name}`;
128
- return registerHook({ id, namespace: id }, (params) => new ChannelHook(opts, params));
90
+ return registerHook({ namespace: 'useChannel' }, (params) => {
91
+ return new ChannelHook(opts, params);
92
+ });
129
93
  }
@@ -1,21 +1,8 @@
1
- import type { JSONValue } from '@devvit/shared-types/json.js';
2
- import type { FormKey } from '@devvit/shared-types/useForm.js';
3
- import type { Form, FormDefinition, FormFunction, FormToFormValues, FormValues } from '../../../../index.js';
1
+ import type { Form, FormFunction, FormKey, FormValues } from '../../../../index.js';
4
2
  import type { RenderContext } from './RenderContext.js';
5
- import type { EventHandler, Hook, HookRef } from './types.js';
6
- declare class UseFormHook implements Hook, FormDefinition {
7
- hookId: string;
8
- state: JSONValue;
9
- onUIEvent?: EventHandler | undefined;
10
- onStateLoaded?: (() => void) | undefined;
11
- form: Form | FormFunction;
12
- onSubmit: (values: FormValues) => void | Promise<void>;
13
- constructor(params: {
14
- hookId: string;
15
- }, form: Form | FormFunction, onSubmit: (values: FormValues) => void | Promise<void>);
16
- }
17
- export declare function useForm<const T extends Form | FormFunction>(form: T, onSubmit: (values: FormToFormValues<T>) => void | Promise<void>): FormKey;
3
+ import type { HookRef } from './types.js';
4
+ export declare function useForm(form: Form | FormFunction, onSubmit: (values: FormValues) => void | Promise<void>): FormKey;
18
5
  export declare function hookRefToFormKey(hookRef: HookRef): FormKey;
19
- export declare function getFormDefinition(renderContext: RenderContext, formKey: FormKey): UseFormHook;
20
- export {};
6
+ export declare function formKeyToHookRef(formKey: FormKey): HookRef;
7
+ export declare function getFormDefinition(renderContext: RenderContext, formKey: FormKey): FormValues;
21
8
  //# sourceMappingURL=useForm.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/useForm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAG/D,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACX,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE9D,cAAM,WAAY,YAAW,IAAI,EAAE,cAAc;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,SAAS,CAAQ;IACxB,SAAS,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC;IACrC,aAAa,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;IACzC,IAAI,EAAE,IAAI,GAAG,YAAY,CAAC;IAC1B,QAAQ,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;gBAGrD,MAAM,EAAE;QAAE,MAAM,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{ formKeyToHookId } from '@devvit/shared-types/useForm.js';
2
1
  import { getFormValues } from '../../../../apis/ui/helpers/getFormValues.js';
3
2
  import { registerHook } from './BlocksHandler.js';
4
3
  class UseFormHook {
@@ -21,7 +20,11 @@ export function useForm(form, onSubmit) {
21
20
  export function hookRefToFormKey(hookRef) {
22
21
  return `form.hook.${hookRef.id}.0`;
23
22
  }
23
+ export function formKeyToHookRef(formKey) {
24
+ // extract the hook id from the form key with a regex
25
+ return { id: formKey.match(/form\.hook\.(.+)\.0/)[1] };
26
+ }
24
27
  export function getFormDefinition(renderContext, formKey) {
25
- const hookId = formKeyToHookId(formKey);
26
- return renderContext.getHook({ id: hookId });
28
+ const hookId = formKeyToHookRef(formKey);
29
+ return renderContext.getHook(hookId);
27
30
  }
@@ -1,8 +1,8 @@
1
1
  import type { JSONValue } from '@devvit/shared-types/json.js';
2
2
  import type { AsyncUseStateInitializer, UseStateInitializer, UseStateResult } from '../../../../types/hooks.js';
3
- export declare function useAsyncState<S extends JSONValue>(initializer: AsyncUseStateInitializer<S>): UseStateResult<S>;
4
3
  export declare function useState(initialState: UseStateInitializer<boolean>): UseStateResult<boolean>;
5
4
  export declare function useState(initialState: UseStateInitializer<number>): UseStateResult<number>;
6
5
  export declare function useState(initialState: UseStateInitializer<string>): UseStateResult<string>;
7
6
  export declare function useState<S extends JSONValue>(initialState: UseStateInitializer<S>): UseStateResult<S>;
7
+ export declare function useAsyncState<S extends JSONValue>(initializer: AsyncUseStateInitializer<S>): UseStateResult<S>;
8
8
  //# sourceMappingURL=useState.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useState.d.ts","sourceRoot":"","sources":["../../../../../src/devvit/internals/blocks/handler/useState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,KAAK,EACV,wBAAwB,EAExB,mBAAmB,EACnB,cAAc,EACf,MAAM,4BAA4B,CAAC;AA+CpC,wBAAgB,aAAa,C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Aa,CAAC,CAAC,SAAS,SAAS,EAC/C,WAAW,EAAE,wBAAwB,CAAC,CAAC,CAAC,GACvC,cAAc,CAAC,CAAC,CAAC,CAMnB"}
@@ -12,6 +12,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
12
12
  var _AsyncStateHook_changed, _AsyncStateHook_ctx, _AsyncStateHook_initializer, _AsyncStateHook_hookId;
13
13
  import { registerHook } from './BlocksHandler.js';
14
14
  import { RenderInterruptError } from './types.js';
15
+ export function useState(initialState) {
16
+ const hook = registerHook({ namespace: 'useState' }, ({ invalidate: changed }) => {
17
+ const state = initialState instanceof Function ? initialState() : initialState;
18
+ if (state instanceof Promise) {
19
+ throw new Error("Cannot use async initializer with context.useState. Please use useAsyncState instead by importing it: import { Devvit, useAsyncState } from '@devvit/public-api';");
20
+ }
21
+ const setter = (action) => {
22
+ hook.state = action instanceof Function ? action(hook.state) : action;
23
+ changed();
24
+ };
25
+ return { state, setter };
26
+ });
27
+ return [hook.state, hook.setter];
28
+ }
15
29
  class AsyncStateHook {
16
30
  constructor(initializer, params) {
17
31
  this.state = { value: null, loading: false };
@@ -55,17 +69,3 @@ export function useAsyncState(initializer) {
55
69
  });
56
70
  return [hook.state.value, hook.setter.bind(hook)];
57
71
  }
58
- export function useState(initialState) {
59
- const hook = registerHook({ namespace: 'useState' }, (params) => {
60
- const state = initialState instanceof Function ? initialState() : initialState;
61
- if (state instanceof Promise) {
62
- return new AsyncStateHook(() => state, params);
63
- }
64
- const setter = (action) => {
65
- hook.state.value = action instanceof Function ? action(hook.state.value) : action;
66
- params.invalidate();
67
- };
68
- return { state: { value: state }, setter };
69
- });
70
- return [hook.state.value, hook.setter.bind(hook)];
71
- }
@@ -1 +1 @@
1
- {"version":3,"file":"useChannel.d.ts","sourceRoot":"","sources":["../../../../src/devvit/internals/blocks/useChannel.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,yBAAyB,CAAC;AAIjC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,gBAAgB,GAAG,cAAc,CA6H/E"}
1
+ {"version":3,"file":"useChannel.d.ts","sourceRoot":"","sources":["../../../../src/devvit/internals/blocks/useChannel.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,yBAAyB,CAAC;AAIjC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,gBAAgB,GAAG,cAAc,CAoH/E"}
@@ -5,7 +5,6 @@ import { Hook } from '../../../types/hooks.js';
5
5
  import { ChannelStatus } from '../../../types/realtime.js';
6
6
  export function makeUseChannelHook(reconciler) {
7
7
  function useChannel(options) {
8
- const debug = false;
9
8
  const hookIndex = reconciler.currentHookIndex;
10
9
  const currentState = reconciler.getCurrentComponentState();
11
10
  const previousState = reconciler.getPreviousComponentState();
@@ -13,13 +12,11 @@ export function makeUseChannelHook(reconciler) {
13
12
  assertNonNull(appId, 'useChannel - app is missing from Context');
14
13
  const installationId = reconciler.metadata[Header.Installation]?.values[0];
15
14
  assertNonNull(installationId, 'useChannel - installation is missing from Context');
16
- async function send(msg) {
17
- if (debug)
18
- console.debug('[realtime] sends', msg);
15
+ async function send(data) {
19
16
  const name = currentState[hookIndex].channel;
20
17
  if (currentState[hookIndex].active) {
21
18
  if (currentState[hookIndex].connected) {
22
- await reconciler.realtime.send(name, msg);
19
+ await reconciler.realtime.send(name, data);
23
20
  }
24
21
  else {
25
22
  throw Error(`Failed to send to channel '${name}'; it is active but not yet connected`);
@@ -31,8 +28,6 @@ export function makeUseChannelHook(reconciler) {
31
28
  }
32
29
  function subscribe() {
33
30
  if (!currentState[hookIndex].active) {
34
- if (debug)
35
- console.debug('[realtime] subscribe');
36
31
  const name = currentState[hookIndex].channel;
37
32
  currentState[hookIndex].active = true;
38
33
  reconciler.addRealtimeChannel(name);
@@ -40,8 +35,6 @@ export function makeUseChannelHook(reconciler) {
40
35
  }
41
36
  function unsubscribe() {
42
37
  if (currentState[hookIndex].active) {
43
- if (debug)
44
- console.debug('[realtime] unsubscribe');
45
38
  const name = currentState[hookIndex].channel;
46
39
  currentState[hookIndex].active = false;
47
40
  reconciler.removeRealtimeChannel(name);
@@ -52,20 +45,14 @@ export function makeUseChannelHook(reconciler) {
52
45
  let result;
53
46
  switch (event.status) {
54
47
  case RealtimeSubscriptionStatus.REALTIME_SUBSCRIBED:
55
- if (debug)
56
- console.debug('[realtime] onSubscribed()');
57
48
  currentState[hookIndex].connected = true;
58
49
  result = options.onSubscribed?.();
59
50
  break;
60
51
  case RealtimeSubscriptionStatus.REALTIME_UNSUBSCRIBED:
61
- if (debug)
62
- console.debug('[realtime] onUnsubscribed()');
63
52
  currentState[hookIndex].connected = false;
64
53
  result = options.onUnsubscribed?.();
65
54
  break;
66
55
  default:
67
- if (debug)
68
- console.debug('[realtime] receives', event.event?.data);
69
56
  result = options.onMessage(event.event?.data ?? {});
70
57
  break;
71
58
  }
@@ -1 +1 @@
1
- {"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../../../src/devvit/internals/blocks/useForm.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAoB,MAAM,yBAAyB,CAAC;AAE7E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,wBAAgB,eAAe,CAAC,UAAU,EAAE,gBAAgB,GAAG,WAAW,CAkDzE"}
1
+ {"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../../../src/devvit/internals/blocks/useForm.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAoB,MAAM,yBAAyB,CAAC;AAE7E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,wBAAgB,eAAe,CAAC,UAAU,EAAE,gBAAgB,GAAG,WAAW,CAkDzE"}
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/devvit/internals/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAI/C,OAAO,KAAK,EAAE,WAAW,EAAoB,MAAM,wBAAwB,CAAC;AAG5E,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,QAAQ,EAClB,MAAM,CAAC,EAAE,MAAM,EACf,SAAS,CAAC,EAAE,MAAM,GACjB,WAAW,CA4Bb"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/devvit/internals/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAI/C,OAAO,KAAK,EAAE,WAAW,EAAoB,MAAM,wBAAwB,CAAC;AAE5E,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,QAAQ,EAClB,MAAM,CAAC,EAAE,MAAM,EACf,SAAS,CAAC,EAAE,MAAM,GACjB,WAAW,CA4Bb"}