@@ -23,12 +23,12 @@ specified if nonstandard extensions are in use.
2323"""
2424struct Asset
2525 filetype:: String
26- name:: Union{Nothing, String}
26+ name:: String
2727 url:: String
2828end
2929
3030Asset (name, url) = Asset (getextension (url), name, url)
31- Asset (url) = Asset (nothing , url)
31+ Asset (url) = Asset (" " , url)
3232Asset (x:: Pair ) = Asset (string (x[1 ]), x[2 ])
3333Asset (x:: Asset ) = x
3434function Asset (spec:: Dict )
@@ -42,23 +42,21 @@ function Asset(spec::Dict)
4242 error (" Invalid Asset dict specification: missing key \" url\" ." )
4343 end
4444 filetype = get (spec, " type" , getextension (url))
45- name = get (spec, " name" , nothing )
45+ name = get (spec, " name" , " " )
4646
4747 return Asset (filetype, name, url)
4848end
4949
5050function JSON. lower (asset:: Asset )
51- Dict (
52- " type" => asset. filetype,
53- " url" => dep2url (asset. url),
54- " name" => asset. name,
55- )
51+ (type= asset. filetype, url= dep2url (asset. url), name= asset. name,)
5652end
5753
5854
5955function tojs (asset:: Asset )
60- return js " WebIO.importResource($(JSON.lower(asset)))"
56+ la = JSON. lower (asset)
57+ return _assettojs (la)
6158end
59+ _assettojs (la) = js " WebIO.importResource($la)"
6260
6361"""
6462 Sync(assets...)
@@ -79,12 +77,12 @@ julia> WebIO.Sync(Asset("foo.js"), "bar" => "bar.js")
7977Sync(Asset[Asset("js", nothing, "foo.js"), Asset("js", "bar", "bar.js")])
8078```
8179"""
82- struct Sync
83- # This is untyped because we can't define a union type that includes all of
84- # Asset, Sync, and Async that is then used within Sync and Async.
85- imports:: Array{Any}
86-
87- Sync (imports :: Array ) = new ([ ensure_asset (asset) for asset in imports])
80+ struct Sync{A <: Array }
81+ imports :: A
82+ Sync (imports :: A ) where A <: Array = begin
83+ assets = [ ensure_asset (asset) for asset in imports]
84+ new {typeof(assets)} (assets)
85+ end
8886end
8987Sync (assets... ) = Sync ([assets... ])
9088
@@ -99,32 +97,31 @@ constructor for an [`Asset`](@ref), or a [`Sync`](@ref) or [`Async`](@ref).
9997
10098If the imports need to be imported sequentially, use [`Sync`](@ref) instead.
10199"""
102- struct Async
103- # See comment about (lack of) typing in Sync above.
104- imports:: Array{Any}
100+ struct Async{A<: Array }
101+ imports:: A
105102
106- Async (imports:: Array ) = new ([ensure_asset (asset) for asset in imports])
103+ function Async (imports:: A ) where A<: Array
104+ assets = [ensure_asset (asset) for asset in imports]
105+ new {typeof(assets)} (assets)
106+ end
107107end
108108Async (assets... ) = Async ([assets... ])
109109
110110# This allows js"await $(Async([....]))" on a tree of assets!
111111function tojs (asset:: Union{Async,Sync} )
112- return js " WebIO.importBlock($(lowerassets(asset)))"
112+ lowered = lowerassets (asset)
113+ return _synctojs (lowered)
113114end
115+ # Function barrier
116+ _synctojs (la) = js " WebIO.importBlock($la)"
114117
115118# The output of lowerassets is initially sent with the Scope
116119# this should trigger loading of the assets before onmount callbacks
117120lowerassets (x) = JSON. lower (Asset (x))
118121lowerassets (x:: Asset ) = JSON. lower (x)
119- lowerassets (x:: Async ) = Dict (
120- " type" => " async_block" ,
121- " data" => map (lowerassets, x. imports),
122- )
122+ lowerassets (x:: Async ) = (type= " async_block" , data= map (lowerassets, x. imports))
123123lowerassets (x:: AbstractArray ) = lowerassets (Async (x))
124- lowerassets (x:: Sync ) = Dict (
125- " type" => " sync_block" ,
126- " data" => map (lowerassets, x. imports),
127- )
124+ lowerassets (x:: Sync ) = (type= " sync_block" , data= map (lowerassets, x. imports))
128125
129126"""
130127 ensure_asset(asset)
0 commit comments