Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 252a3b22 authored by Yannick Li's avatar Yannick Li
Browse files

Merge branch 'master' into 19-runblocking-is-not-available-in-javascript

parents ba0e2fd3 032c63e8
No related branches found
No related tags found
1 merge request!11Resolve "runBlocking is not available in JavaScript"
Pipeline #202044 passed
......@@ -19,23 +19,78 @@
package client
import client.utils.CServiceAdapter
import client.utils.coroutineBlocking
import client.utils.ActiveTransaction
import crdtlib.crdt.DeltaCRDT
import crdtlib.utils.ClientUId
import crdtlib.utils.SimpleEnvironment
class ClientEnvironment(session: Session, uid: ClientUId) : SimpleEnvironment(uid) {
/**
* This class represents a Concordant client environment.
* @property session the session attached to this environment.
* @property uid the client unique identifier.
*/
class ClientEnvironment(val session: Session, uid: ClientUId) : SimpleEnvironment(uid) {
/**
* On read handler called after a getter function on an object.
* @param obj the object on which getter was called.
*/
override fun onRead(obj: DeltaCRDT) {
// 1 - Vérifier que obj est un objet ouvert
// 2 - Récupérer cobjectuid du delta
// 3 - Récupérer l'object dans le c-service
// 4 - obj.merge(newVersion)
// Assert session and collection are opened
if (this.session.isClosed) throw RuntimeException("The session has been closed.")
val collection = this.session.openedCollections.values.elementAtOrNull(0)
if (collection == null) throw RuntimeException("There is no opened collection.")
// Assert we are in a transaction.
if (ActiveTransaction == null) throw RuntimeException("Code should be executed in a transaction")
// Assert the object is opened
val infos = collection.openedObjects[obj]
if (infos == null) throw RuntimeException("This object has been closed.")
// Assert the object unique identifier
val objectUId = infos.first
// Get distant version of the object
lateinit var distantObj: DeltaCRDT
coroutineBlocking {
distantObj = CServiceAdapter.getObject(session.getDbName(), objectUId, this)
}
// Merge distant value with the loacl one
obj.merge(distantObj)
}
/**
* On write handler called after an updater function on an object.
* @param obj the object on which updater was called.
* @param delta the delta generated by the updater function.
*/
override fun onWrite(obj: DeltaCRDT, delta: DeltaCRDT) {
// 1 - Vérifier objet ouvert
// 2 - Vérifier si readOnly
// 3 - récupérer cobjectuid
// 4 - faire push dans le cservice
// Assert session and collection are opened
if (this.session.isClosed) throw RuntimeException("The session has been closed.")
val collection = this.session.openedCollections.values.elementAtOrNull(0)
if (collection == null) throw RuntimeException("There is no opened collection.")
// Assert we are in a transaction.
if (ActiveTransaction == null) throw RuntimeException("Code should be executed in a transaction")
// Assert the object is opened
val infos = collection.openedObjects[obj]
if (infos == null) throw RuntimeException("This object has been closed.")
// Assert the object is not in read-only mode
val readOnly = infos.second
if (readOnly) throw RuntimeException("This object has been opened in read-only mode.")
// Get the object unique identifier
val objectUId = infos.first
// Push the new local version to backend
coroutineBlocking {
CServiceAdapter.updateObject(session.getDbName(), objectUId, obj)
}
}
}
......@@ -57,7 +57,7 @@ class Collection {
/**
* The objects opened within this collection.
*/
private val openedObjects: MutableMap<CObjectUId, DeltaCRDT> = mutableMapOf()
internal val openedObjects: MutableMap<DeltaCRDT, Pair<CObjectUId, Boolean>> = mutableMapOf()
/**
* Default constructor.
......@@ -85,18 +85,10 @@ class Collection {
val objectUId : CObjectUId = CObjectUId(this.id, type, objectId)
val obj : DeltaCRDT = CServiceAdapter.getObject(this.attachedSession.getDbName(), objectUId, this.attachedSession.environment)
this.openedObjects[objectUId]=obj
this.openedObjects[obj] = Pair(objectUId, readOnly)
return obj
}
/**
* Notifies this collection that an object has been closed.
* @param objectUId the closed object unique identifier.
*/
internal fun notifyClosedObject(objectUId: CObjectUId) {
this.openedObjects.remove(objectUId)
}
/**
* Closes this collection.
*/
......
......@@ -54,12 +54,12 @@ class Session {
/**
* The collections opened within this session.
*/
private val openedCollections: MutableMap<CollectionUId, Collection> = mutableMapOf()
internal val openedCollections: MutableMap<CollectionUId, Collection> = mutableMapOf()
/**
* Is this session closed.
*/
private var isClosed: Boolean = false
internal var isClosed: Boolean = false
/**
* Private constructor.
......
/*
* Copyright © 2020, Concordant and contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package client.utils
expect fun coroutineBlocking(block: suspend () -> Unit)
\ No newline at end of file
expect fun coroutineBlocking(block: suspend () -> Unit)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment