11import invariant from "tiny-invariant" ;
22
3+ import type { JsonValue } from "catcolab-api" ;
34import { type Api , type ExternRef , type LiveDoc , getLiveDoc } from "../api" ;
45import { type LiveDiagramDocument , getLiveDiagram } from "../diagram" ;
56import { type LiveModelDocument , getLiveModel } from "../model" ;
67import { type Notebook , newNotebook } from "../notebook" ;
78import type { TheoryLibrary } from "../stdlib" ;
89import type { Analysis } from "./types" ;
910
11+ type AnalysisType = "model" | "diagram" ;
12+
1013/** Common base type for all analysis documents. */
11- export type BaseAnalysisDocument = {
14+ export type BaseAnalysisDocument < T extends AnalysisType > = {
1215 type : "analysis" ;
1316
1417 /** User-defined name of analysis. */
1518 name : string ;
1619
1720 /** Reference to the document that the analysis is of. */
18- analysisOf : ExternRef ;
21+ analysisOf : ExternRef < T > ;
1922
2023 /** Content of the analysis.
2124
@@ -27,38 +30,25 @@ export type BaseAnalysisDocument = {
2730} ;
2831
2932/** A document defining an analysis of a model. */
30- export type ModelAnalysisDocument = BaseAnalysisDocument & {
31- analysisOf : { taxon : "model" } ;
32- } ;
33+ export type ModelAnalysisDocument = BaseAnalysisDocument < "model" > ;
3334
3435/** A document defining an analysis of a diagram. */
35- export type DiagramAnalysisDocument = BaseAnalysisDocument & {
36- analysisOf : { taxon : "diagram" } ;
37- } ;
36+ export type DiagramAnalysisDocument = BaseAnalysisDocument < "diagram" > ;
3837
3938/** A document defining an analysis. */
4039export type AnalysisDocument = ModelAnalysisDocument | DiagramAnalysisDocument ;
4140
42- /** Create an empty model analysis. */
43- export const newModelAnalysisDocument = ( refId : string ) : ModelAnalysisDocument => ( {
44- name : "" ,
45- type : "analysis" ,
46- analysisOf : {
47- tag : "extern-ref" ,
48- refId,
49- taxon : "model" ,
50- } ,
51- notebook : newNotebook ( ) ,
52- } ) ;
53-
54- /** Create an empty diagram analysis. */
55- export const newDiagramAnalysisDocument = ( refId : string ) : DiagramAnalysisDocument => ( {
41+ /** Create an empty analysis. */
42+ export const newAnalysisDocument = (
43+ taxon : AnalysisType ,
44+ refId : string ,
45+ ) : BaseAnalysisDocument < typeof taxon > => ( {
5646 name : "" ,
5747 type : "analysis" ,
5848 analysisOf : {
5949 tag : "extern-ref" ,
6050 refId,
61- taxon : "diagram" ,
51+ taxon,
6252 } ,
6353 notebook : newNotebook ( ) ,
6454} ) ;
@@ -94,6 +84,21 @@ export type LiveDiagramAnalysisDocument = {
9484/** An analysis document "live" for editing. */
9585export type LiveAnalysisDocument = LiveModelAnalysisDocument | LiveDiagramAnalysisDocument ;
9686
87+ /** Create a new analysis in the backend. */
88+ export async function createAnalysis ( type : AnalysisType , ofRefId : string , api : Api ) {
89+ const init = newAnalysisDocument ( type , ofRefId ) ;
90+
91+ const result = await api . rpc . new_ref . mutate ( {
92+ content : init as JsonValue ,
93+ permissions : {
94+ anyone : "Read" ,
95+ } ,
96+ } ) ;
97+ invariant ( result . tag === "Ok" , "Failed to create a new analysis" ) ;
98+
99+ return result . content ;
100+ }
101+
97102/** Retrieve an analysis and make it "live" for editing. */
98103export async function getLiveAnalysis (
99104 refId : string ,
0 commit comments