checking out new stuff
This commit is contained in:
parent
1b63f5efde
commit
159f37474d
3
.eslintignore
Normal file
3
.eslintignore
Normal file
@ -0,0 +1,3 @@
|
||||
build/
|
||||
y.js
|
||||
interfaces/
|
13
.eslintrc
Normal file
13
.eslintrc
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"env": {
|
||||
"es6": true
|
||||
},
|
||||
"rules": {
|
||||
"strict": 0,
|
||||
"camelcase": [1, {"properties": "never"}]
|
||||
},
|
||||
"parser": "babel-eslint",
|
||||
"globals": {
|
||||
"Buffer": true
|
||||
}
|
||||
}
|
10
.flowconfig
Normal file
10
.flowconfig
Normal file
@ -0,0 +1,10 @@
|
||||
[ignore]
|
||||
./build_node
|
||||
|
||||
[include]
|
||||
./src
|
||||
|
||||
[libs]
|
||||
./interfaces
|
||||
|
||||
[options]
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
/node_modules/
|
||||
node_modules
|
||||
bower_components
|
||||
build
|
||||
.directory
|
||||
.c9
|
||||
.codio
|
||||
|
@ -1,6 +1,8 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Kevin Jahns <kevin.jahns@rwth-aachen.de>.
|
||||
Copyright (c) 2014
|
||||
- Kevin Jahns <kevin.jahns@rwth-aachen.de>.
|
||||
- Chair of Computer Science 5 (Databases & Information Systems), RWTH Aachen University, Germany
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
139
README.md
139
README.md
@ -1,139 +0,0 @@
|
||||
|
||||
# 
|
||||
|
||||
Note: This branch implements IndexedDB / Redis to store operations. This is highly experimental!!!!
|
||||
|
||||
[](https://travis-ci.org/y-js/yjs)
|
||||
|
||||
Yjs is a framework for optimistic concurrency control and automatic conflict resolution on arbitrary data types. The framework implements a new OT-like concurrency algorithm and provides similar functionality as [ShareJs] and [OpenCoweb]. Yjs was designed to handle concurrent actions on arbitrary complex data types like Text, Json, and XML. We provide a tutorial and some applications for this framework on our [homepage](http://y-js.org/).
|
||||
|
||||
You can create you own data types easily. Therefore, you can take matters into your own hand by defining the meaning of the shared types and ensure that it is valid, while Yjs ensures data consistency (everyone will eventually end up with the same data). We already provide data types for
|
||||
|
||||
| Name | Description
|
||||
| ---------------------------------------------------- | ---------------------------------------------
|
||||
y-object | Add, update, and remove properties of an object. Circular references are supported. Included in Yjs
|
||||
[y-list](https://github.com/y-js/y-list) | A shared linked list implementation. Circular references are supported
|
||||
[y-selections](https://github.com/y-js/y-selections) | Manages selections on types that use linear structures (e.g. the y-list type). You can select a range of elements and assign meaning to them.
|
||||
[y-xml](https://github.com/y-js/y-xml) | An implementation of the DOM. You can create a two way binding to Browser DOM objects
|
||||
[y-text](https://github.com/y-js/y-text) | Collaborate on text. You can create a two way binding to textareas, input elements, or HTML elements (e.g. *h1*, or *p*)
|
||||
[y-richtext](https://github.com/y-js/y-richtext) | Collaborate on rich text. You can create a two way binding to several editors
|
||||
|
||||
Unlike other frameworks, Yjs supports P2P message propagation and is not bound to a specific communication protocol. Therefore, Yjs is extremely scalable and can be used in a wide range of application scenarios.
|
||||
|
||||
We support several communication protocols as so called *Connectors*. You can create your own connector too - read [this wiki page](https://github.com/y-js/yjs/wiki/Custom-Connectors). Currently, we support the following communication protocols:
|
||||
|
||||
Name | Description
|
||||
---------------------------------------- | -------------------------------------------------------
|
||||
[y-xmpp](https://github.com/y-js/y-xmpp) | Propagate updates in a XMPP multi-user-chat room ([XEP-0045](http://xmpp.org/extensions/xep-0045.html))
|
||||
[y-webrtc](https://github.com/y-js/y-webrtc) | Propagate updates Browser2Browser via WebRTC
|
||||
[y-test](https://github.com/y-js/y-test) | A Connector for testing purposes. It is designed to simulate delays that happen in worst case scenarios
|
||||
|
||||
|
||||
You can use Yjs client-, and server- side. You can get it as via npm, and bower. We even provide polymer elements for Yjs!
|
||||
|
||||
The advantages over similar frameworks are support for
|
||||
* .. P2P message propagation and arbitrary communication protocols
|
||||
* .. arbitrary complex data types
|
||||
* .. offline editing: Only relevant changes are propagated on rejoin (unimplemented)
|
||||
* .. AnyUndo: Undo *any* action that was executed in constant time (unimplemented)
|
||||
* .. Intention Preservation: When working on Text, the intention of your changes are preserved. This is particularily important when working offline. Every type has a notion on how we define Intention Preservation on it.
|
||||
|
||||
|
||||
## Use it!
|
||||
You can find a tutorial, and examples on the [website](http://y-js.org). Furthermore, the [github wiki](https://github.com/y-js/yjs/wiki) offers more information about how you can use Yjs in your application.
|
||||
|
||||
Either clone this git repository, install it with [bower](http://bower.io/), or install it with [npm](https://www.npmjs.org/package/yjs).
|
||||
|
||||
### Bower
|
||||
```
|
||||
bower install y-js/yjs
|
||||
```
|
||||
Then you include the libraries directly from the installation folder.
|
||||
```
|
||||
<script src="./bower_components/yjs/y.js"></script>
|
||||
```
|
||||
|
||||
### Npm
|
||||
```
|
||||
npm install yjs --save
|
||||
```
|
||||
|
||||
And use it like this with *npm*:
|
||||
```
|
||||
Y = require("yjs");
|
||||
```
|
||||
|
||||
# Y()
|
||||
In order to create an instance of Y, you need to have a connection object (instance of a Connector). Then, you can create a shared data type like this:
|
||||
```
|
||||
var y = new Y(connector);
|
||||
```
|
||||
|
||||
|
||||
# Y.Object
|
||||
Yjs includes only one type by default - the Y.Object type. It mimics the behaviour of a JSON Object. You can create, update, and remove properies on the Y.Object type. Furthermore, you can observe changes on this type as you can observe changes on Javascript Objects with [Object.observe](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe) - an ECMAScript 7 proposal which is likely to become accepted by the committee. Until then, we have our own implementation.
|
||||
|
||||
|
||||
##### Reference
|
||||
* Create
|
||||
```
|
||||
var y = new Y.Object();
|
||||
```
|
||||
* Create with existing Object
|
||||
```
|
||||
var y = new Y.Object({number: 73});
|
||||
```
|
||||
* Every instance of Y is an Y.Object
|
||||
```
|
||||
var y = new Y(connector);
|
||||
```
|
||||
* .val()
|
||||
* Retrieve all properties of this type as a JSON Object
|
||||
* .val(name)
|
||||
* Retrieve the value of a property
|
||||
* .val(name, value)
|
||||
* Set/update a property. Returns `this` Y.Object
|
||||
* .delete(name)
|
||||
* Delete a property
|
||||
* .observe(observer)
|
||||
* The `observer` is called whenever something on this object changes. Throws *add*, *update*, and *delete* events
|
||||
* .unobserve(f)
|
||||
* Delete an observer
|
||||
|
||||
# A note on intention preservation
|
||||
When users create/update/delete the same property concurrently, only one change will prevail. Changes on different properties do not conflict with each other.
|
||||
|
||||
# A note on time complexities
|
||||
* .val()
|
||||
* O(|properties|)
|
||||
* .val(name)
|
||||
* O(1)
|
||||
* .val(name, value)
|
||||
* O(1)
|
||||
* .delete(name)
|
||||
* O(1)
|
||||
* Apply a delete operation from another user
|
||||
* O(1)
|
||||
* Apply an update operation from another user (set/update a property)
|
||||
* Yjs does not transform against operations that do not conflict with each other.
|
||||
* An operation conflicts with another operation if it changes the same property.
|
||||
* Overall worst case complexety: O(|conflicts|!)
|
||||
|
||||
# Status
|
||||
Yjs is a work in progress. Different versions of the *y-* repositories may not work together. Just drop me a line if you run into troubles.
|
||||
|
||||
## Get help
|
||||
[](https://gitter.im/y-js/yjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
|
||||
Please report _any_ issues to the [Github issue page](https://github.com/y-js/yjs/issues)! I try to fix them very soon, if possible.
|
||||
|
||||
## Contribution
|
||||
I created this framework during my bachelor thesis at the chair of computer science 5 [(i5)](http://dbis.rwth-aachen.de/cms), RWTH University. Since December 2014 I'm working on Yjs as a part of my student worker job at the i5.
|
||||
|
||||
## License
|
||||
Yjs is licensed under the [MIT License](./LICENSE.txt).
|
||||
|
||||
<yjs@dbis.rwth-aachen.de>
|
||||
|
||||
[ShareJs]: https://github.com/share/ShareJS
|
||||
[OpenCoweb]: https://github.com/opencoweb/coweb
|
33
bower.json
33
bower.json
@ -1,33 +0,0 @@
|
||||
{
|
||||
"name": "yjs",
|
||||
"version": "0.5.2",
|
||||
"homepage": "https://github.com/DadaMonad/yjs",
|
||||
"authors": [
|
||||
"Kevin Jahns <kevin.jahns@rwth-aachen.de>"
|
||||
],
|
||||
"description": "A Framework that enables Real-Time collaboration on arbitrary data structures.",
|
||||
"main": [
|
||||
"./y.js",
|
||||
"./y-object.html",
|
||||
"./build/node/y.js"
|
||||
],
|
||||
"keywords": [
|
||||
"OT",
|
||||
"collaboration",
|
||||
"synchronization",
|
||||
"ShareJS",
|
||||
"Coweb",
|
||||
"concurrency"
|
||||
],
|
||||
"license": "MIT",
|
||||
"ignore": [
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"test",
|
||||
"extras",
|
||||
"test"
|
||||
],
|
||||
"devDependencies": {
|
||||
"y-test" : "y-test#~0.4.0"
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
|
||||
# Directories
|
||||
|
||||
### build/browser
|
||||
You find the browserified (not minified) version of yjs here. This is nice for debugging, since it also includes sourcemaps. For production, however, you should use the version that you find in the main directory.
|
||||
|
||||
### build/node
|
||||
Yjs for nodejs is located here. You can only use the submodules, or require 'y' in your node project. Also works with browserify.
|
||||
|
||||
### build/test
|
||||
Start build/test/index.html' in your browser, to perform testing Yjs.
|
@ -1,7 +0,0 @@
|
||||
|
||||
<polymer-element name="y-object" hidden attributes="val connector y">
|
||||
</polymer-element>
|
||||
<polymer-element name="y-property" hidden attributes="val name y">
|
||||
</polymer-element>
|
||||
|
||||
<script src="./y-object.js"></script>
|
File diff suppressed because one or more lines are too long
2272
build/browser/y.js
2272
build/browser/y.js
File diff suppressed because one or more lines are too long
@ -1,71 +0,0 @@
|
||||
var ConnectorClass, adaptConnector;
|
||||
|
||||
ConnectorClass = require("./ConnectorClass");
|
||||
|
||||
adaptConnector = function(connector, engine, HB, execution_listener) {
|
||||
var applyHB, encode_state_vector, f, getHB, getStateVector, name, parse_state_vector, send_;
|
||||
for (name in ConnectorClass) {
|
||||
f = ConnectorClass[name];
|
||||
connector[name] = f;
|
||||
}
|
||||
connector.setIsBoundToY();
|
||||
send_ = function(o) {
|
||||
if ((o.uid.creator === HB.getUserId()) && (typeof o.uid.op_number !== "string") && (HB.getUserId() !== "_temp")) {
|
||||
return connector.broadcast(o);
|
||||
}
|
||||
};
|
||||
if (connector.invokeSync != null) {
|
||||
HB.setInvokeSyncHandler(connector.invokeSync);
|
||||
}
|
||||
execution_listener.push(send_);
|
||||
encode_state_vector = function(v) {
|
||||
var results, value;
|
||||
results = [];
|
||||
for (name in v) {
|
||||
value = v[name];
|
||||
results.push({
|
||||
user: name,
|
||||
state: value
|
||||
});
|
||||
}
|
||||
return results;
|
||||
};
|
||||
parse_state_vector = function(v) {
|
||||
var i, len, s, state_vector;
|
||||
state_vector = {};
|
||||
for (i = 0, len = v.length; i < len; i++) {
|
||||
s = v[i];
|
||||
state_vector[s.user] = s.state;
|
||||
}
|
||||
return state_vector;
|
||||
};
|
||||
getStateVector = function() {
|
||||
return encode_state_vector(HB.getOperationCounter());
|
||||
};
|
||||
getHB = function(v) {
|
||||
var hb, json, state_vector;
|
||||
state_vector = parse_state_vector(v);
|
||||
hb = HB._encode(state_vector);
|
||||
json = {
|
||||
hb: hb,
|
||||
state_vector: encode_state_vector(HB.getOperationCounter())
|
||||
};
|
||||
return json;
|
||||
};
|
||||
applyHB = function(hb, fromHB) {
|
||||
return engine.applyOp(hb, fromHB);
|
||||
};
|
||||
connector.getStateVector = getStateVector;
|
||||
connector.getHB = getHB;
|
||||
connector.applyHB = applyHB;
|
||||
if (connector.receive_handlers == null) {
|
||||
connector.receive_handlers = [];
|
||||
}
|
||||
return connector.receive_handlers.push(function(sender, op) {
|
||||
if (op.uid.creator !== HB.getUserId()) {
|
||||
return engine.applyOp(op);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = adaptConnector;
|
@ -1,415 +0,0 @@
|
||||
module.exports = {
|
||||
init: function(options) {
|
||||
var req;
|
||||
req = (function(_this) {
|
||||
return function(name, choices) {
|
||||
if (options[name] != null) {
|
||||
if ((choices == null) || choices.some(function(c) {
|
||||
return c === options[name];
|
||||
})) {
|
||||
return _this[name] = options[name];
|
||||
} else {
|
||||
throw new Error("You can set the '" + name + "' option to one of the following choices: " + JSON.encode(choices));
|
||||
}
|
||||
} else {
|
||||
throw new Error("You must specify " + name + ", when initializing the Connector!");
|
||||
}
|
||||
};
|
||||
})(this);
|
||||
req("syncMethod", ["syncAll", "master-slave"]);
|
||||
req("role", ["master", "slave"]);
|
||||
req("user_id");
|
||||
if (typeof this.on_user_id_set === "function") {
|
||||
this.on_user_id_set(this.user_id);
|
||||
}
|
||||
if (options.perform_send_again != null) {
|
||||
this.perform_send_again = options.perform_send_again;
|
||||
} else {
|
||||
this.perform_send_again = true;
|
||||
}
|
||||
if (this.role === "master") {
|
||||
this.syncMethod = "syncAll";
|
||||
}
|
||||
this.is_synced = false;
|
||||
this.connections = {};
|
||||
if (this.receive_handlers == null) {
|
||||
this.receive_handlers = [];
|
||||
}
|
||||
this.connections = {};
|
||||
this.current_sync_target = null;
|
||||
this.sent_hb_to_all_users = false;
|
||||
return this.is_initialized = true;
|
||||
},
|
||||
onUserEvent: function(f) {
|
||||
if (this.connections_listeners == null) {
|
||||
this.connections_listeners = [];
|
||||
}
|
||||
return this.connections_listeners.push(f);
|
||||
},
|
||||
isRoleMaster: function() {
|
||||
return this.role === "master";
|
||||
},
|
||||
isRoleSlave: function() {
|
||||
return this.role === "slave";
|
||||
},
|
||||
findNewSyncTarget: function() {
|
||||
var c, ref, user;
|
||||
this.current_sync_target = null;
|
||||
if (this.syncMethod === "syncAll") {
|
||||
ref = this.connections;
|
||||
for (user in ref) {
|
||||
c = ref[user];
|
||||
if (!c.is_synced) {
|
||||
this.performSync(user);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.current_sync_target == null) {
|
||||
this.setStateSynced();
|
||||
}
|
||||
return null;
|
||||
},
|
||||
userLeft: function(user) {
|
||||
var f, i, len, ref, results;
|
||||
delete this.connections[user];
|
||||
this.findNewSyncTarget();
|
||||
if (this.connections_listeners != null) {
|
||||
ref = this.connections_listeners;
|
||||
results = [];
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
f = ref[i];
|
||||
results.push(f({
|
||||
action: "userLeft",
|
||||
user: user
|
||||
}));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
},
|
||||
userJoined: function(user, role) {
|
||||
var base, f, i, len, ref, results;
|
||||
if (role == null) {
|
||||
throw new Error("Internal: You must specify the role of the joined user! E.g. userJoined('uid:3939','slave')");
|
||||
}
|
||||
if ((base = this.connections)[user] == null) {
|
||||
base[user] = {};
|
||||
}
|
||||
this.connections[user].is_synced = false;
|
||||
if ((!this.is_synced) || this.syncMethod === "syncAll") {
|
||||
if (this.syncMethod === "syncAll") {
|
||||
this.performSync(user);
|
||||
} else if (role === "master") {
|
||||
this.performSyncWithMaster(user);
|
||||
}
|
||||
}
|
||||
if (this.connections_listeners != null) {
|
||||
ref = this.connections_listeners;
|
||||
results = [];
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
f = ref[i];
|
||||
results.push(f({
|
||||
action: "userJoined",
|
||||
user: user,
|
||||
role: role
|
||||
}));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
},
|
||||
whenSynced: function(args) {
|
||||
if (args.constructor === Function) {
|
||||
args = [args];
|
||||
}
|
||||
if (this.is_synced) {
|
||||
return args[0].apply(this, args.slice(1));
|
||||
} else {
|
||||
if (this.compute_when_synced == null) {
|
||||
this.compute_when_synced = [];
|
||||
}
|
||||
return this.compute_when_synced.push(args);
|
||||
}
|
||||
},
|
||||
onReceive: function(f) {
|
||||
return this.receive_handlers.push(f);
|
||||
},
|
||||
|
||||
/*
|
||||
* Broadcast a message to all connected peers.
|
||||
* @param message {Object} The message to broadcast.
|
||||
*
|
||||
broadcast: (message)->
|
||||
throw new Error "You must implement broadcast!"
|
||||
|
||||
*
|
||||
* Send a message to a peer, or set of peers
|
||||
*
|
||||
send: (peer_s, message)->
|
||||
throw new Error "You must implement send!"
|
||||
*/
|
||||
performSync: function(user) {
|
||||
var _hb, hb, i, len, o;
|
||||
if (this.current_sync_target == null) {
|
||||
this.current_sync_target = user;
|
||||
this.send(user, {
|
||||
sync_step: "getHB",
|
||||
send_again: "true",
|
||||
data: this.getStateVector()
|
||||
});
|
||||
if (!this.sent_hb_to_all_users) {
|
||||
this.sent_hb_to_all_users = true;
|
||||
hb = this.getHB([]).hb;
|
||||
_hb = [];
|
||||
for (i = 0, len = hb.length; i < len; i++) {
|
||||
o = hb[i];
|
||||
_hb.push(o);
|
||||
if (_hb.length > 10) {
|
||||
this.broadcast({
|
||||
sync_step: "applyHB_",
|
||||
data: _hb
|
||||
});
|
||||
_hb = [];
|
||||
}
|
||||
}
|
||||
return this.broadcast({
|
||||
sync_step: "applyHB",
|
||||
data: _hb
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
performSyncWithMaster: function(user) {
|
||||
var _hb, hb, i, len, o;
|
||||
this.current_sync_target = user;
|
||||
this.send(user, {
|
||||
sync_step: "getHB",
|
||||
send_again: "true",
|
||||
data: this.getStateVector()
|
||||
});
|
||||
hb = this.getHB([]).hb;
|
||||
_hb = [];
|
||||
for (i = 0, len = hb.length; i < len; i++) {
|
||||
o = hb[i];
|
||||
_hb.push(o);
|
||||
if (_hb.length > 10) {
|
||||
this.broadcast({
|
||||
sync_step: "applyHB_",
|
||||
data: _hb
|
||||
});
|
||||
_hb = [];
|
||||
}
|
||||
}
|
||||
return this.broadcast({
|
||||
sync_step: "applyHB",
|
||||
data: _hb
|
||||
});
|
||||
},
|
||||
setStateSynced: function() {
|
||||
var args, el, f, i, len, ref;
|
||||
if (!this.is_synced) {
|
||||
this.is_synced = true;
|
||||
if (this.compute_when_synced != null) {
|
||||
ref = this.compute_when_synced;
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
el = ref[i];
|
||||
f = el[0];
|
||||
args = el.slice(1);
|
||||
f.apply(args);
|
||||
}
|
||||
delete this.compute_when_synced;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
},
|
||||
whenReceivedStateVector: function(f) {
|
||||
if (this.when_received_state_vector_listeners == null) {
|
||||
this.when_received_state_vector_listeners = [];
|
||||
}
|
||||
return this.when_received_state_vector_listeners.push(f);
|
||||
},
|
||||
receiveMessage: function(sender, res) {
|
||||
var _hb, data, f, hb, i, j, k, len, len1, len2, o, ref, ref1, results, sendApplyHB, send_again;
|
||||
if (res.sync_step == null) {
|
||||
ref = this.receive_handlers;
|
||||
results = [];
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
f = ref[i];
|
||||
results.push(f(sender, res));
|
||||
}
|
||||
return results;
|
||||
} else {
|
||||
if (sender === this.user_id) {
|
||||
return;
|
||||
}
|
||||
if (res.sync_step === "getHB") {
|
||||
if (this.when_received_state_vector_listeners != null) {
|
||||
ref1 = this.when_received_state_vector_listeners;
|
||||
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
||||
f = ref1[j];
|
||||
f.call(this, res.data);
|
||||
}
|
||||
}
|
||||
delete this.when_received_state_vector_listeners;
|
||||
data = this.getHB(res.data);
|
||||
hb = data.hb;
|
||||
_hb = [];
|
||||
if (this.is_synced) {
|
||||
sendApplyHB = (function(_this) {
|
||||
return function(m) {
|
||||
return _this.send(sender, m);
|
||||
};
|
||||
})(this);
|
||||
} else {
|
||||
sendApplyHB = (function(_this) {
|
||||
return function(m) {
|
||||
return _this.broadcast(m);
|
||||
};
|
||||
})(this);
|
||||
}
|
||||
for (k = 0, len2 = hb.length; k < len2; k++) {
|
||||
o = hb[k];
|
||||
_hb.push(o);
|
||||
if (_hb.length > 10) {
|
||||
sendApplyHB({
|
||||
sync_step: "applyHB_",
|
||||
data: _hb
|
||||
});
|
||||
_hb = [];
|
||||
}
|
||||
}
|
||||
sendApplyHB({
|
||||
sync_step: "applyHB",
|
||||
data: _hb
|
||||
});
|
||||
if ((res.send_again != null) && this.perform_send_again) {
|
||||
send_again = (function(_this) {
|
||||
return function(sv) {
|
||||
return function() {
|
||||
var l, len3;
|
||||
hb = _this.getHB(sv).hb;
|
||||
for (l = 0, len3 = hb.length; l < len3; l++) {
|
||||
o = hb[l];
|
||||
_hb.push(o);
|
||||
if (_hb.length > 10) {
|
||||
_this.send(sender, {
|
||||
sync_step: "applyHB_",
|
||||
data: _hb
|
||||
});
|
||||
_hb = [];
|
||||
}
|
||||
}
|
||||
return _this.send(sender, {
|
||||
sync_step: "applyHB",
|
||||
data: _hb,
|
||||
sent_again: "true"
|
||||
});
|
||||
};
|
||||
};
|
||||
})(this)(data.state_vector);
|
||||
return setTimeout(send_again, 3000);
|
||||
}
|
||||
} else if (res.sync_step === "applyHB") {
|
||||
this.applyHB(res.data, sender === this.current_sync_target);
|
||||
if ((this.syncMethod === "syncAll" || (res.sent_again != null)) && (!this.is_synced) && ((this.current_sync_target === sender) || (this.current_sync_target == null))) {
|
||||
this.connections[sender].is_synced = true;
|
||||
return this.findNewSyncTarget();
|
||||
}
|
||||
} else if (res.sync_step === "applyHB_") {
|
||||
return this.applyHB(res.data, sender === this.current_sync_target);
|
||||
}
|
||||
}
|
||||
},
|
||||
parseMessageFromXml: function(m) {
|
||||
var parse_array, parse_object;
|
||||
parse_array = function(node) {
|
||||
var i, len, n, ref, results;
|
||||
ref = node.children;
|
||||
results = [];
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
n = ref[i];
|
||||
if (n.getAttribute("isArray") === "true") {
|
||||
results.push(parse_array(n));
|
||||
} else {
|
||||
results.push(parse_object(n));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
parse_object = function(node) {
|
||||
var i, int, json, len, n, name, ref, ref1, value;
|
||||
json = {};
|
||||
ref = node.attrs;
|
||||
for (name in ref) {
|
||||
value = ref[name];
|
||||
int = parseInt(value);
|
||||
if (isNaN(int) || ("" + int) !== value) {
|
||||
json[name] = value;
|
||||
} else {
|
||||
json[name] = int;
|
||||
}
|
||||
}
|
||||
ref1 = node.children;
|
||||
for (i = 0, len = ref1.length; i < len; i++) {
|
||||
n = ref1[i];
|
||||
name = n.name;
|
||||
if (n.getAttribute("isArray") === "true") {
|
||||
json[name] = parse_array(n);
|
||||
} else {
|
||||
json[name] = parse_object(n);
|
||||
}
|
||||
}
|
||||
return json;
|
||||
};
|
||||
return parse_object(m);
|
||||
},
|
||||
encodeMessageToXml: function(m, json) {
|
||||
var encode_array, encode_object;
|
||||
encode_object = function(m, json) {
|
||||
var name, value;
|
||||
for (name in json) {
|
||||
value = json[name];
|
||||
if (value == null) {
|
||||
|
||||
} else if (value.constructor === Object) {
|
||||
encode_object(m.c(name), value);
|
||||
} else if (value.constructor === Array) {
|
||||
encode_array(m.c(name), value);
|
||||
} else {
|
||||
m.setAttribute(name, value);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
};
|
||||
encode_array = function(m, array) {
|
||||
var e, i, len;
|
||||
m.setAttribute("isArray", "true");
|
||||
for (i = 0, len = array.length; i < len; i++) {
|
||||
e = array[i];
|
||||
if (e.constructor === Object) {
|
||||
encode_object(m.c("array-element"), e);
|
||||
} else {
|
||||
encode_array(m.c("array-element"), e);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
};
|
||||
if (json.constructor === Object) {
|
||||
return encode_object(m.c("y", {
|
||||
xmlns: "http://y.ninja/connector-stanza"
|
||||
}), json);
|
||||
} else if (json.constructor === Array) {
|
||||
return encode_array(m.c("y", {
|
||||
xmlns: "http://y.ninja/connector-stanza"
|
||||
}), json);
|
||||
} else {
|
||||
throw new Error("I can't encode this json!");
|
||||
}
|
||||
},
|
||||
setIsBoundToY: function() {
|
||||
if (typeof this.on_bound_to_y === "function") {
|
||||
this.on_bound_to_y();
|
||||
}
|
||||
delete this.when_bound_to_y;
|
||||
return this.is_bound_to_y = true;
|
||||
}
|
||||
};
|
@ -1,120 +0,0 @@
|
||||
var Engine;
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.unprocessed_counter = 0;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.unprocessed_exec_counter = 0;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.unprocessed_types = [];
|
||||
}
|
||||
|
||||
Engine = (function() {
|
||||
function Engine(HB, types) {
|
||||
this.HB = HB;
|
||||
this.types = types;
|
||||
this.unprocessed_ops = [];
|
||||
}
|
||||
|
||||
Engine.prototype.parseOperation = function(json) {
|
||||
var type;
|
||||
type = this.types[json.type];
|
||||
if ((type != null ? type.parse : void 0) != null) {
|
||||
return type.parse(json);
|
||||
} else {
|
||||
throw new Error("You forgot to specify a parser for type " + json.type + ". The message is " + (JSON.stringify(json)) + ".");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
applyOpsBundle: (ops_json)->
|
||||
ops = []
|
||||
for o in ops_json
|
||||
ops.push @parseOperation o
|
||||
for o in ops
|
||||
if not o.execute()
|
||||
@unprocessed_ops.push o
|
||||
@tryUnprocessed()
|
||||
*/
|
||||
|
||||
Engine.prototype.applyOpsCheckDouble = function(ops_json) {
|
||||
var i, len, o, results;
|
||||
results = [];
|
||||
for (i = 0, len = ops_json.length; i < len; i++) {
|
||||
o = ops_json[i];
|
||||
if (this.HB.getOperation(o.uid) == null) {
|
||||
results.push(this.applyOp(o));
|
||||
} else {
|
||||
results.push(void 0);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
Engine.prototype.applyOps = function(ops_json) {
|
||||
return this.applyOp(ops_json);
|
||||
};
|
||||
|
||||
Engine.prototype.applyOp = function(op_json_array, fromHB) {
|
||||
var i, len, o, op_json;
|
||||
if (fromHB == null) {
|
||||
fromHB = false;
|
||||
}
|
||||
if (op_json_array.constructor !== Array) {
|
||||
op_json_array = [op_json_array];
|
||||
}
|
||||
for (i = 0, len = op_json_array.length; i < len; i++) {
|
||||
op_json = op_json_array[i];
|
||||
if (fromHB) {
|
||||
op_json.fromHB = "true";
|
||||
}
|
||||
o = this.parseOperation(op_json);
|
||||
o.parsed_from_json = op_json;
|
||||
if (op_json.fromHB != null) {
|
||||
o.fromHB = op_json.fromHB;
|
||||
}
|
||||
if (this.HB.getOperation(o) != null) {
|
||||
|
||||
} else if (((!this.HB.isExpectedOperation(o)) && (o.fromHB == null)) || (!o.execute())) {
|
||||
this.unprocessed_ops.push(o);
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.unprocessed_types.push(o.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.tryUnprocessed();
|
||||
};
|
||||
|
||||
Engine.prototype.tryUnprocessed = function() {
|
||||
var i, len, old_length, op, ref, unprocessed;
|
||||
while (true) {
|
||||
old_length = this.unprocessed_ops.length;
|
||||
unprocessed = [];
|
||||
ref = this.unprocessed_ops;
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
op = ref[i];
|
||||
if (this.HB.getOperation(op) != null) {
|
||||
|
||||
} else if ((!this.HB.isExpectedOperation(op) && (op.fromHB == null)) || (!op.execute())) {
|
||||
unprocessed.push(op);
|
||||
}
|
||||
}
|
||||
this.unprocessed_ops = unprocessed;
|
||||
if (this.unprocessed_ops.length === old_length) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this.unprocessed_ops.length !== 0) {
|
||||
return this.HB.invokeSync();
|
||||
}
|
||||
};
|
||||
|
||||
return Engine;
|
||||
|
||||
})();
|
||||
|
||||
module.exports = Engine;
|
@ -1,255 +0,0 @@
|
||||
var HistoryBuffer,
|
||||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
HistoryBuffer = (function() {
|
||||
function HistoryBuffer(user_id1) {
|
||||
this.user_id = user_id1;
|
||||
this.emptyGarbage = bind(this.emptyGarbage, this);
|
||||
this.operation_counter = {};
|
||||
this.buffer = {};
|
||||
this.change_listeners = [];
|
||||
this.garbage = [];
|
||||
this.trash = [];
|
||||
this.performGarbageCollection = true;
|
||||
this.garbageCollectTimeout = 30000;
|
||||
this.reserved_identifier_counter = 0;
|
||||
setTimeout(this.emptyGarbage, this.garbageCollectTimeout);
|
||||
}
|
||||
|
||||
HistoryBuffer.prototype.setUserId = function(user_id1, state_vector) {
|
||||
var base, buff, counter_diff, name, o, o_name, ref;
|
||||
this.user_id = user_id1;
|
||||
if ((base = this.buffer)[name = this.user_id] == null) {
|
||||
base[name] = [];
|
||||
}
|
||||
buff = this.buffer[this.user_id];
|
||||
counter_diff = state_vector[this.user_id] || 0;
|
||||
if (this.buffer._temp != null) {
|
||||
ref = this.buffer._temp;
|
||||
for (o_name in ref) {
|
||||
o = ref[o_name];
|
||||
o.uid.creator = this.user_id;
|
||||
o.uid.op_number += counter_diff;
|
||||
buff[o.uid.op_number] = o;
|
||||
}
|
||||
}
|
||||
this.operation_counter[this.user_id] = (this.operation_counter._temp || 0) + counter_diff;
|
||||
delete this.operation_counter._temp;
|
||||
return delete this.buffer._temp;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.emptyGarbage = function() {
|
||||
var i, len, o, ref;
|
||||
ref = this.garbage;
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
o = ref[i];
|
||||
if (typeof o.cleanup === "function") {
|
||||
o.cleanup();
|
||||
}
|
||||
}
|
||||
this.garbage = this.trash;
|
||||
this.trash = [];
|
||||
if (this.garbageCollectTimeout !== -1) {
|
||||
this.garbageCollectTimeoutId = setTimeout(this.emptyGarbage, this.garbageCollectTimeout);
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.getUserId = function() {
|
||||
return this.user_id;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.addToGarbageCollector = function() {
|
||||
var i, len, o, results;
|
||||
if (this.performGarbageCollection) {
|
||||
results = [];
|
||||
for (i = 0, len = arguments.length; i < len; i++) {
|
||||
o = arguments[i];
|
||||
if (o != null) {
|
||||
results.push(this.garbage.push(o));
|
||||
} else {
|
||||
results.push(void 0);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.stopGarbageCollection = function() {
|
||||
this.performGarbageCollection = false;
|
||||
this.setManualGarbageCollect();
|
||||
this.garbage = [];
|
||||
return this.trash = [];
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.setManualGarbageCollect = function() {
|
||||
this.garbageCollectTimeout = -1;
|
||||
clearTimeout(this.garbageCollectTimeoutId);
|
||||
return this.garbageCollectTimeoutId = void 0;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.setGarbageCollectTimeout = function(garbageCollectTimeout) {
|
||||
this.garbageCollectTimeout = garbageCollectTimeout;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.getReservedUniqueIdentifier = function() {
|
||||
return {
|
||||
creator: '_',
|
||||
op_number: "_" + (this.reserved_identifier_counter++)
|
||||
};
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.getOperationCounter = function(user_id) {
|
||||
var ctn, ref, res, user;
|
||||
if (user_id == null) {
|
||||
res = {};
|
||||
ref = this.operation_counter;
|
||||
for (user in ref) {
|
||||
ctn = ref[user];
|
||||
res[user] = ctn;
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return this.operation_counter[user_id];
|
||||
}
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.isExpectedOperation = function(o) {
|
||||
var base, name;
|
||||
if ((base = this.operation_counter)[name = o.uid.creator] == null) {
|
||||
base[name] = 0;
|
||||
}
|
||||
o.uid.op_number <= this.operation_counter[o.uid.creator];
|
||||
return true;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype._encode = function(state_vector) {
|
||||
var json, o, o_json, o_next, o_number, o_prev, ref, u_name, unknown, user;
|
||||
if (state_vector == null) {
|
||||
state_vector = {};
|
||||
}
|
||||
json = [];
|
||||
unknown = function(user, o_number) {
|
||||
if ((user == null) || (o_number == null)) {
|
||||
throw new Error("dah!");
|
||||
}
|
||||
return (state_vector[user] == null) || state_vector[user] <= o_number;
|
||||
};
|
||||
ref = this.buffer;
|
||||
for (u_name in ref) {
|
||||
user = ref[u_name];
|
||||
if (u_name === "_") {
|
||||
continue;
|
||||
}
|
||||
for (o_number in user) {
|
||||
o = user[o_number];
|
||||
if ((o.uid.noOperation == null) && unknown(u_name, o_number)) {
|
||||
o_json = o._encode();
|
||||
if (o.next_cl != null) {
|
||||
o_next = o.next_cl;
|
||||
while ((o_next.next_cl != null) && unknown(o_next.uid.creator, o_next.uid.op_number)) {
|
||||
o_next = o_next.next_cl;
|
||||
}
|
||||
o_json.next = o_next.getUid();
|
||||
} else if (o.prev_cl != null) {
|
||||
o_prev = o.prev_cl;
|
||||
while ((o_prev.prev_cl != null) && unknown(o_prev.uid.creator, o_prev.uid.op_number)) {
|
||||
o_prev = o_prev.prev_cl;
|
||||
}
|
||||
o_json.prev = o_prev.getUid();
|
||||
}
|
||||
json.push(o_json);
|
||||
}
|
||||
}
|
||||
}
|
||||
return json;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.getNextOperationIdentifier = function(user_id) {
|
||||
var uid;
|
||||
if (user_id == null) {
|
||||
user_id = this.user_id;
|
||||
}
|
||||
if (this.operation_counter[user_id] == null) {
|
||||
this.operation_counter[user_id] = 0;
|
||||
}
|
||||
uid = {
|
||||
'creator': user_id,
|
||||
'op_number': this.operation_counter[user_id]
|
||||
};
|
||||
this.operation_counter[user_id]++;
|
||||
return uid;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.getOperation = function(uid) {
|
||||
var o, ref;
|
||||
if (uid.uid != null) {
|
||||
uid = uid.uid;
|
||||
}
|
||||
o = (ref = this.buffer[uid.creator]) != null ? ref[uid.op_number] : void 0;
|
||||
if ((uid.sub != null) && (o != null)) {
|
||||
return o.retrieveSub(uid.sub);
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.addOperation = function(o) {
|
||||
if (this.buffer[o.uid.creator] == null) {
|
||||
this.buffer[o.uid.creator] = {};
|
||||
}
|
||||
if (this.buffer[o.uid.creator][o.uid.op_number] != null) {
|
||||
throw new Error("You must not overwrite operations!");
|
||||
}
|
||||
if ((o.uid.op_number.constructor !== String) && (!this.isExpectedOperation(o)) && (o.fromHB == null)) {
|
||||
throw new Error("this operation was not expected!");
|
||||
}
|
||||
this.addToCounter(o);
|
||||
this.buffer[o.uid.creator][o.uid.op_number] = o;
|
||||
return o;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.removeOperation = function(o) {
|
||||
var ref;
|
||||
return (ref = this.buffer[o.uid.creator]) != null ? delete ref[o.uid.op_number] : void 0;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.setInvokeSyncHandler = function(f) {
|
||||
return this.invokeSync = f;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.invokeSync = function() {};
|
||||
|
||||
HistoryBuffer.prototype.renewStateVector = function(state_vector) {
|
||||
var results, state, user;
|
||||
results = [];
|
||||
for (user in state_vector) {
|
||||
state = state_vector[user];
|
||||
if (((this.operation_counter[user] == null) || (this.operation_counter[user] < state_vector[user])) && (state_vector[user] != null)) {
|
||||
results.push(this.operation_counter[user] = state_vector[user]);
|
||||
} else {
|
||||
results.push(void 0);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
HistoryBuffer.prototype.addToCounter = function(o) {
|
||||
var base, name;
|
||||
if ((base = this.operation_counter)[name = o.uid.creator] == null) {
|
||||
base[name] = 0;
|
||||
}
|
||||
if (o.uid.op_number === this.operation_counter[o.uid.creator]) {
|
||||
this.operation_counter[o.uid.creator]++;
|
||||
}
|
||||
while (this.buffer[o.uid.creator][this.operation_counter[o.uid.creator]] != null) {
|
||||
this.operation_counter[o.uid.creator]++;
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
return HistoryBuffer;
|
||||
|
||||
})();
|
||||
|
||||
module.exports = HistoryBuffer;
|
@ -1,91 +0,0 @@
|
||||
var YObject;
|
||||
|
||||
YObject = (function() {
|
||||
function YObject(_object) {
|
||||
var name, ref, val;
|
||||
this._object = _object != null ? _object : {};
|
||||
if (this._object.constructor === Object) {
|
||||
ref = this._object;
|
||||
for (name in ref) {
|
||||
val = ref[name];
|
||||
if (val.constructor === Object) {
|
||||
this._object[name] = new YObject(val);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error("Y.Object accepts Json Objects only");
|
||||
}
|
||||
}
|
||||
|
||||
YObject.prototype._name = "Object";
|
||||
|
||||
YObject.prototype._getModel = function(types, ops) {
|
||||
var n, o, ref;
|
||||
if (this._model == null) {
|
||||
this._model = new ops.MapManager(this).execute();
|
||||
ref = this._object;
|
||||
for (n in ref) {
|
||||
o = ref[n];
|
||||
this._model.val(n, o);
|
||||
}
|
||||
}
|
||||
delete this._object;
|
||||
return this._model;
|
||||
};
|
||||
|
||||
YObject.prototype._setModel = function(_model) {
|
||||
this._model = _model;
|
||||
return delete this._object;
|
||||
};
|
||||
|
||||
YObject.prototype.observe = function(f) {
|
||||
this._model.observe(f);
|
||||
return this;
|
||||
};
|
||||
|
||||
YObject.prototype.unobserve = function(f) {
|
||||
this._model.unobserve(f);
|
||||
return this;
|
||||
};
|
||||
|
||||
YObject.prototype.val = function(name, content) {
|
||||
var n, ref, res, v;
|
||||
if (this._model != null) {
|
||||
return this._model.val.apply(this._model, arguments);
|
||||
} else {
|
||||
if (content != null) {
|
||||
return this._object[name] = content;
|
||||
} else if (name != null) {
|
||||
return this._object[name];
|
||||
} else {
|
||||
res = {};
|
||||
ref = this._object;
|
||||
for (n in ref) {
|
||||
v = ref[n];
|
||||
res[n] = v;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
YObject.prototype["delete"] = function(name) {
|
||||
this._model["delete"](name);
|
||||
return this;
|
||||
};
|
||||
|
||||
return YObject;
|
||||
|
||||
})();
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
if (window.Y != null) {
|
||||
window.Y.Object = YObject;
|
||||
} else {
|
||||
throw new Error("You must first import Y!");
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof module !== "undefined" && module !== null) {
|
||||
module.exports = YObject;
|
||||
}
|
@ -1,670 +0,0 @@
|
||||
var slice = [].slice,
|
||||
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
hasProp = {}.hasOwnProperty;
|
||||
|
||||
module.exports = function() {
|
||||
var execution_listener, ops;
|
||||
ops = {};
|
||||
execution_listener = [];
|
||||
ops.Operation = (function() {
|
||||
function Operation(custom_type, uid, content, content_operations) {
|
||||
var name, op;
|
||||
if (custom_type != null) {
|
||||
this.custom_type = custom_type;
|
||||
}
|
||||
this.is_deleted = false;
|
||||
this.garbage_collected = false;
|
||||
this.event_listeners = [];
|
||||
if (uid != null) {
|
||||
this.uid = uid;
|
||||
}
|
||||
if (content === void 0) {
|
||||
|
||||
} else if ((content != null) && (content.creator != null)) {
|
||||
this.saveOperation('content', content);
|
||||
} else {
|
||||
this.content = content;
|
||||
}
|
||||
if (content_operations != null) {
|
||||
this.content_operations = {};
|
||||
for (name in content_operations) {
|
||||
op = content_operations[name];
|
||||
this.saveOperation(name, op, 'content_operations');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Operation.prototype.type = "Operation";
|
||||
|
||||
Operation.prototype.getContent = function(name) {
|
||||
var content, n, ref, ref1, v;
|
||||
if (this.content != null) {
|
||||
if (this.content.getCustomType != null) {
|
||||
return this.content.getCustomType();
|
||||
} else if (this.content.constructor === Object) {
|
||||
if (name != null) {
|
||||
if (this.content[name] != null) {
|
||||
return this.content[name];
|
||||
} else {
|
||||
return this.content_operations[name].getCustomType();
|
||||
}
|
||||
} else {
|
||||
content = {};
|
||||
ref = this.content;
|
||||
for (n in ref) {
|
||||
v = ref[n];
|
||||
content[n] = v;
|
||||
}
|
||||
if (this.content_operations != null) {
|
||||
ref1 = this.content_operations;
|
||||
for (n in ref1) {
|
||||
v = ref1[n];
|
||||
v = v.getCustomType();
|
||||
content[n] = v;
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
} else {
|
||||
return this.content;
|
||||
}
|
||||
} else {
|
||||
return this.content;
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.retrieveSub = function() {
|
||||
throw new Error("sub properties are not enable on this operation type!");
|
||||
};
|
||||
|
||||
Operation.prototype.observe = function(f) {
|
||||
return this.event_listeners.push(f);
|
||||
};
|
||||
|
||||
Operation.prototype.unobserve = function(f) {
|
||||
return this.event_listeners = this.event_listeners.filter(function(g) {
|
||||
return f !== g;
|
||||
});
|
||||
};
|
||||
|
||||
Operation.prototype.deleteAllObservers = function() {
|
||||
return this.event_listeners = [];
|
||||
};
|
||||
|
||||
Operation.prototype["delete"] = function() {
|
||||
(new ops.Delete(void 0, this)).execute();
|
||||
return null;
|
||||
};
|
||||
|
||||
Operation.prototype.callEvent = function() {
|
||||
var callon;
|
||||
if (this.custom_type != null) {
|
||||
callon = this.getCustomType();
|
||||
} else {
|
||||
callon = this;
|
||||
}
|
||||
return this.forwardEvent.apply(this, [callon].concat(slice.call(arguments)));
|
||||
};
|
||||
|
||||
Operation.prototype.forwardEvent = function() {
|
||||
var args, f, j, len, op, ref, results;
|
||||
op = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
||||
ref = this.event_listeners;
|
||||
results = [];
|
||||
for (j = 0, len = ref.length; j < len; j++) {
|
||||
f = ref[j];
|
||||
results.push(f.call.apply(f, [op].concat(slice.call(args))));
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
Operation.prototype.isDeleted = function() {
|
||||
return this.is_deleted;
|
||||
};
|
||||
|
||||
Operation.prototype.applyDelete = function(garbagecollect) {
|
||||
if (garbagecollect == null) {
|
||||
garbagecollect = true;
|
||||
}
|
||||
if (!this.garbage_collected) {
|
||||
this.is_deleted = true;
|
||||
if (garbagecollect) {
|
||||
this.garbage_collected = true;
|
||||
return this.HB.addToGarbageCollector(this);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.cleanup = function() {
|
||||
this.HB.removeOperation(this);
|
||||
return this.deleteAllObservers();
|
||||
};
|
||||
|
||||
Operation.prototype.setParent = function(parent1) {
|
||||
this.parent = parent1;
|
||||
};
|
||||
|
||||
Operation.prototype.getParent = function() {
|
||||
return this.parent;
|
||||
};
|
||||
|
||||
Operation.prototype.getUid = function() {
|
||||
var map_uid;
|
||||
if (this.uid.noOperation == null) {
|
||||
return this.uid;
|
||||
} else {
|
||||
if (this.uid.alt != null) {
|
||||
map_uid = this.uid.alt.cloneUid();
|
||||
map_uid.sub = this.uid.sub;
|
||||
return map_uid;
|
||||
} else {
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.cloneUid = function() {
|
||||
var n, ref, uid, v;
|
||||
uid = {};
|
||||
ref = this.getUid();
|
||||
for (n in ref) {
|
||||
v = ref[n];
|
||||
uid[n] = v;
|
||||
}
|
||||
return uid;
|
||||
};
|
||||
|
||||
Operation.prototype.execute = function() {
|
||||
var j, l, len;
|
||||
if (this.validateSavedOperations()) {
|
||||
this.is_executed = true;
|
||||
if (this.uid == null) {
|
||||
this.uid = this.HB.getNextOperationIdentifier();
|
||||
}
|
||||
if (this.uid.noOperation == null) {
|
||||
this.HB.addOperation(this);
|
||||
for (j = 0, len = execution_listener.length; j < len; j++) {
|
||||
l = execution_listener[j];
|
||||
l(this._encode());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.saveOperation = function(name, op, base) {
|
||||
var base1, dest, j, last_path, len, path, paths;
|
||||
if (base == null) {
|
||||
base = "this";
|
||||
}
|
||||
if ((op != null) && (op._getModel != null)) {
|
||||
op = op._getModel(this.custom_types, this.operations);
|
||||
}
|
||||
if (op == null) {
|
||||
|
||||
} else if ((op.execute != null) || !((op.op_number != null) && (op.creator != null))) {
|
||||
if (base === "this") {
|
||||
return this[name] = op;
|
||||
} else {
|
||||
dest = this[base];
|
||||
paths = name.split("/");
|
||||
last_path = paths.pop();
|
||||
for (j = 0, len = paths.length; j < len; j++) {
|
||||
path = paths[j];
|
||||
dest = dest[path];
|
||||
}
|
||||
return dest[last_path] = op;
|
||||
}
|
||||
} else {
|
||||
if (this.unchecked == null) {
|
||||
this.unchecked = {};
|
||||
}
|
||||
if ((base1 = this.unchecked)[base] == null) {
|
||||
base1[base] = {};
|
||||
}
|
||||
return this.unchecked[base][name] = op;
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.validateSavedOperations = function() {
|
||||
var base, base_name, dest, j, last_path, len, name, op, op_uid, path, paths, ref, success, uninstantiated;
|
||||
uninstantiated = {};
|
||||
success = true;
|
||||
ref = this.unchecked;
|
||||
for (base_name in ref) {
|
||||
base = ref[base_name];
|
||||
for (name in base) {
|
||||
op_uid = base[name];
|
||||
op = this.HB.getOperation(op_uid);
|
||||
if (op) {
|
||||
if (base_name === "this") {
|
||||
this[name] = op;
|
||||
} else {
|
||||
dest = this[base_name];
|
||||
paths = name.split("/");
|
||||
last_path = paths.pop();
|
||||
for (j = 0, len = paths.length; j < len; j++) {
|
||||
path = paths[j];
|
||||
dest = dest[path];
|
||||
}
|
||||
dest[last_path] = op;
|
||||
}
|
||||
} else {
|
||||
if (uninstantiated[base_name] == null) {
|
||||
uninstantiated[base_name] = {};
|
||||
}
|
||||
uninstantiated[base_name][name] = op_uid;
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!success) {
|
||||
this.unchecked = uninstantiated;
|
||||
return false;
|
||||
} else {
|
||||
delete this.unchecked;
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.getCustomType = function() {
|
||||
var Type, j, len, ref, t;
|
||||
if (this.custom_type == null) {
|
||||
return this;
|
||||
} else {
|
||||
if (this.custom_type.constructor === String) {
|
||||
Type = this.custom_types;
|
||||
ref = this.custom_type.split(".");
|
||||
for (j = 0, len = ref.length; j < len; j++) {
|
||||
t = ref[j];
|
||||
Type = Type[t];
|
||||
}
|
||||
this.custom_type = new Type();
|
||||
this.custom_type._setModel(this);
|
||||
}
|
||||
return this.custom_type;
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype._encode = function(json) {
|
||||
var n, o, operations, ref, ref1;
|
||||
if (json == null) {
|
||||
json = {};
|
||||
}
|
||||
json.type = this.type;
|
||||
json.uid = this.getUid();
|
||||
if (this.custom_type != null) {
|
||||
if (this.custom_type.constructor === String) {
|
||||
json.custom_type = this.custom_type;
|
||||
} else {
|
||||
json.custom_type = this.custom_type._name;
|
||||
}
|
||||
}
|
||||
if (((ref = this.content) != null ? ref.getUid : void 0) != null) {
|
||||
json.content = this.content.getUid();
|
||||
} else {
|
||||
json.content = this.content;
|
||||
}
|
||||
if (this.content_operations != null) {
|
||||
operations = {};
|
||||
ref1 = this.content_operations;
|
||||
for (n in ref1) {
|
||||
o = ref1[n];
|
||||
if (o._getModel != null) {
|
||||
o = o._getModel(this.custom_types, this.operations);
|
||||
}
|
||||
operations[n] = o.getUid();
|
||||
}
|
||||
json.content_operations = operations;
|
||||
}
|
||||
return json;
|
||||
};
|
||||
|
||||
return Operation;
|
||||
|
||||
})();
|
||||
ops.Delete = (function(superClass) {
|
||||
extend(Delete, superClass);
|
||||
|
||||
function Delete(custom_type, uid, deletes) {
|
||||
this.saveOperation('deletes', deletes);
|
||||
Delete.__super__.constructor.call(this, custom_type, uid);
|
||||
}
|
||||
|
||||
Delete.prototype.type = "Delete";
|
||||
|
||||
Delete.prototype._encode = function() {
|
||||
return {
|
||||
'type': "Delete",
|
||||
'uid': this.getUid(),
|
||||
'deletes': this.deletes.getUid()
|
||||
};
|
||||
};
|
||||
|
||||
Delete.prototype.execute = function() {
|
||||
var res;
|
||||
if (this.validateSavedOperations()) {
|
||||
res = Delete.__super__.execute.apply(this, arguments);
|
||||
if (res) {
|
||||
this.deletes.applyDelete(this);
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
return Delete;
|
||||
|
||||
})(ops.Operation);
|
||||
ops.Delete.parse = function(o) {
|
||||
var deletes_uid, uid;
|
||||
uid = o['uid'], deletes_uid = o['deletes'];
|
||||
return new this(null, uid, deletes_uid);
|
||||
};
|
||||
ops.Insert = (function(superClass) {
|
||||
extend(Insert, superClass);
|
||||
|
||||
function Insert(custom_type, content, content_operations, parent, uid, prev_cl, next_cl, origin) {
|
||||
this.saveOperation('parent', parent);
|
||||
this.saveOperation('prev_cl', prev_cl);
|
||||
this.saveOperation('next_cl', next_cl);
|
||||
if (origin != null) {
|
||||
this.saveOperation('origin', origin);
|
||||
} else {
|
||||
this.saveOperation('origin', prev_cl);
|
||||
}
|
||||
Insert.__super__.constructor.call(this, custom_type, uid, content, content_operations);
|
||||
}
|
||||
|
||||
Insert.prototype.type = "Insert";
|
||||
|
||||
Insert.prototype.val = function() {
|
||||
return this.getContent();
|
||||
};
|
||||
|
||||
Insert.prototype.getNext = function(i) {
|
||||
var n;
|
||||
if (i == null) {
|
||||
i = 1;
|
||||
}
|
||||
n = this;
|
||||
while (i > 0 && (n.next_cl != null)) {
|
||||
n = n.next_cl;
|
||||
if (!n.is_deleted) {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
if (n.is_deleted) {
|
||||
null;
|
||||
}
|
||||
return n;
|
||||
};
|
||||
|
||||
Insert.prototype.getPrev = function(i) {
|
||||
var n;
|
||||
if (i == null) {
|
||||
i = 1;
|
||||
}
|
||||
n = this;
|
||||
while (i > 0 && (n.prev_cl != null)) {
|
||||
n = n.prev_cl;
|
||||
if (!n.is_deleted) {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
if (n.is_deleted) {
|
||||
return null;
|
||||
} else {
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
||||
Insert.prototype.applyDelete = function(o) {
|
||||
var callLater, garbagecollect;
|
||||
if (this.deleted_by == null) {
|
||||
this.deleted_by = [];
|
||||
}
|
||||
callLater = false;
|
||||
if ((this.parent != null) && !this.is_deleted && (o != null)) {
|
||||
callLater = true;
|
||||
}
|
||||
if (o != null) {
|
||||
this.deleted_by.push(o);
|
||||
}
|
||||
garbagecollect = false;
|
||||
if (this.next_cl.isDeleted()) {
|
||||
garbagecollect = true;
|
||||
}
|
||||
Insert.__super__.applyDelete.call(this, garbagecollect);
|
||||
if (callLater) {
|
||||
this.parent.callOperationSpecificDeleteEvents(this, o);
|
||||
}
|
||||
if ((this.prev_cl != null) && this.prev_cl.isDeleted()) {
|
||||
return this.prev_cl.applyDelete();
|
||||
}
|
||||
};
|
||||
|
||||
Insert.prototype.cleanup = function() {
|
||||
var d, j, len, o, ref;
|
||||
if (this.next_cl.isDeleted()) {
|
||||
ref = this.deleted_by;
|
||||
for (j = 0, len = ref.length; j < len; j++) {
|
||||
d = ref[j];
|
||||
d.cleanup();
|
||||
}
|
||||
o = this.next_cl;
|
||||
while (o.type !== "Delimiter") {
|
||||
if (o.origin === this) {
|
||||
o.origin = this.prev_cl;
|
||||
}
|
||||
o = o.next_cl;
|
||||
}
|
||||
this.prev_cl.next_cl = this.next_cl;
|
||||
this.next_cl.prev_cl = this.prev_cl;
|
||||
if (this.content instanceof ops.Operation && !(this.content instanceof ops.Insert)) {
|
||||
this.content.referenced_by--;
|
||||
if (this.content.referenced_by <= 0 && !this.content.is_deleted) {
|
||||
this.content.applyDelete();
|
||||
}
|
||||
}
|
||||
delete this.content;
|
||||
return Insert.__super__.cleanup.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
Insert.prototype.getDistanceToOrigin = function() {
|
||||
var d, o;
|
||||
d = 0;
|
||||
o = this.prev_cl;
|
||||
while (true) {
|
||||
if (this.origin === o) {
|
||||
break;
|
||||
}
|
||||
d++;
|
||||
o = o.prev_cl;
|
||||
}
|
||||
return d;
|
||||
};
|
||||
|
||||
Insert.prototype.execute = function() {
|
||||
var base1, distance_to_origin, i, o;
|
||||
if (!this.validateSavedOperations()) {
|
||||
return false;
|
||||
} else {
|
||||
if (this.content instanceof ops.Operation) {
|
||||
this.content.insert_parent = this;
|
||||
if ((base1 = this.content).referenced_by == null) {
|
||||
base1.referenced_by = 0;
|
||||
}
|
||||
this.content.referenced_by++;
|
||||
}
|
||||
if (this.parent != null) {
|
||||
if (this.prev_cl == null) {
|
||||
this.prev_cl = this.parent.beginning;
|
||||
}
|
||||
if (this.origin == null) {
|
||||
this.origin = this.prev_cl;
|
||||
} else if (this.origin === "Delimiter") {
|
||||
this.origin = this.parent.beginning;
|
||||
}
|
||||
if (this.next_cl == null) {
|
||||
this.next_cl = this.parent.end;
|
||||
}
|
||||
}
|
||||
if (this.prev_cl != null) {
|
||||
distance_to_origin = this.getDistanceToOrigin();
|
||||
o = this.prev_cl.next_cl;
|
||||
i = distance_to_origin;
|
||||
while (true) {
|
||||
if (o !== this.next_cl) {
|
||||
if (o.getDistanceToOrigin() === i) {
|
||||
if (o.uid.creator < this.uid.creator) {
|
||||
this.prev_cl = o;
|
||||
distance_to_origin = i + 1;
|
||||
} else {
|
||||
|
||||
}
|
||||
} else if (o.getDistanceToOrigin() < i) {
|
||||
if (i - distance_to_origin <= o.getDistanceToOrigin()) {
|
||||
this.prev_cl = o;
|
||||
distance_to_origin = i + 1;
|
||||
} else {
|
||||
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
o = o.next_cl;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.next_cl = this.prev_cl.next_cl;
|
||||
this.prev_cl.next_cl = this;
|
||||
this.next_cl.prev_cl = this;
|
||||
}
|
||||
this.setParent(this.prev_cl.getParent());
|
||||
Insert.__super__.execute.apply(this, arguments);
|
||||
this.parent.callOperationSpecificInsertEvents(this);
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
Insert.prototype.getPosition = function() {
|
||||
var position, prev;
|
||||
position = 0;
|
||||
prev = this.prev_cl;
|
||||
while (true) {
|
||||
if (prev instanceof ops.Delimiter) {
|
||||
break;
|
||||
}
|
||||
if (!prev.isDeleted()) {
|
||||
position++;
|
||||
}
|
||||
prev = prev.prev_cl;
|
||||
}
|
||||
return position;
|
||||
};
|
||||
|
||||
Insert.prototype._encode = function(json) {
|
||||
if (json == null) {
|
||||
json = {};
|
||||
}
|
||||
json.prev = this.prev_cl.getUid();
|
||||
json.next = this.next_cl.getUid();
|
||||
if (this.origin.type === "Delimiter") {
|
||||
json.origin = "Delimiter";
|
||||
} else if (this.origin !== this.prev_cl) {
|
||||
json.origin = this.origin.getUid();
|
||||
}
|
||||
json.parent = this.parent.getUid();
|
||||
return Insert.__super__._encode.call(this, json);
|
||||
};
|
||||
|
||||
return Insert;
|
||||
|
||||
})(ops.Operation);
|
||||
ops.Insert.parse = function(json) {
|
||||
var content, content_operations, next, origin, parent, prev, uid;
|
||||
content = json['content'], content_operations = json['content_operations'], uid = json['uid'], prev = json['prev'], next = json['next'], origin = json['origin'], parent = json['parent'];
|
||||
return new this(null, content, content_operations, parent, uid, prev, next, origin);
|
||||
};
|
||||
ops.Delimiter = (function(superClass) {
|
||||
extend(Delimiter, superClass);
|
||||
|
||||
function Delimiter(prev_cl, next_cl, origin) {
|
||||
this.saveOperation('prev_cl', prev_cl);
|
||||
this.saveOperation('next_cl', next_cl);
|
||||
this.saveOperation('origin', prev_cl);
|
||||
Delimiter.__super__.constructor.call(this, null, {
|
||||
noOperation: true
|
||||
});
|
||||
}
|
||||
|
||||
Delimiter.prototype.type = "Delimiter";
|
||||
|
||||
Delimiter.prototype.applyDelete = function() {
|
||||
var o;
|
||||
Delimiter.__super__.applyDelete.call(this);
|
||||
o = this.prev_cl;
|
||||
while (o != null) {
|
||||
o.applyDelete();
|
||||
o = o.prev_cl;
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
Delimiter.prototype.cleanup = function() {
|
||||
return Delimiter.__super__.cleanup.call(this);
|
||||
};
|
||||
|
||||
Delimiter.prototype.execute = function() {
|
||||
var ref, ref1;
|
||||
if (((ref = this.unchecked) != null ? ref['next_cl'] : void 0) != null) {
|
||||
return Delimiter.__super__.execute.apply(this, arguments);
|
||||
} else if ((ref1 = this.unchecked) != null ? ref1['prev_cl'] : void 0) {
|
||||
if (this.validateSavedOperations()) {
|
||||
if (this.prev_cl.next_cl != null) {
|
||||
throw new Error("Probably duplicated operations");
|
||||
}
|
||||
this.prev_cl.next_cl = this;
|
||||
return Delimiter.__super__.execute.apply(this, arguments);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if ((this.prev_cl != null) && (this.prev_cl.next_cl == null)) {
|
||||
delete this.prev_cl.unchecked.next_cl;
|
||||
this.prev_cl.next_cl = this;
|
||||
return Delimiter.__super__.execute.apply(this, arguments);
|
||||
} else if ((this.prev_cl != null) || (this.next_cl != null) || true) {
|
||||
return Delimiter.__super__.execute.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
Delimiter.prototype._encode = function() {
|
||||
var ref, ref1;
|
||||
return {
|
||||
'type': this.type,
|
||||
'uid': this.getUid(),
|
||||
'prev': (ref = this.prev_cl) != null ? ref.getUid() : void 0,
|
||||
'next': (ref1 = this.next_cl) != null ? ref1.getUid() : void 0
|
||||
};
|
||||
};
|
||||
|
||||
return Delimiter;
|
||||
|
||||
})(ops.Operation);
|
||||
ops.Delimiter.parse = function(json) {
|
||||
var next, prev, uid;
|
||||
uid = json['uid'], prev = json['prev'], next = json['next'];
|
||||
return new this(uid, prev, next);
|
||||
};
|
||||
return {
|
||||
'operations': ops,
|
||||
'execution_listener': execution_listener
|
||||
};
|
||||
};
|
@ -1,579 +0,0 @@
|
||||
var basic_ops_uninitialized,
|
||||
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
hasProp = {}.hasOwnProperty;
|
||||
|
||||
basic_ops_uninitialized = require("./Basic");
|
||||
|
||||
module.exports = function() {
|
||||
var basic_ops, ops;
|
||||
basic_ops = basic_ops_uninitialized();
|
||||
ops = basic_ops.operations;
|
||||
ops.MapManager = (function(superClass) {
|
||||
extend(MapManager, superClass);
|
||||
|
||||
function MapManager(custom_type, uid, content, content_operations) {
|
||||
this._map = {};
|
||||
MapManager.__super__.constructor.call(this, custom_type, uid, content, content_operations);
|
||||
}
|
||||
|
||||
MapManager.prototype.type = "MapManager";
|
||||
|
||||
MapManager.prototype.applyDelete = function() {
|
||||
var name, p, ref;
|
||||
ref = this._map;
|
||||
for (name in ref) {
|
||||
p = ref[name];
|
||||
p.applyDelete();
|
||||
}
|
||||
return MapManager.__super__.applyDelete.call(this);
|
||||
};
|
||||
|
||||
MapManager.prototype.cleanup = function() {
|
||||
return MapManager.__super__.cleanup.call(this);
|
||||
};
|
||||
|
||||
MapManager.prototype.map = function(f) {
|
||||
var n, ref, v;
|
||||
ref = this._map;
|
||||
for (n in ref) {
|
||||
v = ref[n];
|
||||
f(n, v);
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
MapManager.prototype.val = function(name, content) {
|
||||
var o, prop, ref, rep, res, result;
|
||||
if (arguments.length > 1) {
|
||||
if ((content != null) && (content._getModel != null)) {
|
||||
rep = content._getModel(this.custom_types, this.operations);
|
||||
} else {
|
||||
rep = content;
|
||||
}
|
||||
this.retrieveSub(name).replace(rep);
|
||||
return this.getCustomType();
|
||||
} else if (name != null) {
|
||||
prop = this._map[name];
|
||||
if ((prop != null) && !prop.isContentDeleted()) {
|
||||
res = prop.val();
|
||||
if (res instanceof ops.Operation) {
|
||||
return res.getCustomType();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
return void 0;
|
||||
}
|
||||
} else {
|
||||
result = {};
|
||||
ref = this._map;
|
||||
for (name in ref) {
|
||||
o = ref[name];
|
||||
if (!o.isContentDeleted()) {
|
||||
result[name] = o.val();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
MapManager.prototype["delete"] = function(name) {
|
||||
var ref;
|
||||
if ((ref = this._map[name]) != null) {
|
||||
ref.deleteContent();
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
MapManager.prototype.retrieveSub = function(property_name) {
|
||||
var event_properties, event_this, rm, rm_uid;
|
||||
if (this._map[property_name] == null) {
|
||||
event_properties = {
|
||||
name: property_name
|
||||
};
|
||||
event_this = this;
|
||||
rm_uid = {
|
||||
noOperation: true,
|
||||
sub: property_name,
|
||||
alt: this
|
||||
};
|
||||
rm = new ops.ReplaceManager(null, event_properties, event_this, rm_uid);
|
||||
this._map[property_name] = rm;
|
||||
rm.setParent(this, property_name);
|
||||
rm.execute();
|
||||
}
|
||||
return this._map[property_name];
|
||||
};
|
||||
|
||||
return MapManager;
|
||||
|
||||
})(ops.Operation);
|
||||
ops.MapManager.parse = function(json) {
|
||||
var content, content_operations, custom_type, uid;
|
||||
uid = json['uid'], custom_type = json['custom_type'], content = json['content'], content_operations = json['content_operations'];
|
||||
return new this(custom_type, uid, content, content_operations);
|
||||
};
|
||||
ops.ListManager = (function(superClass) {
|
||||
extend(ListManager, superClass);
|
||||
|
||||
function ListManager(custom_type, uid, content, content_operations) {
|
||||
this.beginning = new ops.Delimiter(void 0, void 0);
|
||||
this.end = new ops.Delimiter(this.beginning, void 0);
|
||||
this.beginning.next_cl = this.end;
|
||||
this.beginning.execute();
|
||||
this.end.execute();
|
||||
ListManager.__super__.constructor.call(this, custom_type, uid, content, content_operations);
|
||||
}
|
||||
|
||||
ListManager.prototype.type = "ListManager";
|
||||
|
||||
ListManager.prototype.applyDelete = function() {
|
||||
var o;
|
||||
o = this.beginning;
|
||||
while (o != null) {
|
||||
o.applyDelete();
|
||||
o = o.next_cl;
|
||||
}
|
||||
return ListManager.__super__.applyDelete.call(this);
|
||||
};
|
||||
|
||||
ListManager.prototype.cleanup = function() {
|
||||
return ListManager.__super__.cleanup.call(this);
|
||||
};
|
||||
|
||||
ListManager.prototype.toJson = function(transform_to_value) {
|
||||
var i, j, len, o, results, val;
|
||||
if (transform_to_value == null) {
|
||||
transform_to_value = false;
|
||||
}
|
||||
val = this.val();
|
||||
results = [];
|
||||
for (o = j = 0, len = val.length; j < len; o = ++j) {
|
||||
i = val[o];
|
||||
if (o instanceof ops.Object) {
|
||||
results.push(o.toJson(transform_to_value));
|
||||
} else if (o instanceof ops.ListManager) {
|
||||
results.push(o.toJson(transform_to_value));
|
||||
} else if (transform_to_value && o instanceof ops.Operation) {
|
||||
results.push(o.val());
|
||||
} else {
|
||||
results.push(o);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
ListManager.prototype.execute = function() {
|
||||
if (this.validateSavedOperations()) {
|
||||
this.beginning.setParent(this);
|
||||
this.end.setParent(this);
|
||||
return ListManager.__super__.execute.apply(this, arguments);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
ListManager.prototype.getLastOperation = function() {
|
||||
return this.end.prev_cl;
|
||||
};
|
||||
|
||||
ListManager.prototype.getFirstOperation = function() {
|
||||
return this.beginning.next_cl;
|
||||
};
|
||||
|
||||
ListManager.prototype.toArray = function() {
|
||||
var o, result;
|
||||
o = this.beginning.next_cl;
|
||||
result = [];
|
||||
while (o !== this.end) {
|
||||
if (!o.is_deleted) {
|
||||
result.push(o.val());
|
||||
}
|
||||
o = o.next_cl;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
ListManager.prototype.map = function(f) {
|
||||
var o, result;
|
||||
o = this.beginning.next_cl;
|
||||
result = [];
|
||||
while (o !== this.end) {
|
||||
if (!o.is_deleted) {
|
||||
result.push(f(o));
|
||||
}
|
||||
o = o.next_cl;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
ListManager.prototype.fold = function(init, f) {
|
||||
var o;
|
||||
o = this.beginning.next_cl;
|
||||
while (o !== this.end) {
|
||||
if (!o.is_deleted) {
|
||||
init = f(init, o);
|
||||
}
|
||||
o = o.next_cl;
|
||||
}
|
||||
return init;
|
||||
};
|
||||
|
||||
ListManager.prototype.val = function(pos) {
|
||||
var o;
|
||||
if (pos != null) {
|
||||
o = this.getOperationByPosition(pos + 1);
|
||||
if (!(o instanceof ops.Delimiter)) {
|
||||
return o.val();
|
||||
} else {
|
||||
throw new Error("this position does not exist");
|
||||
}
|
||||
} else {
|
||||
return this.toArray();
|
||||
}
|
||||
};
|
||||
|
||||
ListManager.prototype.ref = function(pos) {
|
||||
var o;
|
||||
if (pos != null) {
|
||||
o = this.getOperationByPosition(pos + 1);
|
||||
if (!(o instanceof ops.Delimiter)) {
|
||||
return o;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
throw new Error("you must specify a position parameter");
|
||||
}
|
||||
};
|
||||
|
||||
ListManager.prototype.getOperationByPosition = function(position) {
|
||||
var o;
|
||||
o = this.beginning;
|
||||
while (true) {
|
||||
if (o instanceof ops.Delimiter && (o.prev_cl != null)) {
|
||||
o = o.prev_cl;
|
||||
while (o.isDeleted() && (o.prev_cl != null)) {
|
||||
o = o.prev_cl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (position <= 0 && !o.isDeleted()) {
|
||||
break;
|
||||
}
|
||||
o = o.next_cl;
|
||||
if (!o.isDeleted()) {
|
||||
position -= 1;
|
||||
}
|
||||
}
|
||||
return o;
|
||||
};
|
||||
|
||||
ListManager.prototype.push = function(content) {
|
||||
return this.insertAfter(this.end.prev_cl, [content]);
|
||||
};
|
||||
|
||||
ListManager.prototype.insertAfter = function(left, contents) {
|
||||
var c, j, len, right, tmp;
|
||||
right = left.next_cl;
|
||||
while (right.isDeleted()) {
|
||||
right = right.next_cl;
|
||||
}
|
||||
left = right.prev_cl;
|
||||
if (contents instanceof ops.Operation) {
|
||||
(new ops.Insert(null, content, null, void 0, void 0, left, right)).execute();
|
||||
} else {
|
||||
for (j = 0, len = contents.length; j < len; j++) {
|
||||
c = contents[j];
|
||||
if ((c != null) && (c._name != null) && (c._getModel != null)) {
|
||||
c = c._getModel(this.custom_types, this.operations);
|
||||
}
|
||||
tmp = (new ops.Insert(null, c, null, void 0, void 0, left, right)).execute();
|
||||
left = tmp;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
ListManager.prototype.insert = function(position, contents) {
|
||||
var ith;
|
||||
ith = this.getOperationByPosition(position);
|
||||
return this.insertAfter(ith, contents);
|
||||
};
|
||||
|
||||
ListManager.prototype["delete"] = function(position, length) {
|
||||
var d, delete_ops, i, j, o, ref;
|
||||
if (length == null) {
|
||||
length = 1;
|
||||
}
|
||||
o = this.getOperationByPosition(position + 1);
|
||||
delete_ops = [];
|
||||
for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
|
||||
if (o instanceof ops.Delimiter) {
|
||||
break;
|
||||
}
|
||||
d = (new ops.Delete(null, void 0, o)).execute();
|
||||
o = o.next_cl;
|
||||
while ((!(o instanceof ops.Delimiter)) && o.isDeleted()) {
|
||||
o = o.next_cl;
|
||||
}
|
||||
delete_ops.push(d._encode());
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
ListManager.prototype.callOperationSpecificInsertEvents = function(op) {
|
||||
var getContentType;
|
||||
getContentType = function(content) {
|
||||
if (content instanceof ops.Operation) {
|
||||
return content.getCustomType();
|
||||
} else {
|
||||
return content;
|
||||
}
|
||||
};
|
||||
return this.callEvent([
|
||||
{
|
||||
type: "insert",
|
||||
reference: op,
|
||||
position: op.getPosition(),
|
||||
object: this.getCustomType(),
|
||||
changedBy: op.uid.creator,
|
||||
value: getContentType(op.val())
|
||||
}
|
||||
]);
|
||||
};
|
||||
|
||||
ListManager.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {
|
||||
return this.callEvent([
|
||||
{
|
||||
type: "delete",
|
||||
reference: op,
|
||||
position: op.getPosition(),
|
||||
object: this.getCustomType(),
|
||||
length: 1,
|
||||
changedBy: del_op.uid.creator,
|
||||
oldValue: op.val()
|
||||
}
|
||||
]);
|
||||
};
|
||||
|
||||
return ListManager;
|
||||
|
||||
})(ops.Operation);
|
||||
ops.ListManager.parse = function(json) {
|
||||
var content, content_operations, custom_type, uid;
|
||||
uid = json['uid'], custom_type = json['custom_type'], content = json['content'], content_operations = json['content_operations'];
|
||||
return new this(custom_type, uid, content, content_operations);
|
||||
};
|
||||
ops.Composition = (function(superClass) {
|
||||
extend(Composition, superClass);
|
||||
|
||||
function Composition(custom_type, _composition_value, composition_value_operations, uid, tmp_composition_ref) {
|
||||
var n, o;
|
||||
this._composition_value = _composition_value;
|
||||
Composition.__super__.constructor.call(this, custom_type, uid);
|
||||
if (tmp_composition_ref != null) {
|
||||
this.tmp_composition_ref = tmp_composition_ref;
|
||||
} else {
|
||||
this.composition_ref = this.end.prev_cl;
|
||||
}
|
||||
if (composition_value_operations != null) {
|
||||
this.composition_value_operations = {};
|
||||
for (n in composition_value_operations) {
|
||||
o = composition_value_operations[n];
|
||||
this.saveOperation(n, o, '_composition_value');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Composition.prototype.type = "Composition";
|
||||
|
||||
Composition.prototype.execute = function() {
|
||||
var composition_ref;
|
||||
if (this.validateSavedOperations()) {
|
||||
this.getCustomType()._setCompositionValue(this._composition_value);
|
||||
delete this._composition_value;
|
||||
if (this.tmp_composition_ref) {
|
||||
composition_ref = this.HB.getOperation(this.tmp_composition_ref);
|
||||
if (composition_ref != null) {
|
||||
delete this.tmp_composition_ref;
|
||||
this.composition_ref = composition_ref;
|
||||
}
|
||||
}
|
||||
return Composition.__super__.execute.apply(this, arguments);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Composition.prototype.callOperationSpecificInsertEvents = function(op) {
|
||||
var o;
|
||||
if (this.tmp_composition_ref != null) {
|
||||
if (op.uid.creator === this.tmp_composition_ref.creator && op.uid.op_number === this.tmp_composition_ref.op_number) {
|
||||
this.composition_ref = op;
|
||||
delete this.tmp_composition_ref;
|
||||
op = op.next_cl;
|
||||
if (op === this.end) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
o = this.end.prev_cl;
|
||||
while (o !== op) {
|
||||
this.getCustomType()._unapply(o.undo_delta);
|
||||
o = o.prev_cl;
|
||||
}
|
||||
while (o !== this.end) {
|
||||
o.undo_delta = this.getCustomType()._apply(o.val());
|
||||
o = o.next_cl;
|
||||
}
|
||||
this.composition_ref = this.end.prev_cl;
|
||||
return this.callEvent([
|
||||
{
|
||||
type: "update",
|
||||
changedBy: op.uid.creator,
|
||||
newValue: this.val()
|
||||
}
|
||||
]);
|
||||
};
|
||||
|
||||
Composition.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {};
|
||||
|
||||
Composition.prototype.applyDelta = function(delta, operations) {
|
||||
(new ops.Insert(null, delta, operations, this, null, this.end.prev_cl, this.end)).execute();
|
||||
return void 0;
|
||||
};
|
||||
|
||||
Composition.prototype._encode = function(json) {
|
||||
var custom, n, o, ref;
|
||||
if (json == null) {
|
||||
json = {};
|
||||
}
|
||||
custom = this.getCustomType()._getCompositionValue();
|
||||
json.composition_value = custom.composition_value;
|
||||
if (custom.composition_value_operations != null) {
|
||||
json.composition_value_operations = {};
|
||||
ref = custom.composition_value_operations;
|
||||
for (n in ref) {
|
||||
o = ref[n];
|
||||
json.composition_value_operations[n] = o.getUid();
|
||||
}
|
||||
}
|
||||
if (this.composition_ref != null) {
|
||||
json.composition_ref = this.composition_ref.getUid();
|
||||
} else {
|
||||
json.composition_ref = this.tmp_composition_ref;
|
||||
}
|
||||
return Composition.__super__._encode.call(this, json);
|
||||
};
|
||||
|
||||
return Composition;
|
||||
|
||||
})(ops.ListManager);
|
||||
ops.Composition.parse = function(json) {
|
||||
var composition_ref, composition_value, composition_value_operations, custom_type, uid;
|
||||
uid = json['uid'], custom_type = json['custom_type'], composition_value = json['composition_value'], composition_value_operations = json['composition_value_operations'], composition_ref = json['composition_ref'];
|
||||
return new this(custom_type, composition_value, composition_value_operations, uid, composition_ref);
|
||||
};
|
||||
ops.ReplaceManager = (function(superClass) {
|
||||
extend(ReplaceManager, superClass);
|
||||
|
||||
function ReplaceManager(custom_type, event_properties1, event_this1, uid) {
|
||||
this.event_properties = event_properties1;
|
||||
this.event_this = event_this1;
|
||||
if (this.event_properties['object'] == null) {
|
||||
this.event_properties['object'] = this.event_this.getCustomType();
|
||||
}
|
||||
ReplaceManager.__super__.constructor.call(this, custom_type, uid);
|
||||
}
|
||||
|
||||
ReplaceManager.prototype.type = "ReplaceManager";
|
||||
|
||||
ReplaceManager.prototype.callEventDecorator = function(events) {
|
||||
var event, j, len, name, prop, ref;
|
||||
if (!this.isDeleted()) {
|
||||
for (j = 0, len = events.length; j < len; j++) {
|
||||
event = events[j];
|
||||
ref = this.event_properties;
|
||||
for (name in ref) {
|
||||
prop = ref[name];
|
||||
event[name] = prop;
|
||||
}
|
||||
}
|
||||
this.event_this.callEvent(events);
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
ReplaceManager.prototype.callOperationSpecificInsertEvents = function(op) {
|
||||
var old_value;
|
||||
if (op.next_cl.type === "Delimiter" && op.prev_cl.type !== "Delimiter") {
|
||||
if (!op.is_deleted) {
|
||||
old_value = op.prev_cl.val();
|
||||
this.callEventDecorator([
|
||||
{
|
||||
type: "update",
|
||||
changedBy: op.uid.creator,
|
||||
oldValue: old_value
|
||||
}
|
||||
]);
|
||||
}
|
||||
op.prev_cl.applyDelete();
|
||||
} else if (op.next_cl.type !== "Delimiter") {
|
||||
op.applyDelete();
|
||||
} else {
|
||||
this.callEventDecorator([
|
||||
{
|
||||
type: "add",
|
||||
changedBy: op.uid.creator
|
||||
}
|
||||
]);
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
ReplaceManager.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {
|
||||
if (op.next_cl.type === "Delimiter") {
|
||||
return this.callEventDecorator([
|
||||
{
|
||||
type: "delete",
|
||||
changedBy: del_op.uid.creator,
|
||||
oldValue: op.val()
|
||||
}
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
ReplaceManager.prototype.replace = function(content, replaceable_uid) {
|
||||
var o, relp;
|
||||
o = this.getLastOperation();
|
||||
relp = (new ops.Insert(null, content, null, this, replaceable_uid, o, o.next_cl)).execute();
|
||||
return void 0;
|
||||
};
|
||||
|
||||
ReplaceManager.prototype.isContentDeleted = function() {
|
||||
return this.getLastOperation().isDeleted();
|
||||
};
|
||||
|
||||
ReplaceManager.prototype.deleteContent = function() {
|
||||
var last_op;
|
||||
last_op = this.getLastOperation();
|
||||
if ((!last_op.isDeleted()) && last_op.type !== "Delimiter") {
|
||||
(new ops.Delete(null, void 0, this.getLastOperation().uid)).execute();
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
|
||||
ReplaceManager.prototype.val = function() {
|
||||
var o;
|
||||
o = this.getLastOperation();
|
||||
return typeof o.val === "function" ? o.val() : void 0;
|
||||
};
|
||||
|
||||
return ReplaceManager;
|
||||
|
||||
})(ops.ListManager);
|
||||
return basic_ops;
|
||||
};
|
@ -1,90 +0,0 @@
|
||||
var bindToChildren;
|
||||
|
||||
bindToChildren = function(that) {
|
||||
var attr, i, j, ref;
|
||||
for (i = j = 0, ref = that.children.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
|
||||
attr = that.children.item(i);
|
||||
if (attr.name != null) {
|
||||
attr.val = that.val.val(attr.name);
|
||||
}
|
||||
}
|
||||
return that.val.observe(function(events) {
|
||||
var event, k, len, newVal, results;
|
||||
results = [];
|
||||
for (k = 0, len = events.length; k < len; k++) {
|
||||
event = events[k];
|
||||
if (event.name != null) {
|
||||
results.push((function() {
|
||||
var l, ref1, results1;
|
||||
results1 = [];
|
||||
for (i = l = 0, ref1 = that.children.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) {
|
||||
attr = that.children.item(i);
|
||||
if ((attr.name != null) && attr.name === event.name) {
|
||||
newVal = that.val.val(attr.name);
|
||||
if (attr.val !== newVal) {
|
||||
results1.push(attr.val = newVal);
|
||||
} else {
|
||||
results1.push(void 0);
|
||||
}
|
||||
} else {
|
||||
results1.push(void 0);
|
||||
}
|
||||
}
|
||||
return results1;
|
||||
})());
|
||||
} else {
|
||||
results.push(void 0);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
});
|
||||
};
|
||||
|
||||
Polymer("y-object", {
|
||||
ready: function() {
|
||||
if (this.connector != null) {
|
||||
this.val = new Y(this.connector);
|
||||
return bindToChildren(this);
|
||||
} else if (this.val != null) {
|
||||
return bindToChildren(this);
|
||||
}
|
||||
},
|
||||
valChanged: function() {
|
||||
if ((this.val != null) && this.val._name === "Object") {
|
||||
return bindToChildren(this);
|
||||
}
|
||||
},
|
||||
connectorChanged: function() {
|
||||
if (this.val == null) {
|
||||
this.val = new Y(this.connector);
|
||||
return bindToChildren(this);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Polymer("y-property", {
|
||||
ready: function() {
|
||||
if ((this.val != null) && (this.name != null)) {
|
||||
if (this.val.constructor === Object) {
|
||||
this.val = this.parentElement.val(this.name, new Y.Object(this.val)).val(this.name);
|
||||
} else if (typeof this.val === "string") {
|
||||
this.parentElement.val(this.name, this.val);
|
||||
}
|
||||
if (this.val._name === "Object") {
|
||||
return bindToChildren(this);
|
||||
}
|
||||
}
|
||||
},
|
||||
valChanged: function() {
|
||||
var ref;
|
||||
if ((this.val != null) && (this.name != null)) {
|
||||
if (this.val.constructor === Object) {
|
||||
return this.val = this.parentElement.val.val(this.name, new Y.Object(this.val)).val(this.name);
|
||||
} else if (this.val._name === "Object") {
|
||||
return bindToChildren(this);
|
||||
} else if ((((ref = this.parentElement.val) != null ? ref.val : void 0) != null) && this.val !== this.parentElement.val.val(this.name)) {
|
||||
return this.parentElement.val.val(this.name, this.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
@ -1,45 +0,0 @@
|
||||
var Engine, HistoryBuffer, adaptConnector, createY, structured_ops_uninitialized;
|
||||
|
||||
structured_ops_uninitialized = require("./Operations/Structured");
|
||||
|
||||
HistoryBuffer = require("./HistoryBuffer");
|
||||
|
||||
Engine = require("./Engine");
|
||||
|
||||
adaptConnector = require("./ConnectorAdapter");
|
||||
|
||||
createY = function(connector) {
|
||||
var HB, ct, engine, model, ops, ops_manager, user_id;
|
||||
if (connector.user_id != null) {
|
||||
user_id = connector.user_id;
|
||||
} else {
|
||||
user_id = "_temp";
|
||||
connector.when_received_state_vector_listeners = [
|
||||
function(state_vector) {
|
||||
return HB.setUserId(this.user_id, state_vector);
|
||||
}
|
||||
];
|
||||
}
|
||||
HB = new HistoryBuffer(user_id);
|
||||
ops_manager = structured_ops_uninitialized(HB, this.constructor);
|
||||
ops = ops_manager.operations;
|
||||
engine = new Engine(HB, ops);
|
||||
adaptConnector(connector, engine, HB, ops_manager.execution_listener);
|
||||
ops.Operation.prototype.HB = HB;
|
||||
ops.Operation.prototype.operations = ops;
|
||||
ops.Operation.prototype.engine = engine;
|
||||
ops.Operation.prototype.connector = connector;
|
||||
ops.Operation.prototype.custom_types = this.constructor;
|
||||
ct = new createY.Object();
|
||||
model = new ops.MapManager(ct, HB.getReservedUniqueIdentifier()).execute();
|
||||
ct._setModel(model);
|
||||
return ct;
|
||||
};
|
||||
|
||||
module.exports = createY;
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.Y = createY;
|
||||
}
|
||||
|
||||
createY.Object = require("./ObjectType");
|
@ -1,27 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test Yjs!</title>
|
||||
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script src="../../node_modules/mocha/mocha.js" class="awesome"></script>
|
||||
<script>
|
||||
mocha.setup('bdd');
|
||||
mocha.ui('bdd');
|
||||
mocha.reporter('html');
|
||||
</script>
|
||||
<script src="object-test.js"></script>
|
||||
<script src="xml-test.js"></script>
|
||||
<script src="list-test.js"></script>
|
||||
<script src="text-test.js"></script>
|
||||
<script>
|
||||
//mocha.checkLeaks();
|
||||
//mocha.run();
|
||||
window.onerror = null;
|
||||
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
|
||||
else { mocha.run(); }
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
22440
build/test/list-test.js
22440
build/test/list-test.js
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
22458
build/test/text-test.js
22458
build/test/text-test.js
File diff suppressed because one or more lines are too long
32404
build/test/xml-test.js
32404
build/test/xml-test.js
File diff suppressed because one or more lines are too long
@ -1,5 +0,0 @@
|
||||
# Examples
|
||||
|
||||
Here you find some (hopefully) usefull examples on how to use Yjs!
|
||||
|
||||
Feel free to use the code of the examples in your own project. They include basic examples how to use Yjs.
|
@ -1,14 +0,0 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=utf-8 />
|
||||
<title>Y Example</title>
|
||||
<script src="../../../webcomponentsjs/webcomponents.min.js"></script>
|
||||
<link rel="import" href="../../../polymer/polymer.html">
|
||||
|
||||
<link rel="import" href="y-test.html">
|
||||
</head>
|
||||
<body>
|
||||
<y-test></y-test>
|
||||
</body>
|
||||
</html>
|
@ -1,57 +0,0 @@
|
||||
<script src="../../build/browser/y.js"></script>
|
||||
<script src="../../../y-text/build/browser/y-text.js"></script>
|
||||
<link rel="import" href="../../build/browser/y-object.html">
|
||||
<link rel="import" href="../../../y-xmpp/build/browser/y-xmpp.html">
|
||||
<link rel="import" href="../../../paper-slider/paper-slider.html">
|
||||
|
||||
<polymer-element name="y-test" attributes="y connector stuff">
|
||||
<template>
|
||||
<h1 id="text" contentEditable> Check this out !</h1>
|
||||
<y-xmpp id="connector" connector={{connector}} room="testy-xmpp-polymer" syncMode="syncAll" debug="true"></y-xmpp>
|
||||
<y-object connector={{connector}} val={{y}}>
|
||||
<y-property name="slider" val={{slider}}>
|
||||
</y-property>
|
||||
<y-property name="stuff" val={{stuff}}>
|
||||
<y-property id="otherstuff" name="otherstuff" val={{otherstuff}}>
|
||||
</y-property>
|
||||
</y-property>
|
||||
</y-object>
|
||||
<y-object val={{otherstuff}}>
|
||||
<y-property name="nostuff" val={{nostuff}}>
|
||||
</y-property>
|
||||
</y-object>
|
||||
<paper-slider min="0" max="200" immediateValue={{slider}}></paper-slider>
|
||||
</template>
|
||||
<script>
|
||||
Polymer({
|
||||
ready: function(){
|
||||
window.y_stuff_property = this.$.otherstuff;
|
||||
this.y.val("slider",50)
|
||||
var that = this;
|
||||
this.connector.whenSynced(function(){
|
||||
if(that.y.val("text") == null){
|
||||
that.y.val("text",new Y.Text("stuff"));
|
||||
}
|
||||
that.y.val("text").bind(that.$.text,that.shadowRoot)
|
||||
})
|
||||
|
||||
// Everything is initialized. Lets test stuff!
|
||||
window.y_test = this;
|
||||
window.y_test.y.val("stuff",{otherstuff:{nostuff:"this is no stuff"}})
|
||||
setTimeout(function(){
|
||||
var res = y_test.y.val("stuff");
|
||||
if(!(y_test.nostuff === "this is no stuff")){
|
||||
console.log("Deep inherit doesn't work!")
|
||||
}
|
||||
window.y_stuff_property.val = {nostuff: "this is also no stuff"};
|
||||
setTimeout(function(){
|
||||
if(!(y_test.nostuff === "this is also no stuff")){
|
||||
console.log("Element val overwrite doesn't work")
|
||||
}
|
||||
console.log("Everything is fine :)");
|
||||
},500)
|
||||
},500);
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</polymer-element>
|
@ -1,21 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=utf-8 />
|
||||
<title>Y Example</title>
|
||||
<script src="../../build/browser/y.js"></script>
|
||||
<script src="../../../y-text/build/browser/y-text.js"></script>
|
||||
<script src="../../../y-xmpp/y-xmpp.js"></script>
|
||||
<script src="./index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 contentEditable> yjs Tutorial</h1>
|
||||
<p> Collaborative Json editing with <a href="https://github.com/rwth-acis/yjs/">yjs</a>
|
||||
and XMPP Connector. </p>
|
||||
|
||||
<textarea style="width:80%;" rows=40 id="textfield"></textarea>
|
||||
|
||||
<p> <a href="https://github.com/y-js/yjs/">yjs</a> is a Framework for Real-Time collaboration on arbitrary data types.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
@ -1,26 +0,0 @@
|
||||
|
||||
|
||||
connector = new Y.XMPP().join("testy-xmpp-json3", {syncMode: "syncAll"});
|
||||
connector.debug = true
|
||||
|
||||
y = new Y(connector);
|
||||
|
||||
window.onload = function(){
|
||||
var textbox = document.getElementById("textfield");
|
||||
y.observe(function(events){
|
||||
for(var i=0; i<events.length; i++){
|
||||
var event = events[i];
|
||||
if(event.name === "textfield" && event.type !== "delete"){
|
||||
y.val("textfield").bind(textbox);
|
||||
y.val("headline").bind(document.querySelector("h1"))
|
||||
}
|
||||
}
|
||||
});
|
||||
connector.whenSynced(function(){
|
||||
if(y.val("textfield") == null){
|
||||
y.val("headline", new Y.Text("headline"));
|
||||
y.val("textfield",new Y.Text("stuff"))
|
||||
}
|
||||
})
|
||||
|
||||
};
|
@ -1,172 +0,0 @@
|
||||
|
||||
class DBTransaction
|
||||
constructor: (@t)->
|
||||
|
||||
setOperation: (op)->
|
||||
that = this
|
||||
new Promise (resolve, reject)->
|
||||
req = that.t.objectStore("HistoryBuffer").put op
|
||||
req.onsuccess = (event)->
|
||||
resolve op
|
||||
req.onerror = (event)->
|
||||
reject "Could not set Operation!"
|
||||
|
||||
getOperation: (uid)->
|
||||
that = this
|
||||
new Promise (resolve, reject)->
|
||||
req = that.t.objectStore("HistoryBuffer").get uid
|
||||
req.onsuccess = (event)->
|
||||
resolve req.result
|
||||
req.onerror = (event)->
|
||||
reject "Could not get Operation!"
|
||||
|
||||
getOperations: (state_map)->
|
||||
flow = Promise.resolve()
|
||||
ops = []
|
||||
that = this
|
||||
hb = that.t.objectStore("HistoryBuffer")
|
||||
|
||||
that.getStateVector().then (end_state_vector)->
|
||||
for end_state of end_state_vector
|
||||
# convert to the db-structure
|
||||
do (end_state = end_state)->
|
||||
start_state =
|
||||
user: end_state.name
|
||||
state: state_map[end_state] ? 0
|
||||
|
||||
flow = flow.then ()->
|
||||
from = [start_state.user, start_state.number]
|
||||
to = [end_state.user, end_state.number]
|
||||
range = IDBKeyRange.bound from, to
|
||||
defer = Promise.defer()
|
||||
|
||||
hb.openCursor(range).onsuccess = ()->
|
||||
cursor = event.target.result
|
||||
if cursor?
|
||||
ops.push cursor.value # add Operation
|
||||
cursor.continue()
|
||||
else
|
||||
# got all ops from this user
|
||||
defer.resolve ops
|
||||
defer.promise
|
||||
|
||||
setState: (state)->
|
||||
that = this
|
||||
new Promise (resolve, reject)->
|
||||
req = that.t.objectStore("StateVector").put state
|
||||
req.onsuccess = (event)->
|
||||
resolve state
|
||||
req.onerror = (event)->
|
||||
reject "Could not set state vector!"
|
||||
|
||||
getState: (user)->
|
||||
defer = Promise.defer()
|
||||
req = @t.objectStore("StateVector").get user
|
||||
req.onsuccess = (event)->
|
||||
defer.resolve req.result
|
||||
req.onerror = (event)->
|
||||
defer.reject "Could not get state vector!"
|
||||
defer.promise
|
||||
|
||||
getStateVector: ()->
|
||||
defer = Promise.defer()
|
||||
state_vector = []
|
||||
@t.objectStore("StateVector").openCursor().onsuccess = ()->
|
||||
cursor = event.target.result
|
||||
if cursor?
|
||||
state = cursor.value
|
||||
state_vector.push state
|
||||
cursor.continue()
|
||||
else
|
||||
# got all ops from this user
|
||||
defer.resolve state_vector
|
||||
defer.promise
|
||||
|
||||
|
||||
|
||||
class Transaction
|
||||
constructor: (@t)->
|
||||
|
||||
updateOperation: (op)->
|
||||
@t.setOperation op
|
||||
|
||||
addOperation: (op)->
|
||||
that = this
|
||||
@t.getState op.uid[0]
|
||||
.then (state)->
|
||||
# only add operation if this is an expected operation
|
||||
if not state?
|
||||
state =
|
||||
user: op.uid[0]
|
||||
number: 0
|
||||
if op.uid[1] is state.number
|
||||
state.number++
|
||||
that.t.setState state
|
||||
else
|
||||
return Promise.reject("Unexpected Operation")
|
||||
.then that.t.setOperation op
|
||||
|
||||
getOperation: (uid)->
|
||||
@t.getOperation uid
|
||||
|
||||
getState: (user)->
|
||||
@t.getState user
|
||||
|
||||
getOperations: (state_vector)->
|
||||
@t.getOperations state_vector
|
||||
|
||||
|
||||
class window.DB
|
||||
constructor: ()->
|
||||
@ready = (new Promise (resolve, reject)->
|
||||
req = indexedDB.open "Testy", 7
|
||||
req.onerror = ()->
|
||||
reject "Couldn't open the IndexedDB database!"
|
||||
req.onsuccess = (event)->
|
||||
resolve event.target.result
|
||||
req.onupgradeneeded = (event)->
|
||||
db = event.target.result
|
||||
objectStore = db.createObjectStore "HistoryBuffer", {keyPath: "uid"}
|
||||
objectStore = db.createObjectStore "StateVector", {keyPath: "user"}
|
||||
|
||||
).catch (message)->
|
||||
throw new Error message
|
||||
|
||||
requestTransaction: ()->
|
||||
@ready.then (db)->
|
||||
new Promise (resolve, reject)->
|
||||
resolve new Transaction( new DBTransaction(db.transaction(["HistoryBuffer", "StateVector"], "readwrite")) )
|
||||
|
||||
removeDatabase: ()->
|
||||
req = indexedDB.deleteDatabase "Testy"
|
||||
req.onsuccess = ()->
|
||||
console.log("Deleted database successfully");
|
||||
req.onblocked = ()->
|
||||
console.log("Database is currently being blocked")
|
||||
console.dir arguments
|
||||
req.onerror = ()->
|
||||
console.log("Couldn't delete database")
|
||||
console.dir arguments
|
||||
null
|
||||
|
||||
window.db = new DB()
|
||||
|
||||
window.addDummyDataSet = ()->
|
||||
db.requestTransaction().then (t)->
|
||||
t.getState("dmonad").then (state)->
|
||||
state ?= {number: 0}
|
||||
t.addOperation({uid: ["dmonad", state.number]})
|
||||
|
||||
window.getOp = (num = 3)->
|
||||
db.requestTransaction().then (t)->
|
||||
t.getOperation(["dmonad", num])
|
||||
.then (op)->
|
||||
console.log("yay:")
|
||||
console.log(op)
|
||||
|
||||
window.getOps = (state_map = {dmonad: 5})->
|
||||
db.requestTransaction().then (t)->
|
||||
t.getOperations(state_map)
|
||||
.then (op)->
|
||||
console.log("yay:")
|
||||
console.log(op)
|
@ -1,11 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=utf-8 />
|
||||
<title>Y Example</title>
|
||||
<script src="./index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 contentEditable> Tutorial</h1>
|
||||
</body>
|
||||
</html>
|
@ -1,272 +0,0 @@
|
||||
// Generated by CoffeeScript 1.9.2
|
||||
(function() {
|
||||
var DBTransaction, Transaction;
|
||||
|
||||
DBTransaction = (function() {
|
||||
function DBTransaction(t1) {
|
||||
this.t = t1;
|
||||
}
|
||||
|
||||
DBTransaction.prototype.setOperation = function(op) {
|
||||
var that;
|
||||
that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
var req;
|
||||
req = that.t.objectStore("HistoryBuffer").put(op);
|
||||
req.onsuccess = function(event) {
|
||||
return resolve(op);
|
||||
};
|
||||
return req.onerror = function(event) {
|
||||
return reject("Could not set Operation!");
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
DBTransaction.prototype.getOperation = function(uid) {
|
||||
var that;
|
||||
that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
var req;
|
||||
req = that.t.objectStore("HistoryBuffer").get(uid);
|
||||
req.onsuccess = function(event) {
|
||||
return resolve(req.result);
|
||||
};
|
||||
return req.onerror = function(event) {
|
||||
return reject("Could not get Operation!");
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
DBTransaction.prototype.getOperations = function(state_map) {
|
||||
var flow, hb, ops, that;
|
||||
flow = Promise.resolve();
|
||||
ops = [];
|
||||
that = this;
|
||||
hb = that.t.objectStore("HistoryBuffer");
|
||||
return that.getStateVector().then(function(end_state_vector) {
|
||||
var end_state, results;
|
||||
results = [];
|
||||
for (end_state in end_state_vector) {
|
||||
results.push((function(end_state) {
|
||||
var ref, start_state;
|
||||
start_state = {
|
||||
user: end_state.name,
|
||||
state: (ref = state_map[end_state]) != null ? ref : 0
|
||||
};
|
||||
return flow = flow.then(function() {
|
||||
var defer, from, range, to;
|
||||
from = [start_state.user, start_state.number];
|
||||
to = [end_state.user, end_state.number];
|
||||
range = IDBKeyRange.bound(from, to);
|
||||
defer = Promise.defer();
|
||||
hb.openCursor(range).onsuccess = function() {
|
||||
var cursor;
|
||||
cursor = event.target.result;
|
||||
if (cursor != null) {
|
||||
ops.push(cursor.value);
|
||||
return cursor["continue"]();
|
||||
} else {
|
||||
return defer.resolve(ops);
|
||||
}
|
||||
};
|
||||
return defer.promise;
|
||||
});
|
||||
})(end_state));
|
||||
}
|
||||
return results;
|
||||
});
|
||||
};
|
||||
|
||||
DBTransaction.prototype.setState = function(state) {
|
||||
var that;
|
||||
that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
var req;
|
||||
req = that.t.objectStore("StateVector").put(state);
|
||||
req.onsuccess = function(event) {
|
||||
return resolve(state);
|
||||
};
|
||||
return req.onerror = function(event) {
|
||||
return reject("Could not set state vector!");
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
DBTransaction.prototype.getState = function(user) {
|
||||
var defer, req;
|
||||
defer = Promise.defer();
|
||||
req = this.t.objectStore("StateVector").get(user);
|
||||
req.onsuccess = function(event) {
|
||||
return defer.resolve(req.result);
|
||||
};
|
||||
req.onerror = function(event) {
|
||||
return defer.reject("Could not get state vector!");
|
||||
};
|
||||
return defer.promise;
|
||||
};
|
||||
|
||||
DBTransaction.prototype.getStateVector = function() {
|
||||
var defer, state_vector;
|
||||
defer = Promise.defer();
|
||||
state_vector = [];
|
||||
this.t.objectStore("StateVector").openCursor().onsuccess = function() {
|
||||
var cursor, state;
|
||||
cursor = event.target.result;
|
||||
if (cursor != null) {
|
||||
state = cursor.value;
|
||||
state_vector.push(state);
|
||||
return cursor["continue"]();
|
||||
} else {
|
||||
return defer.resolve(state_vector);
|
||||
}
|
||||
};
|
||||
return defer.promise;
|
||||
};
|
||||
|
||||
return DBTransaction;
|
||||
|
||||
})();
|
||||
|
||||
Transaction = (function() {
|
||||
function Transaction(t1) {
|
||||
this.t = t1;
|
||||
}
|
||||
|
||||
Transaction.prototype.updateOperation = function(op) {
|
||||
return this.t.setOperation(op);
|
||||
};
|
||||
|
||||
Transaction.prototype.addOperation = function(op) {
|
||||
var that;
|
||||
that = this;
|
||||
return this.t.getState(op.uid[0]).then(function(state) {
|
||||
if (state == null) {
|
||||
state = {
|
||||
user: op.uid[0],
|
||||
number: 0
|
||||
};
|
||||
}
|
||||
if (op.uid[1] === state.number) {
|
||||
state.number++;
|
||||
return that.t.setState(state);
|
||||
} else {
|
||||
return Promise.reject("Unexpected Operation");
|
||||
}
|
||||
}).then(that.t.setOperation(op));
|
||||
};
|
||||
|
||||
Transaction.prototype.getOperation = function(uid) {
|
||||
return this.t.getOperation(uid);
|
||||
};
|
||||
|
||||
Transaction.prototype.getState = function(user) {
|
||||
return this.t.getState(user);
|
||||
};
|
||||
|
||||
Transaction.prototype.getOperations = function(state_vector) {
|
||||
return this.t.getOperations(state_vector);
|
||||
};
|
||||
|
||||
return Transaction;
|
||||
|
||||
})();
|
||||
|
||||
window.DB = (function() {
|
||||
function DB() {
|
||||
this.ready = (new Promise(function(resolve, reject) {
|
||||
var req;
|
||||
req = indexedDB.open("Testy", 7);
|
||||
req.onerror = function() {
|
||||
return reject("Couldn't open the IndexedDB database!");
|
||||
};
|
||||
req.onsuccess = function(event) {
|
||||
return resolve(event.target.result);
|
||||
};
|
||||
return req.onupgradeneeded = function(event) {
|
||||
var db, objectStore;
|
||||
db = event.target.result;
|
||||
objectStore = db.createObjectStore("HistoryBuffer", {
|
||||
keyPath: "uid"
|
||||
});
|
||||
return objectStore = db.createObjectStore("StateVector", {
|
||||
keyPath: "user"
|
||||
});
|
||||
};
|
||||
}))["catch"](function(message) {
|
||||
throw new Error(message);
|
||||
});
|
||||
}
|
||||
|
||||
DB.prototype.requestTransaction = function() {
|
||||
return this.ready.then(function(db) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
return resolve(new Transaction(new DBTransaction(db.transaction(["HistoryBuffer", "StateVector"], "readwrite"))));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
DB.prototype.removeDatabase = function() {
|
||||
var req;
|
||||
req = indexedDB.deleteDatabase("Testy");
|
||||
req.onsuccess = function() {
|
||||
return console.log("Deleted database successfully");
|
||||
};
|
||||
req.onblocked = function() {
|
||||
console.log("Database is currently being blocked");
|
||||
return console.dir(arguments);
|
||||
};
|
||||
req.onerror = function() {
|
||||
console.log("Couldn't delete database");
|
||||
return console.dir(arguments);
|
||||
};
|
||||
return null;
|
||||
};
|
||||
|
||||
return DB;
|
||||
|
||||
})();
|
||||
|
||||
window.db = new DB();
|
||||
|
||||
window.addDummyDataSet = function() {
|
||||
return db.requestTransaction().then(function(t) {
|
||||
return t.getState("dmonad").then(function(state) {
|
||||
if (state == null) {
|
||||
state = {
|
||||
number: 0
|
||||
};
|
||||
}
|
||||
return t.addOperation({
|
||||
uid: ["dmonad", state.number]
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
window.getOp = function(num) {
|
||||
if (num == null) {
|
||||
num = 3;
|
||||
}
|
||||
return db.requestTransaction().then(function(t) {
|
||||
return t.getOperation(["dmonad", num]).then(function(op) {
|
||||
console.log("yay:");
|
||||
return console.log(op);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
window.getOps = function(state_map) {
|
||||
if (state_map == null) {
|
||||
state_map = {
|
||||
dmonad: 5
|
||||
};
|
||||
}
|
||||
return db.requestTransaction().then(function(t) {
|
||||
return t.getOperations(state_map).then(function(op) {
|
||||
console.log("yay:");
|
||||
return console.log(op);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
}).call(this);
|
114
gulpfile.coffee
114
gulpfile.coffee
@ -1,114 +0,0 @@
|
||||
gulp = require('gulp')
|
||||
coffee = require('gulp-coffee')
|
||||
concat = require('gulp-concat')
|
||||
uglify = require 'gulp-uglify'
|
||||
sourcemaps = require('gulp-sourcemaps')
|
||||
browserify = require('gulp-browserify')
|
||||
rename = require 'gulp-rename'
|
||||
rimraf = require 'gulp-rimraf'
|
||||
gulpif = require 'gulp-if'
|
||||
ignore = require 'gulp-ignore'
|
||||
git = require 'gulp-git'
|
||||
debug = require 'gulp-debug'
|
||||
coffeelint = require 'gulp-coffeelint'
|
||||
mocha = require 'gulp-mocha'
|
||||
run = require 'gulp-run'
|
||||
ljs = require 'gulp-ljs'
|
||||
plumber = require 'gulp-plumber'
|
||||
cache = require 'gulp-cached'
|
||||
coffeeify = require 'gulp-coffeeify'
|
||||
exit = require 'gulp-exit'
|
||||
|
||||
gulp.task 'default', ['build_browser']
|
||||
|
||||
files =
|
||||
lib : ['./lib/**/*.coffee']
|
||||
browser : ['./lib/y.coffee','./lib/y-object.coffee']
|
||||
test : ['./test/**/*test.coffee', '../y-*/test/*test.coffee']
|
||||
#test : ['./test/Json_test.coffee', './test/Text_test.coffee']
|
||||
gulp : ['./gulpfile.coffee']
|
||||
examples : ['./examples/**/*.js']
|
||||
other: ['./lib/**/*', './test/*']
|
||||
|
||||
files.all = []
|
||||
for name,file_list of files
|
||||
if name isnt 'build'
|
||||
files.all = files.all.concat file_list
|
||||
|
||||
gulp.task 'deploy_nodejs', ->
|
||||
gulp.src files.lib
|
||||
.pipe sourcemaps.init()
|
||||
.pipe coffee()
|
||||
.pipe sourcemaps.write './'
|
||||
.pipe gulp.dest 'build/node/'
|
||||
.pipe gulpif '!**/', git.add({args : "-A"})
|
||||
|
||||
gulp.task 'deploy', ['mocha', 'build_browser', 'deploy_nodejs', 'lint', 'codo']
|
||||
|
||||
gulp.task 'build_browser', ->
|
||||
gulp.src files.browser, { read: false }
|
||||
.pipe plumber()
|
||||
.pipe browserify
|
||||
transform: ['coffeeify']
|
||||
extensions: ['.coffee']
|
||||
debug : true
|
||||
.pipe rename
|
||||
extname: ".js"
|
||||
.pipe gulp.dest './build/browser/'
|
||||
.pipe uglify()
|
||||
.pipe gulp.dest '.'
|
||||
|
||||
gulp.src files.test, {read: false}
|
||||
.pipe plumber()
|
||||
.pipe browserify
|
||||
transform: ['coffeeify']
|
||||
extensions: ['.coffee']
|
||||
debug: true
|
||||
.pipe rename
|
||||
extname: ".js"
|
||||
dirname: "./"
|
||||
.pipe gulp.dest './build/test/'
|
||||
|
||||
gulp.task 'build_node', ->
|
||||
gulp.src files.lib
|
||||
.pipe plumber()
|
||||
.pipe coffee({bare:true})
|
||||
.pipe gulp.dest './build/node'
|
||||
|
||||
gulp.task 'build', ['build_node', 'build_browser'], ->
|
||||
|
||||
gulp.task 'watch', ['build'], ->
|
||||
gulp.watch files.all, ['build']
|
||||
|
||||
gulp.task 'mocha', ->
|
||||
gulp.src files.test, { read: false }
|
||||
.pipe mocha {reporter : 'list'}
|
||||
.pipe exit()
|
||||
|
||||
gulp.task 'lint', ->
|
||||
gulp.src files.all
|
||||
.pipe ignore.include '**/*.coffee'
|
||||
.pipe coffeelint {
|
||||
"max_line_length":
|
||||
"level": "ignore"
|
||||
}
|
||||
.pipe coffeelint.reporter()
|
||||
|
||||
gulp.task 'literate', ->
|
||||
gulp.src files.examples
|
||||
.pipe ljs { code : true }
|
||||
.pipe rename
|
||||
basename : "README"
|
||||
extname : ".md"
|
||||
.pipe gulp.dest 'examples/'
|
||||
.pipe gulpif '!**/', git.add({args : "-A"})
|
||||
|
||||
gulp.task 'codo', [], ()->
|
||||
command = './node_modules/codo/bin/codo -o "./doc" --name "yjs" --readme "README.md" --undocumented false --private true --title "yjs API" ./lib - LICENSE.txt '
|
||||
run(command).exec()
|
||||
|
||||
gulp.task 'clean', ->
|
||||
gulp.src ['./build/{browser,test,node}/**/*.{js,map}','./doc/'], { read: false }
|
||||
.pipe rimraf()
|
||||
|
||||
gulp.task 'default', ['clean','build'], ->
|
77
gulpfile.js
Normal file
77
gulpfile.js
Normal file
@ -0,0 +1,77 @@
|
||||
/*eslint-env node */
|
||||
|
||||
var gulp = require("gulp");
|
||||
var sourcemaps = require("gulp-sourcemaps");
|
||||
var babel = require("gulp-babel");
|
||||
var uglify = require("gulp-uglify");
|
||||
var minimist = require("minimist");
|
||||
var eslint = require("gulp-eslint");
|
||||
var jasmine = require("gulp-jasmine");
|
||||
var concat = require("gulp-concat");
|
||||
|
||||
var moduleName = "y.js";
|
||||
|
||||
var files = {
|
||||
y: ["src/**/*.js"],
|
||||
lint: ["src/**/*.js", "gulpfile.js"],
|
||||
tests: ["tests/**/*.js"]
|
||||
};
|
||||
|
||||
var options = minimist(process.argv.slice(2), {
|
||||
string: "export",
|
||||
default: { export: "ignore" }
|
||||
});
|
||||
|
||||
gulp.task("test", function () {
|
||||
return gulp.src(files.y.concat(files.tests))
|
||||
.pipe(sourcemaps.init())
|
||||
.pipe(concat(moduleName))
|
||||
.pipe(babel({
|
||||
loose: "all",
|
||||
modules: "common"
|
||||
}))
|
||||
.pipe(uglify())
|
||||
.pipe(gulp.dest("build"))
|
||||
.pipe(jasmine({
|
||||
verbose: true,
|
||||
includeStuckTrace: true
|
||||
}))
|
||||
.pipe(sourcemaps.write("."));
|
||||
});
|
||||
|
||||
gulp.task("build_browser", function () {
|
||||
return gulp.src(files.y)
|
||||
.pipe(sourcemaps.init())
|
||||
.pipe(babel({
|
||||
loose: "all",
|
||||
modules: "ignore"
|
||||
}))
|
||||
.pipe(concat(moduleName))
|
||||
.pipe(uglify())
|
||||
.pipe(sourcemaps.write("."))
|
||||
.pipe(gulp.dest("."));
|
||||
});
|
||||
|
||||
gulp.task("build_node", function(){
|
||||
gulp.src(files.y)
|
||||
.pipe(sourcemaps.init())
|
||||
.pipe(babel({
|
||||
loose: "all",
|
||||
modules: "common"
|
||||
}))
|
||||
.pipe(sourcemaps.write("."))
|
||||
.pipe(gulp.dest("./build_node"));
|
||||
});
|
||||
|
||||
gulp.task("lint", function(){
|
||||
return gulp.y(files.lint)
|
||||
.pipe(eslint())
|
||||
.pipe(eslint.format())
|
||||
.pipe(eslint.failOnError());
|
||||
});
|
||||
|
||||
gulp.task("develop", function(){
|
||||
return gulp.watch(files.src, ["build"]);
|
||||
});
|
||||
|
||||
gulp.task("default", ["build"]);
|
5
interfaces/jasmine.js
Normal file
5
interfaces/jasmine.js
Normal file
@ -0,0 +1,5 @@
|
||||
/* @flow */
|
||||
|
||||
declare var describe : Function;
|
||||
declare var it : Function;
|
||||
declare var expect : Function;
|
@ -1,61 +0,0 @@
|
||||
|
||||
ConnectorClass = require "./ConnectorClass"
|
||||
#
|
||||
# @param {Engine} engine The transformation engine
|
||||
# @param {HistoryBuffer} HB
|
||||
# @param {Array<Function>} execution_listener You must ensure that whenever an operation is executed, every function in this Array is called.
|
||||
#
|
||||
adaptConnector = (connector, engine, HB, execution_listener)->
|
||||
|
||||
for name, f of ConnectorClass
|
||||
connector[name] = f
|
||||
|
||||
connector.setIsBoundToY()
|
||||
|
||||
send_ = (o)->
|
||||
if (o.uid.creator is HB.getUserId()) and
|
||||
(typeof o.uid.op_number isnt "string") and # TODO: i don't think that we need this anymore..
|
||||
(HB.getUserId() isnt "_temp")
|
||||
connector.broadcast o
|
||||
|
||||
if connector.invokeSync?
|
||||
HB.setInvokeSyncHandler connector.invokeSync
|
||||
|
||||
execution_listener.push send_
|
||||
# For the XMPPConnector: lets send it as an array
|
||||
# therefore, we have to restructure it later
|
||||
encode_state_vector = (v)->
|
||||
for name,value of v
|
||||
user: name
|
||||
state: value
|
||||
parse_state_vector = (v)->
|
||||
state_vector = {}
|
||||
for s in v
|
||||
state_vector[s.user] = s.state
|
||||
state_vector
|
||||
|
||||
getStateVector = ()->
|
||||
encode_state_vector HB.getOperationCounter()
|
||||
|
||||
getHB = (v)->
|
||||
state_vector = parse_state_vector v
|
||||
hb = HB._encode state_vector
|
||||
json =
|
||||
hb: hb
|
||||
state_vector: encode_state_vector HB.getOperationCounter()
|
||||
json
|
||||
|
||||
applyHB = (hb, fromHB)->
|
||||
engine.applyOp hb, fromHB
|
||||
|
||||
connector.getStateVector = getStateVector
|
||||
connector.getHB = getHB
|
||||
connector.applyHB = applyHB
|
||||
|
||||
connector.receive_handlers ?= []
|
||||
connector.receive_handlers.push (sender, op)->
|
||||
if op.uid.creator isnt HB.getUserId()
|
||||
engine.applyOp op
|
||||
|
||||
|
||||
module.exports = adaptConnector
|
@ -1,355 +0,0 @@
|
||||
|
||||
module.exports =
|
||||
#
|
||||
# @params new Connector(options)
|
||||
# @param options.syncMethod {String} is either "syncAll" or "master-slave".
|
||||
# @param options.role {String} The role of this client
|
||||
# (slave or master (only used when syncMethod is master-slave))
|
||||
# @param options.perform_send_again {Boolean} Whetehr to whether to resend the HB after some time period. This reduces sync errors, but has some overhead (optional)
|
||||
#
|
||||
init: (options)->
|
||||
req = (name, choices)=>
|
||||
if options[name]?
|
||||
if (not choices?) or choices.some((c)->c is options[name])
|
||||
@[name] = options[name]
|
||||
else
|
||||
throw new Error "You can set the '"+name+"' option to one of the following choices: "+JSON.encode(choices)
|
||||
else
|
||||
throw new Error "You must specify "+name+", when initializing the Connector!"
|
||||
|
||||
req "syncMethod", ["syncAll", "master-slave"]
|
||||
req "role", ["master", "slave"]
|
||||
req "user_id"
|
||||
@on_user_id_set?(@user_id)
|
||||
|
||||
# whether to resend the HB after some time period. This reduces sync errors.
|
||||
# But this is not necessary in the test-connector
|
||||
if options.perform_send_again?
|
||||
@perform_send_again = options.perform_send_again
|
||||
else
|
||||
@perform_send_again = true
|
||||
|
||||
# A Master should sync with everyone! TODO: really? - for now its safer this way!
|
||||
if @role is "master"
|
||||
@syncMethod = "syncAll"
|
||||
|
||||
# is set to true when this is synced with all other connections
|
||||
@is_synced = false
|
||||
# Peerjs Connections: key: conn-id, value: object
|
||||
@connections = {}
|
||||
# List of functions that shall process incoming data
|
||||
@receive_handlers ?= []
|
||||
|
||||
# whether this instance is bound to any y instance
|
||||
@connections = {}
|
||||
@current_sync_target = null
|
||||
@sent_hb_to_all_users = false
|
||||
@is_initialized = true
|
||||
|
||||
onUserEvent: (f)->
|
||||
@connections_listeners ?= []
|
||||
@connections_listeners.push f
|
||||
|
||||
isRoleMaster: ->
|
||||
@role is "master"
|
||||
|
||||
isRoleSlave: ->
|
||||
@role is "slave"
|
||||
|
||||
findNewSyncTarget: ()->
|
||||
@current_sync_target = null
|
||||
if @syncMethod is "syncAll"
|
||||
for user, c of @connections
|
||||
if not c.is_synced
|
||||
@performSync user
|
||||
break
|
||||
if not @current_sync_target?
|
||||
@setStateSynced()
|
||||
null
|
||||
|
||||
userLeft: (user)->
|
||||
delete @connections[user]
|
||||
@findNewSyncTarget()
|
||||
if @connections_listeners?
|
||||
for f in @connections_listeners
|
||||
f {
|
||||
action: "userLeft"
|
||||
user: user
|
||||
}
|
||||
|
||||
|
||||
userJoined: (user, role)->
|
||||
if not role?
|
||||
throw new Error "Internal: You must specify the role of the joined user! E.g. userJoined('uid:3939','slave')"
|
||||
# a user joined the room
|
||||
@connections[user] ?= {}
|
||||
@connections[user].is_synced = false
|
||||
|
||||
if (not @is_synced) or @syncMethod is "syncAll"
|
||||
if @syncMethod is "syncAll"
|
||||
@performSync user
|
||||
else if role is "master"
|
||||
# TODO: What if there are two masters? Prevent sending everything two times!
|
||||
@performSyncWithMaster user
|
||||
|
||||
if @connections_listeners?
|
||||
for f in @connections_listeners
|
||||
f {
|
||||
action: "userJoined"
|
||||
user: user
|
||||
role: role
|
||||
}
|
||||
|
||||
#
|
||||
# Execute a function _when_ we are connected. If not connected, wait until connected.
|
||||
# @param f {Function} Will be executed on the Connector context.
|
||||
#
|
||||
whenSynced: (args)->
|
||||
if args.constructor is Function
|
||||
args = [args]
|
||||
if @is_synced
|
||||
args[0].apply this, args[1..]
|
||||
else
|
||||
@compute_when_synced ?= []
|
||||
@compute_when_synced.push args
|
||||
|
||||
#
|
||||
# Execute an function when a message is received.
|
||||
# @param f {Function} Will be executed on the PeerJs-Connector context. f will be called with (sender_id, broadcast {true|false}, message).
|
||||
#
|
||||
onReceive: (f)->
|
||||
@receive_handlers.push f
|
||||
|
||||
###
|
||||
# Broadcast a message to all connected peers.
|
||||
# @param message {Object} The message to broadcast.
|
||||
#
|
||||
broadcast: (message)->
|
||||
throw new Error "You must implement broadcast!"
|
||||
|
||||
#
|
||||
# Send a message to a peer, or set of peers
|
||||
#
|
||||
send: (peer_s, message)->
|
||||
throw new Error "You must implement send!"
|
||||
###
|
||||
|
||||
#
|
||||
# perform a sync with a specific user.
|
||||
#
|
||||
performSync: (user)->
|
||||
if not @current_sync_target?
|
||||
@current_sync_target = user
|
||||
@send user,
|
||||
sync_step: "getHB"
|
||||
send_again: "true"
|
||||
data: @getStateVector()
|
||||
if not @sent_hb_to_all_users
|
||||
@sent_hb_to_all_users = true
|
||||
|
||||
hb = @getHB([]).hb
|
||||
_hb = []
|
||||
for o in hb
|
||||
_hb.push o
|
||||
if _hb.length > 10
|
||||
@broadcast
|
||||
sync_step: "applyHB_"
|
||||
data: _hb
|
||||
_hb = []
|
||||
@broadcast
|
||||
sync_step: "applyHB"
|
||||
data: _hb
|
||||
|
||||
|
||||
|
||||
#
|
||||
# When a master node joined the room, perform this sync with him. It will ask the master for the HB,
|
||||
# and will broadcast his own HB
|
||||
#
|
||||
performSyncWithMaster: (user)->
|
||||
@current_sync_target = user
|
||||
@send user,
|
||||
sync_step: "getHB"
|
||||
send_again: "true"
|
||||
data: @getStateVector()
|
||||
hb = @getHB([]).hb
|
||||
_hb = []
|
||||
for o in hb
|
||||
_hb.push o
|
||||
if _hb.length > 10
|
||||
@broadcast
|
||||
sync_step: "applyHB_"
|
||||
data: _hb
|
||||
_hb = []
|
||||
@broadcast
|
||||
sync_step: "applyHB"
|
||||
data: _hb
|
||||
|
||||
#
|
||||
# You are sure that all clients are synced, call this function.
|
||||
#
|
||||
setStateSynced: ()->
|
||||
if not @is_synced
|
||||
@is_synced = true
|
||||
if @compute_when_synced?
|
||||
for el in @compute_when_synced
|
||||
f = el[0]
|
||||
args = el[1..]
|
||||
f.apply(args)
|
||||
delete @compute_when_synced
|
||||
null
|
||||
|
||||
# executed when the a state_vector is received. listener will be called only once!
|
||||
whenReceivedStateVector: (f)->
|
||||
@when_received_state_vector_listeners ?= []
|
||||
@when_received_state_vector_listeners.push f
|
||||
|
||||
|
||||
#
|
||||
# You received a raw message, and you know that it is intended for to Yjs. Then call this function.
|
||||
#
|
||||
receiveMessage: (sender, res)->
|
||||
if not res.sync_step?
|
||||
for f in @receive_handlers
|
||||
f sender, res
|
||||
else
|
||||
if sender is @user_id
|
||||
return
|
||||
if res.sync_step is "getHB"
|
||||
# call listeners
|
||||
if @when_received_state_vector_listeners?
|
||||
for f in @when_received_state_vector_listeners
|
||||
f.call this, res.data
|
||||
delete @when_received_state_vector_listeners
|
||||
|
||||
data = @getHB(res.data)
|
||||
hb = data.hb
|
||||
_hb = []
|
||||
# always broadcast, when not synced.
|
||||
# This reduces errors, when the clients goes offline prematurely.
|
||||
# When this client only syncs to one other clients, but looses connectors,
|
||||
# before syncing to the other clients, the online clients have different states.
|
||||
# Since we do not want to perform regular syncs, this is a good alternative
|
||||
if @is_synced
|
||||
sendApplyHB = (m)=>
|
||||
@send sender, m
|
||||
else
|
||||
sendApplyHB = (m)=>
|
||||
@broadcast m
|
||||
|
||||
for o in hb
|
||||
_hb.push o
|
||||
if _hb.length > 10
|
||||
sendApplyHB
|
||||
sync_step: "applyHB_"
|
||||
data: _hb
|
||||
_hb = []
|
||||
|
||||
sendApplyHB
|
||||
sync_step : "applyHB"
|
||||
data: _hb
|
||||
|
||||
if res.send_again? and @perform_send_again
|
||||
send_again = do (sv = data.state_vector)=>
|
||||
()=>
|
||||
hb = @getHB(sv).hb
|
||||
for o in hb
|
||||
_hb.push o
|
||||
if _hb.length > 10
|
||||
@send sender,
|
||||
sync_step: "applyHB_"
|
||||
data: _hb
|
||||
_hb = []
|
||||
@send sender,
|
||||
sync_step: "applyHB",
|
||||
data: _hb
|
||||
sent_again: "true"
|
||||
setTimeout send_again, 3000
|
||||
else if res.sync_step is "applyHB"
|
||||
@applyHB(res.data, sender is @current_sync_target)
|
||||
|
||||
if (@syncMethod is "syncAll" or res.sent_again?) and (not @is_synced) and ((@current_sync_target is sender) or (not @current_sync_target?))
|
||||
@connections[sender].is_synced = true
|
||||
@findNewSyncTarget()
|
||||
|
||||
else if res.sync_step is "applyHB_"
|
||||
@applyHB(res.data, sender is @current_sync_target)
|
||||
|
||||
|
||||
# Currently, the HB encodes operations as JSON. For the moment I want to keep it
|
||||
# that way. Maybe we support encoding in the HB as XML in the future, but for now I don't want
|
||||
# too much overhead. Y is very likely to get changed a lot in the future
|
||||
#
|
||||
# Because we don't want to encode JSON as string (with character escaping, wich makes it pretty much unreadable)
|
||||
# we encode the JSON as XML.
|
||||
#
|
||||
# When the HB support encoding as XML, the format should look pretty much like this.
|
||||
|
||||
# does not support primitive values as array elements
|
||||
# expects an ltx (less than xml) object
|
||||
parseMessageFromXml: (m)->
|
||||
parse_array = (node)->
|
||||
for n in node.children
|
||||
if n.getAttribute("isArray") is "true"
|
||||
parse_array n
|
||||
else
|
||||
parse_object n
|
||||
|
||||
parse_object = (node)->
|
||||
json = {}
|
||||
for name, value of node.attrs
|
||||
int = parseInt(value)
|
||||
if isNaN(int) or (""+int) isnt value
|
||||
json[name] = value
|
||||
else
|
||||
json[name] = int
|
||||
for n in node.children
|
||||
name = n.name
|
||||
if n.getAttribute("isArray") is "true"
|
||||
json[name] = parse_array n
|
||||
else
|
||||
json[name] = parse_object n
|
||||
json
|
||||
parse_object m
|
||||
|
||||
# encode message in xml
|
||||
# we use string because Strophe only accepts an "xml-string"..
|
||||
# So {a:4,b:{c:5}} will look like
|
||||
# <y a="4">
|
||||
# <b c="5"></b>
|
||||
# </y>
|
||||
# m - ltx element
|
||||
# json - guess it ;)
|
||||
#
|
||||
encodeMessageToXml: (m, json)->
|
||||
# attributes is optional
|
||||
encode_object = (m, json)->
|
||||
for name,value of json
|
||||
if not value?
|
||||
# nop
|
||||
else if value.constructor is Object
|
||||
encode_object m.c(name), value
|
||||
else if value.constructor is Array
|
||||
encode_array m.c(name), value
|
||||
else
|
||||
m.setAttribute(name,value)
|
||||
m
|
||||
encode_array = (m, array)->
|
||||
m.setAttribute("isArray","true")
|
||||
for e in array
|
||||
if e.constructor is Object
|
||||
encode_object m.c("array-element"), e
|
||||
else
|
||||
encode_array m.c("array-element"), e
|
||||
m
|
||||
if json.constructor is Object
|
||||
encode_object m.c("y",{xmlns:"http://y.ninja/connector-stanza"}), json
|
||||
else if json.constructor is Array
|
||||
encode_array m.c("y",{xmlns:"http://y.ninja/connector-stanza"}), json
|
||||
else
|
||||
throw new Error "I can't encode this json!"
|
||||
|
||||
setIsBoundToY: ()->
|
||||
@on_bound_to_y?()
|
||||
delete @when_bound_to_y
|
||||
@is_bound_to_y = true
|
@ -1,115 +0,0 @@
|
||||
|
||||
window?.unprocessed_counter = 0 # del this
|
||||
window?.unprocessed_exec_counter = 0 # TODO
|
||||
window?.unprocessed_types = []
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# The Engine handles how and in which order to execute operations and add operations to the HistoryBuffer.
|
||||
#
|
||||
class Engine
|
||||
|
||||
#
|
||||
# @param {HistoryBuffer} HB
|
||||
# @param {Object} types list of available types
|
||||
#
|
||||
constructor: (@HB, @types)->
|
||||
@unprocessed_ops = []
|
||||
|
||||
#
|
||||
# Parses an operatio from the json format. It uses the specified parser in your OperationType module.
|
||||
#
|
||||
parseOperation: (json)->
|
||||
type = @types[json.type]
|
||||
if type?.parse?
|
||||
type.parse json
|
||||
else
|
||||
throw new Error "You forgot to specify a parser for type #{json.type}. The message is #{JSON.stringify json}."
|
||||
|
||||
|
||||
#
|
||||
# Apply a set of operations. E.g. the operations you received from another users HB._encode().
|
||||
# @note You must not use this method when you already have ops in your HB!
|
||||
###
|
||||
applyOpsBundle: (ops_json)->
|
||||
ops = []
|
||||
for o in ops_json
|
||||
ops.push @parseOperation o
|
||||
for o in ops
|
||||
if not o.execute()
|
||||
@unprocessed_ops.push o
|
||||
@tryUnprocessed()
|
||||
###
|
||||
|
||||
#
|
||||
# Same as applyOps but operations that are already in the HB are not applied.
|
||||
# @see Engine.applyOps
|
||||
#
|
||||
applyOpsCheckDouble: (ops_json)->
|
||||
for o in ops_json
|
||||
if not @HB.getOperation(o.uid)?
|
||||
@applyOp o
|
||||
|
||||
#
|
||||
# Apply a set of operations. (Helper for using applyOp on Arrays)
|
||||
# @see Engine.applyOp
|
||||
applyOps: (ops_json)->
|
||||
@applyOp ops_json
|
||||
|
||||
#
|
||||
# Apply an operation that you received from another peer.
|
||||
# TODO: make this more efficient!!
|
||||
# - operations may only executed in order by creator, order them in object of arrays (key by creator)
|
||||
# - you can probably make something like dependencies (creator1 waits for creator2)
|
||||
applyOp: (op_json_array, fromHB = false)->
|
||||
if op_json_array.constructor isnt Array
|
||||
op_json_array = [op_json_array]
|
||||
for op_json in op_json_array
|
||||
if fromHB
|
||||
op_json.fromHB = "true" # execute immediately, if
|
||||
# $parse_and_execute will return false if $o_json was parsed and executed, otherwise the parsed operadion
|
||||
o = @parseOperation op_json
|
||||
o.parsed_from_json = op_json
|
||||
if op_json.fromHB?
|
||||
o.fromHB = op_json.fromHB
|
||||
# @HB.addOperation o
|
||||
if @HB.getOperation(o)?
|
||||
# nop
|
||||
else if ((not @HB.isExpectedOperation(o)) and (not o.fromHB?)) or (not o.execute())
|
||||
@unprocessed_ops.push o
|
||||
window?.unprocessed_types.push o.type # TODO: delete this
|
||||
@tryUnprocessed()
|
||||
|
||||
#
|
||||
# Call this method when you applied a new operation.
|
||||
# It checks if operations that were previously not executable are now executable.
|
||||
#
|
||||
tryUnprocessed: ()->
|
||||
while true
|
||||
old_length = @unprocessed_ops.length
|
||||
unprocessed = []
|
||||
for op in @unprocessed_ops
|
||||
if @HB.getOperation(op)?
|
||||
# nop
|
||||
else if (not @HB.isExpectedOperation(op) and (not op.fromHB?)) or (not op.execute())
|
||||
unprocessed.push op
|
||||
@unprocessed_ops = unprocessed
|
||||
if @unprocessed_ops.length is old_length
|
||||
break
|
||||
if @unprocessed_ops.length isnt 0
|
||||
@HB.invokeSync()
|
||||
|
||||
|
||||
module.exports = Engine
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,227 +0,0 @@
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# An object that holds all applied operations.
|
||||
#
|
||||
# @note The HistoryBuffer is commonly abbreviated to HB.
|
||||
#
|
||||
class HistoryBuffer
|
||||
|
||||
#
|
||||
# Creates an empty HB.
|
||||
# @param {Object} user_id Creator of the HB.
|
||||
#
|
||||
constructor: (@user_id)->
|
||||
@operation_counter = {}
|
||||
@buffer = {}
|
||||
@change_listeners = []
|
||||
@garbage = [] # Will be cleaned on next call of garbageCollector
|
||||
@trash = [] # Is deleted. Wait until it is not used anymore.
|
||||
@performGarbageCollection = true
|
||||
@garbageCollectTimeout = 30000
|
||||
@reserved_identifier_counter = 0
|
||||
setTimeout @emptyGarbage, @garbageCollectTimeout
|
||||
|
||||
# At the beginning (when the user id was not assigned yet),
|
||||
# the operations are added to buffer._temp. When you finally get your user id,
|
||||
# the operations are copies from buffer._temp to buffer[id]. Furthermore, when buffer[id] does already contain operations
|
||||
# (because of a previous session), the uid.op_numbers of the operations have to be reassigned.
|
||||
# This is what this function does. It adds them to buffer[id],
|
||||
# and assigns them the correct uid.op_number and uid.creator
|
||||
setUserId: (@user_id, state_vector)->
|
||||
@buffer[@user_id] ?= []
|
||||
buff = @buffer[@user_id]
|
||||
|
||||
# we assumed that we started with counter = 0.
|
||||
# when we receive tha state_vector, and actually have
|
||||
# counter = 10. Then we have to add 10 to every op_counter
|
||||
counter_diff = state_vector[@user_id] or 0
|
||||
|
||||
if @buffer._temp?
|
||||
for o_name,o of @buffer._temp
|
||||
o.uid.creator = @user_id
|
||||
o.uid.op_number += counter_diff
|
||||
buff[o.uid.op_number] = o
|
||||
|
||||
@operation_counter[@user_id] = (@operation_counter._temp or 0) + counter_diff
|
||||
|
||||
delete @operation_counter._temp
|
||||
delete @buffer._temp
|
||||
|
||||
|
||||
emptyGarbage: ()=>
|
||||
for o in @garbage
|
||||
#if @getOperationCounter(o.uid.creator) > o.uid.op_number
|
||||
o.cleanup?()
|
||||
|
||||
@garbage = @trash
|
||||
@trash = []
|
||||
if @garbageCollectTimeout isnt -1
|
||||
@garbageCollectTimeoutId = setTimeout @emptyGarbage, @garbageCollectTimeout
|
||||
undefined
|
||||
|
||||
#
|
||||
# Get the user id with wich the History Buffer was initialized.
|
||||
#
|
||||
getUserId: ()->
|
||||
@user_id
|
||||
|
||||
addToGarbageCollector: ()->
|
||||
if @performGarbageCollection
|
||||
for o in arguments
|
||||
if o?
|
||||
@garbage.push o
|
||||
|
||||
stopGarbageCollection: ()->
|
||||
@performGarbageCollection = false
|
||||
@setManualGarbageCollect()
|
||||
@garbage = []
|
||||
@trash = []
|
||||
|
||||
setManualGarbageCollect: ()->
|
||||
@garbageCollectTimeout = -1
|
||||
clearTimeout @garbageCollectTimeoutId
|
||||
@garbageCollectTimeoutId = undefined
|
||||
|
||||
setGarbageCollectTimeout: (@garbageCollectTimeout)->
|
||||
|
||||
#
|
||||
# I propose to use it in your Framework, to create something like a root element.
|
||||
# An operation with this identifier is not propagated to other clients.
|
||||
# This is why everybode must create the same operation with this uid.
|
||||
#
|
||||
getReservedUniqueIdentifier: ()->
|
||||
{
|
||||
creator : '_'
|
||||
op_number : "_#{@reserved_identifier_counter++}"
|
||||
}
|
||||
|
||||
#
|
||||
# Get the operation counter that describes the current state of the document.
|
||||
#
|
||||
getOperationCounter: (user_id)->
|
||||
if not user_id?
|
||||
res = {}
|
||||
for user,ctn of @operation_counter
|
||||
res[user] = ctn
|
||||
res
|
||||
else
|
||||
@operation_counter[user_id]
|
||||
|
||||
isExpectedOperation: (o)->
|
||||
@operation_counter[o.uid.creator] ?= 0
|
||||
o.uid.op_number <= @operation_counter[o.uid.creator]
|
||||
true #TODO: !! this could break stuff. But I dunno why
|
||||
|
||||
#
|
||||
# Encode this operation in such a way that it can be parsed by remote peers.
|
||||
# TODO: Make this more efficient!
|
||||
_encode: (state_vector={})->
|
||||
json = []
|
||||
unknown = (user, o_number)->
|
||||
if (not user?) or (not o_number?)
|
||||
throw new Error "dah!"
|
||||
not state_vector[user]? or state_vector[user] <= o_number
|
||||
|
||||
for u_name,user of @buffer
|
||||
# TODO next, if @state_vector[user] <= state_vector[user]
|
||||
if u_name is "_"
|
||||
continue
|
||||
for o_number,o of user
|
||||
if (not o.uid.noOperation?) and unknown(u_name, o_number)
|
||||
# its necessary to send it, and not known in state_vector
|
||||
o_json = o._encode()
|
||||
if o.next_cl? # applies for all ops but the most right delimiter!
|
||||
# search for the next _known_ operation. (When state_vector is {} then this is the Delimiter)
|
||||
o_next = o.next_cl
|
||||
while o_next.next_cl? and unknown(o_next.uid.creator, o_next.uid.op_number)
|
||||
o_next = o_next.next_cl
|
||||
o_json.next = o_next.getUid()
|
||||
else if o.prev_cl? # most right delimiter only!
|
||||
# same as the above with prev.
|
||||
o_prev = o.prev_cl
|
||||
while o_prev.prev_cl? and unknown(o_prev.uid.creator, o_prev.uid.op_number)
|
||||
o_prev = o_prev.prev_cl
|
||||
o_json.prev = o_prev.getUid()
|
||||
json.push o_json
|
||||
|
||||
json
|
||||
|
||||
#
|
||||
# Get the number of operations that were created by a user.
|
||||
# Accordingly you will get the next operation number that is expected from that user.
|
||||
# This will increment the operation counter.
|
||||
#
|
||||
getNextOperationIdentifier: (user_id)->
|
||||
if not user_id?
|
||||
user_id = @user_id
|
||||
if not @operation_counter[user_id]?
|
||||
@operation_counter[user_id] = 0
|
||||
uid =
|
||||
'creator' : user_id
|
||||
'op_number' : @operation_counter[user_id]
|
||||
@operation_counter[user_id]++
|
||||
uid
|
||||
|
||||
#
|
||||
# Retrieve an operation from a unique id.
|
||||
#
|
||||
# when uid has a "sub" property, the value of it will be applied
|
||||
# on the operations retrieveSub method (which must! be defined)
|
||||
#
|
||||
getOperation: (uid)->
|
||||
if uid.uid?
|
||||
uid = uid.uid
|
||||
o = @buffer[uid.creator]?[uid.op_number]
|
||||
if uid.sub? and o?
|
||||
o.retrieveSub uid.sub
|
||||
else
|
||||
o
|
||||
|
||||
#
|
||||
# Add an operation to the HB. Note that this will not link it against
|
||||
# other operations (it wont executed)
|
||||
#
|
||||
addOperation: (o)->
|
||||
if not @buffer[o.uid.creator]?
|
||||
@buffer[o.uid.creator] = {}
|
||||
if @buffer[o.uid.creator][o.uid.op_number]?
|
||||
throw new Error "You must not overwrite operations!"
|
||||
if (o.uid.op_number.constructor isnt String) and (not @isExpectedOperation(o)) and (not o.fromHB?) # you already do this in the engine, so delete it here!
|
||||
throw new Error "this operation was not expected!"
|
||||
@addToCounter(o)
|
||||
@buffer[o.uid.creator][o.uid.op_number] = o
|
||||
o
|
||||
|
||||
removeOperation: (o)->
|
||||
delete @buffer[o.uid.creator]?[o.uid.op_number]
|
||||
|
||||
# When the HB determines inconsistencies, then the invokeSync
|
||||
# handler wil be called, which should somehow invoke the sync with another collaborator.
|
||||
# The parameter of the sync handler is the user_id with wich an inconsistency was determined
|
||||
setInvokeSyncHandler: (f)->
|
||||
@invokeSync = f
|
||||
|
||||
# empty per default # TODO: do i need this?
|
||||
invokeSync: ()->
|
||||
|
||||
# after you received the HB of another user (in the sync process),
|
||||
# you renew your own state_vector to the state_vector of the other user
|
||||
renewStateVector: (state_vector)->
|
||||
for user,state of state_vector
|
||||
if ((not @operation_counter[user]?) or (@operation_counter[user] < state_vector[user])) and state_vector[user]?
|
||||
@operation_counter[user] = state_vector[user]
|
||||
|
||||
#
|
||||
# Increment the operation_counter that defines the current state of the Engine.
|
||||
#
|
||||
addToCounter: (o)->
|
||||
@operation_counter[o.uid.creator] ?= 0
|
||||
# TODO: check if operations are send in order
|
||||
if o.uid.op_number is @operation_counter[o.uid.creator]
|
||||
@operation_counter[o.uid.creator]++
|
||||
while @buffer[o.uid.creator][@operation_counter[o.uid.creator]]?
|
||||
@operation_counter[o.uid.creator]++
|
||||
undefined
|
||||
|
||||
module.exports = HistoryBuffer
|
@ -1,74 +0,0 @@
|
||||
|
||||
class YObject
|
||||
|
||||
constructor: (@_object = {})->
|
||||
if @_object.constructor is Object
|
||||
for name, val of @_object
|
||||
if val.constructor is Object
|
||||
@_object[name] = new YObject(val)
|
||||
else
|
||||
throw new Error "Y.Object accepts Json Objects only"
|
||||
|
||||
_name: "Object"
|
||||
|
||||
_getModel: (types, ops)->
|
||||
if not @_model?
|
||||
@_model = new ops.MapManager(@).execute()
|
||||
for n,o of @_object
|
||||
@_model.val n, o
|
||||
delete @_object
|
||||
@_model
|
||||
|
||||
_setModel: (@_model)->
|
||||
delete @_object
|
||||
|
||||
observe: (f)->
|
||||
@_model.observe f
|
||||
@
|
||||
|
||||
unobserve: (f)->
|
||||
@_model.unobserve f
|
||||
@
|
||||
|
||||
#
|
||||
# @overload val()
|
||||
# Get this as a Json object.
|
||||
# @return [Json]
|
||||
#
|
||||
# @overload val(name)
|
||||
# Get value of a property.
|
||||
# @param {String} name Name of the object property.
|
||||
# @return [*] Depends on the value of the property.
|
||||
#
|
||||
# @overload val(name, content)
|
||||
# Set a new property.
|
||||
# @param {String} name Name of the object property.
|
||||
# @param {Object|String} content Content of the object property.
|
||||
# @return [Object Type] This object. (supports chaining)
|
||||
#
|
||||
val: (name, content)->
|
||||
if @_model?
|
||||
@_model.val.apply @_model, arguments
|
||||
else
|
||||
if content?
|
||||
@_object[name] = content
|
||||
else if name?
|
||||
@_object[name]
|
||||
else
|
||||
res = {}
|
||||
for n,v of @_object
|
||||
res[n] = v
|
||||
res
|
||||
|
||||
delete: (name)->
|
||||
@_model.delete(name)
|
||||
@
|
||||
|
||||
if window?
|
||||
if window.Y?
|
||||
window.Y.Object = YObject
|
||||
else
|
||||
throw new Error "You must first import Y!"
|
||||
|
||||
if module?
|
||||
module.exports = YObject
|
@ -1,678 +0,0 @@
|
||||
module.exports = ()->
|
||||
# @see Engine.parse
|
||||
ops = {}
|
||||
execution_listener = []
|
||||
|
||||
#
|
||||
# @private
|
||||
# @abstract
|
||||
# @nodoc
|
||||
# A generic interface to ops.
|
||||
#
|
||||
# An operation has the following methods:
|
||||
# * _encode: encodes an operation (needed only if instance of this operation is sent).
|
||||
# * execute: execute the effects of this operations. Good examples are Insert-type and AddName-type
|
||||
# * val: in the case that the operation holds a value
|
||||
#
|
||||
# Furthermore an encodable operation has a parser. We extend the parser object in order to parse encoded operations.
|
||||
#
|
||||
class ops.Operation
|
||||
|
||||
#
|
||||
# @param {Object} uid A unique identifier.
|
||||
# If uid is undefined, a new uid will be created before at the end of the execution sequence
|
||||
#
|
||||
constructor: (custom_type, uid, content, content_operations)->
|
||||
if custom_type?
|
||||
@custom_type = custom_type
|
||||
@is_deleted = false
|
||||
@garbage_collected = false
|
||||
@event_listeners = [] # TODO: rename to observers or sth like that
|
||||
if uid?
|
||||
@uid = uid
|
||||
|
||||
# see encode to see, why we are doing it this way
|
||||
if content is undefined
|
||||
# nop
|
||||
else if content? and content.creator?
|
||||
@saveOperation 'content', content
|
||||
else
|
||||
@content = content
|
||||
if content_operations?
|
||||
@content_operations = {}
|
||||
for name, op of content_operations
|
||||
@saveOperation name, op, 'content_operations'
|
||||
|
||||
type: "Operation"
|
||||
|
||||
getContent: (name)->
|
||||
if @content?
|
||||
if @content.getCustomType?
|
||||
@content.getCustomType()
|
||||
else if @content.constructor is Object
|
||||
if name?
|
||||
if @content[name]?
|
||||
@content[name]
|
||||
else
|
||||
@content_operations[name].getCustomType()
|
||||
else
|
||||
content = {}
|
||||
for n,v of @content
|
||||
content[n] = v
|
||||
if @content_operations?
|
||||
for n,v of @content_operations
|
||||
v = v.getCustomType()
|
||||
content[n] = v
|
||||
content
|
||||
else
|
||||
@content
|
||||
else
|
||||
@content
|
||||
|
||||
retrieveSub: ()->
|
||||
throw new Error "sub properties are not enable on this operation type!"
|
||||
|
||||
#
|
||||
# Add an event listener. It depends on the operation which events are supported.
|
||||
# @param {Function} f f is executed in case the event fires.
|
||||
#
|
||||
observe: (f)->
|
||||
@event_listeners.push f
|
||||
|
||||
#
|
||||
# Deletes function from the observer list
|
||||
# @see Operation.observe
|
||||
#
|
||||
# @overload unobserve(event, f)
|
||||
# @param f {Function} The function that you want to delete
|
||||
unobserve: (f)->
|
||||
@event_listeners = @event_listeners.filter (g)->
|
||||
f isnt g
|
||||
|
||||
#
|
||||
# Deletes all subscribed event listeners.
|
||||
# This should be called, e.g. after this has been replaced.
|
||||
# (Then only one replace event should fire. )
|
||||
# This is also called in the cleanup method.
|
||||
deleteAllObservers: ()->
|
||||
@event_listeners = []
|
||||
|
||||
delete: ()->
|
||||
(new ops.Delete undefined, @).execute()
|
||||
null
|
||||
|
||||
#
|
||||
# Fire an event.
|
||||
# TODO: Do something with timeouts. You don't want this to fire for every operation (e.g. insert).
|
||||
# TODO: do you need callEvent+forwardEvent? Only one suffices probably
|
||||
callEvent: ()->
|
||||
if @custom_type?
|
||||
callon = @getCustomType()
|
||||
else
|
||||
callon = @
|
||||
@forwardEvent callon, arguments...
|
||||
|
||||
#
|
||||
# Fire an event and specify in which context the listener is called (set 'this').
|
||||
# TODO: do you need this ?
|
||||
forwardEvent: (op, args...)->
|
||||
for f in @event_listeners
|
||||
f.call op, args...
|
||||
|
||||
isDeleted: ()->
|
||||
@is_deleted
|
||||
|
||||
applyDelete: (garbagecollect = true)->
|
||||
if not @garbage_collected
|
||||
#console.log "applyDelete: #{@type}"
|
||||
@is_deleted = true
|
||||
if garbagecollect
|
||||
@garbage_collected = true
|
||||
@HB.addToGarbageCollector @
|
||||
|
||||
cleanup: ()->
|
||||
#console.log "cleanup: #{@type}"
|
||||
@HB.removeOperation @
|
||||
@deleteAllObservers()
|
||||
|
||||
#
|
||||
# Set the parent of this operation.
|
||||
#
|
||||
setParent: (@parent)->
|
||||
|
||||
#
|
||||
# Get the parent of this operation.
|
||||
#
|
||||
getParent: ()->
|
||||
@parent
|
||||
|
||||
#
|
||||
# Computes a unique identifier (uid) that identifies this operation.
|
||||
#
|
||||
getUid: ()->
|
||||
if not @uid.noOperation?
|
||||
@uid
|
||||
else
|
||||
if @uid.alt? # could be (safely) undefined
|
||||
map_uid = @uid.alt.cloneUid()
|
||||
map_uid.sub = @uid.sub
|
||||
map_uid
|
||||
else
|
||||
undefined
|
||||
|
||||
cloneUid: ()->
|
||||
uid = {}
|
||||
for n,v of @getUid()
|
||||
uid[n] = v
|
||||
uid
|
||||
|
||||
#
|
||||
# @private
|
||||
# If not already done, set the uid
|
||||
# Add this to the HB
|
||||
# Notify the all the listeners.
|
||||
#
|
||||
execute: ()->
|
||||
if @validateSavedOperations()
|
||||
@is_executed = true
|
||||
if not @uid?
|
||||
# When this operation was created without a uid, then set it here.
|
||||
# There is only one other place, where this can be done - before an Insertion
|
||||
# is executed (because we need the creator_id)
|
||||
@uid = @HB.getNextOperationIdentifier()
|
||||
if not @uid.noOperation?
|
||||
@HB.addOperation @
|
||||
for l in execution_listener
|
||||
l @_encode()
|
||||
@
|
||||
else
|
||||
false
|
||||
|
||||
#
|
||||
# @private
|
||||
# Operations may depend on other operations (linked lists, etc.).
|
||||
# The saveOperation and validateSavedOperations methods provide
|
||||
# an easy way to refer to these operations via an uid or object reference.
|
||||
#
|
||||
# For example: We can create a new Delete operation that deletes the operation $o like this
|
||||
# - var d = new Delete(uid, $o); or
|
||||
# - var d = new Delete(uid, $o.getUid());
|
||||
# Either way we want to access $o via d.deletes. In the second case validateSavedOperations must be called first.
|
||||
#
|
||||
# @overload saveOperation(name, op_uid)
|
||||
# @param {String} name The name of the operation. After validating (with validateSavedOperations) the instantiated operation will be accessible via this[name].
|
||||
# @param {Object} op_uid A uid that refers to an operation
|
||||
# @overload saveOperation(name, op)
|
||||
# @param {String} name The name of the operation. After calling this function op is accessible via this[name].
|
||||
# @param {Operation} op An Operation object
|
||||
#
|
||||
saveOperation: (name, op, base = "this")->
|
||||
if op? and op._getModel?
|
||||
op = op._getModel(@custom_types, @operations)
|
||||
#
|
||||
# Every instance of $Operation must have an $execute function.
|
||||
# We use duck-typing to check if op is instantiated since there
|
||||
# could exist multiple classes of $Operation
|
||||
#
|
||||
if not op?
|
||||
# nop
|
||||
else if op.execute? or not (op.op_number? and op.creator?)
|
||||
# is instantiated, or op is string. Currently "Delimiter" is saved as string
|
||||
# (in combination with @parent you can retrieve the delimiter..)
|
||||
if base is "this"
|
||||
@[name] = op
|
||||
else
|
||||
dest = @[base]
|
||||
paths = name.split("/")
|
||||
last_path = paths.pop()
|
||||
for path in paths
|
||||
dest = dest[path]
|
||||
dest[last_path] = op
|
||||
else
|
||||
# not initialized. Do it when calling $validateSavedOperations()
|
||||
@unchecked ?= {}
|
||||
@unchecked[base] ?= {}
|
||||
@unchecked[base][name] = op
|
||||
|
||||
#
|
||||
# @private
|
||||
# After calling this function all not instantiated operations will be accessible.
|
||||
# @see Operation.saveOperation
|
||||
#
|
||||
# @return [Boolean] Whether it was possible to instantiate all operations.
|
||||
#
|
||||
validateSavedOperations: ()->
|
||||
uninstantiated = {}
|
||||
success = true
|
||||
for base_name, base of @unchecked
|
||||
for name, op_uid of base
|
||||
op = @HB.getOperation op_uid
|
||||
if op
|
||||
if base_name is "this"
|
||||
@[name] = op
|
||||
else
|
||||
dest = @[base_name]
|
||||
paths = name.split("/")
|
||||
last_path = paths.pop()
|
||||
for path in paths
|
||||
dest = dest[path]
|
||||
dest[last_path] = op
|
||||
else
|
||||
uninstantiated[base_name] ?= {}
|
||||
uninstantiated[base_name][name] = op_uid
|
||||
success = false
|
||||
if not success
|
||||
@unchecked = uninstantiated
|
||||
return false
|
||||
else
|
||||
delete @unchecked
|
||||
return @
|
||||
|
||||
getCustomType: ()->
|
||||
if not @custom_type?
|
||||
# throw new Error "This operation was not initialized with a custom type"
|
||||
@
|
||||
else
|
||||
if @custom_type.constructor is String
|
||||
# has not been initialized yet (only the name is specified)
|
||||
Type = @custom_types
|
||||
for t in @custom_type.split(".")
|
||||
Type = Type[t]
|
||||
@custom_type = new Type()
|
||||
@custom_type._setModel @
|
||||
@custom_type
|
||||
|
||||
#
|
||||
# @private
|
||||
# Encode this operation in such a way that it can be parsed by remote peers.
|
||||
#
|
||||
_encode: (json = {})->
|
||||
json.type = @type
|
||||
json.uid = @getUid()
|
||||
if @custom_type?
|
||||
if @custom_type.constructor is String
|
||||
json.custom_type = @custom_type
|
||||
else
|
||||
json.custom_type = @custom_type._name
|
||||
|
||||
if @content?.getUid?
|
||||
json.content = @content.getUid()
|
||||
else
|
||||
json.content = @content
|
||||
if @content_operations?
|
||||
operations = {}
|
||||
for n,o of @content_operations
|
||||
if o._getModel?
|
||||
o = o._getModel(@custom_types, @operations)
|
||||
operations[n] = o.getUid()
|
||||
json.content_operations = operations
|
||||
json
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# A simple Delete-type operation that deletes an operation.
|
||||
#
|
||||
class ops.Delete extends ops.Operation
|
||||
|
||||
#
|
||||
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
|
||||
# @param {Object} deletes UID or reference of the operation that this to be deleted.
|
||||
#
|
||||
constructor: (custom_type, uid, deletes)->
|
||||
@saveOperation 'deletes', deletes
|
||||
super custom_type, uid
|
||||
|
||||
type: "Delete"
|
||||
|
||||
#
|
||||
# @private
|
||||
# Convert all relevant information of this operation to the json-format.
|
||||
# This result can be sent to other clients.
|
||||
#
|
||||
_encode: ()->
|
||||
{
|
||||
'type': "Delete"
|
||||
'uid': @getUid()
|
||||
'deletes': @deletes.getUid()
|
||||
}
|
||||
|
||||
#
|
||||
# @private
|
||||
# Apply the deletion.
|
||||
#
|
||||
execute: ()->
|
||||
if @validateSavedOperations()
|
||||
res = super
|
||||
if res
|
||||
@deletes.applyDelete @
|
||||
res
|
||||
else
|
||||
false
|
||||
|
||||
#
|
||||
# Define how to parse Delete operations.
|
||||
#
|
||||
ops.Delete.parse = (o)->
|
||||
{
|
||||
'uid' : uid
|
||||
'deletes': deletes_uid
|
||||
} = o
|
||||
new this(null, uid, deletes_uid)
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# A simple insert-type operation.
|
||||
#
|
||||
# An insert operation is always positioned between two other insert operations.
|
||||
# Internally this is realized as associative lists, whereby each insert operation has a predecessor and a successor.
|
||||
# For the sake of efficiency we maintain two lists:
|
||||
# - The short-list (abbrev. sl) maintains only the operations that are not deleted (unimplemented, good idea?)
|
||||
# - The complete-list (abbrev. cl) maintains all operations
|
||||
#
|
||||
class ops.Insert extends ops.Operation
|
||||
|
||||
#
|
||||
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
|
||||
# @param {Operation} prev_cl The predecessor of this operation in the complete-list (cl)
|
||||
# @param {Operation} next_cl The successor of this operation in the complete-list (cl)
|
||||
#
|
||||
constructor: (custom_type, content, content_operations, parent, uid, prev_cl, next_cl, origin)->
|
||||
@saveOperation 'parent', parent
|
||||
@saveOperation 'prev_cl', prev_cl
|
||||
@saveOperation 'next_cl', next_cl
|
||||
if origin?
|
||||
@saveOperation 'origin', origin
|
||||
else
|
||||
@saveOperation 'origin', prev_cl
|
||||
super custom_type, uid, content, content_operations
|
||||
|
||||
type: "Insert"
|
||||
|
||||
val: ()->
|
||||
@getContent()
|
||||
|
||||
getNext: (i=1)->
|
||||
n = @
|
||||
while i > 0 and n.next_cl?
|
||||
n = n.next_cl
|
||||
if not n.is_deleted
|
||||
i--
|
||||
if n.is_deleted
|
||||
null
|
||||
n
|
||||
|
||||
getPrev: (i=1)->
|
||||
n = @
|
||||
while i > 0 and n.prev_cl?
|
||||
n = n.prev_cl
|
||||
if not n.is_deleted
|
||||
i--
|
||||
if n.is_deleted
|
||||
null
|
||||
else
|
||||
n
|
||||
|
||||
#
|
||||
# set content to null and other stuff
|
||||
# @private
|
||||
#
|
||||
applyDelete: (o)->
|
||||
@deleted_by ?= []
|
||||
callLater = false
|
||||
if @parent? and not @is_deleted and o? # o? : if not o?, then the delimiter deleted this Insertion. Furthermore, it would be wrong to call it. TODO: make this more expressive and save
|
||||
# call iff wasn't deleted earlyer
|
||||
callLater = true
|
||||
if o?
|
||||
@deleted_by.push o
|
||||
garbagecollect = false
|
||||
if @next_cl.isDeleted()
|
||||
garbagecollect = true
|
||||
super garbagecollect
|
||||
if callLater
|
||||
@parent.callOperationSpecificDeleteEvents(this, o)
|
||||
if @prev_cl? and @prev_cl.isDeleted()
|
||||
# garbage collect prev_cl
|
||||
@prev_cl.applyDelete()
|
||||
|
||||
cleanup: ()->
|
||||
if @next_cl.isDeleted()
|
||||
# delete all ops that delete this insertion
|
||||
for d in @deleted_by
|
||||
d.cleanup()
|
||||
|
||||
# throw new Error "right is not deleted. inconsistency!, wrararar"
|
||||
# change origin references to the right
|
||||
o = @next_cl
|
||||
while o.type isnt "Delimiter"
|
||||
if o.origin is @
|
||||
o.origin = @prev_cl
|
||||
o = o.next_cl
|
||||
# reconnect left/right
|
||||
@prev_cl.next_cl = @next_cl
|
||||
@next_cl.prev_cl = @prev_cl
|
||||
|
||||
# delete content
|
||||
# - we must not do this in applyDelete, because this would lead to inconsistencies
|
||||
# (e.g. the following operation order must be invertible :
|
||||
# Insert refers to content, then the content is deleted)
|
||||
# Therefore, we have to do this in the cleanup
|
||||
# * NODE: We never delete Insertions!
|
||||
if @content instanceof ops.Operation and not (@content instanceof ops.Insert)
|
||||
@content.referenced_by--
|
||||
if @content.referenced_by <= 0 and not @content.is_deleted
|
||||
@content.applyDelete()
|
||||
delete @content
|
||||
super
|
||||
# else
|
||||
# Someone inserted something in the meantime.
|
||||
# Remember: this can only be garbage collected when next_cl is deleted
|
||||
|
||||
#
|
||||
# @private
|
||||
# The amount of positions that $this operation was moved to the right.
|
||||
#
|
||||
getDistanceToOrigin: ()->
|
||||
d = 0
|
||||
o = @prev_cl
|
||||
while true
|
||||
if @origin is o
|
||||
break
|
||||
d++
|
||||
o = o.prev_cl
|
||||
d
|
||||
|
||||
#
|
||||
# @private
|
||||
# Include this operation in the associative lists.
|
||||
execute: ()->
|
||||
if not @validateSavedOperations()
|
||||
return false
|
||||
else
|
||||
if @content instanceof ops.Operation
|
||||
@content.insert_parent = @ # TODO: this is probably not necessary and only nice for debugging
|
||||
@content.referenced_by ?= 0
|
||||
@content.referenced_by++
|
||||
if @parent?
|
||||
if not @prev_cl?
|
||||
@prev_cl = @parent.beginning
|
||||
if not @origin?
|
||||
@origin = @prev_cl
|
||||
else if @origin is "Delimiter"
|
||||
@origin = @parent.beginning
|
||||
if not @next_cl?
|
||||
@next_cl = @parent.end
|
||||
if @prev_cl?
|
||||
distance_to_origin = @getDistanceToOrigin() # most cases: 0
|
||||
o = @prev_cl.next_cl
|
||||
i = distance_to_origin # loop counter
|
||||
|
||||
# $this has to find a unique position between origin and the next known character
|
||||
# case 1: $origin equals $o.origin: the $creator parameter decides if left or right
|
||||
# let $OL= [o1,o2,o3,o4], whereby $this is to be inserted between o1 and o4
|
||||
# o2,o3 and o4 origin is 1 (the position of o2)
|
||||
# there is the case that $this.creator < o2.creator, but o3.creator < $this.creator
|
||||
# then o2 knows o3. Since on another client $OL could be [o1,o3,o4] the problem is complex
|
||||
# therefore $this would be always to the right of o3
|
||||
# case 2: $origin < $o.origin
|
||||
# if current $this insert_position > $o origin: $this ins
|
||||
# else $insert_position will not change
|
||||
# (maybe we encounter case 1 later, then this will be to the right of $o)
|
||||
# case 3: $origin > $o.origin
|
||||
# $this insert_position is to the left of $o (forever!)
|
||||
while true
|
||||
if o isnt @next_cl
|
||||
# $o happened concurrently
|
||||
if o.getDistanceToOrigin() is i
|
||||
# case 1
|
||||
if o.uid.creator < @uid.creator
|
||||
@prev_cl = o
|
||||
distance_to_origin = i + 1
|
||||
else
|
||||
# nop
|
||||
else if o.getDistanceToOrigin() < i
|
||||
# case 2
|
||||
if i - distance_to_origin <= o.getDistanceToOrigin()
|
||||
@prev_cl = o
|
||||
distance_to_origin = i + 1
|
||||
else
|
||||
#nop
|
||||
else
|
||||
# case 3
|
||||
break
|
||||
i++
|
||||
o = o.next_cl
|
||||
else
|
||||
# $this knows that $o exists,
|
||||
break
|
||||
# now reconnect everything
|
||||
@next_cl = @prev_cl.next_cl
|
||||
@prev_cl.next_cl = @
|
||||
@next_cl.prev_cl = @
|
||||
|
||||
@setParent @prev_cl.getParent() # do Insertions always have a parent?
|
||||
super # notify the execution_listeners
|
||||
@parent.callOperationSpecificInsertEvents(this)
|
||||
@
|
||||
|
||||
#
|
||||
# Compute the position of this operation.
|
||||
#
|
||||
getPosition: ()->
|
||||
position = 0
|
||||
prev = @prev_cl
|
||||
while true
|
||||
if prev instanceof ops.Delimiter
|
||||
break
|
||||
if not prev.isDeleted()
|
||||
position++
|
||||
prev = prev.prev_cl
|
||||
position
|
||||
|
||||
#
|
||||
# Convert all relevant information of this operation to the json-format.
|
||||
# This result can be send to other clients.
|
||||
#
|
||||
_encode: (json = {})->
|
||||
json.prev = @prev_cl.getUid()
|
||||
json.next = @next_cl.getUid()
|
||||
|
||||
if @origin.type is "Delimiter"
|
||||
json.origin = "Delimiter"
|
||||
else if @origin isnt @prev_cl
|
||||
json.origin = @origin.getUid()
|
||||
|
||||
# if not (json.prev? and json.next?)
|
||||
json.parent = @parent.getUid()
|
||||
|
||||
super json
|
||||
|
||||
ops.Insert.parse = (json)->
|
||||
{
|
||||
'content' : content
|
||||
'content_operations' : content_operations
|
||||
'uid' : uid
|
||||
'prev': prev
|
||||
'next': next
|
||||
'origin' : origin
|
||||
'parent' : parent
|
||||
} = json
|
||||
new this null, content, content_operations, parent, uid, prev, next, origin
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# A delimiter is placed at the end and at the beginning of the associative lists.
|
||||
# This is necessary in order to have a beginning and an end even if the content
|
||||
# of the Engine is empty.
|
||||
#
|
||||
class ops.Delimiter extends ops.Operation
|
||||
#
|
||||
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
|
||||
# @param {Operation} prev_cl The predecessor of this operation in the complete-list (cl)
|
||||
# @param {Operation} next_cl The successor of this operation in the complete-list (cl)
|
||||
#
|
||||
constructor: (prev_cl, next_cl, origin)->
|
||||
@saveOperation 'prev_cl', prev_cl
|
||||
@saveOperation 'next_cl', next_cl
|
||||
@saveOperation 'origin', prev_cl
|
||||
super null, {noOperation: true}
|
||||
|
||||
type: "Delimiter"
|
||||
|
||||
applyDelete: ()->
|
||||
super()
|
||||
o = @prev_cl
|
||||
while o?
|
||||
o.applyDelete()
|
||||
o = o.prev_cl
|
||||
undefined
|
||||
|
||||
cleanup: ()->
|
||||
super()
|
||||
|
||||
#
|
||||
# @private
|
||||
#
|
||||
execute: ()->
|
||||
if @unchecked?['next_cl']?
|
||||
super
|
||||
else if @unchecked?['prev_cl']
|
||||
if @validateSavedOperations()
|
||||
if @prev_cl.next_cl?
|
||||
throw new Error "Probably duplicated operations"
|
||||
@prev_cl.next_cl = @
|
||||
super
|
||||
else
|
||||
false
|
||||
else if @prev_cl? and not @prev_cl.next_cl?
|
||||
delete @prev_cl.unchecked.next_cl
|
||||
@prev_cl.next_cl = @
|
||||
super
|
||||
else if @prev_cl? or @next_cl? or true # TODO: are you sure? This can happen right?
|
||||
super
|
||||
#else
|
||||
# throw new Error "Delimiter is unsufficient defined!"
|
||||
|
||||
#
|
||||
# @private
|
||||
#
|
||||
_encode: ()->
|
||||
{
|
||||
'type' : @type
|
||||
'uid' : @getUid()
|
||||
'prev' : @prev_cl?.getUid()
|
||||
'next' : @next_cl?.getUid()
|
||||
}
|
||||
|
||||
ops.Delimiter.parse = (json)->
|
||||
{
|
||||
'uid' : uid
|
||||
'prev' : prev
|
||||
'next' : next
|
||||
} = json
|
||||
new this(uid, prev, next)
|
||||
|
||||
# This is what this module exports after initializing it with the HistoryBuffer
|
||||
{
|
||||
'operations' : ops
|
||||
'execution_listener' : execution_listener
|
||||
}
|
@ -1,533 +0,0 @@
|
||||
basic_ops_uninitialized = require "./Basic"
|
||||
|
||||
module.exports = ()->
|
||||
basic_ops = basic_ops_uninitialized()
|
||||
ops = basic_ops.operations
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# Manages map like objects. E.g. Json-Type and XML attributes.
|
||||
#
|
||||
class ops.MapManager extends ops.Operation
|
||||
|
||||
#
|
||||
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
|
||||
#
|
||||
constructor: (custom_type, uid, content, content_operations)->
|
||||
@_map = {}
|
||||
super custom_type, uid, content, content_operations
|
||||
|
||||
type: "MapManager"
|
||||
|
||||
applyDelete: ()->
|
||||
for name,p of @_map
|
||||
p.applyDelete()
|
||||
super()
|
||||
|
||||
cleanup: ()->
|
||||
super()
|
||||
|
||||
map: (f)->
|
||||
for n,v of @_map
|
||||
f(n,v)
|
||||
undefined
|
||||
|
||||
#
|
||||
# @see JsonOperations.val
|
||||
#
|
||||
val: (name, content)->
|
||||
if arguments.length > 1
|
||||
if content? and content._getModel?
|
||||
rep = content._getModel(@custom_types, @operations)
|
||||
else
|
||||
rep = content
|
||||
@retrieveSub(name).replace rep
|
||||
@getCustomType()
|
||||
else if name?
|
||||
prop = @_map[name]
|
||||
if prop? and not prop.isContentDeleted()
|
||||
res = prop.val()
|
||||
if res instanceof ops.Operation
|
||||
res.getCustomType()
|
||||
else
|
||||
res
|
||||
else
|
||||
undefined
|
||||
else
|
||||
result = {}
|
||||
for name,o of @_map
|
||||
if not o.isContentDeleted()
|
||||
result[name] = o.val()
|
||||
result
|
||||
|
||||
delete: (name)->
|
||||
@_map[name]?.deleteContent()
|
||||
@
|
||||
|
||||
retrieveSub: (property_name)->
|
||||
if not @_map[property_name]?
|
||||
event_properties =
|
||||
name: property_name
|
||||
event_this = @
|
||||
rm_uid =
|
||||
noOperation: true
|
||||
sub: property_name
|
||||
alt: @
|
||||
rm = new ops.ReplaceManager null, event_properties, event_this, rm_uid # this operation shall not be saved in the HB
|
||||
@_map[property_name] = rm
|
||||
rm.setParent @, property_name
|
||||
rm.execute()
|
||||
@_map[property_name]
|
||||
|
||||
ops.MapManager.parse = (json)->
|
||||
{
|
||||
'uid' : uid
|
||||
'custom_type' : custom_type
|
||||
'content' : content
|
||||
'content_operations' : content_operations
|
||||
} = json
|
||||
new this(custom_type, uid, content, content_operations)
|
||||
|
||||
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# Manages a list of Insert-type operations.
|
||||
#
|
||||
class ops.ListManager extends ops.Operation
|
||||
|
||||
#
|
||||
# A ListManager maintains a non-empty list that has a beginning and an end (both Delimiters!)
|
||||
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
|
||||
# @param {Delimiter} beginning Reference or Object.
|
||||
# @param {Delimiter} end Reference or Object.
|
||||
constructor: (custom_type, uid, content, content_operations)->
|
||||
@beginning = new ops.Delimiter undefined, undefined
|
||||
@end = new ops.Delimiter @beginning, undefined
|
||||
@beginning.next_cl = @end
|
||||
@beginning.execute()
|
||||
@end.execute()
|
||||
super custom_type, uid, content, content_operations
|
||||
|
||||
type: "ListManager"
|
||||
|
||||
|
||||
applyDelete: ()->
|
||||
o = @beginning
|
||||
while o?
|
||||
o.applyDelete()
|
||||
o = o.next_cl
|
||||
super()
|
||||
|
||||
cleanup: ()->
|
||||
super()
|
||||
|
||||
|
||||
toJson: (transform_to_value = false)->
|
||||
val = @val()
|
||||
for i, o in val
|
||||
if o instanceof ops.Object
|
||||
o.toJson(transform_to_value)
|
||||
else if o instanceof ops.ListManager
|
||||
o.toJson(transform_to_value)
|
||||
else if transform_to_value and o instanceof ops.Operation
|
||||
o.val()
|
||||
else
|
||||
o
|
||||
|
||||
#
|
||||
# @private
|
||||
# @see Operation.execute
|
||||
#
|
||||
execute: ()->
|
||||
if @validateSavedOperations()
|
||||
@beginning.setParent @
|
||||
@end.setParent @
|
||||
super
|
||||
else
|
||||
false
|
||||
|
||||
# Get the element previous to the delemiter at the end
|
||||
getLastOperation: ()->
|
||||
@end.prev_cl
|
||||
|
||||
# similar to the above
|
||||
getFirstOperation: ()->
|
||||
@beginning.next_cl
|
||||
|
||||
# Transforms the the list to an array
|
||||
# Doesn't return left-right delimiter.
|
||||
toArray: ()->
|
||||
o = @beginning.next_cl
|
||||
result = []
|
||||
while o isnt @end
|
||||
if not o.is_deleted
|
||||
result.push o.val()
|
||||
o = o.next_cl
|
||||
result
|
||||
|
||||
map: (f)->
|
||||
o = @beginning.next_cl
|
||||
result = []
|
||||
while o isnt @end
|
||||
if not o.is_deleted
|
||||
result.push f(o)
|
||||
o = o.next_cl
|
||||
result
|
||||
|
||||
fold: (init, f)->
|
||||
o = @beginning.next_cl
|
||||
while o isnt @end
|
||||
if not o.is_deleted
|
||||
init = f(init, o)
|
||||
o = o.next_cl
|
||||
init
|
||||
|
||||
val: (pos)->
|
||||
if pos?
|
||||
o = @getOperationByPosition(pos+1)
|
||||
if not (o instanceof ops.Delimiter)
|
||||
o.val()
|
||||
else
|
||||
throw new Error "this position does not exist"
|
||||
else
|
||||
@toArray()
|
||||
|
||||
ref: (pos)->
|
||||
if pos?
|
||||
o = @getOperationByPosition(pos+1)
|
||||
if not (o instanceof ops.Delimiter)
|
||||
o
|
||||
else
|
||||
null
|
||||
# throw new Error "this position does not exist"
|
||||
else
|
||||
throw new Error "you must specify a position parameter"
|
||||
|
||||
#
|
||||
# Retrieves the x-th not deleted element.
|
||||
# e.g. "abc" : the 1th character is "a"
|
||||
# the 0th character is the left Delimiter
|
||||
#
|
||||
getOperationByPosition: (position)->
|
||||
o = @beginning
|
||||
while true
|
||||
# find the i-th op
|
||||
if o instanceof ops.Delimiter and o.prev_cl?
|
||||
# the user or you gave a position parameter that is to big
|
||||
# for the current array. Therefore we reach a Delimiter.
|
||||
# Then, we'll just return the last character.
|
||||
o = o.prev_cl
|
||||
while o.isDeleted() and o.prev_cl?
|
||||
o = o.prev_cl
|
||||
break
|
||||
if position <= 0 and not o.isDeleted()
|
||||
break
|
||||
|
||||
o = o.next_cl
|
||||
if not o.isDeleted()
|
||||
position -= 1
|
||||
o
|
||||
|
||||
push: (content)->
|
||||
@insertAfter @end.prev_cl, [content]
|
||||
|
||||
insertAfter: (left, contents)->
|
||||
right = left.next_cl
|
||||
while right.isDeleted()
|
||||
right = right.next_cl # find the first character to the right, that is not deleted. In the case that position is 0, its the Delimiter.
|
||||
left = right.prev_cl
|
||||
|
||||
# TODO: always expect an array as content. Then you can combine this with the other option (else)
|
||||
if contents instanceof ops.Operation
|
||||
(new ops.Insert null, content, null, undefined, undefined, left, right).execute()
|
||||
else
|
||||
for c in contents
|
||||
if c? and c._name? and c._getModel?
|
||||
c = c._getModel(@custom_types, @operations)
|
||||
tmp = (new ops.Insert null, c, null, undefined, undefined, left, right).execute()
|
||||
left = tmp
|
||||
@
|
||||
|
||||
#
|
||||
# Inserts an array of content into this list.
|
||||
# @Note: This expects an array as content!
|
||||
#
|
||||
# @return {ListManager Type} This String object.
|
||||
#
|
||||
insert: (position, contents)->
|
||||
ith = @getOperationByPosition position
|
||||
# the (i-1)th character. e.g. "abc" the 1th character is "a"
|
||||
# the 0th character is the left Delimiter
|
||||
@insertAfter ith, contents
|
||||
|
||||
#
|
||||
# Deletes a part of the word.
|
||||
#
|
||||
# @return {ListManager Type} This String object
|
||||
#
|
||||
delete: (position, length = 1)->
|
||||
o = @getOperationByPosition(position+1) # position 0 in this case is the deletion of the first character
|
||||
|
||||
delete_ops = []
|
||||
for i in [0...length]
|
||||
if o instanceof ops.Delimiter
|
||||
break
|
||||
d = (new ops.Delete null, undefined, o).execute()
|
||||
o = o.next_cl
|
||||
while (not (o instanceof ops.Delimiter)) and o.isDeleted()
|
||||
o = o.next_cl
|
||||
delete_ops.push d._encode()
|
||||
@
|
||||
|
||||
|
||||
callOperationSpecificInsertEvents: (op)->
|
||||
getContentType = (content)->
|
||||
if content instanceof ops.Operation
|
||||
content.getCustomType()
|
||||
else
|
||||
content
|
||||
@callEvent [
|
||||
type: "insert"
|
||||
reference: op
|
||||
position: op.getPosition()
|
||||
object: @getCustomType()
|
||||
changedBy: op.uid.creator
|
||||
value: getContentType op.val()
|
||||
]
|
||||
|
||||
callOperationSpecificDeleteEvents: (op, del_op)->
|
||||
@callEvent [
|
||||
type: "delete"
|
||||
reference: op
|
||||
position: op.getPosition()
|
||||
object: @getCustomType() # TODO: You can combine getPosition + getParent in a more efficient manner! (only left Delimiter will hold @parent)
|
||||
length: 1
|
||||
changedBy: del_op.uid.creator
|
||||
oldValue: op.val()
|
||||
]
|
||||
|
||||
ops.ListManager.parse = (json)->
|
||||
{
|
||||
'uid' : uid
|
||||
'custom_type': custom_type
|
||||
'content' : content
|
||||
'content_operations' : content_operations
|
||||
} = json
|
||||
new this(custom_type, uid, content, content_operations)
|
||||
|
||||
class ops.Composition extends ops.ListManager
|
||||
|
||||
constructor: (custom_type, @_composition_value, composition_value_operations, uid, tmp_composition_ref)->
|
||||
# we can't use @seveOperation 'composition_ref', tmp_composition_ref here,
|
||||
# because then there is a "loop" (insertion refers to parent, refers to insertion..)
|
||||
# This is why we have to check in @callOperationSpecificInsertEvents until we find it
|
||||
super custom_type, uid
|
||||
if tmp_composition_ref?
|
||||
@tmp_composition_ref = tmp_composition_ref
|
||||
else
|
||||
@composition_ref = @end.prev_cl
|
||||
if composition_value_operations?
|
||||
@composition_value_operations = {}
|
||||
for n,o of composition_value_operations
|
||||
@saveOperation n, o, '_composition_value'
|
||||
|
||||
type: "Composition"
|
||||
|
||||
#
|
||||
# @private
|
||||
# @see Operation.execute
|
||||
#
|
||||
execute: ()->
|
||||
if @validateSavedOperations()
|
||||
@getCustomType()._setCompositionValue @_composition_value
|
||||
delete @_composition_value
|
||||
# check if tmp_composition_ref already exists
|
||||
if @tmp_composition_ref
|
||||
composition_ref = @HB.getOperation @tmp_composition_ref
|
||||
if composition_ref?
|
||||
delete @tmp_composition_ref
|
||||
@composition_ref = composition_ref
|
||||
super
|
||||
else
|
||||
false
|
||||
|
||||
#
|
||||
# This is called, when the Insert-operation was successfully executed.
|
||||
#
|
||||
callOperationSpecificInsertEvents: (op)->
|
||||
if @tmp_composition_ref?
|
||||
if op.uid.creator is @tmp_composition_ref.creator and op.uid.op_number is @tmp_composition_ref.op_number
|
||||
@composition_ref = op
|
||||
delete @tmp_composition_ref
|
||||
op = op.next_cl
|
||||
if op is @end
|
||||
return
|
||||
else
|
||||
return
|
||||
|
||||
o = @end.prev_cl
|
||||
while o isnt op
|
||||
@getCustomType()._unapply o.undo_delta
|
||||
o = o.prev_cl
|
||||
while o isnt @end
|
||||
o.undo_delta = @getCustomType()._apply o.val()
|
||||
o = o.next_cl
|
||||
@composition_ref = @end.prev_cl
|
||||
|
||||
@callEvent [
|
||||
type: "update"
|
||||
changedBy: op.uid.creator
|
||||
newValue: @val()
|
||||
]
|
||||
|
||||
callOperationSpecificDeleteEvents: (op, del_op)->
|
||||
return
|
||||
|
||||
#
|
||||
# Create a new Delta
|
||||
# - inserts new Content at the end of the list
|
||||
# - updates the composition_value
|
||||
# - updates the composition_ref
|
||||
#
|
||||
# @param delta The delta that is applied to the composition_value
|
||||
#
|
||||
applyDelta: (delta, operations)->
|
||||
(new ops.Insert null, delta, operations, @, null, @end.prev_cl, @end).execute()
|
||||
undefined
|
||||
|
||||
#
|
||||
# Encode this operation in such a way that it can be parsed by remote peers.
|
||||
#
|
||||
_encode: (json = {})->
|
||||
custom = @getCustomType()._getCompositionValue()
|
||||
json.composition_value = custom.composition_value
|
||||
if custom.composition_value_operations?
|
||||
json.composition_value_operations = {}
|
||||
for n,o of custom.composition_value_operations
|
||||
json.composition_value_operations[n] = o.getUid()
|
||||
if @composition_ref?
|
||||
json.composition_ref = @composition_ref.getUid()
|
||||
else
|
||||
json.composition_ref = @tmp_composition_ref
|
||||
super json
|
||||
|
||||
ops.Composition.parse = (json)->
|
||||
{
|
||||
'uid' : uid
|
||||
'custom_type': custom_type
|
||||
'composition_value' : composition_value
|
||||
'composition_value_operations' : composition_value_operations
|
||||
'composition_ref' : composition_ref
|
||||
} = json
|
||||
new this(custom_type, composition_value, composition_value_operations, uid, composition_ref)
|
||||
|
||||
|
||||
#
|
||||
# @nodoc
|
||||
# Adds support for replace. The ReplaceManager manages Replaceable operations.
|
||||
# Each Replaceable holds a value that is now replaceable.
|
||||
#
|
||||
# The TextType-type has implemented support for replace
|
||||
# @see TextType
|
||||
#
|
||||
class ops.ReplaceManager extends ops.ListManager
|
||||
#
|
||||
# @param {Object} event_properties Decorates the event that is thrown by the RM
|
||||
# @param {Object} event_this The object on which the event shall be executed
|
||||
# @param {Operation} initial_content Initialize this with a Replaceable that holds the initial_content.
|
||||
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
|
||||
# @param {Delimiter} beginning Reference or Object.
|
||||
# @param {Delimiter} end Reference or Object.
|
||||
constructor: (custom_type, @event_properties, @event_this, uid)->
|
||||
if not @event_properties['object']?
|
||||
@event_properties['object'] = @event_this.getCustomType()
|
||||
super custom_type, uid
|
||||
|
||||
type: "ReplaceManager"
|
||||
|
||||
#
|
||||
# This doesn't throw the same events as the ListManager. Therefore, the
|
||||
# Replaceables also not throw the same events.
|
||||
# So, ReplaceManager and ListManager both implement
|
||||
# these functions that are called when an Insertion is executed (at the end).
|
||||
#
|
||||
#
|
||||
callEventDecorator: (events)->
|
||||
if not @isDeleted()
|
||||
for event in events
|
||||
for name,prop of @event_properties
|
||||
event[name] = prop
|
||||
@event_this.callEvent events
|
||||
undefined
|
||||
|
||||
#
|
||||
# This is called, when the Insert-type was successfully executed.
|
||||
# TODO: consider doing this in a more consistent manner. This could also be
|
||||
# done with execute. But currently, there are no specital Insert-ops for ListManager.
|
||||
#
|
||||
callOperationSpecificInsertEvents: (op)->
|
||||
if op.next_cl.type is "Delimiter" and op.prev_cl.type isnt "Delimiter"
|
||||
# this replaces another Replaceable
|
||||
if not op.is_deleted # When this is received from the HB, this could already be deleted!
|
||||
old_value = op.prev_cl.val()
|
||||
@callEventDecorator [
|
||||
type: "update"
|
||||
changedBy: op.uid.creator
|
||||
oldValue: old_value
|
||||
]
|
||||
op.prev_cl.applyDelete()
|
||||
else if op.next_cl.type isnt "Delimiter"
|
||||
# This won't be recognized by the user, because another
|
||||
# concurrent operation is set as the current value of the RM
|
||||
op.applyDelete()
|
||||
else # prev _and_ next are Delimiters. This is the first created Replaceable in the RM
|
||||
@callEventDecorator [
|
||||
type: "add"
|
||||
changedBy: op.uid.creator
|
||||
]
|
||||
undefined
|
||||
|
||||
callOperationSpecificDeleteEvents: (op, del_op)->
|
||||
if op.next_cl.type is "Delimiter"
|
||||
@callEventDecorator [
|
||||
type: "delete"
|
||||
changedBy: del_op.uid.creator
|
||||
oldValue: op.val()
|
||||
]
|
||||
|
||||
|
||||
#
|
||||
# Replace the existing word with a new word.
|
||||
#
|
||||
# @param content {Operation} The new value of this ReplaceManager.
|
||||
# @param replaceable_uid {UID} Optional: Unique id of the Replaceable that is created
|
||||
#
|
||||
replace: (content, replaceable_uid)->
|
||||
o = @getLastOperation()
|
||||
relp = (new ops.Insert null, content, null, @, replaceable_uid, o, o.next_cl).execute()
|
||||
# TODO: delete repl (for debugging)
|
||||
undefined
|
||||
|
||||
isContentDeleted: ()->
|
||||
@getLastOperation().isDeleted()
|
||||
|
||||
deleteContent: ()->
|
||||
last_op = @getLastOperation()
|
||||
if (not last_op.isDeleted()) and last_op.type isnt "Delimiter"
|
||||
(new ops.Delete null, undefined, @getLastOperation().uid).execute()
|
||||
undefined
|
||||
|
||||
#
|
||||
# Get the value of this
|
||||
# @return {String}
|
||||
#
|
||||
val: ()->
|
||||
o = @getLastOperation()
|
||||
#if o instanceof ops.Delimiter
|
||||
# throw new Error "Replace Manager doesn't contain anything."
|
||||
o.val?() # ? - for the case that (currently) the RM does not contain anything (then o is a Delimiter)
|
||||
|
||||
|
||||
|
||||
basic_ops
|
@ -1,55 +0,0 @@
|
||||
|
||||
bindToChildren = (that)->
|
||||
for i in [0...that.children.length]
|
||||
attr = that.children.item(i)
|
||||
if attr.name?
|
||||
attr.val = that.val.val(attr.name)
|
||||
that.val.observe (events)->
|
||||
for event in events
|
||||
if event.name?
|
||||
for i in [0...that.children.length]
|
||||
attr = that.children.item(i)
|
||||
if attr.name? and attr.name is event.name
|
||||
newVal = that.val.val(attr.name)
|
||||
if attr.val isnt newVal
|
||||
attr.val = newVal
|
||||
|
||||
Polymer "y-object",
|
||||
ready: ()->
|
||||
if @connector?
|
||||
@val = new Y @connector
|
||||
bindToChildren @
|
||||
else if @val?
|
||||
bindToChildren @
|
||||
|
||||
valChanged: ()->
|
||||
if @val? and @val._name is "Object"
|
||||
bindToChildren @
|
||||
|
||||
connectorChanged: ()->
|
||||
if (not @val?)
|
||||
@val = new Y @connector
|
||||
bindToChildren @
|
||||
|
||||
Polymer "y-property",
|
||||
ready: ()->
|
||||
if @val? and @name?
|
||||
if @val.constructor is Object
|
||||
@val = @parentElement.val(@name,new Y.Object(@val)).val(@name)
|
||||
# TODO: please use instanceof instead of ._name,
|
||||
# since it is more safe (consider someone putting a custom Object type here)
|
||||
else if typeof @val is "string"
|
||||
@parentElement.val(@name,@val)
|
||||
if @val._name is "Object"
|
||||
bindToChildren @
|
||||
|
||||
valChanged: ()->
|
||||
if @val? and @name?
|
||||
if @val.constructor is Object
|
||||
@val = @parentElement.val.val(@name, new Y.Object(@val)).val(@name)
|
||||
# TODO: please use instanceof instead of ._name,
|
||||
# since it is more safe (consider someone putting a custom Object type here)
|
||||
else if @val._name is "Object"
|
||||
bindToChildren @
|
||||
else if @parentElement.val?.val? and @val isnt @parentElement.val.val(@name)
|
||||
@parentElement.val.val @name, @val
|
38
lib/y.coffee
38
lib/y.coffee
@ -1,38 +0,0 @@
|
||||
|
||||
structured_ops_uninitialized = require "./Operations/Structured"
|
||||
|
||||
HistoryBuffer = require "./HistoryBuffer"
|
||||
Engine = require "./Engine"
|
||||
adaptConnector = require "./ConnectorAdapter"
|
||||
|
||||
createY = (connector)->
|
||||
if connector.user_id?
|
||||
user_id = connector.user_id # TODO: change to getUniqueId()
|
||||
else
|
||||
user_id = "_temp"
|
||||
connector.when_received_state_vector_listeners = [(state_vector)->
|
||||
HB.setUserId this.user_id, state_vector
|
||||
]
|
||||
HB = new HistoryBuffer user_id
|
||||
ops_manager = structured_ops_uninitialized HB, this.constructor
|
||||
ops = ops_manager.operations
|
||||
|
||||
engine = new Engine HB, ops
|
||||
adaptConnector connector, engine, HB, ops_manager.execution_listener
|
||||
|
||||
ops.Operation.prototype.HB = HB
|
||||
ops.Operation.prototype.operations = ops
|
||||
ops.Operation.prototype.engine = engine
|
||||
ops.Operation.prototype.connector = connector
|
||||
ops.Operation.prototype.custom_types = this.constructor
|
||||
|
||||
ct = new createY.Object()
|
||||
model = new ops.MapManager(ct, HB.getReservedUniqueIdentifier()).execute()
|
||||
ct._setModel model
|
||||
ct
|
||||
|
||||
module.exports = createY
|
||||
if window?
|
||||
window.Y = createY
|
||||
|
||||
createY.Object = require "./ObjectType"
|
64
package.json
64
package.json
@ -1,65 +1,41 @@
|
||||
{
|
||||
"name": "yjs",
|
||||
"version": "0.5.2",
|
||||
"description": "A Framework that enables Real-Time Collaboration on arbitrary data structures.",
|
||||
"main": "./build/node/y.js",
|
||||
"version": "0.6.0",
|
||||
"description": "A framework for real-time p2p shared editing on arbitrary complex data types",
|
||||
"main": "y.js",
|
||||
"scripts": {
|
||||
"prepublish": "./node_modules/gulp/bin/gulp.js build_node",
|
||||
"test": "./node_modules/gulp/bin/gulp.js mocha"
|
||||
"test": "gulp test"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/rwth-acis/yjs"
|
||||
"url": "https://github.com/y-js/yjs.git"
|
||||
},
|
||||
"keywords": [
|
||||
"OT",
|
||||
"Operational Transformation",
|
||||
"collaboration",
|
||||
"synchronization",
|
||||
"ShareJS",
|
||||
"Coweb",
|
||||
"ShareJs",
|
||||
"OpenCoweb",
|
||||
"concurrency"
|
||||
],
|
||||
"author": "Kevin Jahns",
|
||||
"email": "kevin.jahns@rwth-aachen.de",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/rwth-acis/yjs/issues"
|
||||
},
|
||||
"homepage": "https://dadamonad.github.io/yjs/",
|
||||
"dependencies": {
|
||||
"url": "https://github.com/y-js/yjs/issues"
|
||||
},
|
||||
"homepage": "http://y-js.org",
|
||||
"devDependencies": {
|
||||
"chai": "^2.2.0",
|
||||
"codo": "^2.0.9",
|
||||
"coffee-errors": "~0.8.6",
|
||||
"coffee-script": "^1.7.1",
|
||||
"coffeeify": "^0.6.0",
|
||||
"gulp": "^3.8.7",
|
||||
"gulp-browserify": "^0.5.0",
|
||||
"gulp-cached": "^1.0.1",
|
||||
"gulp-coffee": "^2.1.1",
|
||||
"gulp-coffeeify": "^0.1.2",
|
||||
"gulp-coffeelint": "^0.3.3",
|
||||
"gulp-concat": "^2.3.4",
|
||||
"gulp-copy": "0.0.2",
|
||||
"gulp-debug": "^1.0.0",
|
||||
"gulp-exit": "0.0.2",
|
||||
"gulp-git": "^0.5.0",
|
||||
"gulp-if": "^1.2.4",
|
||||
"gulp-ignore": "^1.2.0",
|
||||
"gulp-ljs": "^0.1.1",
|
||||
"gulp-mocha": "^0.5.2",
|
||||
"gulp-plumber": "^0.6.6",
|
||||
"gulp-rename": "^1.2.0",
|
||||
"gulp-rimraf": "^0.1.0",
|
||||
"gulp-run": "^1.6.3",
|
||||
"gulp-sourcemaps": "^1.1.1",
|
||||
"gulp-uglify": "^0.3.1",
|
||||
"gulp-watch": "^3.0.0",
|
||||
"jquery": "^2.1.1",
|
||||
"underscore": "^1.6.0",
|
||||
"mocha": "^2.1.0",
|
||||
"sinon": "^1.12.2",
|
||||
"sinon-chai": "^2.7.0"
|
||||
"babel-eslint": "^3.1.15",
|
||||
"eslint": "^0.22.1",
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-babel": "^5.1.0",
|
||||
"gulp-concat": "^2.5.2",
|
||||
"gulp-eslint": "^0.13.2",
|
||||
"gulp-jasmine": "^2.0.1",
|
||||
"gulp-sourcemaps": "^1.5.2",
|
||||
"gulp-uglify": "^1.2.0",
|
||||
"minimist": "^1.1.1"
|
||||
}
|
||||
}
|
||||
|
14
src/Buffer.js
Normal file
14
src/Buffer.js
Normal file
@ -0,0 +1,14 @@
|
||||
/* @flow */
|
||||
|
||||
class Buffer {
|
||||
i : number;
|
||||
constructor () {
|
||||
this.i = 4;
|
||||
}
|
||||
}
|
||||
|
||||
function add(x : string){
|
||||
return x + 4;
|
||||
}
|
||||
|
||||
add("5");
|
4
src/y.js
Normal file
4
src/y.js
Normal file
@ -0,0 +1,4 @@
|
||||
/* @flow */
|
||||
/* global Buffer */
|
||||
|
||||
var buffer = new Buffer(3);
|
@ -1,219 +0,0 @@
|
||||
chai = require('chai')
|
||||
expect = chai.expect
|
||||
sinon = require('sinon')
|
||||
sinonChai = require('sinon-chai')
|
||||
_ = require("underscore")
|
||||
|
||||
chai.use(sinonChai)
|
||||
|
||||
Connector = require "../../y-test/lib/y-test.coffee"
|
||||
Y = null # need global reference!
|
||||
|
||||
module.exports = class Test
|
||||
constructor: (@name_suffix = "", Yjs)->
|
||||
Y = Yjs
|
||||
@number_of_test_cases_multiplier = 1
|
||||
@repeat_this = 1 * @number_of_test_cases_multiplier
|
||||
@doSomething_amount = 123 * @number_of_test_cases_multiplier
|
||||
@number_of_engines = 5 + @number_of_test_cases_multiplier - 1
|
||||
|
||||
@time = 0 # denotes to the time when run was started
|
||||
@ops = 0 # number of operations (used with @time)
|
||||
@time_now = 0 # current time
|
||||
@max_depth = 3
|
||||
|
||||
@debug = false
|
||||
|
||||
@reinitialize()
|
||||
for gf in @getGeneratingFunctions(0)
|
||||
if not (gf.types? and gf.f?)
|
||||
throw new Error "Generating Functions are not initialized properly!"
|
||||
for t in gf.types
|
||||
if not t?
|
||||
throw new Error "You havent includedt this type in Y (do require 'y-whatever')"
|
||||
|
||||
reinitialize: ()->
|
||||
@users = []
|
||||
for i in [0...@number_of_engines]
|
||||
u = @makeNewUser (i+@name_suffix)
|
||||
for user in @users
|
||||
u._model.connector.join(user._model.connector) # TODO: change the test-connector to make this more convenient
|
||||
@users.push u
|
||||
@initUsers?(@users[0])
|
||||
@flushAll()
|
||||
|
||||
# is called by implementing class
|
||||
makeNewUser: (user)->
|
||||
user._model.HB.stopGarbageCollection()
|
||||
user
|
||||
|
||||
getSomeUser: ()->
|
||||
i = _.random 0, (@users.length-1)
|
||||
@users[i]
|
||||
|
||||
getRandomText: (chars, min_length = 0)->
|
||||
chars ?= "abcdefghijklmnopqrstuvwxyz"
|
||||
length = _.random min_length, 10
|
||||
#length = 1
|
||||
nextchar = chars[(_.random 0, (chars.length-1))]
|
||||
text = ""
|
||||
_(length).times ()-> text += nextchar
|
||||
text
|
||||
|
||||
getRandomObject: ()->
|
||||
result = {}
|
||||
key1 = @getRandomKey()
|
||||
key2 = @getRandomKey()
|
||||
val1 = @getRandomText()
|
||||
val2 = null
|
||||
if _.random(0,1) is 1
|
||||
val2 = @getRandomObject()
|
||||
else
|
||||
val2 = @getRandomText()
|
||||
result[key1] = val1
|
||||
result[key2] = val2
|
||||
result
|
||||
|
||||
getRandomKey: ()->
|
||||
@getRandomText [1,2,'x','y'], 1 # only 4 keys
|
||||
|
||||
getGeneratingFunctions: (user_num)=>
|
||||
types = @users[user_num]._model.operations
|
||||
[]
|
||||
getRandomRoot: (user_num)->
|
||||
throw new Error "implement me!"
|
||||
|
||||
compare: (o1, o2, depth = (@max_depth+1))->
|
||||
if o1 is o2 or depth <= 0
|
||||
true
|
||||
else if o1._name? and o1._name isnt o2._name
|
||||
throw new Error "different types"
|
||||
else if o1._model?
|
||||
@compare o1._model, o2._model, depth
|
||||
else if o1.type is "MapManager"
|
||||
for name, val of o1.val()
|
||||
@compare(val, o2.val(name), depth-1)
|
||||
else if o1.type is "ListManager"
|
||||
for val,i in o1.val()
|
||||
@compare(val, o2.val(i), depth-1)
|
||||
else if o1.constructor is Array and o2.constructor is Array
|
||||
if o1.length isnt o2.length
|
||||
throw new Error "The Arrays do not have the same size!"
|
||||
for o,i in o1
|
||||
@compare o, o2[i], (depth-1)
|
||||
else if o1 isnt o2
|
||||
throw new Error "different values"
|
||||
else
|
||||
throw new Error "I don't know what to do .. "
|
||||
|
||||
generateRandomOp: (user_num)=>
|
||||
y = @getRandomRoot(user_num)
|
||||
choices = @getGeneratingFunctions(user_num).filter (gf)->
|
||||
_.some gf.types, (type)->
|
||||
y instanceof type
|
||||
|
||||
if choices.length is 0
|
||||
console.dir(y)
|
||||
throw new Error "You forgot to specify a test generation methot for this Operation! (#{y.type})"
|
||||
i = _.random 0, (choices.length-1)
|
||||
choices[i].f y
|
||||
|
||||
applyRandomOp: (user_num)=>
|
||||
user = @users[user_num]
|
||||
user._model.connector.flushOneRandom()
|
||||
|
||||
doSomething: ()->
|
||||
user_num = _.random (@number_of_engines-1)
|
||||
choices = [@applyRandomOp, @generateRandomOp]
|
||||
choice = _.random (choices.length-1)
|
||||
choices[choice](user_num)
|
||||
|
||||
flushAll: (final)->
|
||||
# TODO:!!
|
||||
final = false
|
||||
if @users.length <= 1 or not final
|
||||
for user,user_number in @users
|
||||
user._model.connector.flushAll()
|
||||
else
|
||||
for user,user_number in @users[1..]
|
||||
user._model.connector.flushAll()
|
||||
ops = @users[1].getHistoryBuffer()._encode @users[0].HB.getOperationCounter()
|
||||
@users[0].engine.applyOpsCheckDouble ops
|
||||
|
||||
|
||||
compareAll: (test_number)->
|
||||
@flushAll(true)
|
||||
|
||||
@time += (new Date()).getTime() - @time_now
|
||||
|
||||
number_of_created_operations = 0
|
||||
for i in [0...(@users.length)]
|
||||
number_of_created_operations += @users[i]._model.connector.getOpsInExecutionOrder().length
|
||||
@ops += number_of_created_operations*@users.length
|
||||
|
||||
ops_per_msek = Math.floor(@ops/@time)
|
||||
if test_number? # and @debug
|
||||
console.log "#{test_number}/#{@repeat_this}: #{number_of_created_operations} were created and applied on (#{@users.length}) users ops in a different order." + " Over all we consumed #{@ops} operations in #{@time/1000} seconds (#{ops_per_msek} ops/msek)."
|
||||
|
||||
for i in [0...(@users.length-1)]
|
||||
if @debug
|
||||
if not _.isEqual @getContent(i), @getContent(i+1)
|
||||
printOpsInExecutionOrder = (otnumber, otherotnumber)=>
|
||||
ops = _.filter @users[otnumber]._model.connector.getOpsInExecutionOrder(), (o)->
|
||||
typeof o.uid.op_name isnt 'string' and o.uid.creator isnt '_'
|
||||
for s,j in ops
|
||||
console.log "op#{j} = " + (JSON.stringify s)
|
||||
console.log ""
|
||||
s = "ops = ["
|
||||
for o,j in ops
|
||||
if j isnt 0
|
||||
s += ", "
|
||||
s += "op#{j}"
|
||||
s += "]"
|
||||
console.log s
|
||||
console.log "@test_user.engine.applyOps ops"
|
||||
console.log "expect(@test_user.val('name').val()).to.equal(\"#{@users[otherotnumber].val('name').val()}\")"
|
||||
ops
|
||||
console.log ""
|
||||
console.log "Found an OT Puzzle!"
|
||||
console.log "OT states:"
|
||||
for u,j in @users
|
||||
console.log "OT#{j}: "+u.val('name').val()
|
||||
console.log "\nOT execution order (#{i},#{i+1}):"
|
||||
printOpsInExecutionOrder i, i+1
|
||||
console.log ""
|
||||
ops = printOpsInExecutionOrder i+1, i
|
||||
|
||||
console.log ""
|
||||
expect(@compare(@users[i], @users[i+1])).to.not.be.undefined
|
||||
|
||||
run: ()->
|
||||
if @debug
|
||||
console.log ''
|
||||
for times in [1..@repeat_this]
|
||||
@time_now = (new Date).getTime()
|
||||
for i in [1..Math.floor(@doSomething_amount/2)]
|
||||
@doSomething()
|
||||
@flushAll(false)
|
||||
for u in @users
|
||||
u._model.HB.emptyGarbage()
|
||||
for i in [1..Math.floor(@doSomething_amount/2)]
|
||||
@doSomething()
|
||||
|
||||
@compareAll(times)
|
||||
@testHBencoding()
|
||||
if times isnt @repeat_this
|
||||
@reinitialize()
|
||||
|
||||
testHBencoding: ()->
|
||||
# in case of JsonFramework, every user will create its JSON first! therefore, the testusers id must be small than all the others (see InsertType)
|
||||
@users[@users.length] = @makeNewUser (-1) # this does not want to join with anymody
|
||||
|
||||
@users[@users.length-1]._model.HB.renewStateVector @users[0]._model.HB.getOperationCounter()
|
||||
@users[@users.length-1]._model.engine.applyOps @users[0]._model.HB._encode()
|
||||
|
||||
#if @getContent(@users.length-1) isnt @getContent(0)
|
||||
# console.log "testHBencoding:"
|
||||
# console.log "Unprocessed ops first: #{@users[0].engine.unprocessed_ops.length}"
|
||||
# console.log "Unprocessed ops last: #{@users[@users.length-1].engine.unprocessed_ops.length}"
|
||||
expect(@compare(@users[@users.length-1], @users[0])).to.not.be.undefined
|
@ -1,324 +0,0 @@
|
||||
chai = require('chai')
|
||||
expect = chai.expect
|
||||
should = chai.should()
|
||||
sinon = require('sinon')
|
||||
sinonChai = require('sinon-chai')
|
||||
_ = require("underscore")
|
||||
|
||||
chai.use(sinonChai)
|
||||
|
||||
Y = require "../lib/y.coffee"
|
||||
Y.Test = require "../../y-test/lib/y-test.coffee"
|
||||
Y.Text = require "../../y-text/lib/y-text"
|
||||
Y.List = require "../../y-list/lib/y-list"
|
||||
|
||||
TestSuite = require "./TestSuite"
|
||||
class ObjectTest extends TestSuite
|
||||
|
||||
constructor: (suffix)->
|
||||
super suffix, Y
|
||||
|
||||
makeNewUser: (userId)->
|
||||
conn = new Y.Test userId
|
||||
super new Y conn
|
||||
|
||||
type: "ObjectTest"
|
||||
|
||||
getRandomRoot: (user_num, root, depth = @max_depth)->
|
||||
root ?= @users[user_num]
|
||||
if depth is 0 or _.random(0,1) is 1 # take root
|
||||
root
|
||||
else # take child
|
||||
depth--
|
||||
elems = null
|
||||
if root._name is "Object"
|
||||
elems =
|
||||
for oname,val of root.val()
|
||||
val
|
||||
else if root._name is "Array"
|
||||
elems = root.val()
|
||||
else
|
||||
return root
|
||||
|
||||
elems = elems.filter (elem)->
|
||||
elem? and ((elem._name is "Array") or (elem._name is "Object"))
|
||||
if elems.length is 0
|
||||
root
|
||||
else
|
||||
p = elems[_.random(0, elems.length-1)]
|
||||
@getRandomRoot user_num, p, depth
|
||||
|
||||
getGeneratingFunctions: (user_num)->
|
||||
super(user_num).concat [
|
||||
f : (y)=> # Delete Object Property
|
||||
list = for name, o of y.val()
|
||||
name
|
||||
if list.length > 0
|
||||
key = list[_.random(0,list.length-1)]
|
||||
y.delete(key)
|
||||
types: [Y.Object]
|
||||
,
|
||||
f : (y)=> # SET Object Property
|
||||
y.val(@getRandomKey(), new Y.Object(@getRandomObject()))
|
||||
types: [Y.Object]
|
||||
,
|
||||
f : (y)=> # SET PROPERTY TEXT
|
||||
y.val(@getRandomKey(), new Y.Text(@getRandomText()))
|
||||
types: [Y.Object]
|
||||
,
|
||||
f : (y)=> # SET PROPERTY List
|
||||
y.val(@getRandomKey(), new Y.List(@getRandomText().split("")))
|
||||
types: [Y.Object]
|
||||
,
|
||||
f : (y)=> # Delete Array Element
|
||||
list = y.val()
|
||||
if list.length > 0
|
||||
i = _.random(0,list.length-1)
|
||||
y.delete(i)
|
||||
types: [Y.List]
|
||||
,
|
||||
f : (y)=> # insert Object mutable
|
||||
l = y.val().length
|
||||
y.insert(_.random(0, l-1), new Y.Object(@getRandomObject()))
|
||||
types: [Y.List]
|
||||
,
|
||||
f : (y)=> # insert Text mutable
|
||||
l = y.val().length
|
||||
y.insert(_.random(0, l-1), new Y.Text(@getRandomText()))
|
||||
types : [Y.List]
|
||||
,
|
||||
f : (y)=> # insert List mutable
|
||||
l = y.val().length
|
||||
y.insert(_.random(0, l-1), new Y.List(@getRandomText().split("")))
|
||||
types : [Y.List]
|
||||
,
|
||||
f : (y)=> # insert Number (primitive object)
|
||||
l = y.val().length
|
||||
y.insert(_.random(0,l-1), _.random(0,42))
|
||||
types : [Y.List]
|
||||
,
|
||||
f : (y)=> # SET Object Property (circular)
|
||||
y.val(@getRandomKey(), @getRandomRoot user_num)
|
||||
types: [Y.Object]
|
||||
,
|
||||
f : (y)=> # insert Object mutable (circular)
|
||||
l = y.val().length
|
||||
y.insert(_.random(0, l-1), @getRandomRoot user_num)
|
||||
types: [Y.List]
|
||||
,
|
||||
f : (y)=> # INSERT TEXT
|
||||
y
|
||||
pos = _.random 0, (y.val().length-1)
|
||||
y.insert pos, @getRandomText()
|
||||
null
|
||||
types: [Y.Text]
|
||||
,
|
||||
f : (y)-> # DELETE TEXT
|
||||
if y.val().length > 0
|
||||
pos = _.random 0, (y.val().length-1) # TODO: put here also arbitrary number (test behaviour in error cases)
|
||||
length = _.random 0, (y.val().length - pos)
|
||||
ops1 = y.delete pos, length
|
||||
undefined
|
||||
types : [Y.Text]
|
||||
]
|
||||
|
||||
module.exports = ObjectTest
|
||||
|
||||
describe "Object Test", ->
|
||||
@timeout 500000
|
||||
|
||||
beforeEach (done)->
|
||||
@yTest = new ObjectTest()
|
||||
@users = @yTest.users
|
||||
|
||||
@test_user = @yTest.makeNewUser "test_user"
|
||||
done()
|
||||
|
||||
it "can handle many engines, many operations, concurrently (random)", ->
|
||||
console.log "" # TODO
|
||||
@yTest.run()
|
||||
|
||||
it "has a working test suite", ->
|
||||
@yTest.compareAll()
|
||||
|
||||
it "handles double-late-join", ->
|
||||
test = new ObjectTest("double")
|
||||
test.run()
|
||||
@yTest.run()
|
||||
u1 = test.users[0]
|
||||
u2 = @yTest.users[1]
|
||||
ops1 = u1._model.HB._encode()
|
||||
ops2 = u2._model.HB._encode()
|
||||
u1._model.engine.applyOp ops2, true
|
||||
u2._model.engine.applyOp ops1, true
|
||||
|
||||
expect(@yTest.compare(u1, u2)).to.not.be.undefined
|
||||
|
||||
it "can handle creaton of complex json (1)", ->
|
||||
@yTest.users[0].val('a', new Y.Text('q'))
|
||||
@yTest.users[1].val('a', new Y.Text('t'))
|
||||
@yTest.compareAll()
|
||||
q = @yTest.users[2].val('a')
|
||||
q.insert(0,'A')
|
||||
@yTest.compareAll()
|
||||
expect(@yTest.getSomeUser().val("a").val()).to.equal("At")
|
||||
|
||||
it "can handle creaton of complex json (2)", ->
|
||||
@yTest.getSomeUser().val('x', new Y.Object({'a':'b'}))
|
||||
@yTest.getSomeUser().val('a', new Y.Object({'a':{q: new Y.Text("dtrndtrtdrntdrnrtdnrtdnrtdnrtdnrdnrdt")}}))
|
||||
@yTest.getSomeUser().val('b', new Y.Object({'a':{}}))
|
||||
@yTest.getSomeUser().val('c', new Y.Object({'a':'c'}))
|
||||
@yTest.getSomeUser().val('c', new Y.Object({'a':'b'}))
|
||||
@yTest.compareAll()
|
||||
q = @yTest.getSomeUser().val("a").val("a").val("q")
|
||||
q.insert(0,'A')
|
||||
@yTest.compareAll()
|
||||
expect(@yTest.getSomeUser().val("a").val("a").val("q").val()).to.equal("Adtrndtrtdrntdrnrtdnrtdnrtdnrtdnrdnrdt")
|
||||
|
||||
it "can handle creaton of complex json (3)", ->
|
||||
@yTest.users[0].val('l', new Y.List([1,2,3]))
|
||||
@yTest.users[1].val('l', new Y.List([4,5,6]))
|
||||
@yTest.compareAll()
|
||||
@yTest.users[2].val('l').insert(0,'A')
|
||||
w = @yTest.users[1].val('l').insert(0,new Y.Text('B')).val(0)
|
||||
w.insert 1, "C"
|
||||
expect(w.val()).to.equal("BC")
|
||||
@yTest.compareAll()
|
||||
|
||||
it "handles immutables and primitive data types", ->
|
||||
@yTest.getSomeUser().val('string', "text")
|
||||
@yTest.getSomeUser().val('number', 4)
|
||||
@yTest.getSomeUser().val('object', new Y.Object({q:"rr"}))
|
||||
@yTest.getSomeUser().val('null', null)
|
||||
@yTest.compareAll()
|
||||
expect(@yTest.getSomeUser().val('string')).to.equal "text"
|
||||
expect(@yTest.getSomeUser().val('number')).to.equal 4
|
||||
expect(@yTest.getSomeUser().val('object').val('q')).to.equal "rr"
|
||||
expect(@yTest.getSomeUser().val('null') is null).to.be.ok
|
||||
|
||||
it "handles immutables and primitive data types (2)", ->
|
||||
@yTest.users[0].val('string', "text")
|
||||
@yTest.users[1].val('number', 4)
|
||||
@yTest.users[2].val('object', new Y.Object({q:"rr"}))
|
||||
@yTest.users[0].val('null', null)
|
||||
@yTest.compareAll()
|
||||
expect(@yTest.getSomeUser().val('string')).to.equal "text"
|
||||
expect(@yTest.getSomeUser().val('number')).to.equal 4
|
||||
expect(@yTest.getSomeUser().val('object').val('q')).to.equal "rr"
|
||||
expect(@yTest.getSomeUser().val('null') is null).to.be.ok
|
||||
|
||||
it "Observers work on JSON Types (add type observers, local and foreign)", ->
|
||||
u = @yTest.users[0]
|
||||
@yTest.flushAll()
|
||||
last_task = null
|
||||
observer1 = (changes)->
|
||||
expect(changes.length).to.equal(1)
|
||||
change = changes[0]
|
||||
expect(change.type).to.equal("add")
|
||||
expect(change.object).to.equal(u)
|
||||
expect(change.changedBy).to.equal('0')
|
||||
expect(change.name).to.equal("newStuff")
|
||||
last_task = "observer1"
|
||||
u.observe observer1
|
||||
u.val("newStuff",new Y.Text("someStuff"))
|
||||
expect(last_task).to.equal("observer1")
|
||||
u.unobserve observer1
|
||||
|
||||
observer2 = (changes)->
|
||||
expect(changes.length).to.equal(1)
|
||||
change = changes[0]
|
||||
expect(change.type).to.equal("add")
|
||||
expect(change.object).to.equal(u)
|
||||
expect(change.changedBy).to.equal('1')
|
||||
expect(change.name).to.equal("moreStuff")
|
||||
last_task = "observer2"
|
||||
u.observe observer2
|
||||
v = @yTest.users[1]
|
||||
v.val("moreStuff","someMoreStuff")
|
||||
@yTest.flushAll()
|
||||
expect(last_task).to.equal("observer2")
|
||||
u.unobserve observer2
|
||||
|
||||
it "Observers work on JSON Types (update type observers, local and foreign)", ->
|
||||
u = @yTest.users[0].val("newStuff", new Y.Text("oldStuff")).val("moreStuff",new Y.Text("moreOldStuff"))
|
||||
@yTest.flushAll()
|
||||
last_task = null
|
||||
observer1 = (changes)->
|
||||
expect(changes.length).to.equal(1)
|
||||
change = changes[0]
|
||||
expect(change.type).to.equal("update")
|
||||
expect(change.object).to.equal(u)
|
||||
expect(change.changedBy).to.equal('0')
|
||||
expect(change.name).to.equal("newStuff")
|
||||
expect(change.oldValue.val()).to.equal("oldStuff")
|
||||
last_task = "observer1"
|
||||
u.observe observer1
|
||||
u.val("newStuff","someStuff")
|
||||
expect(last_task).to.equal("observer1")
|
||||
u.unobserve observer1
|
||||
|
||||
observer2 = (changes)->
|
||||
expect(changes.length).to.equal(1)
|
||||
change = changes[0]
|
||||
expect(change.type).to.equal("update")
|
||||
expect(change.object).to.equal(u)
|
||||
expect(change.changedBy).to.equal('1')
|
||||
expect(change.name).to.equal("moreStuff")
|
||||
expect(change.oldValue.val()).to.equal("moreOldStuff")
|
||||
last_task = "observer2"
|
||||
u.observe observer2
|
||||
v = @yTest.users[1]
|
||||
v.val("moreStuff","someMoreStuff")
|
||||
@yTest.flushAll()
|
||||
expect(last_task).to.equal("observer2")
|
||||
u.unobserve observer2
|
||||
|
||||
|
||||
it "Observers work on JSON Types (delete type observers, local and foreign)", ->
|
||||
u = @yTest.users[0].val("newStuff",new Y.Text("oldStuff")).val("moreStuff",new Y.Text("moreOldStuff"))
|
||||
@yTest.flushAll()
|
||||
last_task = null
|
||||
observer1 = (changes)->
|
||||
expect(changes.length).to.equal(1)
|
||||
change = changes[0]
|
||||
expect(change.type).to.equal("delete")
|
||||
expect(change.object).to.equal(u)
|
||||
expect(change.changedBy).to.equal('0')
|
||||
expect(change.name).to.equal("newStuff")
|
||||
expect(change.oldValue.val()).to.equal("oldStuff")
|
||||
last_task = "observer1"
|
||||
u.observe observer1
|
||||
u.delete("newStuff")
|
||||
expect(last_task).to.equal("observer1")
|
||||
u.unobserve observer1
|
||||
|
||||
observer2 = (changes)->
|
||||
expect(changes.length).to.equal(1)
|
||||
change = changes[0]
|
||||
expect(change.type).to.equal("delete")
|
||||
expect(change.object).to.equal(u)
|
||||
expect(change.changedBy).to.equal('1')
|
||||
expect(change.name).to.equal("moreStuff")
|
||||
expect(change.oldValue.val()).to.equal("moreOldStuff")
|
||||
last_task = "observer2"
|
||||
u.observe observer2
|
||||
v = @yTest.users[1]
|
||||
v.delete("moreStuff")
|
||||
@yTest.flushAll()
|
||||
expect(last_task).to.equal("observer2")
|
||||
u.unobserve observer2
|
||||
|
||||
it "can handle circular JSON", ->
|
||||
u = @yTest.users[0]
|
||||
u.val("me", u)
|
||||
@yTest.compareAll()
|
||||
u.val("stuff", new Y.Object({x: true}))
|
||||
u.val("same_stuff", u.val("stuff"))
|
||||
u.val("same_stuff").val("x", 5)
|
||||
expect(u.val("same_stuff").val("x")).to.equal(5)
|
||||
@yTest.compareAll()
|
||||
u.val("stuff").val("y", u.val("stuff"))
|
||||
@yTest.compareAll()
|
||||
|
||||
|
||||
|
30
tests/Buffer.html
Normal file
30
tests/Buffer.html
Normal file
@ -0,0 +1,30 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>Jasmine Spec Runner: repeat</title>
|
||||
|
||||
<!-- Jasmine files -->
|
||||
<link href="../node_modules/jasmine-core/lib/jasmine-core/jasmine.css" rel="stylesheet"/>
|
||||
<script type="text/javascript" src="../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
|
||||
<script type="text/javascript" src="../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
|
||||
|
||||
|
||||
<!-- CUSTOMIZE: The code to be tested and the tests -->
|
||||
|
||||
<script type="text/javascript" src="../build/y.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function() {
|
||||
var jasmineEnv = jasmine.getEnv();
|
||||
|
||||
window.onload = function() {
|
||||
|
||||
jasmineEnv.execute();
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
8
tests/Buffer.js
Normal file
8
tests/Buffer.js
Normal file
@ -0,0 +1,8 @@
|
||||
/* @flow */
|
||||
/*eslint-env node, jasmine */
|
||||
|
||||
describe("A suite", function() {
|
||||
it("contains spec with an expectation", function() {
|
||||
expect(new Buffer(3)).toBe(true);
|
||||
});
|
||||
});
|
@ -1,7 +0,0 @@
|
||||
|
||||
<polymer-element name="y-object" hidden attributes="val connector y">
|
||||
</polymer-element>
|
||||
<polymer-element name="y-property" hidden attributes="val name y">
|
||||
</polymer-element>
|
||||
|
||||
<script src="./y-object.js"></script>
|
@ -1 +0,0 @@
|
||||
!function t(n,e,l){function i(a,h){if(!e[a]){if(!n[a]){var v="function"==typeof require&&require;if(!h&&v)return v(a,!0);if(r)return r(a,!0);throw new Error("Cannot find module '"+a+"'")}var s=e[a]={exports:{}};n[a][0].call(s.exports,function(t){var e=n[a][1][t];return i(e?e:t)},s,s.exports,t,n,e,l)}return e[a].exports}for(var r="function"==typeof require&&require,a=0;a<l.length;a++)i(l[a]);return i}({1:[function(){var t;t=function(t){var n,e,l,i;for(e=l=0,i=t.children.length;i>=0?i>l:l>i;e=i>=0?++l:--l)n=t.children.item(e),null!=n.name&&(n.val=t.val.val(n.name));return t.val.observe(function(l){var i,r,a,h,v;for(v=[],a=0,h=l.length;h>a;a++)i=l[a],null!=i.name?v.push(function(){var l,a,h;for(h=[],e=l=0,a=t.children.length;a>=0?a>l:l>a;e=a>=0?++l:--l)n=t.children.item(e),null!=n.name&&n.name===i.name?(r=t.val.val(n.name),n.val!==r?h.push(n.val=r):h.push(void 0)):h.push(void 0);return h}()):v.push(void 0);return v})},Polymer("y-object",{ready:function(){return null!=this.connector?(this.val=new Y(this.connector),t(this)):null!=this.val?t(this):void 0},valChanged:function(){return null!=this.val&&"Object"===this.val._name?t(this):void 0},connectorChanged:function(){return null==this.val?(this.val=new Y(this.connector),t(this)):void 0}}),Polymer("y-property",{ready:function(){return null!=this.val&&null!=this.name&&(this.val.constructor===Object?this.val=this.parentElement.val(this.name,new Y.Object(this.val)).val(this.name):"string"==typeof this.val&&this.parentElement.val(this.name,this.val),"Object"===this.val._name)?t(this):void 0},valChanged:function(){var n;if(null!=this.val&&null!=this.name){if(this.val.constructor===Object)return this.val=this.parentElement.val.val(this.name,new Y.Object(this.val)).val(this.name);if("Object"===this.val._name)return t(this);if(null!=(null!=(n=this.parentElement.val)?n.val:void 0)&&this.val!==this.parentElement.val.val(this.name))return this.parentElement.val.val(this.name,this.val)}}})},{}]},{},[1]);
|
Loading…
x
Reference in New Issue
Block a user