Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a4aabe69c | ||
|
|
6904e6018d | ||
|
|
5e295b80d7 | ||
|
|
e58f633115 | ||
|
|
65ea42481e | ||
|
|
a220b06ad9 | ||
|
|
2650338b4b |
22
Examples/Ace/index.html
Normal file
22
Examples/Ace/index.html
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style type="text/css" media="screen">
|
||||||
|
#editor {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="editor"></div>
|
||||||
|
<script src="../bower_components/yjs/y.es6"></script>
|
||||||
|
<script src="../bower_components/ace-builds/src/ace.js"></script>
|
||||||
|
|
||||||
|
<script src="./index.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
28
Examples/Ace/index.js
Normal file
28
Examples/Ace/index.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/* global Y, ace */
|
||||||
|
|
||||||
|
// initialize a shared object. This function call returns a promise!
|
||||||
|
Y({
|
||||||
|
db: {
|
||||||
|
name: 'memory'
|
||||||
|
},
|
||||||
|
connector: {
|
||||||
|
name: 'websockets-client',
|
||||||
|
room: 'ace-example-dev'
|
||||||
|
// debug: true
|
||||||
|
// url: 'http://127.0.0.1:2345'
|
||||||
|
},
|
||||||
|
sourceDir: '/bower_components',
|
||||||
|
share: {
|
||||||
|
ace: 'Text' // y.share.textarea is of type Y.Text
|
||||||
|
}
|
||||||
|
}).then(function (y) {
|
||||||
|
window.y = y
|
||||||
|
|
||||||
|
// bind the textarea to a shared text element
|
||||||
|
var editor = ace.edit('editor')
|
||||||
|
editor.setTheme('ace/theme/monokai')
|
||||||
|
editor.getSession().setMode('ace/mode/javascript')
|
||||||
|
|
||||||
|
y.share.ace.bindAce(editor)
|
||||||
|
// thats it..
|
||||||
|
})
|
||||||
@@ -4,13 +4,14 @@
|
|||||||
|
|
||||||
Y({
|
Y({
|
||||||
db: {
|
db: {
|
||||||
name: 'memory'
|
name: 'indexeddb',
|
||||||
|
idbVersion: 20
|
||||||
},
|
},
|
||||||
connector: {
|
connector: {
|
||||||
name: 'websockets-client',
|
name: 'websockets-client',
|
||||||
room: 'richtext-example18',
|
room: 'richtext-example24',
|
||||||
debug: true
|
debug: true
|
||||||
//url: 'http://127.0.0.1:2345'
|
// url: 'http://127.0.0.1:1234'
|
||||||
},
|
},
|
||||||
sourceDir: '/bower_components',
|
sourceDir: '/bower_components',
|
||||||
share: {
|
share: {
|
||||||
@@ -30,3 +31,4 @@ Y({
|
|||||||
// bind quill to richtext type
|
// bind quill to richtext type
|
||||||
y.share.richtext.bind(window.quill)
|
y.share.richtext.bind(window.quill)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
"y-websockets-client": "~0.7.10",
|
"y-websockets-client": "~0.7.10",
|
||||||
"y-text": "~0.7.1",
|
"y-text": "~0.7.1",
|
||||||
"y-indexeddb": "~0.7.1",
|
"y-indexeddb": "~0.7.1",
|
||||||
"quill": "~0.20.1"
|
"quill": "~0.20.1",
|
||||||
|
"ace": "~1.2.3",
|
||||||
|
"ace-builds": "~1.2.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
100
README.md
100
README.md
@@ -1,49 +1,57 @@
|
|||||||
|
|
||||||
# 
|
# 
|
||||||
|
|
||||||
Yjs is a framework for optimistic concurrency control and automatic conflict resolution on shared 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/).
|
Yjs is a framework for optimistic concurrency control and automatic conflict resolution on shared data. The framework provides similar functionality as [ShareJs] and [OpenCoweb], but supports peer-to-peer communication protocols by default. Yjs was designed to handle concurrent actions on arbitrary data like Text, Json, and XML. We also provide support for storing and manipulating your shared data offline. For more information and demo applications visit our [homepage](http://y-js.org/).
|
||||||
|
|
||||||
**NOTE** This project is currently migrating. So there may exist some information that is not true anymore..
|
**NOTE** This project is currently migrating. So there may exist some information that is not true anymore..
|
||||||
|
|
||||||
You can create you own shared 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
|
You can create you own shared types easily.
|
||||||
|
Therefore, you can design the sturcture of your custom type,
|
||||||
|
and ensure data validity, while Yjs ensures data consistency (everyone will eventually end up with the same data).
|
||||||
|
We already provide data types for
|
||||||
|
|
||||||
| Name | Description |
|
| Name | Description |
|
||||||
|----------|-------------------|
|
|----------|-------------------|
|
||||||
|[map](https://github.com/y-js/y-map) | Add, update, and remove properties of an object. Included in Yjs|
|
|[map](https://github.com/y-js/y-map) | Add, update, and remove properties of an object. Included in Yjs|
|
||||||
|[array](https://github.com/y-js/y-array) | A shared linked list implementation |
|
|[array](https://github.com/y-js/y-array) | A shared linked list implementation |
|
||||||
|[selections](https://github.com/y-js/y-selections) | Manages selections on types that use linear structures (e.g. the y-array type). Select a range of elements, and assign meaning to them.|
|
|
||||||
|[xml](https://github.com/y-js/y-xml) | An implementation of the DOM. You can create a two way binding to Browser DOM objects|
|
|[xml](https://github.com/y-js/y-xml) | An implementation of the DOM. You can create a two way binding to Browser DOM objects|
|
||||||
|[text](https://github.com/y-js/y-text) | Collaborate on text. Supports two way binding to textareas, input elements, or HTML elements (e.g. *h1*, or *p*)|
|
|[text](https://github.com/y-js/y-text) | Collaborate on text. Supports two way binding to textareas, input elements, or HTML elements (e.g. *h1*, or *p*)|
|
||||||
|[richtext](https://github.com/y-js/y-richtext) | Collaborate on rich text. Supports two way binding to several editors|
|
|[richtext](https://github.com/y-js/y-richtext) | Collaborate on rich text. Supports 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.
|
Yjs supports P2P message propagation, and 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:
|
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 |
|
|Name | Description |
|
||||||
|----------------|-----------------------------------|
|
|----------------|-----------------------------------|
|
||||||
|[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))|
|
|[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))|
|
||||||
|[webrtc](https://github.com/y-js/y-webrtc) | Propagate updates Browser2Browser via WebRTC|
|
|[webrtc](https://github.com/y-js/y-webrtc) | Propagate updates Browser2Browser via WebRTC|
|
||||||
|
|[websockets](https://github.com/y-js/y-websockets-client) | Exchange updates efficiently in the classical client-server model |
|
||||||
|[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|
|
|[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 are not limited to use a specific database to store the shared data. We provide the following database adapters:
|
||||||
|
|
||||||
|
|Name | Description |
|
||||||
|
|----------------|-----------------------------------|
|
||||||
|
|[memory](https://github.com/y-js/y-memory) | In-memory storage. |
|
||||||
|
|[IndexedDb](https://github.com/y-js/y-indexeddb) | Offline storage for the browser |
|
||||||
|
|
||||||
You can use Yjs client-, and server- side. You can get it as via npm, and bower. We even provide polymer elements for Yjs!
|
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
|
The advantages over similar frameworks are support for
|
||||||
* .. P2P message propagation and arbitrary communication protocols
|
* .. P2P message propagation and arbitrary communication protocols
|
||||||
* .. arbitrary complex data types
|
* .. arbitrary complex data types
|
||||||
* .. offline editing: Changes are stored persistently and only relevant changes are propagated on rejoin
|
* .. offline support: Changes are stored persistently and only relevant changes are propagated on rejoin
|
||||||
* .. AnyUndo: Undo *any* action that was executed in constant time (coming..)
|
|
||||||
* .. 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.
|
* .. 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!
|
## 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.
|
Install yjs and its modules with [bower](http://bower.io/), or with [npm](https://www.npmjs.org/package/yjs).
|
||||||
|
|
||||||
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
|
||||||
```
|
```
|
||||||
bower install y-js/yjs
|
bower install yjs
|
||||||
```
|
```
|
||||||
Then you include the libraries directly from the installation folder.
|
Then you include the libraries directly from the installation folder.
|
||||||
```
|
```
|
||||||
@@ -60,58 +68,30 @@ And use it like this with *npm*:
|
|||||||
Y = require("yjs");
|
Y = require("yjs");
|
||||||
```
|
```
|
||||||
|
|
||||||
# Y()
|
# Text editing example
|
||||||
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({
|
||||||
|
db: {
|
||||||
|
name: 'memory' // store in memory.
|
||||||
|
// name: 'indexeddb'
|
||||||
|
},
|
||||||
|
connector: {
|
||||||
|
name: 'websockets-client', // choose the websockets connector
|
||||||
|
// name: 'webrtc'
|
||||||
|
// name: 'xmpp'
|
||||||
|
room: 'Textarea-example-dev'
|
||||||
|
},
|
||||||
|
sourceDir: '/bower_components', // location of the y-* modules
|
||||||
|
share: {
|
||||||
|
textarea: 'Text' // y.share.textarea is of type Y.Text
|
||||||
|
}
|
||||||
|
// modules: ['Richtext', 'Array'] // optional list of modules you want to import
|
||||||
|
}).then(function (y) {
|
||||||
|
// bind the textarea to a shared text element
|
||||||
|
y.share.textarea.bind(document.getElementById('textfield'))
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
# Y.Map
|
|
||||||
Yjs includes only one type by default - the Y.Map type. It mimics the behaviour of a javascript Object. You can create, update, and remove properies on the Y.Map 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 map = y.set("new_map", Y.Map).then(function(map){
|
|
||||||
map // is my map type
|
|
||||||
});
|
|
||||||
```
|
|
||||||
* Every instance of Y is an Y.Map
|
|
||||||
```
|
|
||||||
var y = new Y(options);
|
|
||||||
```
|
|
||||||
* .get(name)
|
|
||||||
* Retrieve the value of a property. If the value is a type, `.get(name)` returns a promise
|
|
||||||
* .set(name, value)
|
|
||||||
* Set/update a property. `value` may be a primitive type, or a custom type definition (e.g. `Y.Map`)
|
|
||||||
* .delete(name)
|
|
||||||
* Delete a property
|
|
||||||
* .observe(observer)
|
|
||||||
* The `observer` is called whenever something on this object changes. Throws *add*, *update*, and *delete* events
|
|
||||||
* .observePath(path, observer)
|
|
||||||
* `path` is an array of property names. `observer` is called when the property under `path` is set, deleted, or updated
|
|
||||||
* .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
|
|
||||||
* .get(name)
|
|
||||||
* O(1)
|
|
||||||
* .set(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
|
# 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.
|
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.
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "yjs",
|
"name": "yjs",
|
||||||
"version": "0.8.5",
|
"version": "0.8.12",
|
||||||
"homepage": "y-js.org",
|
"homepage": "y-js.org",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Kevin Jahns <kevin.jahns@rwth-aachen.de>"
|
"Kevin Jahns <kevin.jahns@rwth-aachen.de>"
|
||||||
|
|||||||
594
y.es6
594
y.es6
@@ -76,19 +76,21 @@ module.exports = function (Y/* :any */) {
|
|||||||
this.userEventListeners.push(f)
|
this.userEventListeners.push(f)
|
||||||
}
|
}
|
||||||
userLeft (user) {
|
userLeft (user) {
|
||||||
delete this.connections[user]
|
if (this.connections[user] != null) {
|
||||||
if (user === this.currentSyncTarget) {
|
delete this.connections[user]
|
||||||
this.currentSyncTarget = null
|
if (user === this.currentSyncTarget) {
|
||||||
this.findNextSyncTarget()
|
this.currentSyncTarget = null
|
||||||
}
|
this.findNextSyncTarget()
|
||||||
this.syncingClients = this.syncingClients.filter(function (cli) {
|
}
|
||||||
return cli !== user
|
this.syncingClients = this.syncingClients.filter(function (cli) {
|
||||||
})
|
return cli !== user
|
||||||
for (var f of this.userEventListeners) {
|
|
||||||
f({
|
|
||||||
action: 'userLeft',
|
|
||||||
user: user
|
|
||||||
})
|
})
|
||||||
|
for (var f of this.userEventListeners) {
|
||||||
|
f({
|
||||||
|
action: 'userLeft',
|
||||||
|
user: user
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
userJoined (user, role) {
|
userJoined (user, role) {
|
||||||
@@ -172,6 +174,9 @@ module.exports = function (Y/* :any */) {
|
|||||||
Buffer operations, and broadcast them when ready.
|
Buffer operations, and broadcast them when ready.
|
||||||
*/
|
*/
|
||||||
broadcastOps (ops) {
|
broadcastOps (ops) {
|
||||||
|
ops = ops.map(function (op) {
|
||||||
|
return Y.Struct[op.struct].encode(op)
|
||||||
|
})
|
||||||
var self = this
|
var self = this
|
||||||
function broadcastOperations () {
|
function broadcastOperations () {
|
||||||
if (self.broadcastOpBuffer.length > 0) {
|
if (self.broadcastOpBuffer.length > 0) {
|
||||||
@@ -576,6 +581,13 @@ module.exports = function (Y /* :any */) {
|
|||||||
*/
|
*/
|
||||||
constructor (y, opts) {
|
constructor (y, opts) {
|
||||||
this.y = y
|
this.y = y
|
||||||
|
var os = this
|
||||||
|
this.userId = null
|
||||||
|
var resolve
|
||||||
|
this.userIdPromise = new Promise(function (r) {
|
||||||
|
resolve = r
|
||||||
|
})
|
||||||
|
this.userIdPromise.resolve = resolve
|
||||||
// whether to broadcast all applied operations (insert & delete hook)
|
// whether to broadcast all applied operations (insert & delete hook)
|
||||||
this.forwardAppliedOperations = false
|
this.forwardAppliedOperations = false
|
||||||
// E.g. this.listenersById[id] : Array<Listener>
|
// E.g. this.listenersById[id] : Array<Listener>
|
||||||
@@ -597,32 +609,42 @@ module.exports = function (Y /* :any */) {
|
|||||||
// TODO: Use ES7 Weak Maps. This way types that are no longer user,
|
// TODO: Use ES7 Weak Maps. This way types that are no longer user,
|
||||||
// wont be kept in memory.
|
// wont be kept in memory.
|
||||||
this.initializedTypes = {}
|
this.initializedTypes = {}
|
||||||
this.whenUserIdSetListener = null
|
|
||||||
this.waitingTransactions = []
|
this.waitingTransactions = []
|
||||||
this.transactionInProgress = false
|
this.transactionInProgress = false
|
||||||
|
this.transactionIsFlushed = false
|
||||||
if (typeof YConcurrency_TestingMode !== 'undefined') {
|
if (typeof YConcurrency_TestingMode !== 'undefined') {
|
||||||
this.executeOrder = []
|
this.executeOrder = []
|
||||||
}
|
}
|
||||||
this.gc1 = [] // first stage
|
this.gc1 = [] // first stage
|
||||||
this.gc2 = [] // second stage -> after that, remove the op
|
this.gc2 = [] // second stage -> after that, remove the op
|
||||||
this.gcTimeout = opts.gcTimeout || 5000
|
this.gcTimeout = opts.gcTimeout || 50000
|
||||||
var os = this
|
|
||||||
function garbageCollect () {
|
function garbageCollect () {
|
||||||
return new Promise((resolve) => {
|
return os.whenTransactionsFinished().then(function () {
|
||||||
os.requestTransaction(function * () {
|
if (os.gc1.length > 0 || os.gc2.length > 0) {
|
||||||
if (os.y.connector != null && os.y.connector.isSynced) {
|
return new Promise((resolve) => {
|
||||||
for (var i = 0; i < os.gc2.length; i++) {
|
os.requestTransaction(function * () {
|
||||||
var oid = os.gc2[i]
|
if (os.y.connector != null && os.y.connector.isSynced) {
|
||||||
yield* this.garbageCollectOperation(oid)
|
for (var i = 0; i < os.gc2.length; i++) {
|
||||||
}
|
var oid = os.gc2[i]
|
||||||
os.gc2 = os.gc1
|
yield* this.garbageCollectOperation(oid)
|
||||||
os.gc1 = []
|
}
|
||||||
}
|
os.gc2 = os.gc1
|
||||||
|
os.gc1 = []
|
||||||
|
}
|
||||||
|
// TODO: Use setInterval here instead (when garbageCollect is called several times there will be several timeouts..)
|
||||||
|
if (os.gcTimeout > 0) {
|
||||||
|
os.gcInterval = setTimeout(garbageCollect, os.gcTimeout)
|
||||||
|
}
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// TODO: see above
|
||||||
if (os.gcTimeout > 0) {
|
if (os.gcTimeout > 0) {
|
||||||
os.gcInterval = setTimeout(garbageCollect, os.gcTimeout)
|
os.gcInterval = setTimeout(garbageCollect, os.gcTimeout)
|
||||||
}
|
}
|
||||||
resolve()
|
return Promise.resolve()
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
this.garbageCollect = garbageCollect
|
this.garbageCollect = garbageCollect
|
||||||
@@ -701,26 +723,20 @@ module.exports = function (Y /* :any */) {
|
|||||||
this.gcInterval = null
|
this.gcInterval = null
|
||||||
}
|
}
|
||||||
setUserId (userId) {
|
setUserId (userId) {
|
||||||
var self = this
|
if (!this.userIdPromise.inProgress) {
|
||||||
return new Promise(function (resolve) {
|
this.userIdPromise.inProgress = true
|
||||||
|
var self = this
|
||||||
self.requestTransaction(function * () {
|
self.requestTransaction(function * () {
|
||||||
self.userId = userId
|
self.userId = userId
|
||||||
var state = yield* this.getState(userId)
|
var state = yield* this.getState(userId)
|
||||||
self.opClock = state.clock
|
self.opClock = state.clock
|
||||||
if (self.whenUserIdSetListener != null) {
|
self.userIdPromise.resolve(userId)
|
||||||
self.whenUserIdSetListener()
|
|
||||||
self.whenUserIdSetListener = null
|
|
||||||
}
|
|
||||||
resolve()
|
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
return this.userIdPromise
|
||||||
}
|
}
|
||||||
whenUserIdSet (f) {
|
whenUserIdSet (f) {
|
||||||
if (this.userId != null) {
|
this.userIdPromise.then(f)
|
||||||
f()
|
|
||||||
} else {
|
|
||||||
this.whenUserIdSetListener = f
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
getNextOpId () {
|
getNextOpId () {
|
||||||
if (this._nextUserId != null) {
|
if (this._nextUserId != null) {
|
||||||
@@ -742,8 +758,10 @@ module.exports = function (Y /* :any */) {
|
|||||||
apply (ops) {
|
apply (ops) {
|
||||||
for (var key in ops) {
|
for (var key in ops) {
|
||||||
var o = ops[key]
|
var o = ops[key]
|
||||||
var required = Y.Struct[o.struct].requiredOps(o)
|
if (o.id == null || o.id[0] !== this.y.connector.userId) {
|
||||||
this.whenOperationsExist(required, o)
|
var required = Y.Struct[o.struct].requiredOps(o)
|
||||||
|
this.whenOperationsExist(required, o)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
@@ -889,7 +907,7 @@ module.exports = function (Y /* :any */) {
|
|||||||
yield* Y.Struct['Delete'].execute.call(transaction, delop)
|
yield* Y.Struct['Delete'].execute.call(transaction, delop)
|
||||||
}
|
}
|
||||||
|
|
||||||
// notify parent, if it has been initialized as a custom type
|
// notify parent, if it was instanciated as a custom type
|
||||||
if (t != null) {
|
if (t != null) {
|
||||||
yield* t._changed(transaction, Y.utils.copyObject(op))
|
yield* t._changed(transaction, Y.utils.copyObject(op))
|
||||||
}
|
}
|
||||||
@@ -914,29 +932,36 @@ module.exports = function (Y /* :any */) {
|
|||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check if there is another transaction request.
|
||||||
|
// * the last transaction is always a flush :)
|
||||||
getNextRequest () {
|
getNextRequest () {
|
||||||
if (this.waitingTransactions.length === 0) {
|
if (this.waitingTransactions.length === 0) {
|
||||||
this.transactionInProgress = false
|
if (this.transactionIsFlushed) {
|
||||||
if (this.transactionsFinished != null) {
|
this.transactionInProgress = false
|
||||||
this.transactionsFinished.resolve()
|
this.transactionIsFlushed = false
|
||||||
this.transactionsFinished = null
|
if (this.transactionsFinished != null) {
|
||||||
|
this.transactionsFinished.resolve()
|
||||||
|
this.transactionsFinished = null
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
} else {
|
||||||
|
this.transactionIsFlushed = true
|
||||||
|
return function * () {
|
||||||
|
yield* this.flush()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
} else {
|
} else {
|
||||||
|
this.transactionIsFlushed = false
|
||||||
return this.waitingTransactions.shift()
|
return this.waitingTransactions.shift()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
requestTransaction (makeGen/* :any */, callImmediately) {
|
requestTransaction (makeGen/* :any */, callImmediately) {
|
||||||
if (true || callImmediately) { // TODO: decide whether this is ok or not..
|
this.waitingTransactions.push(makeGen)
|
||||||
this.waitingTransactions.push(makeGen)
|
if (!this.transactionInProgress) {
|
||||||
if (!this.transactionInProgress) {
|
this.transactionInProgress = true
|
||||||
this.transactionInProgress = true
|
if (false || callImmediately) { // TODO: decide whether this is ok or not..
|
||||||
this.transact(this.getNextRequest())
|
this.transact(this.getNextRequest())
|
||||||
}
|
} else {
|
||||||
} else {
|
|
||||||
this.waitingTransactions.push(makeGen)
|
|
||||||
if (!this.transactionInProgress) {
|
|
||||||
this.transactionInProgress = true
|
|
||||||
var self = this
|
var self = this
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
self.transact(self.getNextRequest())
|
self.transact(self.getNextRequest())
|
||||||
@@ -1077,6 +1102,19 @@ module.exports = function (Y/* :any */) {
|
|||||||
execute: function *(op) {
|
execute: function *(op) {
|
||||||
var i // loop counter
|
var i // loop counter
|
||||||
var distanceToOrigin = i = yield* Struct.Insert.getDistanceToOrigin.call(this, op) // most cases: 0 (starts from 0)
|
var distanceToOrigin = i = yield* Struct.Insert.getDistanceToOrigin.call(this, op) // most cases: 0 (starts from 0)
|
||||||
|
|
||||||
|
if (op.origin != null) { // TODO: !== instead of !=
|
||||||
|
// we save in origin that op originates in it
|
||||||
|
// we need that later when we eventually garbage collect origin (see transaction)
|
||||||
|
var origin = yield* this.getOperation(op.origin)
|
||||||
|
if (origin.originOf == null) {
|
||||||
|
origin.originOf = []
|
||||||
|
}
|
||||||
|
origin.originOf.push(op.id)
|
||||||
|
yield* this.setOperation(origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
// now we begin to insert op in the list of insertions..
|
||||||
var o
|
var o
|
||||||
var parent
|
var parent
|
||||||
var start
|
var start
|
||||||
@@ -1455,7 +1493,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
var callType = false
|
var callType = false
|
||||||
|
|
||||||
if (target == null || !target.deleted) {
|
if (target == null || !target.deleted) {
|
||||||
yield* this.markDeleted(targetId)
|
yield* this.markDeleted(targetId, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target != null && target.gc == null) {
|
if (target != null && target.gc == null) {
|
||||||
@@ -1527,86 +1565,124 @@ module.exports = function (Y/* :any */) {
|
|||||||
/*
|
/*
|
||||||
Mark an operation as deleted&gc'd
|
Mark an operation as deleted&gc'd
|
||||||
*/
|
*/
|
||||||
* markGarbageCollected (id) {
|
* markGarbageCollected (id, len) {
|
||||||
// this.mem.push(["gc", id]);
|
// this.mem.push(["gc", id]);
|
||||||
var n = yield* this.markDeleted(id)
|
var n = yield* this.markDeleted(id, len)
|
||||||
if (!n.gc) {
|
if (n.id[1] < id[1] && !n.gc) {
|
||||||
if (n.id[1] < id[1]) {
|
// un-extend left
|
||||||
// un-extend left
|
var newlen = n.len - (id[1] - n.id[1])
|
||||||
var newlen = n.len - (id[1] - n.id[1])
|
n.len -= newlen
|
||||||
n.len -= newlen
|
yield* this.ds.put(n)
|
||||||
yield* this.ds.put(n)
|
n = {id: id, len: newlen, gc: false}
|
||||||
n = {id: id, len: newlen, gc: false}
|
|
||||||
yield* this.ds.put(n)
|
|
||||||
}
|
|
||||||
// get prev&next before adding a new operation
|
|
||||||
var prev = yield* this.ds.findPrev(id)
|
|
||||||
var next = yield* this.ds.findNext(id)
|
|
||||||
|
|
||||||
if (id[1] < n.id[1] + n.len - 1) {
|
|
||||||
// un-extend right
|
|
||||||
yield* this.ds.put({id: [id[0], id[1] + 1], len: n.len - 1, gc: false})
|
|
||||||
n.len = 1
|
|
||||||
}
|
|
||||||
// set gc'd
|
|
||||||
n.gc = true
|
|
||||||
// can extend left?
|
|
||||||
if (
|
|
||||||
prev != null &&
|
|
||||||
prev.gc &&
|
|
||||||
Y.utils.compareIds([prev.id[0], prev.id[1] + prev.len], n.id)
|
|
||||||
) {
|
|
||||||
prev.len += n.len
|
|
||||||
yield* this.ds.delete(n.id)
|
|
||||||
n = prev
|
|
||||||
// ds.put n here?
|
|
||||||
}
|
|
||||||
// can extend right?
|
|
||||||
if (
|
|
||||||
next != null &&
|
|
||||||
next.gc &&
|
|
||||||
Y.utils.compareIds([n.id[0], n.id[1] + n.len], next.id)
|
|
||||||
) {
|
|
||||||
n.len += next.len
|
|
||||||
yield* this.ds.delete(next.id)
|
|
||||||
}
|
|
||||||
yield* this.ds.put(n)
|
yield* this.ds.put(n)
|
||||||
}
|
}
|
||||||
|
// get prev&next before adding a new operation
|
||||||
|
var prev = yield* this.ds.findPrev(id)
|
||||||
|
var next = yield* this.ds.findNext(id)
|
||||||
|
|
||||||
|
if (id[1] < n.id[1] + n.len - len && !n.gc) {
|
||||||
|
// un-extend right
|
||||||
|
yield* this.ds.put({id: [id[0], id[1] + 1], len: n.len - 1, gc: false})
|
||||||
|
n.len = 1
|
||||||
|
}
|
||||||
|
// set gc'd
|
||||||
|
n.gc = true
|
||||||
|
// can extend left?
|
||||||
|
if (
|
||||||
|
prev != null &&
|
||||||
|
prev.gc &&
|
||||||
|
Y.utils.compareIds([prev.id[0], prev.id[1] + prev.len], n.id)
|
||||||
|
) {
|
||||||
|
prev.len += n.len
|
||||||
|
yield* this.ds.delete(n.id)
|
||||||
|
n = prev
|
||||||
|
// ds.put n here?
|
||||||
|
}
|
||||||
|
// can extend right?
|
||||||
|
if (
|
||||||
|
next != null &&
|
||||||
|
next.gc &&
|
||||||
|
Y.utils.compareIds([n.id[0], n.id[1] + n.len], next.id)
|
||||||
|
) {
|
||||||
|
n.len += next.len
|
||||||
|
yield* this.ds.delete(next.id)
|
||||||
|
}
|
||||||
|
yield* this.ds.put(n)
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Mark an operation as deleted.
|
Mark an operation as deleted.
|
||||||
|
|
||||||
returns the delete node
|
returns the delete node
|
||||||
*/
|
*/
|
||||||
* markDeleted (id) {
|
* markDeleted (id, length) {
|
||||||
|
if (length == null) {
|
||||||
|
length = 1
|
||||||
|
}
|
||||||
// this.mem.push(["del", id]);
|
// this.mem.push(["del", id]);
|
||||||
var n = yield* this.ds.findWithUpperBound(id)
|
var n = yield* this.ds.findWithUpperBound(id)
|
||||||
if (n != null && n.id[0] === id[0]) {
|
if (n != null && n.id[0] === id[0]) {
|
||||||
if (n.id[1] <= id[1] && id[1] < n.id[1] + n.len) {
|
if (n.id[1] <= id[1] && id[1] <= n.id[1] + n.len) {
|
||||||
// already deleted
|
// id is in n's range
|
||||||
return n
|
var diff = id[1] + length - (n.id[1] + n.len) // overlapping right
|
||||||
} else if (n.id[1] + n.len === id[1] && !n.gc) {
|
if (diff > 0) {
|
||||||
// can extend existing deletion
|
// id+length overlaps n
|
||||||
n.len++
|
if (!n.gc) {
|
||||||
|
n.len += diff
|
||||||
|
} else {
|
||||||
|
diff = n.id[1] + n.len - id[1] // overlapping left (id till n.end)
|
||||||
|
if (diff < length) {
|
||||||
|
// a partial deletion
|
||||||
|
n = {id: [id[0], id[1] + diff], len: length - diff, gc: false}
|
||||||
|
yield* this.ds.put(n)
|
||||||
|
} else {
|
||||||
|
// already gc'd
|
||||||
|
throw new Error('Cannot happen! (it dit though.. :()')
|
||||||
|
// return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// no overlapping, already deleted
|
||||||
|
return n
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// cannot extend left
|
// cannot extend left (there is no left!)
|
||||||
n = {id: id, len: 1, gc: false}
|
n = {id: id, len: length, gc: false}
|
||||||
yield* this.ds.put(n)
|
yield* this.ds.put(n) // TODO: you double-put !!
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// cannot extend left
|
// cannot extend left
|
||||||
n = {id: id, len: 1, gc: false}
|
n = {id: id, len: length, gc: false}
|
||||||
yield* this.ds.put(n)
|
yield* this.ds.put(n)
|
||||||
}
|
}
|
||||||
// can extend right?
|
// can extend right?
|
||||||
var next = yield* this.ds.findNext(n.id)
|
var next = yield* this.ds.findNext(n.id)
|
||||||
if (
|
if (
|
||||||
next != null &&
|
next != null &&
|
||||||
Y.utils.compareIds([n.id[0], n.id[1] + n.len], next.id) &&
|
n.id[0] === next.id[0] &&
|
||||||
!next.gc
|
n.id[1] + n.len >= next.id[1]
|
||||||
) {
|
) {
|
||||||
n.len = n.len + next.len
|
diff = n.id[1] + n.len - next.id[1] // from next.start to n.end
|
||||||
yield* this.ds.delete(next.id)
|
if (next.gc) {
|
||||||
|
if (diff >= 0) {
|
||||||
|
n.len -= diff
|
||||||
|
if (diff > next.len) {
|
||||||
|
// need to create another deletion after $next
|
||||||
|
// TODO: (may not be necessary, because this case shouldn't happen!)
|
||||||
|
// also this is supposed to return a deletion range. which one to choose? n or the new created deletion?
|
||||||
|
throw new Error('This case is not handled (on purpose!)')
|
||||||
|
}
|
||||||
|
} // else: everything is fine :)
|
||||||
|
} else {
|
||||||
|
if (diff >= 0) {
|
||||||
|
if (diff > next.len) {
|
||||||
|
// may be neccessary to extend next.next!
|
||||||
|
// TODO: (may not be necessary, because this case shouldn't happen!)
|
||||||
|
throw new Error('This case is not handled (on purpose!)')
|
||||||
|
}
|
||||||
|
n.len += next.len - diff
|
||||||
|
yield* this.ds.delete(next.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
yield* this.ds.put(n)
|
yield* this.ds.put(n)
|
||||||
return n
|
return n
|
||||||
@@ -1635,19 +1711,9 @@ module.exports = function (Y/* :any */) {
|
|||||||
*/
|
*/
|
||||||
* garbageCollectOperation (id) {
|
* garbageCollectOperation (id) {
|
||||||
this.store.addToDebug('yield* this.garbageCollectOperation(', id, ')')
|
this.store.addToDebug('yield* this.garbageCollectOperation(', id, ')')
|
||||||
// check to increase the state of the respective user
|
|
||||||
var state = yield* this.getState(id[0])
|
|
||||||
if (state.clock === id[1]) {
|
|
||||||
state.clock++
|
|
||||||
// also check if more expected operations were gc'd
|
|
||||||
yield* this.checkDeleteStoreForState(state)
|
|
||||||
// then set the state
|
|
||||||
yield* this.setState(state)
|
|
||||||
}
|
|
||||||
yield* this.markGarbageCollected(id)
|
|
||||||
|
|
||||||
// if op exists, then clean that mess up..
|
|
||||||
var o = yield* this.getOperation(id)
|
var o = yield* this.getOperation(id)
|
||||||
|
yield* this.markGarbageCollected(id, 1) // always mark gc'd
|
||||||
|
// if op exists, then clean that mess up..
|
||||||
if (o != null) {
|
if (o != null) {
|
||||||
/*
|
/*
|
||||||
if (!o.deleted) {
|
if (!o.deleted) {
|
||||||
@@ -1667,23 +1733,31 @@ module.exports = function (Y/* :any */) {
|
|||||||
if (o.right != null) {
|
if (o.right != null) {
|
||||||
var right = yield* this.getOperation(o.right)
|
var right = yield* this.getOperation(o.right)
|
||||||
right.left = o.left
|
right.left = o.left
|
||||||
if (Y.utils.compareIds(right.origin, o.id)) { // rights origin is o
|
|
||||||
|
if (o.originOf != null && o.originOf.length > 0) {
|
||||||
// find new origin of right ops
|
// find new origin of right ops
|
||||||
// origin is the first left deleted operation
|
// origin is the first left deleted operation
|
||||||
var neworigin = o.left
|
var neworigin = o.left
|
||||||
|
var neworigin_ = null
|
||||||
while (neworigin != null) {
|
while (neworigin != null) {
|
||||||
var neworigin_ = yield* this.getOperation(neworigin)
|
neworigin_ = yield* this.getOperation(neworigin)
|
||||||
if (neworigin_.deleted) {
|
if (neworigin_.deleted) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
neworigin = neworigin_.left
|
neworigin = neworigin_.left
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset origin of all right ops (except first right - duh!),
|
||||||
|
|
||||||
|
/* ** The following code does not rely on the the originOf property **
|
||||||
|
I recently added originOf to all Insert Operations (see Struct.Insert.execute),
|
||||||
|
which saves which operations originate in a Insert operation.
|
||||||
|
Garbage collecting without originOf is more memory efficient, but is nearly impossible for large texts, or lists!
|
||||||
|
But I keep this code for now
|
||||||
|
```
|
||||||
// reset origin of right
|
// reset origin of right
|
||||||
right.origin = neworigin
|
right.origin = neworigin
|
||||||
|
// search until you find origin pointer to the left of o
|
||||||
// reset origin of all right ops (except first right - duh!),
|
|
||||||
// until you find origin pointer to the left of o
|
|
||||||
if (right.right != null) {
|
if (right.right != null) {
|
||||||
var i = yield* this.getOperation(right.right)
|
var i = yield* this.getOperation(right.right)
|
||||||
var ids = [o.id, o.right]
|
var ids = [o.id, o.right]
|
||||||
@@ -1704,9 +1778,41 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} /* otherwise, rights origin is to the left of o,
|
```
|
||||||
then there is no right op (from o), that origins in o */
|
*/
|
||||||
yield* this.setOperation(right)
|
// ** Now the new implementation starts **
|
||||||
|
// reset neworigin of all originOf[*]
|
||||||
|
for (var _i in o.originOf) {
|
||||||
|
var originsIn = yield* this.getOperation(o.originOf[_i])
|
||||||
|
if (originsIn != null) {
|
||||||
|
originsIn.origin = neworigin
|
||||||
|
yield* this.setOperation(originsIn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (neworigin != null) {
|
||||||
|
if (neworigin_.originOf == null) {
|
||||||
|
neworigin_.originOf = o.originOf
|
||||||
|
} else {
|
||||||
|
neworigin_.originOf = o.originOf.concat(neworigin_.originOf)
|
||||||
|
}
|
||||||
|
yield* this.setOperation(neworigin_)
|
||||||
|
}
|
||||||
|
// we don't need to set right here, because
|
||||||
|
// right should be in o.originOf => it is set it the previous for loop
|
||||||
|
} else {
|
||||||
|
// we didn't need to reset the origin of right
|
||||||
|
// so we have to set right here
|
||||||
|
yield* this.setOperation(right)
|
||||||
|
}
|
||||||
|
// o may originate in another operation.
|
||||||
|
// Since o is deleted, we have to reset o.origin's `originOf` property
|
||||||
|
if (o.origin != null) {
|
||||||
|
var origin = yield* this.getOperation(o.origin)
|
||||||
|
origin.originOf = origin.originOf.filter(function (_id) {
|
||||||
|
return !Y.utils.compareIds(id, _id)
|
||||||
|
})
|
||||||
|
yield* this.setOperation(origin)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.parent != null) {
|
if (o.parent != null) {
|
||||||
@@ -1751,9 +1857,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
* applyDeleteSet (ds) {
|
* applyDeleteSet (ds) {
|
||||||
var deletions = []
|
var deletions = []
|
||||||
function createDeletions (user, start, len, gc) {
|
function createDeletions (user, start, len, gc) {
|
||||||
for (var c = start; c < start + len; c++) {
|
deletions.push([user, start, len, gc])
|
||||||
deletions.push([user, c, gc])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var user in ds) {
|
for (var user in ds) {
|
||||||
@@ -1808,23 +1912,46 @@ module.exports = function (Y/* :any */) {
|
|||||||
}
|
}
|
||||||
for (var i = 0; i < deletions.length; i++) {
|
for (var i = 0; i < deletions.length; i++) {
|
||||||
var del = deletions[i]
|
var del = deletions[i]
|
||||||
var id = [del[0], del[1]]
|
|
||||||
// always try to delete..
|
// always try to delete..
|
||||||
var addOperation = yield* this.deleteOperation(id)
|
var state = yield* this.getState(del[0])
|
||||||
if (addOperation) {
|
if (del[1] < state.clock) {
|
||||||
// TODO:.. really .. here? You could prevent calling all these functions in operationAdded
|
for (let c = del[1]; c < del[1] + del[2]; c++) {
|
||||||
yield* this.store.operationAdded(this, {struct: 'Delete', target: id})
|
var id = [del[0], c]
|
||||||
|
var addOperation = yield* this.deleteOperation(id)
|
||||||
|
if (addOperation) {
|
||||||
|
// TODO:.. really .. here? You could prevent calling all these functions in operationAdded
|
||||||
|
yield* this.store.operationAdded(this, {struct: 'Delete', target: id})
|
||||||
|
}
|
||||||
|
if (del[3]) {
|
||||||
|
// gc
|
||||||
|
yield* this.garbageCollectOperation(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (del[3]) {
|
||||||
|
yield* this.markGarbageCollected([del[0], del[1]], del[2])
|
||||||
|
} else {
|
||||||
|
yield* this.markDeleted([del[0], del[1]], del[2])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (del[2]) {
|
if (del[3]) {
|
||||||
// gc
|
// check to increase the state of the respective user
|
||||||
yield* this.garbageCollectOperation(id)
|
if (state.clock >= del[1] && state.clock < del[1] + del[2]) {
|
||||||
|
state.clock = del[1] + del[2]
|
||||||
|
// also check if more expected operations were gc'd
|
||||||
|
yield* this.checkDeleteStoreForState(state) // TODO: unneccessary?
|
||||||
|
// then set the state
|
||||||
|
yield* this.setState(state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.store.forwardAppliedOperations) {
|
if (this.store.forwardAppliedOperations) {
|
||||||
var ops = deletions.map(function (d) {
|
for (let c = del[1]; c < del[1] + del[2]; c++) {
|
||||||
return {struct: 'Delete', target: [d[0], d[1]]}
|
var ops = deletions.map(function (d) {
|
||||||
})
|
return {struct: 'Delete', target: [d[0], c]} // TODO: implement Delete with deletion length!
|
||||||
this.store.y.connector.broadcastOps(ops)
|
})
|
||||||
|
this.store.y.connector.broadcastOps(ops)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
* isGarbageCollected (id) {
|
* isGarbageCollected (id) {
|
||||||
@@ -2068,6 +2195,11 @@ module.exports = function (Y/* :any */) {
|
|||||||
return op
|
return op
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
* flush () {
|
||||||
|
yield* this.os.flush()
|
||||||
|
yield* this.ss.flush()
|
||||||
|
yield* this.ds.flush()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Y.Transaction = TransactionInterface
|
Y.Transaction = TransactionInterface
|
||||||
}
|
}
|
||||||
@@ -2095,7 +2227,7 @@ module.exports = function (Y/* :any */) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
The structures usually work asynchronously (you have to wait for the
|
The structures usually work asynchronously (you have to wait for the
|
||||||
database request to finish). EventHandler will help you to make your type
|
database request to finish). EventHandler helps you to make your type
|
||||||
synchronous.
|
synchronous.
|
||||||
*/
|
*/
|
||||||
module.exports = function (Y /* : any*/) {
|
module.exports = function (Y /* : any*/) {
|
||||||
@@ -2282,7 +2414,7 @@ module.exports = function (Y /* : any*/) {
|
|||||||
Defines a smaller relation on Id's
|
Defines a smaller relation on Id's
|
||||||
*/
|
*/
|
||||||
function smaller (a, b) {
|
function smaller (a, b) {
|
||||||
return a[0] < b[0] || (a[0] === b[0] && a[1] < b[1])
|
return a[0] < b[0] || (a[0] === b[0] && (a[1] < b[1] || typeof a[1] < typeof b[1]))
|
||||||
}
|
}
|
||||||
Y.utils.smaller = smaller
|
Y.utils.smaller = smaller
|
||||||
|
|
||||||
@@ -2300,6 +2432,159 @@ module.exports = function (Y /* : any*/) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Y.utils.compareIds = compareIds
|
Y.utils.compareIds = compareIds
|
||||||
|
|
||||||
|
function createEmptyOpsArray (n) {
|
||||||
|
var a = new Array(n)
|
||||||
|
for (var i = 0; i < a.length; i++) {
|
||||||
|
a[i] = {
|
||||||
|
id: [null, null]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSmallLookupBuffer (Store) {
|
||||||
|
/*
|
||||||
|
This buffer implements a very small buffer that temporarily stores operations
|
||||||
|
after they are read / before they are written.
|
||||||
|
The buffer basically implements FIFO. Often requested lookups will be re-queued every time they are looked up / written.
|
||||||
|
|
||||||
|
It can speed up lookups on Operation Stores and State Stores. But it does not require notable use of memory or processing power.
|
||||||
|
|
||||||
|
Good for os and ss, bot not for ds (because it often uses methods that require a flush)
|
||||||
|
|
||||||
|
I tried to optimize this for performance, therefore no highlevel operations.
|
||||||
|
*/
|
||||||
|
class SmallLookupBuffer extends Store {
|
||||||
|
constructor () {
|
||||||
|
super(...arguments)
|
||||||
|
this.writeBuffer = createEmptyOpsArray(5)
|
||||||
|
this.readBuffer = createEmptyOpsArray(10)
|
||||||
|
}
|
||||||
|
* find (id) {
|
||||||
|
var i, r
|
||||||
|
for (i = this.readBuffer.length - 1; i >= 0; i--) {
|
||||||
|
r = this.readBuffer[i]
|
||||||
|
// we don't have to use compareids, because id is always defined!
|
||||||
|
if (r.id[1] === id[1] && r.id[0] === id[0]) {
|
||||||
|
// found r
|
||||||
|
// move r to the end of readBuffer
|
||||||
|
for (; i < this.readBuffer.length - 1; i++) {
|
||||||
|
this.readBuffer[i] = this.readBuffer[i + 1]
|
||||||
|
}
|
||||||
|
this.readBuffer[this.readBuffer.length - 1] = r
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var o
|
||||||
|
for (i = this.writeBuffer.length - 1; i >= 0; i--) {
|
||||||
|
r = this.writeBuffer[i]
|
||||||
|
if (r.id[1] === id[1] && r.id[0] === id[0]) {
|
||||||
|
o = r
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i < 0) {
|
||||||
|
// did not reach break in last loop
|
||||||
|
// read id and put it to the end of readBuffer
|
||||||
|
o = yield* super.find(id)
|
||||||
|
}
|
||||||
|
if (o != null) {
|
||||||
|
for (i = 0; i < this.readBuffer.length - 1; i++) {
|
||||||
|
this.readBuffer[i] = this.readBuffer[i + 1]
|
||||||
|
}
|
||||||
|
this.readBuffer[this.readBuffer.length - 1] = o
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
* put (o) {
|
||||||
|
var id = o.id
|
||||||
|
var i, r // helper variables
|
||||||
|
for (i = this.writeBuffer.length - 1; i >= 0; i--) {
|
||||||
|
r = this.writeBuffer[i]
|
||||||
|
if (r.id[1] === id[1] && r.id[0] === id[0]) {
|
||||||
|
// is already in buffer
|
||||||
|
// forget r, and move o to the end of writeBuffer
|
||||||
|
for (; i < this.writeBuffer.length - 1; i++) {
|
||||||
|
this.writeBuffer[i] = this.writeBuffer[i + 1]
|
||||||
|
}
|
||||||
|
this.writeBuffer[this.writeBuffer.length - 1] = o
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i < 0) {
|
||||||
|
// did not reach break in last loop
|
||||||
|
// write writeBuffer[0]
|
||||||
|
var write = this.writeBuffer[0]
|
||||||
|
if (write.id[0] !== null) {
|
||||||
|
yield* super.put(write)
|
||||||
|
}
|
||||||
|
// put o to the end of writeBuffer
|
||||||
|
for (i = 0; i < this.writeBuffer.length - 1; i++) {
|
||||||
|
this.writeBuffer[i] = this.writeBuffer[i + 1]
|
||||||
|
}
|
||||||
|
this.writeBuffer[this.writeBuffer.length - 1] = o
|
||||||
|
}
|
||||||
|
// check readBuffer for every occurence of o.id, overwrite if found
|
||||||
|
// whether found or not, we'll append o to the readbuffer
|
||||||
|
for (i = 0; i < this.readBuffer.length - 1; i++) {
|
||||||
|
r = this.readBuffer[i + 1]
|
||||||
|
if (r.id[1] === id[1] && r.id[0] === id[0]) {
|
||||||
|
this.readBuffer[i] = o
|
||||||
|
} else {
|
||||||
|
this.readBuffer[i] = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.readBuffer[this.readBuffer.length - 1] = o
|
||||||
|
}
|
||||||
|
* delete (id) {
|
||||||
|
var i, r
|
||||||
|
for (i = 0; i < this.readBuffer.length; i++) {
|
||||||
|
r = this.readBuffer[i]
|
||||||
|
if (r.id[1] === id[1] && r.id[0] === id[0]) {
|
||||||
|
this.readBuffer[i] = {
|
||||||
|
id: [null, null]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yield* this.flush()
|
||||||
|
yield* super.delete(id)
|
||||||
|
}
|
||||||
|
* findWithLowerBound () {
|
||||||
|
yield* this.flush()
|
||||||
|
return yield* super.findWithLowerBound.apply(this, arguments)
|
||||||
|
}
|
||||||
|
* findWithUpperBound () {
|
||||||
|
yield* this.flush()
|
||||||
|
return yield* super.findWithUpperBound.apply(this, arguments)
|
||||||
|
}
|
||||||
|
* findNext () {
|
||||||
|
yield* this.flush()
|
||||||
|
return yield* super.findNext.apply(this, arguments)
|
||||||
|
}
|
||||||
|
* findPrev () {
|
||||||
|
yield* this.flush()
|
||||||
|
return yield* super.findPrev.apply(this, arguments)
|
||||||
|
}
|
||||||
|
* iterate () {
|
||||||
|
yield* this.flush()
|
||||||
|
yield* super.iterate.apply(this, arguments)
|
||||||
|
}
|
||||||
|
* flush () {
|
||||||
|
for (var i = 0; i < this.writeBuffer.length; i++) {
|
||||||
|
var write = this.writeBuffer[i]
|
||||||
|
if (write.id[0] !== null) {
|
||||||
|
yield* super.put(write)
|
||||||
|
this.writeBuffer[i] = {
|
||||||
|
id: [null, null]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SmallLookupBuffer
|
||||||
|
}
|
||||||
|
Y.utils.createSmallLookupBuffer = createSmallLookupBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
},{}],7:[function(require,module,exports){
|
},{}],7:[function(require,module,exports){
|
||||||
@@ -2415,9 +2700,9 @@ class YConfig {
|
|||||||
share: {[key: string]: any};
|
share: {[key: string]: any};
|
||||||
*/
|
*/
|
||||||
constructor (opts, callback) {
|
constructor (opts, callback) {
|
||||||
|
this.options = opts
|
||||||
this.db = new Y[opts.db.name](this, opts.db)
|
this.db = new Y[opts.db.name](this, opts.db)
|
||||||
this.connector = new Y[opts.connector.name](this, opts.connector)
|
this.connector = new Y[opts.connector.name](this, opts.connector)
|
||||||
this.options = opts
|
|
||||||
}
|
}
|
||||||
init (callback) {
|
init (callback) {
|
||||||
var opts = this.options
|
var opts = this.options
|
||||||
@@ -2436,7 +2721,8 @@ class YConfig {
|
|||||||
}
|
}
|
||||||
share[propertyname] = yield* this.getType(id)
|
share[propertyname] = yield* this.getType(id)
|
||||||
}
|
}
|
||||||
setTimeout(callback, 0)
|
this.store.whenTransactionsFinished()
|
||||||
|
.then(callback)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
isConnected () {
|
isConnected () {
|
||||||
|
|||||||
Reference in New Issue
Block a user