@palantir/pack.state.core 0.1.0-beta.2 → 0.1.0

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 (33) hide show
  1. package/.turbo/turbo-lint.log +2 -2
  2. package/.turbo/turbo-transpileBrowser.log +1 -1
  3. package/.turbo/turbo-transpileCjs.log +1 -1
  4. package/.turbo/turbo-transpileEsm.log +1 -1
  5. package/.turbo/turbo-transpileTypes.log +1 -1
  6. package/.turbo/turbo-typecheck.log +1 -1
  7. package/CHANGELOG.md +22 -0
  8. package/build/browser/index.js +48 -0
  9. package/build/browser/index.js.map +1 -1
  10. package/build/cjs/index.cjs +48 -0
  11. package/build/cjs/index.cjs.map +1 -1
  12. package/build/cjs/index.d.cts +13 -1
  13. package/build/esm/index.js +48 -0
  14. package/build/esm/index.js.map +1 -1
  15. package/build/types/__tests__/Transaction.test.d.ts +1 -0
  16. package/build/types/__tests__/Transaction.test.d.ts.map +1 -0
  17. package/build/types/__tests__/testUtils.d.ts.map +1 -1
  18. package/build/types/service/BaseYjsDocumentService.d.ts +5 -1
  19. package/build/types/service/BaseYjsDocumentService.d.ts.map +1 -1
  20. package/build/types/service/InMemoryDocumentService.d.ts.map +1 -1
  21. package/build/types/types/DocumentRefImpl.d.ts.map +1 -1
  22. package/build/types/types/DocumentService.d.ts +5 -1
  23. package/build/types/types/DocumentService.d.ts.map +1 -1
  24. package/build/types/types/StateModule.d.ts +9 -1
  25. package/build/types/types/StateModule.d.ts.map +1 -1
  26. package/package.json +6 -6
  27. package/src/__tests__/Transaction.test.ts +321 -0
  28. package/src/__tests__/testUtils.ts +1 -2
  29. package/src/service/BaseYjsDocumentService.ts +33 -0
  30. package/src/service/InMemoryDocumentService.ts +28 -1
  31. package/src/types/DocumentRefImpl.ts +38 -0
  32. package/src/types/DocumentService.ts +27 -0
  33. package/src/types/StateModule.ts +58 -0
@@ -16,12 +16,17 @@
16
16
 
17
17
  import type { PackAppInternal } from "@palantir/pack.core";
18
18
  import type {
19
+ ActivityEvent,
19
20
  DocumentId,
20
21
  DocumentMetadata,
21
22
  DocumentRef,
22
23
  DocumentSchema,
23
24
  DocumentState,
25
+ EditDescription,
24
26
  Model,
27
+ ModelData,
28
+ PresenceEvent,
29
+ PresenceSubscriptionOptions,
25
30
  RecordCollectionRef,
26
31
  Unsubscribe,
27
32
  } from "@palantir/pack.document-schema.model-types";
@@ -39,8 +44,14 @@ const INVALID_DOC_REF: DocumentRef = Object.freeze(
39
44
  getRecords: () => {
40
45
  throw new Error("Invalid document reference");
41
46
  },
47
+ onActivity: () => () => {},
42
48
  onMetadataChange: () => () => {},
49
+ onPresence: () => () => {},
43
50
  onStateChange: () => () => {},
51
+ updateCustomPresence: () => {},
52
+ withTransaction: () => {
53
+ throw new Error("Invalid document reference");
54
+ },
44
55
  } as const,
45
56
  );
46
57
 
@@ -84,15 +95,42 @@ class DocumentRefImpl<T extends DocumentSchema> implements DocumentRef<T> {
84
95
  return this.#stateModule.getCreateRecordCollectionRef(this, model);
85
96
  }
86
97
 
98
+ onActivity(
99
+ callback: (docRef: DocumentRef<T>, event: ActivityEvent) => void,
100
+ ): Unsubscribe {
101
+ return this.#stateModule.onActivity(this, callback);
102
+ }
103
+
87
104
  onMetadataChange(
88
105
  cb: (docRef: DocumentRef<T>, metadata: DocumentMetadata) => void,
89
106
  ): Unsubscribe {
90
107
  return this.#stateModule.onMetadataChange(this, cb);
91
108
  }
92
109
 
110
+ onPresence(
111
+ callback: (docRef: DocumentRef<T>, event: PresenceEvent) => void,
112
+ options?: PresenceSubscriptionOptions,
113
+ ): Unsubscribe {
114
+ return this.#stateModule.onPresence(this, callback, options);
115
+ }
116
+
93
117
  onStateChange(
94
118
  callback: (docRef: DocumentRef<T>) => void,
95
119
  ): Unsubscribe {
96
120
  return this.#stateModule.onStateChange(this, callback);
97
121
  }
122
+
123
+ updateCustomPresence<M extends Model = Model>(
124
+ model: M,
125
+ eventData: ModelData<M>,
126
+ ): void {
127
+ this.#stateModule.updateCustomPresence(this, model, eventData);
128
+ }
129
+
130
+ withTransaction(
131
+ fn: () => void,
132
+ description?: EditDescription,
133
+ ): void {
134
+ this.#stateModule.withTransaction(this, fn, description);
135
+ }
98
136
  }
@@ -16,13 +16,17 @@
16
16
 
17
17
  import type { Unsubscribe } from "@palantir/pack.core";
18
18
  import type {
19
+ ActivityEvent,
19
20
  DocumentId,
20
21
  DocumentMetadata,
21
22
  DocumentRef,
22
23
  DocumentSchema,
23
24
  DocumentState,
25
+ EditDescription,
24
26
  Model,
25
27
  ModelData,
28
+ PresenceEvent,
29
+ PresenceSubscriptionOptions,
26
30
  RecordCollectionRef,
27
31
  RecordId,
28
32
  RecordRef,
@@ -126,6 +130,12 @@ export interface DocumentService {
126
130
  partialState: Partial<ModelData<R>>,
127
131
  ) => Promise<void>;
128
132
 
133
+ readonly withTransaction: (
134
+ docRef: DocumentRef,
135
+ fn: () => void,
136
+ description?: EditDescription,
137
+ ) => void;
138
+
129
139
  // Collection methods
130
140
  readonly getRecord: <M extends Model>(
131
141
  collection: RecordCollectionRef<M>,
@@ -170,16 +180,33 @@ export interface DocumentService {
170
180
  callback: RecordCollectionChangeCallback<M>,
171
181
  ) => Unsubscribe;
172
182
 
183
+ readonly onActivity: <T extends DocumentSchema>(
184
+ docRef: DocumentRef<T>,
185
+ callback: (docRef: DocumentRef<T>, event: ActivityEvent) => void,
186
+ ) => Unsubscribe;
187
+
173
188
  readonly onMetadataChange: <T extends DocumentSchema>(
174
189
  docRef: DocumentRef<T>,
175
190
  callback: DocumentMetadataChangeCallback<T>,
176
191
  ) => Unsubscribe;
177
192
 
193
+ readonly onPresence: <T extends DocumentSchema>(
194
+ docRef: DocumentRef<T>,
195
+ callback: (docRef: DocumentRef<T>, event: PresenceEvent) => void,
196
+ options?: PresenceSubscriptionOptions,
197
+ ) => Unsubscribe;
198
+
178
199
  readonly onStateChange: <T extends DocumentSchema>(
179
200
  docRef: DocumentRef<T>,
180
201
  callback: DocumentStateChangeCallback<T>,
181
202
  ) => Unsubscribe;
182
203
 
204
+ readonly updateCustomPresence: <M extends Model>(
205
+ docRef: DocumentRef,
206
+ model: M,
207
+ eventData: ModelData<M>,
208
+ ) => void;
209
+
183
210
  readonly onRecordChanged: <M extends Model>(
184
211
  record: RecordRef<M>,
185
212
  callback: RecordChangeCallback<M>,
@@ -21,13 +21,17 @@ import {
21
21
  type Unsubscribe,
22
22
  } from "@palantir/pack.core";
23
23
  import type {
24
+ ActivityEvent,
24
25
  DocumentId,
25
26
  DocumentMetadata,
26
27
  DocumentRef,
27
28
  DocumentSchema,
28
29
  DocumentState,
30
+ EditDescription,
29
31
  Model,
30
32
  ModelData,
33
+ PresenceEvent,
34
+ PresenceSubscriptionOptions,
31
35
  RecordCollectionRef,
32
36
  RecordId,
33
37
  RecordRef,
@@ -73,16 +77,33 @@ export interface StateModule {
73
77
  docRef: DocumentRef<T>,
74
78
  ) => Promise<DocumentState<T>>;
75
79
 
80
+ readonly onActivity: <T extends DocumentSchema>(
81
+ docRef: DocumentRef<T>,
82
+ callback: (docRef: DocumentRef<T>, event: ActivityEvent) => void,
83
+ ) => Unsubscribe;
84
+
76
85
  readonly onMetadataChange: <T extends DocumentSchema>(
77
86
  docRef: DocumentRef<T>,
78
87
  cb: (docRef: DocumentRef<T>, metadata: DocumentMetadata) => void,
79
88
  ) => Unsubscribe;
80
89
 
90
+ readonly onPresence: <T extends DocumentSchema>(
91
+ docRef: DocumentRef<T>,
92
+ callback: (docRef: DocumentRef<T>, event: PresenceEvent) => void,
93
+ options?: PresenceSubscriptionOptions,
94
+ ) => Unsubscribe;
95
+
81
96
  readonly onStateChange: <T extends DocumentSchema>(
82
97
  docRef: DocumentRef<T>,
83
98
  cb: (docRef: DocumentRef<T>) => void,
84
99
  ) => Unsubscribe;
85
100
 
101
+ readonly updateCustomPresence: <M extends Model>(
102
+ docRef: DocumentRef,
103
+ model: M,
104
+ eventData: ModelData<M>,
105
+ ) => void;
106
+
86
107
  readonly getRecordSnapshot: <R extends Model>(
87
108
  recordRef: RecordRef<R>,
88
109
  ) => Promise<ModelData<R>>;
@@ -112,6 +133,12 @@ export interface StateModule {
112
133
  partialState: Partial<ModelData<R>>,
113
134
  ) => Promise<void>;
114
135
 
136
+ readonly withTransaction: (
137
+ docRef: DocumentRef,
138
+ fn: () => void,
139
+ description?: EditDescription,
140
+ ) => void;
141
+
115
142
  readonly onRecordChanged: <M extends Model>(
116
143
  record: RecordRef<M>,
117
144
  callback: RecordChangeCallback<M>,
@@ -178,6 +205,13 @@ export class StateModuleImpl implements StateModule {
178
205
  return this.documentService.getDocumentSnapshot(docRef);
179
206
  }
180
207
 
208
+ onActivity<T extends DocumentSchema>(
209
+ docRef: DocumentRef<T>,
210
+ callback: (docRef: DocumentRef<T>, event: ActivityEvent) => void,
211
+ ): Unsubscribe {
212
+ return this.documentService.onActivity(docRef, callback);
213
+ }
214
+
181
215
  onMetadataChange<T extends DocumentSchema>(
182
216
  docRef: DocumentRef<T>,
183
217
  cb: (doc: DocumentRef<T>, metadata: DocumentMetadata) => void,
@@ -185,6 +219,14 @@ export class StateModuleImpl implements StateModule {
185
219
  return this.documentService.onMetadataChange(docRef, cb);
186
220
  }
187
221
 
222
+ onPresence<T extends DocumentSchema>(
223
+ docRef: DocumentRef<T>,
224
+ callback: (docRef: DocumentRef<T>, event: PresenceEvent) => void,
225
+ options?: PresenceSubscriptionOptions,
226
+ ): Unsubscribe {
227
+ return this.documentService.onPresence(docRef, callback, options);
228
+ }
229
+
188
230
  onStateChange<T extends DocumentSchema>(
189
231
  docRef: DocumentRef<T>,
190
232
  cb: (docRef: DocumentRef<T>) => void,
@@ -192,6 +234,14 @@ export class StateModuleImpl implements StateModule {
192
234
  return this.documentService.onStateChange(docRef, cb);
193
235
  }
194
236
 
237
+ updateCustomPresence<M extends Model>(
238
+ docRef: DocumentRef,
239
+ model: M,
240
+ eventData: ModelData<M>,
241
+ ): void {
242
+ this.documentService.updateCustomPresence(docRef, model, eventData);
243
+ }
244
+
195
245
  async getRecordSnapshot<R extends Model>(
196
246
  recordRef: RecordRef<R>,
197
247
  ): Promise<ModelData<R>> {
@@ -212,6 +262,14 @@ export class StateModuleImpl implements StateModule {
212
262
  return this.documentService.updateRecord(recordRef, partialState);
213
263
  }
214
264
 
265
+ withTransaction(
266
+ docRef: DocumentRef,
267
+ fn: () => void,
268
+ description?: EditDescription,
269
+ ): void {
270
+ this.documentService.withTransaction(docRef, fn, description);
271
+ }
272
+
215
273
  // Collection methods
216
274
  getCreateRecordCollectionRef<M extends Model>(
217
275
  docRef: DocumentRef,