MCPcopy Create free account
hub / github.com/Snapchat/Valdi / loadAsset

Method loadAsset

valdi/src/valdi/runtime/Resources/BytesAssetLoader.cpp:66–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66Shared<snap::valdi_core::Cancelable> BytesAssetLoader::loadAsset(const Value& requestPayload,
67 int32_t preferredWidth,
68 int32_t preferredHeight,
69 const Value& associatedData,
70 const Ref<AssetLoaderCompletion>& completion) {
71 class BytesAssetTransfomer : public IRemoteDownloaderItemHandler {
72 public:
73 ~BytesAssetTransfomer() override = default;
74
75 Result<Value> transform(const StringBox& localFilename, const BytesView& data) const override {
76 return Value(Valdi::makeShared<BytesAsset>(data));
77 }
78
79 std::string_view getItemTypeDescription() const override {
80 return "bytes asset";
81 }
82 };
83
84 static BytesAssetTransfomer transformer;
85
86 auto url = requestPayload.toStringBox();
87
88 StringBox fileName = makeLocalFilename(url);
89
90 auto cancel = Valdi::makeShared<SimpleAtomicCancelable>();
91
92 _downloader->enqueue(fileName,
93 url,
94 transformer,
95 BytesView(),
96 [url, completion, weakThis = Valdi::weakRef(this), cancel](auto result, auto /*loadSource*/) {
97 Result<Ref<LoadedAsset>> retval;
98
99 if (result.failure()) {
100 retval = result.error();
101 } else {
102 retval = result.value().template getTypedRef<LoadedAsset>();
103 if (auto strongThis = weakThis.lock()) {
104 strongThis->_downloader->removeItem(url);
105 }
106 }
107
108 if (!cancel->wasCanceled()) {
109 completion->onLoadComplete(retval);
110 }
111 });
112
113 return cancel.toShared();
114}
115
116} // namespace Valdi

Callers 2

startLoadIfNeededMethod · 0.45
downloadItemMethod · 0.45

Calls 13

makeLocalFilenameFunction · 0.85
weakRefFunction · 0.85
removeItemMethod · 0.80
toSharedMethod · 0.80
errorMethod · 0.65
valueMethod · 0.65
BytesViewClass · 0.50
toStringBoxMethod · 0.45
enqueueMethod · 0.45
failureMethod · 0.45
lockMethod · 0.45
wasCanceledMethod · 0.45

Tested by

no test coverage detected