Skip to content

Commit

Permalink
feat: rename set to jsset
Browse files Browse the repository at this point in the history
  • Loading branch information
srghma committed Oct 7, 2020
1 parent bd7744e commit 2f5dc52
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 58 deletions.
19 changes: 11 additions & 8 deletions src/Halogen/VDom/DOM/Prop/Checkers.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import Effect.Uncurried as EFn
import Halogen.VDom.Attributes (attributes, forEachE) as Attributes
import Halogen.VDom.DOM.Prop.Types (Prop, PropValue)
import Halogen.VDom.DOM.Prop.Util (unsafeGetProperty)
import Halogen.VDom.Set as Set
import Halogen.VDom.JsSet (JsSet)
import Halogen.VDom.JsSet as JsSet
import Halogen.VDom.Types (Namespace)
import Halogen.VDom.Util as Util
import Web.DOM.Element (Element) as DOM
Expand All @@ -39,16 +40,18 @@ checkPropExistsAndIsEqual propName expectedPropValue element = do
throwException $ error $ "Expected element to have a prop " <> Util.quote propName <> " eq to " <> Util.quote (Util.anyToString expectedPropValue) <> ", but it was equal to " <> Util.quote (Util.anyToString propValue) <> " (check warning above for more information)"

-- | Inspired by https://github.com/facebook/react/blob/823dc581fea8814a904579e85a62da6d18258830/packages/react-dom/src/client/ReactDOMComponent.js#L1030
mkExtraAttributeNames DOM.Element Effect (Set.Set String)
mkExtraAttributeNames DOM.Element Effect (JsSet String)
mkExtraAttributeNames el = do
let
namedNodeMap = Attributes.attributes el
(set Set.Set String) ← Set.empty
EFn.runEffectFn2 Attributes.forEachE namedNodeMap (EFn.mkEffectFn1 \attribute → EFn.runEffectFn2 Set.add attribute.name set)
(set JsSet String) ← JsSet.empty
EFn.runEffectFn2 Attributes.forEachE namedNodeMap (EFn.mkEffectFn1 \attribute → EFn.runEffectFn2 JsSet._add attribute.name set)
pure set

checkExtraAttributeNamesIsEmpty forall a . Array (Prop a) -> Set.Set String -> DOM.Element -> Effect Unit
checkExtraAttributeNamesIsEmpty propsToHydrate extraAttributeNames element =
when (Set.size extraAttributeNames > 0) do
checkExtraAttributeNamesIsEmpty forall a . Array (Prop a) -> JsSet String -> DOM.Element -> Effect Unit
checkExtraAttributeNamesIsEmpty propsToHydrate extraAttributeNames element = do
size <- EFn.runEffectFn1 JsSet._size extraAttributeNames
when (size > 0) do
EFn.runEffectFn2 Util.warnAny "Error info: " { extraAttributeNames, element, propsToHydrate }
throwException $ error $ "Extra attributes from the server: " <> (Set.toArray extraAttributeNames # joinWith ", ") <> " (check warning above for more information)"
extraAttributeNames' <- EFn.runEffectFn1 JsSet._toArray extraAttributeNames
throwException $ error $ "Extra attributes from the server: " <> joinWith ", " extraAttributeNames' <> " (check warning above for more information)"
13 changes: 7 additions & 6 deletions src/Halogen/VDom/DOM/Prop/Implementation.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import Foreign.Object as Object
import Halogen.VDom.DOM.Prop.Checkers (checkAttributeExistsAndIsEqual, checkPropExistsAndIsEqual)
import Halogen.VDom.DOM.Prop.Types (ElemRef(..), EmitterInputBuilder, EventListenerAndCurrentEmitterInputBuilder, Prop(..), PropValue)
import Halogen.VDom.DOM.Prop.Util (removeProperty, setProperty, unsafeGetProperty)
import Halogen.VDom.Set as Set
import Halogen.VDom.JsSet (JsSet)
import Halogen.VDom.JsSet as JsSet
import Halogen.VDom.Util (STObject', anyToString, fullAttributeName, quote)
import Halogen.VDom.Util as Util
import Web.DOM.Element (Element) as DOM
Expand All @@ -24,24 +25,24 @@ import Web.Event.EventTarget (eventListener, EventListener) as DOM
import Foreign (unsafeToForeign, typeOf)
import Unsafe.Coerce (unsafeCoerce)

deleteRequiredElement :: EFn.EffectFn2 String (Set.Set String) Unit
deleteRequiredElement :: EFn.EffectFn2 String (JsSet String) Unit
deleteRequiredElement = EFn.mkEffectFn2 \element extraAttributeNames -> do
let isPresent = Fn.runFn2 Set.has element extraAttributeNames
isPresent <- EFn.runEffectFn2 JsSet._has element extraAttributeNames
if isPresent
then EFn.runEffectFn2 Set.delete element extraAttributeNames
then EFn.runEffectFn2 JsSet._delete element extraAttributeNames
else do
EFn.runEffectFn2 Util.warnAny "Error info: " { element, extraAttributeNames }
throwException $ error $ "Cannot delete element " <> quote element <> " that is not present in extraAttributeNames (check warning above for more information)"

checkPropExistsAndIsEqualAndDelete :: EFn.EffectFn5 (Set.Set String) String PropValue DOM.Element String Unit
checkPropExistsAndIsEqualAndDelete :: EFn.EffectFn5 (JsSet String) String PropValue DOM.Element String Unit
checkPropExistsAndIsEqualAndDelete = EFn.mkEffectFn5 \extraAttributeNames propName val el correspondingAttributeName -> do
checkPropExistsAndIsEqual propName val el
EFn.runEffectFn2 deleteRequiredElement correspondingAttributeName extraAttributeNames

hydrateApplyProp
a
. Fn.Fn4
(Set.Set String)
(JsSet String)
DOM.Element
(a Effect Unit)
(STObject' (EventListenerAndCurrentEmitterInputBuilder a))
Expand Down
23 changes: 23 additions & 0 deletions src/Halogen/VDom/JsSet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
exports.empty = function() {
return new Set()
}

exports._delete = function(value, set) {
set.delete(value)
}

exports._add = function(value, set) {
set.add(value)
}

exports._size = function(set) {
return set.size
}

exports._has = function(value, set) {
return set.has(value)
}

exports._toArray = function(set) {
return Array.from(set)
}
20 changes: 20 additions & 0 deletions src/Halogen/VDom/JsSet.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Halogen.VDom.JsSet where

import Prelude

import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2) as EFn

data JsSet a

foreign import empty a . Effect (JsSet a)

foreign import _delete a . EFn.EffectFn2 a (JsSet a) Unit

foreign import _add a . EFn.EffectFn2 a (JsSet a) Unit

foreign import _size a . EFn.EffectFn1 (JsSet a) Int

foreign import _has a . EFn.EffectFn2 a (JsSet a) Boolean

foreign import _toArray a . EFn.EffectFn1 (JsSet a) (Array a)
23 changes: 0 additions & 23 deletions src/Halogen/VDom/Set.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/Halogen/VDom/Set.purs

This file was deleted.

0 comments on commit 2f5dc52

Please sign in to comment.