@@ -0,0 +1,186 @@
1+module Yjs.Yjs
2+ ( ArrayEvent
3+ , Handler(..)
4+ , Origin
5+ , Provider
6+ , Transaction
7+ , XmlEvent
8+ , YArray
9+ , YDoc
10+ , YMap
11+ , YText
12+ , YXmlFragment
13+ , YXmlText
14+ , class Insert
15+ , class Observable
16+ , class Push
17+ , class ToJson
18+ , deleteAt
19+ , getArray
20+ , getYMap
21+ , insert
22+ , mapYArray
23+ , mapYArrayWithIndex
24+ , mkHandler
25+ , mkYDoc
26+ , mkYMap
27+ , mkYText
28+ , mkYXmlFragment
29+ , mkYXmlText
30+ , observe
31+ , observeDeep
32+ , push
33+ , setYMap
34+ , toArray
35+ , toDOM
36+ , toJSON
37+ , transact
38+ , yTextToString
39+ ) where
40+
41+import Prelude
42+
43+import Data.Function.Uncurried (Fn2, mkFn2, runFn2)
44+import Data.Newtype (class Newtype)
45+import Effect (Effect)
46+import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn1, runEffectFn1, runEffectFn2, runEffectFn3)
47+import Foreign (Foreign)
48+import Web.DOM (DocumentFragment)
49+
50+foreign import unsafeEq :: forall t. t -> t -> Boolean
51+
52+newtype Handler event = Handler (EffectFn1 event Unit)
53+
54+instance Newtype (Handler event) (EffectFn1 event Unit)
55+
56+mkHandler ∷ ∀ (event ∷ Type). (event → Effect Unit) → Handler event
57+mkHandler = mkEffectFn1 >>> Handler
58+
59+class Observable a event | a -> event where
60+ observe :: a -> Handler event -> Effect (Effect Unit)
61+ observeDeep :: a -> Handler event -> Effect (Effect Unit)
62+
63+foreign import unsafeObserveImpl :: forall observable event. EffectFn2 observable (Handler event) (Effect Unit)
64+
65+foreign import unsafeObserveDeepImpl :: forall observable event. EffectFn2 observable (Handler event) (Effect Unit)
66+
67+class ToJson elem json | elem -> json where
68+ toJSON :: elem -> json
69+
70+foreign import unsafePushImpl :: forall t elem. EffectFn2 t elem Unit
71+
72+class Push t e where
73+ push :: t -> e -> Effect Unit
74+
75+class Insert t e where
76+ insert :: t -> e -> Int -> Effect Unit
77+
78+foreign import data Provider :: Type
79+
80+foreign import data YDoc :: Type
81+
82+instance Eq YDoc where
83+ eq = unsafeEq
84+
85+foreign import mkYDoc :: Effect YDoc
86+
87+type Transaction = Effect Unit
88+type Origin = String
89+
90+foreign import transactImpl :: EffectFn3 YDoc Transaction Origin Unit
91+
92+transact ∷ YDoc → Effect Unit → Origin → Effect Unit
93+transact = runEffectFn3 transactImpl
94+
95+foreign import data YText :: Type
96+
97+foreign import yTextToString :: YText -> String
98+
99+foreign import mkYTextImpl :: EffectFn1 String YText
100+
101+mkYText ∷ String → Effect YText
102+mkYText = runEffectFn1 mkYTextImpl
103+
104+foreign import data YMap :: Type
105+
106+foreign import mkYMap :: Effect YMap
107+
108+foreign import getYMapImpl :: forall value. Fn2 YMap String value
109+
110+getYMap ∷ ∀ (value ∷ Type). YMap → String → value
111+getYMap = runFn2 getYMapImpl
112+
113+foreign import setYMapImpl :: forall value. EffectFn3 YMap String value Unit
114+
115+setYMap ∷ ∀ (value ∷ Type). YMap → String → value → Effect Unit
116+setYMap = runEffectFn3 setYMapImpl
117+
118+foreign import data YArray :: Type -> Type
119+
120+instance Eq (YArray elem) where
121+ eq = unsafeEq
122+
123+instance Observable (YArray elem) ArrayEvent where
124+ observe arr handler = runEffectFn2 unsafeObserveImpl arr handler
125+ observeDeep arr handler = runEffectFn2 unsafeObserveDeepImpl arr handler
126+
127+instance Push (YArray elem) elem where
128+ push = runEffectFn2 unsafePushImpl
129+
130+foreign import arrayToJSONImpl :: forall elem. YArray elem -> Array Foreign
131+
132+instance ToJson (YArray elem) (Array Foreign) where
133+ toJSON = arrayToJSONImpl
134+
135+foreign import data ArrayEvent :: Type
136+
137+foreign import mapYArray :: forall a b. (a -> b) -> YArray a -> Array b
138+
139+foreign import mapYArrayWithIndexImpl :: forall a b. (Fn2 a Int b) -> YArray a -> Array b
140+
141+mapYArrayWithIndex ∷ ∀ (a ∷ Type) (b ∷ Type). (a → Int → b) → YArray a → Array b
142+mapYArrayWithIndex fn = mapYArrayWithIndexImpl (mkFn2 fn)
143+
144+foreign import getArray :: forall elem. YDoc -> String -> Effect (YArray elem)
145+
146+foreign import toArray :: forall elem. YArray elem -> Array elem
147+
148+foreign import deleteAtImpl :: forall elem. EffectFn3 (YArray elem) Int Int Unit
149+
150+deleteAt ∷ ∀ (elem ∷ Type). YArray elem → Int → Int → Effect Unit
151+deleteAt = runEffectFn3 deleteAtImpl
152+
153+foreign import data YXmlText :: Type
154+
155+foreign import mkYXmlTextImpl :: EffectFn1 String Unit
156+
157+mkYXmlText ∷ String → Effect Unit
158+mkYXmlText = runEffectFn1 mkYXmlTextImpl
159+
160+foreign import data YXmlFragment :: Type
161+
162+instance Eq YXmlFragment where
163+ eq = unsafeEq
164+
165+foreign import mkYXmlFragment :: Effect YXmlFragment
166+
167+foreign import xmlFragmentToJSONImpl :: YXmlFragment -> String
168+
169+instance ToJson YXmlFragment String where
170+ toJSON = xmlFragmentToJSONImpl
171+
172+foreign import data XmlEvent :: Type
173+
174+instance Observable YXmlFragment XmlEvent where
175+ observe xmlFragment handler = runEffectFn2 unsafeObserveImpl xmlFragment handler
176+ observeDeep xmlFragment handler = runEffectFn2 unsafeObserveDeepImpl xmlFragment handler
177+
178+instance Push YXmlFragment elem where
179+ push = runEffectFn2 unsafePushImpl
180+
181+foreign import insertYXmlFragmentImpl :: forall elem. EffectFn3 YXmlFragment elem Int Unit
182+
183+instance Insert YXmlFragment elem where
184+ insert = runEffectFn3 insertYXmlFragmentImpl
185+
186+foreign import toDOM :: YXmlFragment -> DocumentFragment