Skip to content

Commit

Permalink
feat: implement basic create, upsert, update, delete operations
Browse files Browse the repository at this point in the history
  • Loading branch information
s-a-sen committed Jan 25, 2023
1 parent 2a340f6 commit 192eacb
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 11 deletions.
Binary file added browser/extension/images/get_started128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser/extension/images/get_started16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser/extension/images/get_started32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser/extension/images/get_started48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions browser/extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "Мониторим переключение вкладок",
"description": "Расширение для мониторинга вкладок",
"version": "1.0",
"manifest_version": 3,
"permissions": ["tabs", "scripting", "notifications"],
"action": {
"default_icon": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
},
"icons": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
},
"host_permissions": [
"http://0.0.0.0:8000"
],
"background": {
"service_worker": "service-worker.js",
"type": "module"
}
}
71 changes: 71 additions & 0 deletions browser/extension/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let activeTabId, lastUrl, lastTitle;

function getTabInfo(tabId) {
chrome.tabs.get(tabId, function(tab) {
if(lastUrl != tab.url || lastTitle != tab.title) {
console.log(lastUrl = tab.url, lastTitle = tab.title);
// sendTab();
}
});
}

chrome.tabs.onActivated.addListener(function(activeInfo) {
getTabInfo(activeTabId = activeInfo.tabId);
});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(activeTabId == tabId) {
getTabInfo(tabId);
}
});

//
// chrome.runtime.onInstalled.addListener(async () => {
// console.log(await getCurrentTab());
// chrome.notifications.create('NOTFICATION_ID', {
// type: 'basic',
// iconUrl: 'path',
// title: 'notification title',
// message: 'notification message',
// priority: 2
// });
// });
//
function sendTab() {
fetch('http://0.0.0.0:8000', {
method: 'post',
body: "{}"
}).then(function(r) {
return r.json();
}).then(function(data) {
console.log(data);
});
}
// //
// // function handleActivated(activeInfo) {
// // console.log(`Tab ${activeInfo.tabId} was activated`);
// // chrome.notifications.create('NOTFICATION_ID', {
// // type: 'basic',
// // iconUrl: 'path',
// // title: 'notification title',
// // message: 'notification message',
// // priority: 2
// // })
// // }
//
// browser.tabs.onActivated.addListener(() => {
// chrome.notifications.create('NOTFICATION_ID', {
// type: 'basic',
// iconUrl: 'path',
// title: 'notification title',
// message: 'notification message',
// priority: 2
// })
// });
//
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
77 changes: 66 additions & 11 deletions rust/yad_storage/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,64 @@ impl EntityRef {

#[async_trait::async_trait(?Send)]
pub trait PersistentEntity {
async fn create(&self,db: &Database);
async fn upsert(&self,db: &Database);
async fn create(&self,db: &Database) -> anyhow::Result<()>;
async fn upsert(&self,db: &Database) -> anyhow::Result<()>;
async fn update(&self, db: &Database) -> anyhow::Result<()>;
async fn delete(&self, db: &Database) -> anyhow::Result<()>;
}

#[async_trait::async_trait(?Send)]
impl<T> PersistentEntity for T where T: Entity {
async fn create(&self, db: &Database) {
async fn create(&self, db: &Database) -> anyhow::Result<()> {
let id = self.id();
if id.is_some() {
return;
return Ok(());
}
let sql: Query = Query::from("CREATE type::thing($table, $id) CONTENT $data;");
let vars = crate::vars! {
String::from("table") => Value::from(T::collection()),
String::from("id") => Value::from(self.id().unwrap_or(Database::next_id().to_raw())),
String::from("data") => Value::parse(serde_json::to_string(self).unwrap().as_str()),
};
db.execute(sql, Some(vars)).await.unwrap();
db.execute(sql, Some(vars)).await?;
Ok(())
}

async fn upsert(&self,db: &Database) {
async fn upsert(&self,db: &Database) -> anyhow::Result<()> {
let table = T::collection();
let sql = Query::from(format!("INSERT INTO {} $data;", table).as_str());
let vars = crate::vars! {
String::from("data") => Value::parse(serde_json::to_string(self).unwrap().as_str()),
};
db.execute(sql, Some(vars)).await.unwrap();
db.execute(sql, Some(vars)).await?;
Ok(())
}

async fn update(&self, db: &Database) -> anyhow::Result<()> {
if self.id().is_none() {
return Ok(());
}
let sql = Query::from("UPDATE type::thing($table, $id) CONTENT $data");
let vars = crate::vars! {
String::from("table") => Value::from(T::collection()),
String::from("id") => Value::from(self.id().unwrap()),
String::from("data") => Value::parse(serde_json::to_string(self).unwrap().as_str()),
};
db.execute(sql, Some(vars)).await?;
Ok(())
}

async fn delete(&self, db: &Database) -> anyhow::Result<()> {
if self.id().is_none() {
return Ok(());
}
let sql = Query::from("DELETE type::thing($table, $id)");
let vars = crate::vars! {
String::from("table") => Value::from(T::collection()),
String::from("id") => Value::from(self.id().unwrap()),
};
db.execute(sql, Some(vars)).await?;
Ok(())
}
}

Expand Down Expand Up @@ -90,10 +121,10 @@ mod tests {
async fn test_create() {
let db = Database::new("memory", None, None).await.unwrap();
let ent = Someth{id: Some("1".into()), count: 0};
ent.create(&db).await;
ent.create(&db).await.unwrap();

let ent = Someth{id: None, count: 0};
ent.create(&db).await;
ent.create(&db).await.unwrap();
let res = db.execute(Query::from("select * from someth;"), None).await.unwrap();
dbg!(&res);
}
Expand All @@ -102,10 +133,34 @@ mod tests {
async fn test_upsert() {
let db = Database::new("memory", None, None).await.unwrap();
let ent = Someth{id: Some("1".into()), count: 0};
ent.upsert(&db).await;
ent.upsert(&db).await.unwrap();

let ent = Someth{id: None, count: 0};
ent.upsert(&db).await;
ent.upsert(&db).await.unwrap();
let res = db.execute(Query::from("select * from someth;"), None).await.unwrap();
dbg!(&res);
}

#[async_std::test]
async fn test_update() {
let db = Database::new("memory", None, None).await.unwrap();
let ent = Someth{id: Some("1".into()), count: 0};
ent.create(&db).await.unwrap();

let ent = Someth{id: Some("1".into()), count: 1};
ent.update(&db).await.unwrap();
let res = db.execute(Query::from("select * from someth;"), None).await.unwrap();
dbg!(&res);
}

#[async_std::test]
async fn test_delete() {
let db = Database::new("memory", None, None).await.unwrap();
let ent = Someth{id: Some("1".into()), count: 0};
ent.create(&db).await.unwrap();

let ent = Someth{id: Some("1".into()), count: 1};
ent.delete(&db).await.unwrap();
let res = db.execute(Query::from("select * from someth;"), None).await.unwrap();
dbg!(&res);
}
Expand Down

0 comments on commit 192eacb

Please sign in to comment.