Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds back SSR #126

Merged
merged 15 commits into from
Aug 6, 2024
Prev Previous commit
Next Next commit
Adds instances
  • Loading branch information
mikesol committed Aug 3, 2024
commit 78d26f59562e1cbb0ec43bf3fb46deb7c1eb6013
1 change: 1 addition & 0 deletions deku-core/spago.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ package:
- web-events
- web-html
- web-uievents
- yoga-json
9 changes: 6 additions & 3 deletions deku-core/src/Deku/Core.purs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ instance Semigroup Nut where
append (Nut a) (Nut b) =
-- unrolled version of `fixed`
Nut $ mkEffectFn2 \psr di -> do
liftST $ runSTFn1 (un DOMInterpret di).initializeFixedRendering (un PSR psr).ancestry
liftST $ runSTFn1 (un DOMInterpret di).initializeFixedRendering
(un PSR psr).ancestry
-- first `Nut` should not handle any unsubs, they may still be needed for later elements
emptyScope <- liftST $ runSTFn3 newPSR
(Ancestry.fixed 0 (un PSR psr).ancestry)
Expand Down Expand Up @@ -593,7 +594,8 @@ useDynWith
-> Hook (DynControl value)
useDynWith elements options cont = Nut $ mkEffectFn2 \psr di' -> do

liftST $ runSTFn1 (un DOMInterpret di').initializeDynRendering (un PSR psr).ancestry
liftST $ runSTFn1 (un DOMInterpret di').initializeDynRendering
(un PSR psr).ancestry

Region region <- liftST $ (un StaticRegion (un PSR psr).region).region
span <- liftST $ runSTFn2 newSpan region.begin region.bump
Expand Down Expand Up @@ -683,7 +685,8 @@ fixed nuts = Nut $ mkEffectFn2 \psr di -> do
(un PSR psr).signalDisposalQueueShouldBeTriggered
(un PSR psr).region
aref <- liftST $ STRef.new (-1)
liftST $ runSTFn1 (un DOMInterpret di).initializeFixedRendering (un PSR psr).ancestry
liftST $ runSTFn1 (un DOMInterpret di).initializeFixedRendering
(un PSR psr).ancestry

let
handleNuts :: EffectFn1 Nut Unit
Expand Down
52 changes: 52 additions & 0 deletions deku-core/src/Deku/Internal/Ancestry.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ module Deku.Internal.Ancestry

import Prelude

import Control.Monad.Except (except)
import Data.Either (Either(..), either)
import Data.List.NonEmpty (singleton)
import Data.Maybe (Maybe(..))
import Data.String as String
import Foreign (ForeignError(..))
import Yoga.JSON as Yoga

-- Fixed and Element track total
data DekuAncestry
Expand All @@ -26,6 +31,42 @@ data DekuAncestry
| Fixed Int DekuAncestry
| Root

newtype AncestryHelperType = AncestryHelperType { t :: String }
newtype AncestryHelperIA = AncestryHelperIA { i :: Int, a :: DekuAncestry }

derive newtype instance Yoga.ReadForeign AncestryHelperType
derive newtype instance Yoga.WriteForeign AncestryHelperType
derive newtype instance Yoga.ReadForeign AncestryHelperIA
derive newtype instance Yoga.WriteForeign AncestryHelperIA

instance Yoga.ReadForeign DekuAncestry where
readImpl x = do
AncestryHelperType { t } <- Yoga.readImpl x
case t of
"Element" -> do
AncestryHelperIA { i, a } <- Yoga.readImpl x
pure $ Element i a
"Dyn" -> do
AncestryHelperIA { i, a } <- Yoga.readImpl x
pure $ Dyn i a
"Portal" -> do
AncestryHelperIA { i, a } <- Yoga.readImpl x
pure $ Portal i a
"Fixed" -> do
AncestryHelperIA { i, a } <- Yoga.readImpl x
pure $ Fixed i a
"Root" -> pure Root
_ -> except $ Left
(singleton $ ForeignError ("Unknown DekuAncestry type: " <> t))

instance Yoga.WriteForeign DekuAncestry where
writeImpl = case _ of
Element i a -> Yoga.writeImpl { i, a, t: "Element" }
Dyn i a -> Yoga.writeImpl { i, a, t: "Dyn" }
Portal i a -> Yoga.writeImpl { i, a, t: "Portal" }
Fixed i a -> Yoga.writeImpl { i, a, t: "Fixed" }
Root -> Yoga.writeImpl { t: "Root" }

instance Show DekuAncestry where
show (Element i a) = "Element " <> show i <> " " <> show a
show (Dyn i a) = "Dyn " <> show i <> " " <> show a
Expand All @@ -41,6 +82,15 @@ data Ancestry
{ rep :: String, lineage :: DekuAncestry, hasElementParent :: Boolean }
| FakeAncestry { rep :: String }

instance Yoga.ReadForeign Ancestry where
readImpl = map (either RealAncestry FakeAncestry) <<< Yoga.readImpl

instance Yoga.WriteForeign Ancestry where
writeImpl = toEither >>> Yoga.writeImpl
where
toEither (RealAncestry a) = Left a
toEither (FakeAncestry a) = Right a

instance Eq Ancestry where
eq (RealAncestry a) (RealAncestry b) = a.rep == b.rep
eq (FakeAncestry a) (FakeAncestry b) = a.rep == b.rep
Expand All @@ -59,6 +109,8 @@ instance Show Ancestry where

hasElementParent :: Ancestry -> Boolean
hasElementParent (RealAncestry a) = a.hasElementParent
-- todo: this is not correct, as a dyn would cancel this
-- not currently used, but fix it eventually
hasElementParent (FakeAncestry { rep }) = String.contains (String.Pattern "e")
rep

Expand Down
Loading
Loading