added flow support for Transaction.js

This commit is contained in:
Kevin Jahns 2015-11-30 14:26:22 +01:00
parent e9c40f9a83
commit 0ebfae6997
2 changed files with 61 additions and 30 deletions

View File

@ -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 = {
@ -18,3 +19,14 @@ 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'
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>;
}

View File

@ -1,3 +1,4 @@
/* @flow */
'use strict' 'use strict'
/* /*
@ -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) {
* 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) {
/* /*
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) {
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) {
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,9 +372,10 @@ 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 i = yield* this.getOperation(right.right)
var ids = [o.id, o.right] var ids = [o.id, o.right]
while (i != null && ids.some(function (id) { while (ids.some(function (id) {
return Y.utils.compareIds(id, i.origin) return Y.utils.compareIds(id, i.origin)
})) { })) {
if (Y.utils.compareIds(i.origin, o.id)) { if (Y.utils.compareIds(i.origin, o.id)) {
@ -366,7 +384,12 @@ module.exports = function (Y) {
yield* this.setOperation(i) yield* this.setOperation(i)
} }
// get next i // get next i
i = i.right == null ? null : yield* this.getOperation(i.right) if (i.right == null) {
break
} else {
i = 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) {
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) {
id: [state.user], id: [state.user],
clock: state.clock clock: state.clock
} }
// TODO: find a way to skip this step.. (after implementing some dbs..)
if (yield* this.ss.find([state.user])) {
yield* this.ss.put(val) 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) {
} }
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) {
return op return op
} }
} }
Y.Transaction = Transaction Y.Transaction = TransactionInterface
} }