@malloy-publisher/sdk 0.4.0 → 0.4.1

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.
@@ -19,10 +19,39 @@
19
19
  * ask for one kind. Everything else a backend may know about a document, who
20
20
  * changed it and when, who may see it, stays with the backend.
21
21
  *
22
+ * A host hands the SDK ONE storage and keeps handing it the same one. What a
23
+ * storage says about its workspaces decides how the editor routes a save, and
24
+ * the editor asks once, when it opens; a host that re-creates its storage mid
25
+ * session leaves the editor routing by the old answer until the new list
26
+ * resolves.
27
+ *
22
28
  * Every method rejects rather than returning a sentinel when the document is
23
- * not there, so a caller can tell "missing" from "empty".
29
+ * not there, so a caller can tell "missing" from "empty". Absence rejects with
30
+ * a {@link DocumentNotFoundError} specifically, so a caller can tell either of
31
+ * those from a backend that could not be reached: a failed read answered with
32
+ * "there is no document" reads as "the package is the only copy", and saving
33
+ * on that belief overwrites the copy that was actually there.
24
34
  */
25
35
 
36
+ /**
37
+ * Absence, and only absence: the document is not at that locator. Every other
38
+ * rejection means the backend could not answer, which is not the same thing and
39
+ * must not be treated as one.
40
+ */
41
+ export class DocumentNotFoundError extends Error {
42
+ constructor(message: string) {
43
+ super(message);
44
+ // Not `instanceof`: the `es` and `cjs` builds carry their own copy of
45
+ // this class, so an error thrown by one fails the check in the other.
46
+ this.name = "DocumentNotFoundError";
47
+ }
48
+ }
49
+
50
+ /** Whether a rejection means absence rather than a backend that could not answer. */
51
+ export const isDocumentNotFound = (error: unknown): boolean =>
52
+ error instanceof DocumentNotFoundError ||
53
+ (error instanceof Error && error.name === "DocumentNotFoundError");
54
+
26
55
  /**
27
56
  * The kinds of document the SDK authors. A `dashboard` is a
28
57
  * `dashboards/<slug>.malloy` file; a `notebook` is a `.malloynb`.
@@ -34,7 +63,33 @@ export interface Workspace {
34
63
  name: string;
35
64
  /** False for a workspace this reader may list and open but not save into. */
36
65
  writeable: boolean;
66
+ /**
67
+ * What this place is, in the backend's own words, for the editor to show a
68
+ * reader rather than guessing on their behalf.
69
+ */
37
70
  description: string;
71
+ /**
72
+ * This workspace holds the package's system of record: what it keeps for a
73
+ * document IS that document, and the package file is a copy of it or a
74
+ * deploy behind it.
75
+ *
76
+ * Left out, the workspace is a place a reader's work is kept beside the
77
+ * package — the Console's browser copy, a scratch space — and the package
78
+ * file is the record.
79
+ *
80
+ * The contract, which the editor relies on and does not police:
81
+ *
82
+ * - At most one workspace of a storage declares itself authoritative. The
83
+ * editor takes the first it is given and does not arbitrate between two.
84
+ * - It is orthogonal to {@link Workspace.writeable} and to the server's
85
+ * `mutable`. A workspace can be the record and still be read-only to this
86
+ * reader, and a server that refuses writes to its own package says
87
+ * nothing about where the record lives.
88
+ * - A host whose authority differs per package mounts one storage per
89
+ * context rather than flipping this between renders; the editor reads it
90
+ * once, when it opens.
91
+ */
92
+ authoritative?: boolean;
38
93
  }
39
94
 
40
95
  /** One document's address: which workspace, which kind, and its path there. */
@@ -59,15 +114,33 @@ export interface DocumentStorage {
59
114
  type?: DocumentType,
60
115
  ): Promise<DocumentLocator[]>;
61
116
 
62
- /** The document's content. Rejects when there is no document there. */
117
+ /**
118
+ * The document's content. Rejects with a {@link DocumentNotFoundError} when
119
+ * there is no document there, and with anything else when the backend could
120
+ * not be asked.
121
+ */
63
122
  getDocument(locator: DocumentLocator): Promise<string>;
64
123
 
65
- /** Write the content, creating the document or replacing it. */
124
+ /**
125
+ * Write the content, creating the document or replacing it.
126
+ *
127
+ * There is no expected-version slot, so this is last writer wins and a
128
+ * caller cannot tell that it overwrote someone. A backend that has to stop
129
+ * two authors clobbering each other enforces that itself, in the backend.
130
+ */
66
131
  saveDocument(locator: DocumentLocator, content: string): Promise<void>;
67
132
 
68
- /** Remove the document. Rejects when there is no document there. */
133
+ /**
134
+ * Remove the document. Rejects with a {@link DocumentNotFoundError} when
135
+ * there is no document there, and with anything else when the backend could
136
+ * not be asked — a caller must not read the second as the first and report
137
+ * the document gone.
138
+ */
69
139
  deleteDocument(locator: DocumentLocator): Promise<void>;
70
140
 
71
- /** Rename or relocate a document, keeping its content. Rejects when `from` is missing. */
141
+ /**
142
+ * Rename or relocate a document, keeping its content. Rejects with a
143
+ * {@link DocumentNotFoundError} when `from` is missing.
144
+ */
72
145
  moveDocument(from: DocumentLocator, to: DocumentLocator): Promise<void>;
73
146
  }
@@ -7,6 +7,7 @@ export type {
7
7
  DocumentType,
8
8
  Workspace,
9
9
  } from "./DocumentStorage";
10
+ export { DocumentNotFoundError, isDocumentNotFound } from "./DocumentStorage";
10
11
  export {
11
12
  DocumentStorageProvider,
12
13
  useDocumentStorage,
@@ -64,21 +64,26 @@ export default function Package({
64
64
  const [schemaDatabase, setSchemaDatabase] = useState<Database | null>(null);
65
65
  const [creating, setCreating] = useState(false);
66
66
 
67
- // Dashboards saved in this browser and not (yet) in the package: the
68
- // builder's drafts, listed so they are found rather than stumbled on.
67
+ // Dashboards the host keeps for this package and the package does not have:
68
+ // the builder's drafts, listed so they are found rather than stumbled on.
69
+ // Each carries the workspace's own description, because where a document is
70
+ // kept is the backend's to say and only one host's answer is "this browser".
69
71
  const storage = useOptionalDocumentStorage()?.documentStorage;
70
- const [drafts, setDrafts] = useState<DocumentLocator[]>([]);
72
+ const [drafts, setDrafts] = useState<
73
+ { locator: DocumentLocator; where: string }[]
74
+ >([]);
71
75
  const draftPrefix = `${environmentName}/${packageName}/dashboards/`;
72
76
  const refreshDrafts = useCallback(async () => {
73
77
  if (!storage) return;
74
78
  const workspaces = await storage.listWorkspaces(true);
75
- const found: DocumentLocator[] = [];
79
+ const found: { locator: DocumentLocator; where: string }[] = [];
76
80
  for (const workspace of workspaces)
77
81
  for (const locator of await storage.listDocuments(
78
82
  workspace,
79
83
  "dashboard",
80
84
  ))
81
- if (locator.path.startsWith(draftPrefix)) found.push(locator);
85
+ if (locator.path.startsWith(draftPrefix))
86
+ found.push({ locator, where: workspace.description });
82
87
  setDrafts(found);
83
88
  }, [storage, draftPrefix]);
84
89
  useEffect(() => {
@@ -391,12 +396,12 @@ export default function Package({
391
396
  )}
392
397
  {drafts.length > 0 && (
393
398
  <PackageSection title="Drafts" count={drafts.length}>
394
- {drafts.map((locator) => (
399
+ {drafts.map(({ locator, where }) => (
395
400
  <PackageItemRow
396
401
  key={locator.path}
397
402
  type="dashboard"
398
403
  label={draftSlug(locator)}
399
- rightLabel="saved in this browser"
404
+ rightLabel={where}
400
405
  onClick={(event) =>
401
406
  onClick(
402
407
  `/${environmentName}/${packageName}/dashboards/${encodeURIComponent(draftSlug(locator))}/edit`,