@audiotool/nexus 0.0.10 → 0.0.11

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.
@@ -29,22 +29,10 @@ export type AudiotoolClient = {
29
29
  * @returns Promise resolving to a SyncedDocument instance
30
30
  */
31
31
  createSyncedDocument: (opts: {
32
- /** Run in online mode, which means the document state is synced in real time. */
33
- mode: "online";
34
32
  /** The project to sync to; this can be anything containing a project's UUID, e.g. the URL of the studio
35
33
  * when the project is open in the browser.
36
34
  */
37
35
  project: string;
38
- } | {
39
- /** Run in offline mode, i.e. not synced to any particular project.
40
- *
41
- * All changes are discarded on reload.
42
- */
43
- mode: "offline";
44
- /** Whether transaction validation is on. If off, fewer transaction errors will occur,
45
- * but the document might not get accepted by the backend if connecting to a project.
46
- */
47
- validated: boolean;
48
36
  }) => Promise<SyncedDocument>;
49
37
  /**
50
38
  * Collection of Audiotool API service clients.
@@ -41,8 +41,12 @@ export interface NexusGateway {
41
41
  * This function must indicate that the current document state was loaded by returning at least
42
42
  * one transaction. If the document state is empty, that transaction can be empty. The studio
43
43
  * will show the loading spinner until the first transaction is received.
44
+ *
45
+ * After the first transaction, the gateway can return "done" to indicate that no further transactions
46
+ * will be returned, which the caller can use as a signal to stop polling. This is useful in situations
47
+ * where it's known that no synchronization transactions will ever be returned, such as when running in offline mode.
44
48
  */
45
- synchronize(): ReadonlyTransaction[];
49
+ synchronize(): ReadonlyTransaction[] | "done";
46
50
  /** This becomes true when the gateway is blocked in some way from syncing with upstream state.
47
51
  * The document should be locked in this case to prevent data loss.
48
52
  */
@@ -29,3 +29,5 @@
29
29
  export { createAudiotoolClient, type AudiotoolClient, } from '../audiotool-client';
30
30
  export type { SyncedDocument } from '../synced-document';
31
31
  export { getLoginStatus, type LoginStatus } from '../login-status';
32
+ export { createOfflineDocument } from '../synced-document';
33
+ export type { OfflineDocument } from '../synced-document';
package/dist/index.js CHANGED
@@ -2297,7 +2297,7 @@ let NexusEventManager = bt;
2297
2297
  const sizeOf = (i) => i.values().reduce((e, t) => e + t.size, 0), getOrDefault = (i, e) => i.get(e) ?? i.set(e, /* @__PURE__ */ new Set()).get(e) ?? throw_(), mockNexusGateway = () => {
2298
2298
  let i = !1, e = !1;
2299
2299
  return {
2300
- synchronize: () => e ? [] : (e = !0, [new Transaction()]),
2300
+ synchronize: () => e ? "done" : (e = !0, [new Transaction()]),
2301
2301
  send: () => {
2302
2302
  if (i)
2303
2303
  throw new Error("Gateway terminated");
@@ -3306,6 +3306,10 @@ class NexusDocument {
3306
3306
  resolve: r
3307
3307
  } = Promise.withResolvers(), c = asyncInterval(async () => {
3308
3308
  const u = (s(this, M) ?? throw_()).synchronize();
3309
+ if (u === "done")
3310
+ throw new Error(
3311
+ "Gateway returned 'done' before returning the initial transaction."
3312
+ );
3309
3313
  if (u.length === 0)
3310
3314
  return;
3311
3315
  const f = u.flatMap((m) => m.modifications).length;
@@ -3327,6 +3331,10 @@ class NexusDocument {
3327
3331
  if (u.aborted)
3328
3332
  return;
3329
3333
  const f = await s(this, S).acquire(), m = (s(this, M) ?? throw_()).synchronize();
3334
+ if (m === "done") {
3335
+ d.terminate(), f.release();
3336
+ return;
3337
+ }
3330
3338
  await this._applyIncomingTransactions(m, { _takeTransactionLock: !1 }), f.release();
3331
3339
  }, s(this, Nt));
3332
3340
  g(this, Bt, () => d.terminate());
@@ -3558,8 +3566,19 @@ const DocumentService = {
3558
3566
  kind: MethodKind.Unary
3559
3567
  }
3560
3568
  }
3569
+ }, createSyncedDocument = (i, e, t, n) => ({
3570
+ connected: e,
3571
+ createTransaction: () => i.createTransaction(),
3572
+ modify: (o) => i.modify(o),
3573
+ queryEntities: i.queryEntitiesWithoutLock,
3574
+ events: i.events,
3575
+ start: async () => i.takeTransactions({ validator: t, gateway: n }),
3576
+ stop: async () => i.terminate()
3577
+ }), createOfflineDocument = async (i) => {
3578
+ const e = (i == null ? void 0 : i.validated) ?? !0 ? await createWasmNexusValidator() : mockNexusValidator(), t = mockNexusGateway(), n = new NexusDocument(), o = new ValueNotifier(!0), r = createSyncedDocument(n, o, e, t);
3579
+ return await r.start(), r;
3561
3580
  }, createOnlineDocument = async (i, e, t) => {
3562
- var u;
3581
+ var f;
3563
3582
  let n = createWasmNexusValidator();
3564
3583
  const o = await i.projectService.openSession({
3565
3584
  projectName: e
@@ -3569,30 +3588,17 @@ const DocumentService = {
3569
3588
  const r = createRetryingPromiseClient(
3570
3589
  DocumentService,
3571
3590
  createAuthorizedKeepaliveTransport({
3572
- baseUrl: ((u = o.session) == null ? void 0 : u.documentServiceUrl) ?? throw_("backend returned no document service url"),
3591
+ baseUrl: ((f = o.session) == null ? void 0 : f.documentServiceUrl) ?? throw_("backend returned no document service url"),
3573
3592
  useBinaryFormat: !0,
3574
3593
  getToken: t
3575
3594
  })
3576
- );
3577
- await n;
3578
- const c = createCollabGateway(
3595
+ ), c = await n, l = createCollabGateway(
3579
3596
  r,
3580
3597
  await getWasmDocumentState(),
3581
3598
  e
3582
- ), l = new NexusDocument(), d = new ValueNotifier(!1);
3583
- return c.blocked.subscribe((f) => d.setValue(!f)), createSyncedDocument(l, d, await n, c);
3584
- }, createOfflineDocument = async (i) => {
3585
- const e = i ? await createWasmNexusValidator() : mockNexusValidator(), t = mockNexusGateway(), n = new NexusDocument(), o = new ValueNotifier(!0);
3586
- return createSyncedDocument(n, o, e, t);
3587
- }, createSyncedDocument = (i, e, t, n) => ({
3588
- connected: e,
3589
- createTransaction: () => i.createTransaction(),
3590
- modify: (o) => i.modify(o),
3591
- queryEntities: i.queryEntitiesWithoutLock,
3592
- events: i.events,
3593
- start: async () => i.takeTransactions({ validator: t, gateway: n }),
3594
- stop: async () => i.terminate()
3595
- }), createAudiotoolClient = async ({
3599
+ ), d = new NexusDocument(), u = new ValueNotifier(!1);
3600
+ return l.blocked.subscribe((m) => u.setValue(!m)), createSyncedDocument(d, u, c, l);
3601
+ }, createAudiotoolClient = async ({
3596
3602
  authorization: i
3597
3603
  }) => {
3598
3604
  const e = async () => {
@@ -3607,12 +3613,9 @@ const DocumentService = {
3607
3613
  }, t = await createAudiotoolAPI(e);
3608
3614
  return {
3609
3615
  api: t,
3610
- createSyncedDocument: async (n) => {
3611
- if (n.mode === "online") {
3612
- const o = extractProjectName(n.project);
3613
- return await createOnlineDocument(t, o, e);
3614
- } else
3615
- return await createOfflineDocument(n.validated);
3616
+ createSyncedDocument: async ({ project: n }) => {
3617
+ const o = extractProjectName(n);
3618
+ return await createOnlineDocument(t, o, e);
3616
3619
  }
3617
3620
  };
3618
3621
  }, extractProjectName = (i) => {
@@ -3815,5 +3818,6 @@ const DocumentService = {
3815
3818
  };
3816
3819
  export {
3817
3820
  createAudiotoolClient,
3821
+ createOfflineDocument,
3818
3822
  getLoginStatus
3819
3823
  };
@@ -1,8 +1,11 @@
1
+ import { AudiotoolAPI } from './api/audiotool-api';
2
+ import { NexusGateway } from './document/backend/gateway';
3
+ import { NexusValidator } from './document/backend/validator';
4
+ import { NexusDocument } from './document/document';
1
5
  import { NexusEventManager } from './document/event-manager';
2
6
  import { EntityQuery } from './document/query/entity';
3
7
  import { SafeTransactionBuilder, TransactionBuilder } from './document/transaction-builder';
4
8
  import { ValueNotifier } from './utils/observable-notifier-value';
5
- import { AudiotoolAPI } from './api/audiotool-api';
6
9
 
7
10
  /**
8
11
  * An Audiotool project document that synchronizes in real-time with the backend.
@@ -150,6 +153,26 @@ export type SyncedDocument = {
150
153
  **/
151
154
  stop: () => Promise<void>;
152
155
  };
153
- /** Create an online document. */
156
+ /** Create a SyncedDocument wrapper from a NexusDocument. */
157
+ export declare const createSyncedDocument: (document: NexusDocument, connected: ValueNotifier<boolean>, validator: NexusValidator, gateway: NexusGateway) => SyncedDocument;
158
+ /** An offline version of a {@link SyncedDocument} - starts out completely empty,
159
+ * and all changes are discarded on reload/shutdown.
160
+ *
161
+ * Doesn't have start/stop methods since no syncing is happening.
162
+ *
163
+ * Can be safely thrown away after usage.
164
+ */
165
+ export type OfflineDocument = Omit<SyncedDocument, "start" | "stop">;
166
+ /**
167
+ * Create a nexus document that is not synced to the backend; all changes are discarded on reload/shutdown.
168
+ *
169
+ * The returned document is ready to be modified immediately and can be thrown away without calling start/stop.
170
+ *
171
+ * To create a document that is synced with a state from the backend/DAW, use {@link AudiotoolClient.createSyncedDocument}.
172
+ * */
173
+ export declare const createOfflineDocument: (opts?: {
174
+ /** Whether validation is enabled. Turning that off results in fewer transaction errors, but can lead to invalid states. */
175
+ validated?: boolean;
176
+ }) => Promise<OfflineDocument>;
177
+ /** Create a SyncedDocument wrapper that's connected to the backend. */
154
178
  export declare const createOnlineDocument: (api: AudiotoolAPI, projectName: string, getToken: () => Promise<string>) => Promise<SyncedDocument>;
155
- export declare const createOfflineDocument: (validated: boolean) => Promise<SyncedDocument>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audiotool/nexus",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Audiotool Nexus SDK",