tinybase 5.4.0-beta.3 → 5.4.0-beta.5

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 (237) hide show
  1. package/@types/mergeable-store/index.d.cts +5 -4
  2. package/@types/mergeable-store/index.d.ts +5 -4
  3. package/@types/mergeable-store/with-schemas/index.d.cts +5 -4
  4. package/@types/mergeable-store/with-schemas/index.d.ts +5 -4
  5. package/@types/persisters/index.d.cts +1 -0
  6. package/@types/persisters/index.d.ts +1 -0
  7. package/@types/persisters/persister-durable-object-storage/index.d.cts +25 -17
  8. package/@types/persisters/persister-durable-object-storage/index.d.ts +25 -17
  9. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +26 -18
  10. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.ts +26 -18
  11. package/@types/persisters/persister-partykit-server/index.d.cts +12 -0
  12. package/@types/persisters/persister-partykit-server/index.d.ts +12 -0
  13. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +15 -0
  14. package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +15 -0
  15. package/@types/persisters/with-schemas/index.d.cts +1 -0
  16. package/@types/persisters/with-schemas/index.d.ts +1 -0
  17. package/@types/store/index.d.cts +10 -8
  18. package/@types/store/index.d.ts +10 -8
  19. package/@types/store/with-schemas/index.d.cts +10 -8
  20. package/@types/store/with-schemas/index.d.ts +10 -8
  21. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +206 -28
  22. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.ts +206 -28
  23. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +219 -28
  24. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.ts +219 -28
  25. package/cjs/es6/index.cjs +0 -1
  26. package/cjs/es6/indexes/index.cjs +1 -1
  27. package/cjs/es6/indexes/with-schemas/index.cjs +1 -1
  28. package/cjs/es6/metrics/index.cjs +1 -1
  29. package/cjs/es6/metrics/with-schemas/index.cjs +1 -1
  30. package/cjs/es6/min/index.cjs +1 -1
  31. package/cjs/es6/min/index.cjs.gz +0 -0
  32. package/cjs/es6/min/indexes/index.cjs +1 -1
  33. package/cjs/es6/min/indexes/index.cjs.gz +0 -0
  34. package/cjs/es6/min/indexes/with-schemas/index.cjs +1 -1
  35. package/cjs/es6/min/indexes/with-schemas/index.cjs.gz +0 -0
  36. package/cjs/es6/min/metrics/index.cjs +1 -1
  37. package/cjs/es6/min/metrics/index.cjs.gz +0 -0
  38. package/cjs/es6/min/metrics/with-schemas/index.cjs +1 -1
  39. package/cjs/es6/min/metrics/with-schemas/index.cjs.gz +0 -0
  40. package/cjs/es6/min/persisters/persister-durable-object-storage/index.cjs +1 -1
  41. package/cjs/es6/min/persisters/persister-durable-object-storage/index.cjs.gz +0 -0
  42. package/cjs/es6/min/persisters/persister-durable-object-storage/with-schemas/index.cjs +1 -1
  43. package/cjs/es6/min/persisters/persister-durable-object-storage/with-schemas/index.cjs.gz +0 -0
  44. package/cjs/es6/min/queries/index.cjs +1 -1
  45. package/cjs/es6/min/queries/index.cjs.gz +0 -0
  46. package/cjs/es6/min/queries/with-schemas/index.cjs +1 -1
  47. package/cjs/es6/min/queries/with-schemas/index.cjs.gz +0 -0
  48. package/cjs/es6/min/relationships/index.cjs +1 -1
  49. package/cjs/es6/min/relationships/index.cjs.gz +0 -0
  50. package/cjs/es6/min/relationships/with-schemas/index.cjs +1 -1
  51. package/cjs/es6/min/relationships/with-schemas/index.cjs.gz +0 -0
  52. package/cjs/es6/min/synchronizers/synchronizer-ws-server-durable-object/index.cjs +1 -1
  53. package/cjs/es6/min/synchronizers/synchronizer-ws-server-durable-object/index.cjs.gz +0 -0
  54. package/cjs/es6/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.cjs +1 -1
  55. package/cjs/es6/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.cjs.gz +0 -0
  56. package/cjs/es6/min/with-schemas/index.cjs +1 -1
  57. package/cjs/es6/min/with-schemas/index.cjs.gz +0 -0
  58. package/cjs/es6/persisters/persister-durable-object-storage/index.cjs +1 -1
  59. package/cjs/es6/persisters/persister-durable-object-storage/with-schemas/index.cjs +1 -1
  60. package/cjs/es6/persisters/persister-partykit-client/index.cjs +2 -5
  61. package/cjs/es6/persisters/persister-partykit-client/with-schemas/index.cjs +2 -5
  62. package/cjs/es6/queries/index.cjs +0 -1
  63. package/cjs/es6/queries/with-schemas/index.cjs +0 -1
  64. package/cjs/es6/relationships/index.cjs +1 -1
  65. package/cjs/es6/relationships/with-schemas/index.cjs +1 -1
  66. package/cjs/es6/synchronizers/synchronizer-ws-server-durable-object/index.cjs +10 -6
  67. package/cjs/es6/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.cjs +10 -6
  68. package/cjs/es6/with-schemas/index.cjs +0 -1
  69. package/cjs/min/persisters/persister-durable-object-storage/index.cjs +1 -1
  70. package/cjs/min/persisters/persister-durable-object-storage/index.cjs.gz +0 -0
  71. package/cjs/min/persisters/persister-durable-object-storage/with-schemas/index.cjs +1 -1
  72. package/cjs/min/persisters/persister-durable-object-storage/with-schemas/index.cjs.gz +0 -0
  73. package/cjs/min/synchronizers/synchronizer-ws-server-durable-object/index.cjs +1 -1
  74. package/cjs/min/synchronizers/synchronizer-ws-server-durable-object/index.cjs.gz +0 -0
  75. package/cjs/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.cjs +1 -1
  76. package/cjs/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.cjs.gz +0 -0
  77. package/cjs/persisters/persister-durable-object-storage/index.cjs +1 -1
  78. package/cjs/persisters/persister-durable-object-storage/with-schemas/index.cjs +1 -1
  79. package/cjs/persisters/persister-partykit-client/index.cjs +2 -5
  80. package/cjs/persisters/persister-partykit-client/with-schemas/index.cjs +2 -5
  81. package/cjs/synchronizers/synchronizer-ws-server-durable-object/index.cjs +3 -3
  82. package/cjs/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.cjs +3 -3
  83. package/es6/index.js +0 -1
  84. package/es6/indexes/index.js +1 -1
  85. package/es6/indexes/with-schemas/index.js +1 -1
  86. package/es6/metrics/index.js +1 -1
  87. package/es6/metrics/with-schemas/index.js +1 -1
  88. package/es6/min/index.js +1 -1
  89. package/es6/min/index.js.gz +0 -0
  90. package/es6/min/indexes/index.js +1 -1
  91. package/es6/min/indexes/index.js.gz +0 -0
  92. package/es6/min/indexes/with-schemas/index.js +1 -1
  93. package/es6/min/indexes/with-schemas/index.js.gz +0 -0
  94. package/es6/min/metrics/index.js +1 -1
  95. package/es6/min/metrics/index.js.gz +0 -0
  96. package/es6/min/metrics/with-schemas/index.js +1 -1
  97. package/es6/min/metrics/with-schemas/index.js.gz +0 -0
  98. package/es6/min/persisters/persister-durable-object-storage/index.js +1 -1
  99. package/es6/min/persisters/persister-durable-object-storage/index.js.gz +0 -0
  100. package/es6/min/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  101. package/es6/min/persisters/persister-durable-object-storage/with-schemas/index.js.gz +0 -0
  102. package/es6/min/persisters/persister-file/index.js +1 -1
  103. package/es6/min/persisters/persister-file/index.js.gz +0 -0
  104. package/es6/min/persisters/persister-file/with-schemas/index.js +1 -1
  105. package/es6/min/persisters/persister-file/with-schemas/index.js.gz +0 -0
  106. package/es6/min/queries/index.js +1 -1
  107. package/es6/min/queries/index.js.gz +0 -0
  108. package/es6/min/queries/with-schemas/index.js +1 -1
  109. package/es6/min/queries/with-schemas/index.js.gz +0 -0
  110. package/es6/min/relationships/index.js +1 -1
  111. package/es6/min/relationships/index.js.gz +0 -0
  112. package/es6/min/relationships/with-schemas/index.js +1 -1
  113. package/es6/min/relationships/with-schemas/index.js.gz +0 -0
  114. package/es6/min/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
  115. package/es6/min/synchronizers/synchronizer-ws-server-durable-object/index.js.gz +0 -0
  116. package/es6/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
  117. package/es6/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js.gz +0 -0
  118. package/es6/min/ui-react-dom/index.js +1 -1
  119. package/es6/min/ui-react-dom/index.js.gz +0 -0
  120. package/es6/min/ui-react-dom/with-schemas/index.js +1 -1
  121. package/es6/min/ui-react-dom/with-schemas/index.js.gz +0 -0
  122. package/es6/min/ui-react-inspector/index.js +1 -1
  123. package/es6/min/ui-react-inspector/index.js.gz +0 -0
  124. package/es6/min/ui-react-inspector/with-schemas/index.js +1 -1
  125. package/es6/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  126. package/es6/min/with-schemas/index.js +1 -1
  127. package/es6/min/with-schemas/index.js.gz +0 -0
  128. package/es6/persisters/persister-durable-object-storage/index.js +1 -1
  129. package/es6/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  130. package/es6/persisters/persister-file/index.js +1 -1
  131. package/es6/persisters/persister-file/with-schemas/index.js +1 -1
  132. package/es6/persisters/persister-partykit-client/index.js +2 -5
  133. package/es6/persisters/persister-partykit-client/with-schemas/index.js +2 -5
  134. package/es6/queries/index.js +0 -1
  135. package/es6/queries/with-schemas/index.js +0 -1
  136. package/es6/relationships/index.js +1 -1
  137. package/es6/relationships/with-schemas/index.js +1 -1
  138. package/es6/synchronizers/synchronizer-ws-server-durable-object/index.js +10 -6
  139. package/es6/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +10 -6
  140. package/es6/ui-react-dom/index.js +8 -8
  141. package/es6/ui-react-dom/with-schemas/index.js +8 -8
  142. package/es6/ui-react-inspector/index.js +4 -4
  143. package/es6/ui-react-inspector/with-schemas/index.js +4 -4
  144. package/es6/with-schemas/index.js +0 -1
  145. package/min/persisters/persister-durable-object-storage/index.js +1 -1
  146. package/min/persisters/persister-durable-object-storage/index.js.gz +0 -0
  147. package/min/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  148. package/min/persisters/persister-durable-object-storage/with-schemas/index.js.gz +0 -0
  149. package/min/persisters/persister-file/index.js +1 -1
  150. package/min/persisters/persister-file/index.js.gz +0 -0
  151. package/min/persisters/persister-file/with-schemas/index.js +1 -1
  152. package/min/persisters/persister-file/with-schemas/index.js.gz +0 -0
  153. package/min/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
  154. package/min/synchronizers/synchronizer-ws-server-durable-object/index.js.gz +0 -0
  155. package/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
  156. package/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js.gz +0 -0
  157. package/min/ui-react-dom/index.js +1 -1
  158. package/min/ui-react-dom/index.js.gz +0 -0
  159. package/min/ui-react-dom/with-schemas/index.js +1 -1
  160. package/min/ui-react-dom/with-schemas/index.js.gz +0 -0
  161. package/min/ui-react-inspector/index.js +1 -1
  162. package/min/ui-react-inspector/index.js.gz +0 -0
  163. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  164. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  165. package/package.json +1 -1
  166. package/persisters/persister-durable-object-storage/index.js +1 -1
  167. package/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  168. package/persisters/persister-file/index.js +1 -1
  169. package/persisters/persister-file/with-schemas/index.js +1 -1
  170. package/persisters/persister-partykit-client/index.js +2 -5
  171. package/persisters/persister-partykit-client/with-schemas/index.js +2 -5
  172. package/readme.md +2 -2
  173. package/releases.md +17 -5
  174. package/synchronizers/synchronizer-ws-server-durable-object/index.js +3 -3
  175. package/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +3 -3
  176. package/ui-react-dom/index.js +8 -8
  177. package/ui-react-dom/with-schemas/index.js +8 -8
  178. package/ui-react-inspector/index.js +4 -4
  179. package/ui-react-inspector/with-schemas/index.js +4 -4
  180. package/umd/es6/index.js +0 -1
  181. package/umd/es6/indexes/index.js +1 -5
  182. package/umd/es6/indexes/with-schemas/index.js +1 -5
  183. package/umd/es6/metrics/index.js +1 -5
  184. package/umd/es6/metrics/with-schemas/index.js +1 -5
  185. package/umd/es6/min/index.js +1 -1
  186. package/umd/es6/min/index.js.gz +0 -0
  187. package/umd/es6/min/indexes/index.js +1 -1
  188. package/umd/es6/min/indexes/index.js.gz +0 -0
  189. package/umd/es6/min/indexes/with-schemas/index.js +1 -1
  190. package/umd/es6/min/indexes/with-schemas/index.js.gz +0 -0
  191. package/umd/es6/min/metrics/index.js +1 -1
  192. package/umd/es6/min/metrics/index.js.gz +0 -0
  193. package/umd/es6/min/metrics/with-schemas/index.js +1 -1
  194. package/umd/es6/min/metrics/with-schemas/index.js.gz +0 -0
  195. package/umd/es6/min/persisters/persister-durable-object-storage/index.js +1 -1
  196. package/umd/es6/min/persisters/persister-durable-object-storage/index.js.gz +0 -0
  197. package/umd/es6/min/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  198. package/umd/es6/min/persisters/persister-durable-object-storage/with-schemas/index.js.gz +0 -0
  199. package/umd/es6/min/queries/index.js +1 -1
  200. package/umd/es6/min/queries/index.js.gz +0 -0
  201. package/umd/es6/min/queries/with-schemas/index.js +1 -1
  202. package/umd/es6/min/queries/with-schemas/index.js.gz +0 -0
  203. package/umd/es6/min/relationships/index.js +1 -1
  204. package/umd/es6/min/relationships/index.js.gz +0 -0
  205. package/umd/es6/min/relationships/with-schemas/index.js +1 -1
  206. package/umd/es6/min/relationships/with-schemas/index.js.gz +0 -0
  207. package/umd/es6/min/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
  208. package/umd/es6/min/synchronizers/synchronizer-ws-server-durable-object/index.js.gz +0 -0
  209. package/umd/es6/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
  210. package/umd/es6/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js.gz +0 -0
  211. package/umd/es6/min/with-schemas/index.js +1 -1
  212. package/umd/es6/min/with-schemas/index.js.gz +0 -0
  213. package/umd/es6/persisters/persister-durable-object-storage/index.js +1 -1
  214. package/umd/es6/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  215. package/umd/es6/persisters/persister-partykit-client/index.js +2 -5
  216. package/umd/es6/persisters/persister-partykit-client/with-schemas/index.js +2 -5
  217. package/umd/es6/queries/index.js +0 -1
  218. package/umd/es6/queries/with-schemas/index.js +0 -1
  219. package/umd/es6/relationships/index.js +1 -5
  220. package/umd/es6/relationships/with-schemas/index.js +1 -5
  221. package/umd/es6/synchronizers/synchronizer-ws-server-durable-object/index.js +10 -6
  222. package/umd/es6/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +10 -6
  223. package/umd/es6/with-schemas/index.js +0 -1
  224. package/umd/min/persisters/persister-durable-object-storage/index.js +1 -1
  225. package/umd/min/persisters/persister-durable-object-storage/index.js.gz +0 -0
  226. package/umd/min/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  227. package/umd/min/persisters/persister-durable-object-storage/with-schemas/index.js.gz +0 -0
  228. package/umd/min/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
  229. package/umd/min/synchronizers/synchronizer-ws-server-durable-object/index.js.gz +0 -0
  230. package/umd/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
  231. package/umd/min/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js.gz +0 -0
  232. package/umd/persisters/persister-durable-object-storage/index.js +1 -1
  233. package/umd/persisters/persister-durable-object-storage/with-schemas/index.js +1 -1
  234. package/umd/persisters/persister-partykit-client/index.js +2 -5
  235. package/umd/persisters/persister-partykit-client/with-schemas/index.js +2 -5
  236. package/umd/synchronizers/synchronizer-ws-server-durable-object/index.js +3 -3
  237. package/umd/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +3 -3
@@ -22,30 +22,52 @@ import {DurableObject} from 'cloudflare:workers';
22
22
  */
23
23
  export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
24
24
  /**
25
- * The createPersister method is used to return a persister for the
26
- * Durable Object to preserve Store state for when clients are not connected.
25
+ * The constructor is used to create the Durable Object that will synchronize
26
+ * the TinyBase clients.
27
27
  *
28
- * This should almost certainly be a DurableObjectStoragePersister, created
29
- * with the createDurableObjectStoragePersister function.
28
+ * For basic TinyBase synchronization and persistence, you don't need to
29
+ * override this method, but if you do, ensure you call the super constructor.
30
+ * @returns A new instance of the WsServerDurableObject.
31
+ * @category Creation
32
+ * @since v5.4.0
33
+ */
34
+ constructor();
35
+
36
+ /**
37
+ * The createPersister method is used to return a persister for the Durable
38
+ * Object to preserve Store data when clients are not connected.
39
+ *
40
+ * In other words, override this method to enable persistence of the Store
41
+ * data that the Durable Object is synchronizing between clients.
42
+ *
43
+ * This should almost certainly return a DurableObjectStoragePersister,
44
+ * created with the createDurableObjectStoragePersister function. This will
45
+ * ensure that the Store is serialized to the Durable Object KV-based storage.
46
+ *
47
+ * Returning `undefined` from this method will disable persistence.
30
48
  * @example
31
- * This example creates a Persister object against a newly-created
32
- * MergeableStore (within the createPersister method of a
33
- * WsServerDurableObject instance) and then gets the storage reference back
34
- * out again.
49
+ * This example enables Durable Object persistence by creating a Persister
50
+ * object within the createPersister method of a WsServerDurableObject.
35
51
  *
36
52
  * ```js yolo
53
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
37
54
  * import {createDurableObjectStoragePersister} from 'tinybase/persisters/persister-durable-object-storage';
38
55
  * import {createMergeableStore} from 'tinybase';
39
56
  *
40
- * // Within the createPersister method of a WsServerDurableObject instance.
41
- * const store = createMergeableStore().setTables();
42
- * const persister = createDurableObjectStoragePersister(
43
- * store,
44
- * this.ctx.storage,
45
- * );
46
- *
47
- * return persister;
57
+ * export class MyDurableObject extends WsServerDurableObject {
58
+ * createPersister() {
59
+ * const store = createMergeableStore();
60
+ * const persister = createDurableObjectStoragePersister(
61
+ * store,
62
+ * this.ctx.storage,
63
+ * );
64
+ * return persister;
65
+ * }
66
+ * }
48
67
  * ```
68
+ * @returns A new instance of a DurableObjectStoragePersister (or a promise to
69
+ * resolve one) that will be used to persist data to the Durable Object.
70
+ * Return `undefined` if that functionality is not required.
49
71
  * @category Creation
50
72
  * @since v5.4.0
51
73
  */
@@ -57,6 +79,23 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
57
79
  /**
58
80
  * The getPathId method is used to get the Id of the path that is being
59
81
  * served.
82
+ *
83
+ * This is useful for when you want to know which path the current Durable
84
+ * Object is serving - for the purposes of logging, for example.
85
+ * @returns The Id of the path being served by the Durable Object.
86
+ * @example
87
+ * This example logs the path being served by the Durable Object every time a
88
+ * synchronization method is handled.
89
+ *
90
+ * ```js yolo
91
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
92
+ *
93
+ * export class MyDurableObject extends WsServerDurableObject {
94
+ * onMessage() {
95
+ * console.info('Message received on path: ', this.getPathId());
96
+ * }
97
+ * }
98
+ * ```
60
99
  * @category Getter
61
100
  * @since v5.4.0
62
101
  */
@@ -64,18 +103,56 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
64
103
 
65
104
  /**
66
105
  * The getClientIds method is used to access a list of all the connected
67
- * clients on a given path.
106
+ * clients on the path.
68
107
  *
69
- * Note that if you call this method from the onClientId method as a client is
70
- * getting removed, it will still be returned in the list of client Ids.
108
+ * Note that if you call this method from within the onClientId method as a
109
+ * client is getting removed, it will still be returned in the list of client
110
+ * Ids.
111
+ * @returns The Ids of the clients being served by the Durable Object.
112
+ * @example
113
+ * This example logs the list of clients being served by the Durable Object
114
+ * every time a synchronization method is handled.
115
+ *
116
+ * ```js yolo
117
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
118
+ *
119
+ * export class MyDurableObject extends WsServerDurableObject {
120
+ * onMessage() {
121
+ * console.info('Clients on path: ', this.getClientIds());
122
+ * }
123
+ * }
124
+ * ```
71
125
  * @category Getter
72
126
  * @since v5.4.0
73
127
  */
74
- getClientIds(pathId: Id): Ids;
128
+ getClientIds(): Ids;
75
129
 
76
130
  /**
77
131
  * The onPathId method is called when the first client connects to, or the
78
132
  * last client disconnects from, the server with a given path Id.
133
+ *
134
+ * This method is called with the path Id and an IdAddedOrRemoved flag
135
+ * indicating whether it this is being triggered by the first client joining
136
+ * (`1`) or the last client leaving (`-1`).
137
+ * @param pathId The Id of the path being served by the Durable Object.
138
+ * @param addedOrRemoved Whether the path had the first joiner, or the last
139
+ * leaver.
140
+ * @example
141
+ * This example logs the Id of the path being served by the Durable Object
142
+ * when the first client joins (the path Id is 'added'), and when the last
143
+ * client leaves (the path Id is 'removed').
144
+ *
145
+ * ```js yolo
146
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
147
+ *
148
+ * export class MyDurableObject extends WsServerDurableObject {
149
+ * onPathId(pathId, addedOrRemoved) {
150
+ * console.info(
151
+ * (addedOrRemoved ? 'Added' : 'Removed') + ` path ${pathId}`,
152
+ * );
153
+ * }
154
+ * }
155
+ * ```
79
156
  * @category Event
80
157
  * @since v5.4.0
81
158
  */
@@ -84,6 +161,34 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
84
161
  /**
85
162
  * The onClientId method is called when a client connects to, or disconnects
86
163
  * from, the server.
164
+ *
165
+ * This method is called with the path Id, the client Id, and an
166
+ * IdAddedOrRemoved flag indicating whether it this is being triggered by
167
+ * the client joining (`1`) or the client leaving (`-1`).
168
+ *
169
+ * Note that if you call the getClientIds method from within this method as a
170
+ * client is getting removed, it will still be returned in the list of client
171
+ * Ids.
172
+ * @param pathId The Id of the path being served by the Durable Object.
173
+ * @param clientId The Id of the client joining or leaving.
174
+ * @param addedOrRemoved Whether the client is joining or leaving.
175
+ * @example
176
+ * This example logs every client that joins (the client Id is 'added') or
177
+ * leaves (the client Id is 'removed') on the path being served by the Durable
178
+ * Object.
179
+ *
180
+ * ```js yolo
181
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
182
+ *
183
+ * export class MyDurableObject extends WsServerDurableObject {
184
+ * onClientId(pathId, clientId, addedOrRemoved) {
185
+ * console.info(
186
+ * (addedOrRemoved ? 'Added' : 'Removed') +
187
+ * ` client ${clientId} on path ${pathId}`,
188
+ * );
189
+ * }
190
+ * }
191
+ * ```
87
192
  * @category Event
88
193
  * @since v5.4.0
89
194
  */
@@ -92,20 +197,93 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
92
197
  /**
93
198
  * The onMessage method is called when a message is handled by the server.
94
199
  *
95
- * Note that this method is potentially called very frequently, and should be
96
- * performant. A pathId is not passed as an argument, since it has a small
97
- * cost to provide by default. You can use the getPathId method yourself if
98
- * that information is needed.
200
+ * This is useful if you want to debug the synchronization process, though be
201
+ * aware that this method is called very frequently. It is called with the Id
202
+ * of the client the message came _from_, the the Id of the client the message
203
+ * is to be forwarded _to_, and the remainder of the message itself.
204
+ *
205
+ * Since this method is called often, it should be performant. The path Id is
206
+ * not passed as an argument, since it has a small cost to provide by default.
207
+ * You can use the getPathId method yourself if that information is needed.
208
+ * @param fromClientId The Id of the client that send the message.
209
+ * @param toClientId The Id of the client to receive the message (or empty for
210
+ * a broadcast).
211
+ * @param remainder The remainder of the body of the message.
212
+ * @example
213
+ * This example logs every message routed by the Durable Object between
214
+ * clients.
215
+ *
216
+ * ```js yolo
217
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
218
+ *
219
+ * export class MyDurableObject extends WsServerDurableObject {
220
+ * onMessage(fromClientId, toClientId, remainder) {
221
+ * console.info(
222
+ * `Message from '${fromClientId}' to '${toClientId}': ${remainder}`,
223
+ * );
224
+ * }
225
+ * }
226
+ * ```
99
227
  * @category Event
100
228
  * @since v5.4.0
101
229
  */
102
- onMessage(_fromClientId: Id, _toClientId: Id, _remainder: string): void;
230
+ onMessage(fromClientId: Id, toClientId: Id, remainder: string): void;
103
231
  }
104
232
 
105
233
  /**
106
- * The getWsServerDurableObjectFetch function returns a convenient fetch handler
107
- * for a Cloudflare worker to route requests to a WsServerDurableObject for the
108
- * given namespace.
234
+ * The getWsServerDurableObjectFetch function returns a convenient handler for a
235
+ * Cloudflare worker to route requests to the fetch handler of a
236
+ * WsServerDurableObject for the given namespace.
237
+ *
238
+ * The implementation of the function that this returns requires the request to
239
+ * be a WebSocket 'Upgrade' request, and for the client to have provided a
240
+ * `sec-websocket-key` header that the server can use as a unique key for the
241
+ * client.
242
+ *
243
+ * It then takes the path of the HTTP request and routes the upgrade request to
244
+ * a Durable Object (in the given namespace) for that path. From then on, the
245
+ * Durable Object handles all the WebSocket communication.
246
+ *
247
+ * Note that you'll need to have a Wrangler configuration that connects your
248
+ * Durable Object class to the namespace. In other words, you'll have something
249
+ * like this in your `wrangler.toml` file.
250
+ *
251
+ * ```toml
252
+ * [[durable_objects.bindings]]
253
+ * name = "MyDurableObjects"
254
+ * class_name = "MyDurableObject"
255
+ * ```
256
+ *
257
+ * Note that it is not required to use this handler to route TinyBase client
258
+ * requests in your Cloudflare app. If you have your own custom routing logic,
259
+ * path scheme, or authentication, for example, you can easily implement that in
260
+ * the worker's fetch method yourself. See the [Durable Objects
261
+ * documentation](https://developers.cloudflare.com/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#invoking-the-fetch-handler)
262
+ * for examples.
263
+ *
264
+ * You can also pass a newly created request to the Durable Object's `fetch`
265
+ * method. For example, you can overwrite the 'path' that the Durable Object
266
+ * thinks it is serving, perhaps to inject a unique authenticated user Id that
267
+ * wasn't actually provided by the client WebSocket.
268
+ * @param namespace A string for the namespace of the Durable Objects that you
269
+ * want this worker to route requests to.
270
+ * @returns A fetch handler that routes WebSocket upgrade requests to a Durable
271
+ * Object.
272
+ * @example
273
+ * This example sets up default routing of the WebSocket upgrade request to a
274
+ * Durable Object in the `MyDurableObjects` namespace. This would require the
275
+ * `wrangler.toml` configuration shown above.
276
+ *
277
+ * ```js yolo
278
+ * import {
279
+ * WsServerDurableObject,
280
+ * getWsServerDurableObjectFetch,
281
+ * } from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
282
+ *
283
+ * export class MyDurableObject extends WsServerDurableObject {}
284
+ *
285
+ * export default {fetch: getWsServerDurableObjectFetch('MyDurableObjects')};
286
+ * ```
109
287
  * @category Creation
110
288
  * @since v5.4.0
111
289
  */
@@ -22,30 +22,52 @@ import {DurableObject} from 'cloudflare:workers';
22
22
  */
23
23
  export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
24
24
  /**
25
- * The createPersister method is used to return a persister for the
26
- * Durable Object to preserve Store state for when clients are not connected.
25
+ * The constructor is used to create the Durable Object that will synchronize
26
+ * the TinyBase clients.
27
27
  *
28
- * This should almost certainly be a DurableObjectStoragePersister, created
29
- * with the createDurableObjectStoragePersister function.
28
+ * For basic TinyBase synchronization and persistence, you don't need to
29
+ * override this method, but if you do, ensure you call the super constructor.
30
+ * @returns A new instance of the WsServerDurableObject.
31
+ * @category Creation
32
+ * @since v5.4.0
33
+ */
34
+ constructor();
35
+
36
+ /**
37
+ * The createPersister method is used to return a persister for the Durable
38
+ * Object to preserve Store data when clients are not connected.
39
+ *
40
+ * In other words, override this method to enable persistence of the Store
41
+ * data that the Durable Object is synchronizing between clients.
42
+ *
43
+ * This should almost certainly return a DurableObjectStoragePersister,
44
+ * created with the createDurableObjectStoragePersister function. This will
45
+ * ensure that the Store is serialized to the Durable Object KV-based storage.
46
+ *
47
+ * Returning `undefined` from this method will disable persistence.
30
48
  * @example
31
- * This example creates a Persister object against a newly-created
32
- * MergeableStore (within the createPersister method of a
33
- * WsServerDurableObject instance) and then gets the storage reference back
34
- * out again.
49
+ * This example enables Durable Object persistence by creating a Persister
50
+ * object within the createPersister method of a WsServerDurableObject.
35
51
  *
36
52
  * ```js yolo
53
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
37
54
  * import {createDurableObjectStoragePersister} from 'tinybase/persisters/persister-durable-object-storage';
38
55
  * import {createMergeableStore} from 'tinybase';
39
56
  *
40
- * // Within the createPersister method of a WsServerDurableObject instance.
41
- * const store = createMergeableStore().setTables();
42
- * const persister = createDurableObjectStoragePersister(
43
- * store,
44
- * this.ctx.storage,
45
- * );
46
- *
47
- * return persister;
57
+ * export class MyDurableObject extends WsServerDurableObject {
58
+ * createPersister() {
59
+ * const store = createMergeableStore();
60
+ * const persister = createDurableObjectStoragePersister(
61
+ * store,
62
+ * this.ctx.storage,
63
+ * );
64
+ * return persister;
65
+ * }
66
+ * }
48
67
  * ```
68
+ * @returns A new instance of a DurableObjectStoragePersister (or a promise to
69
+ * resolve one) that will be used to persist data to the Durable Object.
70
+ * Return `undefined` if that functionality is not required.
49
71
  * @category Creation
50
72
  * @since v5.4.0
51
73
  */
@@ -57,6 +79,23 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
57
79
  /**
58
80
  * The getPathId method is used to get the Id of the path that is being
59
81
  * served.
82
+ *
83
+ * This is useful for when you want to know which path the current Durable
84
+ * Object is serving - for the purposes of logging, for example.
85
+ * @returns The Id of the path being served by the Durable Object.
86
+ * @example
87
+ * This example logs the path being served by the Durable Object every time a
88
+ * synchronization method is handled.
89
+ *
90
+ * ```js yolo
91
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
92
+ *
93
+ * export class MyDurableObject extends WsServerDurableObject {
94
+ * onMessage() {
95
+ * console.info('Message received on path: ', this.getPathId());
96
+ * }
97
+ * }
98
+ * ```
60
99
  * @category Getter
61
100
  * @since v5.4.0
62
101
  */
@@ -64,18 +103,56 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
64
103
 
65
104
  /**
66
105
  * The getClientIds method is used to access a list of all the connected
67
- * clients on a given path.
106
+ * clients on the path.
68
107
  *
69
- * Note that if you call this method from the onClientId method as a client is
70
- * getting removed, it will still be returned in the list of client Ids.
108
+ * Note that if you call this method from within the onClientId method as a
109
+ * client is getting removed, it will still be returned in the list of client
110
+ * Ids.
111
+ * @returns The Ids of the clients being served by the Durable Object.
112
+ * @example
113
+ * This example logs the list of clients being served by the Durable Object
114
+ * every time a synchronization method is handled.
115
+ *
116
+ * ```js yolo
117
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
118
+ *
119
+ * export class MyDurableObject extends WsServerDurableObject {
120
+ * onMessage() {
121
+ * console.info('Clients on path: ', this.getClientIds());
122
+ * }
123
+ * }
124
+ * ```
71
125
  * @category Getter
72
126
  * @since v5.4.0
73
127
  */
74
- getClientIds(pathId: Id): Ids;
128
+ getClientIds(): Ids;
75
129
 
76
130
  /**
77
131
  * The onPathId method is called when the first client connects to, or the
78
132
  * last client disconnects from, the server with a given path Id.
133
+ *
134
+ * This method is called with the path Id and an IdAddedOrRemoved flag
135
+ * indicating whether it this is being triggered by the first client joining
136
+ * (`1`) or the last client leaving (`-1`).
137
+ * @param pathId The Id of the path being served by the Durable Object.
138
+ * @param addedOrRemoved Whether the path had the first joiner, or the last
139
+ * leaver.
140
+ * @example
141
+ * This example logs the Id of the path being served by the Durable Object
142
+ * when the first client joins (the path Id is 'added'), and when the last
143
+ * client leaves (the path Id is 'removed').
144
+ *
145
+ * ```js yolo
146
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
147
+ *
148
+ * export class MyDurableObject extends WsServerDurableObject {
149
+ * onPathId(pathId, addedOrRemoved) {
150
+ * console.info(
151
+ * (addedOrRemoved ? 'Added' : 'Removed') + ` path ${pathId}`,
152
+ * );
153
+ * }
154
+ * }
155
+ * ```
79
156
  * @category Event
80
157
  * @since v5.4.0
81
158
  */
@@ -84,6 +161,34 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
84
161
  /**
85
162
  * The onClientId method is called when a client connects to, or disconnects
86
163
  * from, the server.
164
+ *
165
+ * This method is called with the path Id, the client Id, and an
166
+ * IdAddedOrRemoved flag indicating whether it this is being triggered by
167
+ * the client joining (`1`) or the client leaving (`-1`).
168
+ *
169
+ * Note that if you call the getClientIds method from within this method as a
170
+ * client is getting removed, it will still be returned in the list of client
171
+ * Ids.
172
+ * @param pathId The Id of the path being served by the Durable Object.
173
+ * @param clientId The Id of the client joining or leaving.
174
+ * @param addedOrRemoved Whether the client is joining or leaving.
175
+ * @example
176
+ * This example logs every client that joins (the client Id is 'added') or
177
+ * leaves (the client Id is 'removed') on the path being served by the Durable
178
+ * Object.
179
+ *
180
+ * ```js yolo
181
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
182
+ *
183
+ * export class MyDurableObject extends WsServerDurableObject {
184
+ * onClientId(pathId, clientId, addedOrRemoved) {
185
+ * console.info(
186
+ * (addedOrRemoved ? 'Added' : 'Removed') +
187
+ * ` client ${clientId} on path ${pathId}`,
188
+ * );
189
+ * }
190
+ * }
191
+ * ```
87
192
  * @category Event
88
193
  * @since v5.4.0
89
194
  */
@@ -92,20 +197,93 @@ export class WsServerDurableObject<Env = unknown> extends DurableObject<Env> {
92
197
  /**
93
198
  * The onMessage method is called when a message is handled by the server.
94
199
  *
95
- * Note that this method is potentially called very frequently, and should be
96
- * performant. A pathId is not passed as an argument, since it has a small
97
- * cost to provide by default. You can use the getPathId method yourself if
98
- * that information is needed.
200
+ * This is useful if you want to debug the synchronization process, though be
201
+ * aware that this method is called very frequently. It is called with the Id
202
+ * of the client the message came _from_, the the Id of the client the message
203
+ * is to be forwarded _to_, and the remainder of the message itself.
204
+ *
205
+ * Since this method is called often, it should be performant. The path Id is
206
+ * not passed as an argument, since it has a small cost to provide by default.
207
+ * You can use the getPathId method yourself if that information is needed.
208
+ * @param fromClientId The Id of the client that send the message.
209
+ * @param toClientId The Id of the client to receive the message (or empty for
210
+ * a broadcast).
211
+ * @param remainder The remainder of the body of the message.
212
+ * @example
213
+ * This example logs every message routed by the Durable Object between
214
+ * clients.
215
+ *
216
+ * ```js yolo
217
+ * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
218
+ *
219
+ * export class MyDurableObject extends WsServerDurableObject {
220
+ * onMessage(fromClientId, toClientId, remainder) {
221
+ * console.info(
222
+ * `Message from '${fromClientId}' to '${toClientId}': ${remainder}`,
223
+ * );
224
+ * }
225
+ * }
226
+ * ```
99
227
  * @category Event
100
228
  * @since v5.4.0
101
229
  */
102
- onMessage(_fromClientId: Id, _toClientId: Id, _remainder: string): void;
230
+ onMessage(fromClientId: Id, toClientId: Id, remainder: string): void;
103
231
  }
104
232
 
105
233
  /**
106
- * The getWsServerDurableObjectFetch function returns a convenient fetch handler
107
- * for a Cloudflare worker to route requests to a WsServerDurableObject for the
108
- * given namespace.
234
+ * The getWsServerDurableObjectFetch function returns a convenient handler for a
235
+ * Cloudflare worker to route requests to the fetch handler of a
236
+ * WsServerDurableObject for the given namespace.
237
+ *
238
+ * The implementation of the function that this returns requires the request to
239
+ * be a WebSocket 'Upgrade' request, and for the client to have provided a
240
+ * `sec-websocket-key` header that the server can use as a unique key for the
241
+ * client.
242
+ *
243
+ * It then takes the path of the HTTP request and routes the upgrade request to
244
+ * a Durable Object (in the given namespace) for that path. From then on, the
245
+ * Durable Object handles all the WebSocket communication.
246
+ *
247
+ * Note that you'll need to have a Wrangler configuration that connects your
248
+ * Durable Object class to the namespace. In other words, you'll have something
249
+ * like this in your `wrangler.toml` file.
250
+ *
251
+ * ```toml
252
+ * [[durable_objects.bindings]]
253
+ * name = "MyDurableObjects"
254
+ * class_name = "MyDurableObject"
255
+ * ```
256
+ *
257
+ * Note that it is not required to use this handler to route TinyBase client
258
+ * requests in your Cloudflare app. If you have your own custom routing logic,
259
+ * path scheme, or authentication, for example, you can easily implement that in
260
+ * the worker's fetch method yourself. See the [Durable Objects
261
+ * documentation](https://developers.cloudflare.com/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#invoking-the-fetch-handler)
262
+ * for examples.
263
+ *
264
+ * You can also pass a newly created request to the Durable Object's `fetch`
265
+ * method. For example, you can overwrite the 'path' that the Durable Object
266
+ * thinks it is serving, perhaps to inject a unique authenticated user Id that
267
+ * wasn't actually provided by the client WebSocket.
268
+ * @param namespace A string for the namespace of the Durable Objects that you
269
+ * want this worker to route requests to.
270
+ * @returns A fetch handler that routes WebSocket upgrade requests to a Durable
271
+ * Object.
272
+ * @example
273
+ * This example sets up default routing of the WebSocket upgrade request to a
274
+ * Durable Object in the `MyDurableObjects` namespace. This would require the
275
+ * `wrangler.toml` configuration shown above.
276
+ *
277
+ * ```js yolo
278
+ * import {
279
+ * WsServerDurableObject,
280
+ * getWsServerDurableObjectFetch,
281
+ * } from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
282
+ *
283
+ * export class MyDurableObject extends WsServerDurableObject {}
284
+ *
285
+ * export default {fetch: getWsServerDurableObjectFetch('MyDurableObjects')};
286
+ * ```
109
287
  * @category Creation
110
288
  * @since v5.4.0
111
289
  */