Skip to content

Commit 3b897ac

Browse files
committed
OPFS wip
1 parent be4fddf commit 3b897ac

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,22 @@ updateModel (InitUpdate ext) prevSt = do
132132
modelConsumerQueue = cons
133133
}
134134
if isJust ext
135-
then
135+
then do
136136
pushActionQueue
137137
nextSt
138138
. Instant
139139
. PureUpdate
140140
$ #modelLoading
141141
.~ False
142+
opfsRead nextSt
142143
else Jsm.selectStorage ("current-" <> vsn) $ \case
143-
Nothing ->
144+
Nothing -> do
144145
pushActionQueue nextSt
145146
. Instant
146147
. PureUpdate
147148
$ #modelLoading
148149
.~ False
150+
opfsRead nextSt
149151
Just uri -> do
150152
finSt <- newModel (nextSt ^. #modelWebOpts) (Just nextSt) uri
151153
pushActionQueue nextSt
@@ -156,6 +158,7 @@ updateModel (InitUpdate ext) prevSt = do
156158
& #modelLoading
157159
.~ False
158160
)
161+
opfsRead finSt
159162
pure
160163
$ ChanUpdate (const nextSt)
161164
]
@@ -480,3 +483,43 @@ syncUri uri = do
480483
. URI.parseURI
481484
$ URI.renderStr nextUri
482485
)
486+
487+
opfsRead :: Model -> JSM ()
488+
opfsRead st =
489+
when (opfsMax >= 0)
490+
. forM_ (zip [0 :: Int ..] $ zip (reverse [0 .. opfsMax]) assets)
491+
. uncurry
492+
$ \assetIdx (opfsIdx, asset) -> do
493+
let fields = fmap (^. #fieldPairValue) $ asset ^. #assetFieldPairs
494+
forM_ (zip [0 :: Int ..] fields) . uncurry $ \fieldIdx field -> do
495+
let optic =
496+
#modelState
497+
. #stAssets
498+
. ix assetIdx
499+
. #assetFieldPairs
500+
. ix fieldIdx
501+
. #fieldPairValue
502+
when (field ^. #fieldType == FieldTypeImage)
503+
. Jsm.opfsRead
504+
( "asset-"
505+
<> inspect @Unicode opfsIdx
506+
<> "-field-"
507+
<> inspect fieldIdx
508+
)
509+
. flip whenJust
510+
$ \uri ->
511+
pushActionQueue st
512+
. Instant
513+
. PureUpdate
514+
$ ( cloneTraversal optic
515+
. #fieldInput
516+
. #uniqueValue
517+
.~ uri
518+
)
519+
. ( cloneTraversal optic
520+
. #fieldOutput
521+
.~ DynamicFieldText uri
522+
)
523+
where
524+
assets = st ^. #modelState . #stAssets
525+
opfsMax = length assets - 1

ghcjs/miso-functora/js/main.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,28 @@ export async function opfsWrite(value, opfsName) {
8282
const stream = await handle.createWritable();
8383
await stream.write(value);
8484
await stream.close();
85-
console.log("OPFS success", opfsName, handle);
85+
console.log("OPFS write success", opfsName, handle);
8686
} catch (e) {
87-
console.log("OPFS failure", opfsName, e);
87+
console.log("OPFS write failure", opfsName, e);
8888
}
8989
return null;
9090
}
9191

92+
export async function opfsRead(opfsName) {
93+
try {
94+
const root = await navigator.storage.getDirectory();
95+
const handle = await root.getFileHandle(opfsName);
96+
const file = await handle.getFile();
97+
const uri = await file.text();
98+
const res = await selectDataUrl(uri);
99+
console.log("OPFS read success", opfsName, res);
100+
return res;
101+
} catch (e) {
102+
console.log("OPFS read failure", opfsName, e);
103+
return null;
104+
}
105+
}
106+
92107
export async function openBrowserPage(url) {
93108
try {
94109
return await Browser.open({ url: url, windowName: "_blank" });

ghcjs/miso-functora/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/miso-functora/src/Functora/Miso/Jsm/Generic.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Functora.Miso.Jsm.Generic
1111
selectBarcode,
1212
selectClipboard,
1313
selectFile,
14+
opfsRead,
1415
genericPromise,
1516
printCurrentPage,
1617
saveFileShow,
@@ -164,6 +165,12 @@ selectFile opfsName file after = do
164165
$ after
165166
. fmap strip
166167

168+
opfsRead :: Unicode -> (Maybe Unicode -> JSM ()) -> JSM ()
169+
opfsRead opfsName after = do
170+
genericPromise @[Unicode] @Unicode "opfsRead" [opfsName]
171+
$ after
172+
. fmap strip
173+
167174
genericPromise ::
168175
forall args res.
169176
( JS.MakeArgs args,

0 commit comments

Comments
 (0)