trying to get the sds lenses to work
[mTask.git] / Shares / mTaskShare.icl
index 481d3ad..edd84d0 100644 (file)
@@ -6,6 +6,7 @@ import Utils.Devices
 import iTasks
 import mTask
 import Data.List
+import Data.Tuple
 from Data.Func import $
 from StdFunc import flip
 
@@ -66,6 +67,8 @@ makeShare withTask identifier value = {MTaskShare
                ,value=value
                }
 
+if` i t e = if i t e
+
 updateShareFromPublish :: MTaskDevice Int BCValue -> Task [MTaskShare]
 updateShareFromPublish dev ident val = updateShares dev $ map $ up ident val
        where
@@ -90,33 +93,36 @@ cleanSharesTask taskid d = updateShares d id //TODO
 //             up :: [String] MTaskShare -> MTaskShare
 //             up ns s = {MTaskShare | s & withTask=[t\\t<-s.withTask|not (isMember t ns)]}
 
+instance == MTaskShare where
+       (==) a b = a.identifier == b.identifier
+
 getRealShare :: MTaskDevice MTaskShare -> Shared BCValue
-getRealShare dev {identifier} = SDSSource {SDSSource
+getRealShare dev share=:{identifier} = sdsFocus (Just identifier) $ SDSSource {SDSSource
        | name = "mTaskShareMap-" +++ toString identifier, read=rr, write=ww}
        where
                rr name iworld = case read deviceStore iworld of
                        (Error e, iworld) = (Error e, iworld)
                        (Ok devices, iworld) = case find ((==)dev) devices of
                                Nothing = (Error $ exception "Device doesn't exist anymore", iworld)
-                               Just {deviceShares} = case find (\s->s.identifier == identifier) deviceShares of
+                               Just {deviceShares} = case find ((==)share) deviceShares of
                                        Nothing = (Error $ exception "Share doesn't exist", iworld)
                                        Just share = (Ok share.MTaskShare.value, iworld)
                        
                // Also send messages
                ww name value iworld
                | not (trace_tn ("Update to: " +++ printToString value)) = undef
-               = case modify (\r->((), map (modFun value) r)) deviceStore iworld of
+               = case modify (tuple () o modifyValue value) deviceStore iworld of
                        (Error e, iworld) = (Error e, iworld)
                        (Ok _, iworld) = case sendMessagesIW [MTUpd identifier value] dev iworld of
                                (Error e, iworld) = (Error e, iworld)
-                               (Ok _, iworld) = (Ok $ const True, iworld)
+                               (Ok _, iworld) = (Ok $ maybe True ((==) identifier), iworld)
 
-               //Selects the correct device
-               modFun value d
-               | d == dev = {d & deviceShares=map (modFun2 value) d.deviceShares}
-               = d
+               modifyValue :: BCValue [MTaskDevice] -> [MTaskDevice]
+               modifyValue v ds = filterMap ((==)dev) deviceUpdate ds
+               where
+                       deviceUpdate d = {MTaskDevice | d
+                               & deviceShares=filterMap ((==)share) shareUpd d.deviceShares}
+                       shareUpd s = {MTaskShare | s & value=v}
 
-               //Selects the correct share
-               modFun2 value share
-               | identifier == share.MTaskShare.identifier = {MTaskShare | share & value=value}
-               = share
+               filterMap :: (a -> Bool) (a -> a) [a] -> [a]
+               filterMap f t xs = [if (f x) (t x) x\\x<-xs]