Skip to content

Commit

Permalink
refactored AssetGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed Jan 8, 2025
1 parent fabfbc7 commit 6950a0f
Show file tree
Hide file tree
Showing 8 changed files with 966 additions and 821 deletions.
33 changes: 19 additions & 14 deletions crates/atlaspack/src/atlaspack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;
use std::sync::Arc;

use atlaspack_config::atlaspack_rc_config_loader::{AtlaspackRcConfigLoader, LoadConfigOptions};
use atlaspack_core::asset_graph::{AssetGraph, AssetNode};
use atlaspack_core::asset_graph::{AssetGraph, AssetGraphNode, AssetNode};
use atlaspack_core::config_loader::ConfigLoader;
use atlaspack_core::plugin::{PluginContext, PluginLogger, PluginOptions};
use atlaspack_core::types::AtlaspackOptions;
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Atlaspack {

let asset_graph = match request_result {
RequestResult::AssetGraph(result) => {
self.commit_assets(result.graph.assets.as_slice())?;
self.commit_assets(result.graph.nodes().collect())?;

result.graph
}
Expand All @@ -139,10 +139,14 @@ impl Atlaspack {
})
}

fn commit_assets(&self, assets: &[AssetNode]) -> anyhow::Result<()> {
fn commit_assets(&self, assets: Vec<&AssetGraphNode>) -> anyhow::Result<()> {
let mut txn = self.db.environment().write_txn()?;

for AssetNode { asset, .. } in assets.iter() {
for asset_node in assets {
let AssetGraphNode::Asset(AssetNode { asset, .. }) = asset_node else {
continue;
};

self.db.put(&mut txn, &asset.id, asset.code.bytes())?;
if let Some(map) = &asset.map {
// TODO: For some reason to_buffer strips data when rkyv was upgraded, so now we use json
Expand Down Expand Up @@ -185,25 +189,26 @@ mod tests {
rpc(),
)?;

let assets = ["foo", "bar", "baz"];

atlaspack.commit_assets(
&assets
.iter()
.enumerate()
.map(|(idx, asset)| AssetNode {
let assets_names = ["foo", "bar", "baz"];
let assets = assets_names
.iter()
.enumerate()
.map(|(idx, asset)| {
AssetGraphNode::Asset(AssetNode {
asset: Asset {
id: idx.to_string(),
code: Code::from(asset.to_string()),
..Asset::default()
},
requested_symbols: HashSet::new(),
})
.collect::<Vec<AssetNode>>(),
)?;
})
.collect::<Vec<AssetGraphNode>>();

atlaspack.commit_assets(assets.iter().collect())?;

let txn = db.environment().read_txn()?;
for (idx, asset) in assets.iter().enumerate() {
for (idx, asset) in assets_names.iter().enumerate() {
let entry = db.get(&txn, &idx.to_string())?;
assert_eq!(entry, Some(asset.to_string().into()));
}
Expand Down
Loading

0 comments on commit 6950a0f

Please sign in to comment.