Skip to content
Snippets Groups Projects
Commit 0ebfae69 authored by Kevin Jahns's avatar Kevin Jahns
Browse files

added flow support for Transaction.js

parent e9c40f9a
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,8 @@ type YGlobal = { ...@@ -4,7 +4,8 @@ type YGlobal = {
utils: Object, utils: Object,
Struct: any, Struct: any,
AbstractDatabase: any, AbstractDatabase: any,
AbstractConnector: any AbstractConnector: any,
Transaction: any
} }
type YConfig = { type YConfig = {
...@@ -17,4 +18,15 @@ declare var YConcurrency_TestingMode : boolean ...@@ -17,4 +18,15 @@ declare var YConcurrency_TestingMode : boolean
type Transaction<A> = Generator<any, A, any> type Transaction<A> = Generator<any, A, any>
type SyncRole = 'master' | 'slave' type SyncRole = 'master' | 'slave'
\ No newline at end of file
declare class Store {
find: (id:Id) => Transaction<any>;
put: (n:any) => Transaction<void>;
delete: (id:Id) => Transaction<void>;
findWithLowerBound: (start:Id) => Transaction<any>;
findWithUpperBound: (end:Id) => Transaction<any>;
findNext: (id:Id) => Transaction<any>;
findPrev: (id:Id) => Transaction<any>;
iterate: (t:any,start:?Id,end:?Id,gen:any) => Transaction<any>;
}
\ No newline at end of file
/* @flow */
'use strict' 'use strict'
/* /*
...@@ -73,8 +74,14 @@ ...@@ -73,8 +74,14 @@
- this is called only by `getOperations(startSS)`. It makes an operation - this is called only by `getOperations(startSS)`. It makes an operation
applyable on a given SS. applyable on a given SS.
*/ */
module.exports = function (Y) { module.exports = function (Y/* :YGlobal */) {
class Transaction { class TransactionInterface {
/* ::
store: Y.AbstractDatabase;
ds: Store;
os: Store;
ss: Store;
*/
/* /*
Get a type based on the id of its model. Get a type based on the id of its model.
If it does not exist yes, create it. If it does not exist yes, create it.
...@@ -116,7 +123,7 @@ module.exports = function (Y) { ...@@ -116,7 +123,7 @@ module.exports = function (Y) {
* deleteList (start) { * deleteList (start) {
if (this.store.y.connector.isSynced) { if (this.store.y.connector.isSynced) {
while (start != null && this.store.y.connector.isSynced) { while (start != null && this.store.y.connector.isSynced) {
start = (yield* this.getOperation(start)) start = yield* this.getOperation(start)
start.gc = true start.gc = true
yield* this.setOperation(start) yield* this.setOperation(start)
// TODO: will always reset the parent.. // TODO: will always reset the parent..
...@@ -131,7 +138,7 @@ module.exports = function (Y) { ...@@ -131,7 +138,7 @@ module.exports = function (Y) {
/* /*
Mark an operation as deleted, and add it to the GC, if possible. Mark an operation as deleted, and add it to the GC, if possible.
*/ */
* deleteOperation (targetId, preventCallType) { * deleteOperation (targetId, preventCallType) /* :Generator<any, any, any> */ {
var target = yield* this.getOperation(targetId) var target = yield* this.getOperation(targetId)
var callType = false var callType = false
...@@ -173,7 +180,12 @@ module.exports = function (Y) { ...@@ -173,7 +180,12 @@ module.exports = function (Y) {
target.opContent = null target.opContent = null
} }
} }
var left = target.left != null ? yield* this.getOperation(target.left) : null var left
if (target.left != null) {
left = yield* this.getOperation(target.left)
} else {
left = null
}
this.store.addToGarbageCollector(target, left) this.store.addToGarbageCollector(target, left)
...@@ -185,7 +197,12 @@ module.exports = function (Y) { ...@@ -185,7 +197,12 @@ module.exports = function (Y) {
Because this delete can't be responsible for left being gc'd, Because this delete can't be responsible for left being gc'd,
we don't have to add left to the gc.. we don't have to add left to the gc..
*/ */
var right = target.right != null ? yield* this.getOperation(target.right) : null var right
if (target.right != null) {
right = yield* this.getOperation(target.right)
} else {
right = null
}
if ( if (
right != null && right != null &&
this.store.addToGarbageCollector(right, target) this.store.addToGarbageCollector(right, target)
...@@ -355,18 +372,24 @@ module.exports = function (Y) { ...@@ -355,18 +372,24 @@ module.exports = function (Y) {
// reset origin of all right ops (except first right - duh!), // reset origin of all right ops (except first right - duh!),
// until you find origin pointer to the left of o // until you find origin pointer to the left of o
var i = right.right == null ? null : yield* this.getOperation(right.right) if (right.right != null) {
var ids = [o.id, o.right] var i = yield* this.getOperation(right.right)
while (i != null && ids.some(function (id) { var ids = [o.id, o.right]
return Y.utils.compareIds(id, i.origin) while (ids.some(function (id) {
})) { return Y.utils.compareIds(id, i.origin)
if (Y.utils.compareIds(i.origin, o.id)) { })) {
// reset origin of i if (Y.utils.compareIds(i.origin, o.id)) {
i.origin = neworigin // reset origin of i
yield* this.setOperation(i) i.origin = neworigin
yield* this.setOperation(i)
}
// get next i
if (i.right == null) {
break
} else {
i = yield* this.getOperation(i.right)
}
} }
// get next i
i = i.right == null ? null : yield* this.getOperation(i.right)
} }
} /* otherwise, rights origin is to the left of o, } /* otherwise, rights origin is to the left of o,
then there is no right op (from o), that origins in o */ then there is no right op (from o), that origins in o */
...@@ -470,7 +493,7 @@ module.exports = function (Y) { ...@@ -470,7 +493,7 @@ module.exports = function (Y) {
createDeletions(user, d[0], d[1], d[2]) createDeletions(user, d[0], d[1], d[2])
} }
} }
for (var i in deletions) { for (var i = 0; i < deletions.length; i++) {
var del = deletions[i] var del = deletions[i]
var id = [del[0], del[1]] var id = [del[0], del[1]]
// always try to delete.. // always try to delete..
...@@ -546,16 +569,11 @@ module.exports = function (Y) { ...@@ -546,16 +569,11 @@ module.exports = function (Y) {
id: [state.user], id: [state.user],
clock: state.clock clock: state.clock
} }
// TODO: find a way to skip this step.. (after implementing some dbs..) yield* this.ss.put(val)
if (yield* this.ss.find([state.user])) {
yield* this.ss.put(val)
} else {
yield* this.ss.put(val)
}
} }
* getState (user) { * getState (user) {
var n var n = yield* this.ss.find([user])
var clock = (n = yield* this.ss.find([user])) == null ? null : n.clock var clock = n == null ? null : n.clock
if (clock == null) { if (clock == null) {
clock = 0 clock = 0
} }
...@@ -602,7 +620,8 @@ module.exports = function (Y) { ...@@ -602,7 +620,8 @@ module.exports = function (Y) {
} }
var res = [] var res = []
for (var op of ops) { for (var op of ops) {
res.push(yield* this.makeOperationReady(startSS, op)) var o = yield* this.makeOperationReady(startSS, op)
res.push(o)
} }
return res return res
} }
...@@ -667,5 +686,5 @@ module.exports = function (Y) { ...@@ -667,5 +686,5 @@ module.exports = function (Y) {
return op return op
} }
} }
Y.Transaction = Transaction Y.Transaction = TransactionInterface
} }
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