This commit is contained in:
Kevin Jahns 2015-12-03 18:02:37 +01:00
parent 08f0702fcd
commit 96f8f77dc4
6 changed files with 431 additions and 333 deletions

View File

@ -9,8 +9,8 @@ Y({
connector: { connector: {
name: 'websockets-client', name: 'websockets-client',
room: 'Puzzle-example2', room: 'Puzzle-example2',
url: 'http://127.0.0.1:2345',
debug: true debug: true
// url: 'http://127.0.0.1:2345'
}, },
sourceDir: '/bower_components', sourceDir: '/bower_components',
share: { share: {

View File

@ -8,8 +8,8 @@ Y({
connector: { connector: {
name: 'websockets-client', name: 'websockets-client',
room: 'Textarea-example', room: 'Textarea-example',
url: 'http://127.0.0.1:2345',
debug: true debug: true
// url: 'http://127.0.0.1:2345'
}, },
sourceDir: '/bower_components', sourceDir: '/bower_components',
share: { share: {

65
y.es6
View File

@ -1162,6 +1162,14 @@ module.exports = function (Y/* :any */) {
id: this.os.getNextOpId() id: this.os.getNextOpId()
} }
*/ */
create: function (id) {
return {
start: null,
end: null,
struct: "List",
id: id
}
},
encode: function (op) { encode: function (op) {
return { return {
struct: 'List', struct: 'List',
@ -1228,6 +1236,13 @@ module.exports = function (Y/* :any */) {
id: this.os.getNextOpId() id: this.os.getNextOpId()
} }
*/ */
create: function (id) {
return {
id: id,
map: {},
struct: "Map"
}
},
encode: function (op) { encode: function (op) {
return { return {
struct: 'Map', struct: 'Map',
@ -1363,6 +1378,14 @@ module.exports = function (Y/* :any */) {
} }
return t return t
} }
* createType (typedefinition) {
var structname = typedefinition.struct
var id = this.store.getNextOpId()
var op = Y.Struct[structname].create(id)
op.type = typedefinition.name
yield* this.applyCreatedOperations.call(this, [op])
return yield* this.getType(id)
}
/* /*
Apply operations that this user created (no remote ones!) Apply operations that this user created (no remote ones!)
* does not check for Struct.*.requiredOps() * does not check for Struct.*.requiredOps()
@ -1373,9 +1396,11 @@ module.exports = function (Y/* :any */) {
for (var i = 0; i < ops.length; i++) { for (var i = 0; i < ops.length; i++) {
var op = ops[i] var op = ops[i]
yield* this.store.tryExecute.call(this, op) yield* this.store.tryExecute.call(this, op)
send.push(Y.Struct[op.struct].encode(op)) if (op.id == null || op.id[0] !== '_') {
send.push(Y.Struct[op.struct].encode(op))
}
} }
if (!this.store.y.connector.isDisconnected()) { // TODO: && !this.store.forwardAppliedOperations (but then i don't send delete ops) if (!this.store.y.connector.isDisconnected() && send.length > 0) { // TODO: && !this.store.forwardAppliedOperations (but then i don't send delete ops)
// is connected, and this is not going to be send in addOperation // is connected, and this is not going to be send in addOperation
this.store.y.connector.broadcast({ this.store.y.connector.broadcast({
type: 'update', type: 'update',
@ -1829,11 +1854,12 @@ module.exports = function (Y/* :any */) {
} else { } else {
// need to generate this operation // need to generate this operation
if (this.store._nextUserId == null) { if (this.store._nextUserId == null) {
var typename = id[1].split('_')[0] var struct = id[1].split('_')[0]
this.store._nextUserId = id // this.store._nextUserId = id
yield* Y[typename].createType.call(this) var op = Y.Struct[struct].create(id)
delete this.store._nextUserId yield* this.setOperation(op)
return yield* this.os.find(id) // delete this.store._nextUserId
return op
} else { } else {
// Can only generate one operation at a time // Can only generate one operation at a time
return null return null
@ -2131,8 +2157,8 @@ module.exports = function (Y /* : any*/) {
A wrapper for the definition of a custom type. A wrapper for the definition of a custom type.
Every custom type must have three properties: Every custom type must have three properties:
* createType * struct
- Defines the model of a newly created custom type and returns the type - Structname of this type
* initType * initType
- Given a model, creates a custom type - Given a model, creates a custom type
* class * class
@ -2140,20 +2166,23 @@ module.exports = function (Y /* : any*/) {
*/ */
class CustomType { // eslint-disable-line class CustomType { // eslint-disable-line
/* :: /* ::
createType: any; struct: any;
initType: any; initType: any;
class: Function; class: Function;
name: String;
*/ */
constructor (def) { constructor (def) {
if (def.createType == null || if (def.struct == null ||
def.initType == null || def.initType == null ||
def.class == null def.class == null ||
def.name == null
) { ) {
throw new Error('Custom type was not initialized correctly!') throw new Error('Custom type was not initialized correctly!')
} }
this.createType = def.createType this.struct = def.struct
this.initType = def.initType this.initType = def.initType
this.class = def.class this.class = def.class
this.name = def.name
} }
} }
Y.utils.CustomType = CustomType Y.utils.CustomType = CustomType
@ -2313,7 +2342,15 @@ class YConfig {
this.db.requestTransaction(function * requestTransaction () { this.db.requestTransaction(function * requestTransaction () {
// create shared object // create shared object
for (var propertyname in opts.share) { for (var propertyname in opts.share) {
share[propertyname] = yield* this.getType(['_', opts.share[propertyname] + '_' + propertyname]) var typename = opts.share[propertyname]
var id = ['_', Y[typename].struct + '_' + propertyname]
var op = yield* this.getOperation(id)
if (op.type !== typename) {
// not already in the db
op.type = typename
yield* this.setOperation(op)
}
share[propertyname] = yield* this.getType(id)
} }
setTimeout(callback, 0) setTimeout(callback, 0)
}) })

File diff suppressed because one or more lines are too long

691
y.js

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long