Mountain/IPC/WindServiceHandlers/Model/
ModelUpdateContent.rs1
2use std::sync::Arc;
11
12use serde_json::{Value, json};
13
14use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
15
16pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
17 let Uri = Arguments
18 .first()
19 .and_then(|V| V.as_str())
20 .ok_or("model:updateContent requires uri".to_string())?
21 .to_owned();
22
23 let NewContent = Arguments
24 .get(1)
25 .and_then(|V| V.as_str())
26 .ok_or("model:updateContent requires content".to_string())?
27 .to_owned();
28
29 let (NewVersion, LanguageId) = match RunTime.Environment.ApplicationState.Feature.Documents.Get(&Uri) {
30 None => return Err(format!("model:updateContent - model not open: {}", Uri)),
31
32 Some(mut Document) => {
33 Document.Version += 1;
34
35 Document.Lines = NewContent.lines().map(|L| L.to_owned()).collect();
36
37 Document.IsDirty = true;
38
39 let Version = Document.Version;
40
41 let LangId = Document.LanguageIdentifier.clone();
42
43 RunTime
44 .Environment
45 .ApplicationState
46 .Feature
47 .Documents
48 .AddOrUpdate(Uri.clone(), Document);
49
50 (Version, LangId)
51 },
52 };
53
54 let UriForCocoon = Uri.clone();
56 let ContentForCocoon = NewContent.clone();
57 let VersionForCocoon = NewVersion;
58 tokio::spawn(async move {
59 let _ = crate::Vine::Client::SendNotification::Fn(
60 "cocoon-main".to_string(),
61 "$acceptModelChanged".to_string(),
62 serde_json::json!([
63 { "external": UriForCocoon, "$mid": 1 },
64 { "content": ContentForCocoon, "versionId": VersionForCocoon, "isDirty": true, "changes": [] }
65 ]),
66 )
67 .await;
68 });
69
70 Ok(json!({
71 "uri": Uri,
72 "content": NewContent,
73 "version": NewVersion,
74 "languageId": LanguageId,
75 }))
76}