Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8dd7d1862 | ||
|
|
9e9f238b12 | ||
|
|
723fc77627 | ||
|
|
9eac8d9398 | ||
|
|
0b14e90585 | ||
|
|
4726c71dd0 | ||
|
|
59d859b38b |
@@ -43,9 +43,7 @@ Y({
|
||||
// call drawLine every time an array is appended
|
||||
y.share.drawing.observe(function (event) {
|
||||
if (event.type === 'insert') {
|
||||
event.values().then(function (values) {
|
||||
values.forEach(drawLine)
|
||||
})
|
||||
event.values.forEach(drawLine)
|
||||
} else {
|
||||
// just remove all elements (thats what we do anyway)
|
||||
svg.selectAll('path').remove()
|
||||
@@ -53,7 +51,7 @@ Y({
|
||||
})
|
||||
// draw all existing content
|
||||
for (var i = 0; i < drawing.length; i++) {
|
||||
drawing.get(i).then(drawLine)
|
||||
drawLine(drawing.get(i))
|
||||
}
|
||||
|
||||
// clear canvas on request
|
||||
@@ -64,9 +62,7 @@ Y({
|
||||
var sharedLine = null
|
||||
function dragstart () {
|
||||
drawing.insert(drawing.length, [Y.Array])
|
||||
drawing.get(drawing.length - 1).then(function (array) {
|
||||
sharedLine = array
|
||||
})
|
||||
sharedLine = drawing.get(drawing.length - 1)
|
||||
}
|
||||
|
||||
// After one dragged event is recognized, we ignore them for 33ms.
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="../bower_components/quill/dist/quill.snow.css" />
|
||||
<!-- quill does not include dist files! We are using the hosted version instead -->
|
||||
<!--link rel="stylesheet" href="../bower_components/quill/dist/quill.snow.css" /-->
|
||||
<link href="https://cdn.quilljs.com/1.0.4/quill.snow.css" rel="stylesheet">
|
||||
<link href="//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css" rel="stylesheet">
|
||||
<link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/styles/monokai-sublime.min.css" rel="stylesheet">
|
||||
<style>
|
||||
@@ -17,10 +19,12 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Include the Quill library -->
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.js" type="text/javascript"></script>
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/highlight.min.js" type="text/javascript"></script>
|
||||
<script src="https://cdn.quilljs.com/1.0.4/quill.js"></script>
|
||||
<!-- quill does not include dist files! We are using the hosted version instead (see above)
|
||||
<script src="../bower_components/quill/dist/quill.js"></script>
|
||||
-->
|
||||
<script src="../bower_components/yjs/y.es6"></script>
|
||||
<script src="./index.js"></script>
|
||||
</body>
|
||||
|
||||
@@ -7,8 +7,8 @@ Y({
|
||||
name: 'memory'
|
||||
},
|
||||
connector: {
|
||||
name: 'webrtc',
|
||||
room: 'richtext-example-quill-beta'
|
||||
name: 'websockets-client',
|
||||
room: 'richtext-example-quill-1.0-test'
|
||||
},
|
||||
sourceDir: '/bower_components',
|
||||
share: {
|
||||
@@ -38,3 +38,15 @@ Y({
|
||||
y.share.richtext.bind(window.quill)
|
||||
})
|
||||
|
||||
Y({
|
||||
db: {
|
||||
name: 'indexeddb'
|
||||
},
|
||||
connector: {
|
||||
name: 'websockets-client',
|
||||
room: 'test42'
|
||||
},
|
||||
share: {
|
||||
state : 'Map'
|
||||
}
|
||||
}).then((y) => { window.y = y })
|
||||
@@ -16,8 +16,6 @@ Y({
|
||||
}).then(function (y) {
|
||||
window.yXml = y
|
||||
// bind xml type to a dom, and put it in body
|
||||
y.share.xml.getDom().then(function (dom) {
|
||||
window.sharedDom = dom
|
||||
document.body.appendChild(dom)
|
||||
})
|
||||
window.sharedDom = y.share.xml.getDom()
|
||||
document.body.appendChild(window.sharedDom)
|
||||
})
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
"y-text": "latest",
|
||||
"y-indexeddb": "latest",
|
||||
"y-xml": "latest",
|
||||
"quill": "~0.20.1",
|
||||
"quill": "^1.0.0-rc.2",
|
||||
"ace": "~1.2.3",
|
||||
"ace-builds": "~1.2.3",
|
||||
"jquery": "~2.2.2",
|
||||
|
||||
204
README.md
204
README.md
@@ -1,98 +1,137 @@
|
||||
|
||||
# 
|
||||
|
||||
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/).
|
||||
Yjs is a framework for p2p shared editing on structured data like (rich-)text, json, and XML.
|
||||
It is similar to [ShareJs] and [OpenCoweb], but easy to use.
|
||||
For additional information, demos, and tutorials visit [y-js.org](http://y-js.org/).
|
||||
|
||||
You can create you own shared types easily.
|
||||
Therefore, you can design the structure 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 abstract data types for
|
||||
### Extensions
|
||||
Yjs only knows how to resolve conflicts on shared data. You have to choose a ..
|
||||
* *Connector* - a communication protocol that propagates changes to the clients
|
||||
* *Database* - a database to store your changes
|
||||
* one or more *Types* - that represent the shared data
|
||||
|
||||
Connectors, Databases, and Types are available as modules that extend Yjs. Here is a list of the modules we know of:
|
||||
|
||||
##### Connectors
|
||||
|
||||
|Name | Description |
|
||||
|----------------|-----------------------------------|
|
||||
|[webrtc](https://github.com/y-js/y-webrtc) | Propagate updates Browser2Browser via WebRTC|
|
||||
|[websockets](https://github.com/y-js/y-websockets-client) | Set up [a central server](https://github.com/y-js/y-websockets-client), and connect to it via websockets |
|
||||
|[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))|
|
||||
|[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|
|
||||
|
||||
##### 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 |
|
||||
|[leveldb](https://github.com/y-js/y-leveldb) | Persistent storage for node apps |
|
||||
|
||||
|
||||
##### Types
|
||||
|
||||
| Name | Description |
|
||||
|----------|-------------------|
|
||||
|[map](https://github.com/y-js/y-map) | A shared Map implementation. Maps from text to any stringify-able object |
|
||||
|[array](https://github.com/y-js/y-array) | A shared Array implementation |
|
||||
|[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*>). Also supports the [Ace Editor](https://ace.c9.io) |
|
||||
|[text](https://github.com/y-js/y-text) | Collaborate on text. Supports two way binding to the [Ace Editor](https://ace.c9.io), textareas, input elements, and HTML elements (e.g. <*h1*>, or <*p*>) |
|
||||
|[richtext](https://github.com/y-js/y-richtext) | Collaborate on rich text. Supports two way binding to the [Quill Rich Text Editor](http://quilljs.com/)|
|
||||
|
||||
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 |
|
||||
|----------------|-----------------------------------|
|
||||
|[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|
|
||||
|[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|
|
||||
|
||||
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 |
|
||||
|
||||
The advantages over similar frameworks are support for
|
||||
* .. P2P message propagation and arbitrary communication protocols
|
||||
* .. share any type of data. The types provide a convenient interface
|
||||
* .. offline support: Changes are stored persistently and only relevant changes are propagated on rejoin
|
||||
* .. 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!
|
||||
Install yjs and its modules with [bower](http://bower.io/), or with [npm](https://www.npmjs.org/package/yjs).
|
||||
## Use it!
|
||||
Install Yjs, and its modules with [bower](http://bower.io/), or [npm](https://www.npmjs.org/package/yjs).
|
||||
|
||||
### Bower
|
||||
```
|
||||
bower install yjs --save
|
||||
bower install --save yjs y-array % add all y-* modules you want to use
|
||||
```
|
||||
Then you include the libraries directly from the installation folder.
|
||||
You only need to include the `y.js` file. Yjs is able to automatically require missing modules.
|
||||
```
|
||||
<script src="./bower_components/yjs/y.js"></script>
|
||||
```
|
||||
|
||||
### Npm
|
||||
```
|
||||
npm install yjs --save
|
||||
npm install --save yjs % add all y-* modules you want to use
|
||||
```
|
||||
|
||||
And use it like this with *npm*:
|
||||
If you don't include via script tag, you have to explicitly include all modules! (Same goes for other module systems)
|
||||
```
|
||||
Y = require("yjs");
|
||||
var Y = require('yjs')
|
||||
require('y-array')(Y) // add the y-array type to Yjs
|
||||
require('y-websockets-client')(Y)
|
||||
require('y-memory')(Y)
|
||||
require('y-array')(Y)
|
||||
require('y-map')(Y)
|
||||
require('y-text')(Y)
|
||||
// ..
|
||||
// do the same for all modules you want to use
|
||||
```
|
||||
|
||||
### ES6 Syntax
|
||||
```
|
||||
import Y from 'yjs'
|
||||
import yArray from 'y-array'
|
||||
import yWebsocketsClient from 'y-webrtc'
|
||||
import yMemory from 'y-memory'
|
||||
import yArray from 'y-array'
|
||||
import yMap from 'y-map'
|
||||
import yText from 'y-text'
|
||||
// ..
|
||||
Y.extend(yArray, yWebsocketsClient, yMemory, yArray, yMap, yText /*, .. */)
|
||||
```
|
||||
|
||||
# Text editing example
|
||||
Install dependencies
|
||||
```
|
||||
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
|
||||
}
|
||||
// types: ['Richtext', 'Array'] // optional list of types you want to import
|
||||
}).then(function (y) {
|
||||
// bind the textarea to a shared text element
|
||||
y.share.textarea.bind(document.getElementById('textfield'))
|
||||
}
|
||||
bower i yjs y-memory y-webrtc y-array y-text
|
||||
```
|
||||
|
||||
# Api
|
||||
Here is a simple example of a shared textarea
|
||||
```
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<script src="./bower_components/yjs/y.js"></script>
|
||||
<script>
|
||||
Y({
|
||||
db: {
|
||||
name: 'memory' // use memory database adapter.
|
||||
// name: 'indexeddb'
|
||||
// name: 'leveldb'
|
||||
},
|
||||
connector: {
|
||||
name: 'webrtc', // use webrtc connector
|
||||
// name: 'websockets-client'
|
||||
// name: 'xmpp'
|
||||
room: 'my-room' // clients connecting to the same room share data
|
||||
},
|
||||
sourceDir: '/bower_components', // location of the y-* modules (browser only)
|
||||
share: {
|
||||
textarea: 'Text' // y.share.textarea is of type y-text
|
||||
}
|
||||
}).then(function (y) {
|
||||
// The Yjs instance `y` is available
|
||||
// y.share.* contains the shared types
|
||||
|
||||
// Bind the textarea to y.share.textarea
|
||||
y.share.textarea.bind(document.querySelector('textarea'))
|
||||
}
|
||||
</script>
|
||||
<textarea></textarea>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## Get Help & Give Help
|
||||
There are some friendly people on [](https://gitter.im/y-js/yjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) who are eager to help, and answer questions. Please join!
|
||||
|
||||
Report _any_ issues to the [Github issue page](https://github.com/y-js/yjs/issues)! I try to fix them very soon, if possible.
|
||||
|
||||
# API
|
||||
|
||||
### Y(options)
|
||||
* options.db
|
||||
@@ -104,14 +143,14 @@ Y({
|
||||
* All of our connectors specify an `url` property that defines the connection endpoint of the used connector.
|
||||
* All of our connectors also have a default connection endpoint that you can use for development.
|
||||
* Have a look at the used connector repository to see all available options.
|
||||
* options.sourceDir
|
||||
* Path where all y-* modules are stored.
|
||||
* options.sourceDir (browser only)
|
||||
* Path where all y-* modules are stored
|
||||
* Defaults to `/bower_components`
|
||||
* Not required when running on `nodejs` / `iojs`
|
||||
* When using browserify you can specify all used modules like this:
|
||||
* When using nodejs you need to manually extend Yjs:
|
||||
```
|
||||
var Y = require('yjs')
|
||||
// you need to require the db, connector, and *all* types you use!
|
||||
// you have to require a db, connector, and *all* types you use!
|
||||
require('y-memory')(Y)
|
||||
require('y-webrtc')(Y)
|
||||
require('y-map')(Y)
|
||||
@@ -119,13 +158,13 @@ require('y-map')(Y)
|
||||
```
|
||||
* options.share
|
||||
* Specify on `options.share[arbitraryName]` types that are shared among all users.
|
||||
* E.g. Specify `options.share[arbitraryName] = 'Array'` to require y-array and create an Y.Array type on `y.share[arbitraryName]`.
|
||||
* E.g. Specify `options.share[arbitraryName] = 'Array'` to require y-array and create an y-array type on `y.share[arbitraryName]`.
|
||||
* If userA doesn't specify `options.share[arbitraryName]`, it won't be available for userA.
|
||||
* If userB specifies `options.share[arbitraryName]`, it still won't be available for userA. But all the updates are send from userB to userA.
|
||||
* In contrast to Y.Map, types on `y.share.*` cannot be overwritten or deleted. Instead, they are merged among all users. This feature is only available on `y.share.*`
|
||||
* In contrast to y-map, types on `y.share.*` cannot be overwritten or deleted. Instead, they are merged among all users. This feature is only available on `y.share.*`
|
||||
* Weird behavior: It is supported that two users specify different types with the same property name.
|
||||
E.g. userA specifies `options.share.x = 'Array'`, and userB specifies `options.share.x = 'Text'`. But they'll only share data if they specified the same type with the same property name
|
||||
* options.type
|
||||
E.g. userA specifies `options.share.x = 'Array'`, and userB specifies `options.share.x = 'Text'`. But they only share data if they specified the same type with the same property name
|
||||
* options.type (browser only)
|
||||
* Array of modules that Yjs needs to require, before instantiating a shared type.
|
||||
* By default Yjs requires the specified database adapter, the specified connector, and all modules that are used in `options.share.*`
|
||||
* Put all types here that you intend to use, but are not used in y.share.*
|
||||
@@ -170,18 +209,19 @@ The promise returns an instance of Y. We denote it with a lower case `y`.
|
||||
* y.db.userId :: String
|
||||
* The used user id for this client. **Never overwrite this**
|
||||
|
||||
## Get help
|
||||
There are some friendly people on [](https://gitter.im/y-js/yjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) who may help you with your problem, and answer your questions.
|
||||
|
||||
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.
|
||||
If you want to see an issue fixed, please subscribe to the thread (or remind me via gitter).
|
||||
|
||||
|
||||
## Changelog
|
||||
|
||||
### 12.0.0
|
||||
* **Types are synchronous and never return a promise (except explicitly stated)**
|
||||
* `y.share.map.get('map type') // => returns a y-map instead of a promise`
|
||||
* The event property `oldValues`, and `values` contain a list of values (without wrapper)
|
||||
* Support for the [y-leveldb](https://github.com/y-js/y-leveldb) database adapter
|
||||
* [y-richtext](https://github.com/y-js/y-richtext) supports Quill@1.0.0-rc.2
|
||||
* Only the types are affected by this release. You have to upgrade y-array@10.0.0, y-map@10.0.0, y-richtext@9.0.0, and y-xml@10.0.0
|
||||
|
||||
### 11.0.0
|
||||
|
||||
* **All types now return a single event instead of list of events**
|
||||
* **All types return a single event instead of list of events**
|
||||
* Insert events contain a list of values
|
||||
* Improved performance for large insertions & deletions
|
||||
* Several bugfixes (offline editing related)
|
||||
@@ -195,7 +235,7 @@ If you want to see an issue fixed, please subscribe to the thread (or remind me
|
||||
### 9.0.0
|
||||
There were several rolling updates from 0.6 to 0.8. We consider Yjs stable since a long time,
|
||||
and intend to continue stable releases. From this release forward y-* modules will implement peer-dependencies for npm, and dependencies for bower.
|
||||
Furthermore, incompatible yjs instances will now throw errors when syncing - this feature was influenced by #48. The versioning jump was influenced by react (see [here](https://facebook.github.io/react/blog/2016/02/19/new-versioning-scheme.html))
|
||||
Furthermore, incompatible yjs instances throw errors now when syncing - this feature was influenced by #48. The versioning jump was influenced by react (see [here](https://facebook.github.io/react/blog/2016/02/19/new-versioning-scheme.html))
|
||||
|
||||
|
||||
### 0.6.0
|
||||
@@ -215,7 +255,7 @@ This is a complete rewrite of the 0.5 version of Yjs. Since Yjs 0.6.0 it is poss
|
||||
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 is licensed under the [MIT License](./LICENSE).
|
||||
|
||||
<yjs@dbis.rwth-aachen.de>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "yjs",
|
||||
"version": "11.2.5",
|
||||
"version": "12.0.3",
|
||||
"homepage": "y-js.org",
|
||||
"authors": [
|
||||
"Kevin Jahns <kevin.jahns@rwth-aachen.de>"
|
||||
|
||||
225
y.es6
225
y.es6
@@ -67,6 +67,16 @@ module.exports = function (Y/* :any */) {
|
||||
this.whenSyncedListeners = []
|
||||
return this.y.db.stopGarbageCollector()
|
||||
}
|
||||
repair () {
|
||||
console.info('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues')
|
||||
for (var name in this.connections) {
|
||||
this.connections[name].isSynced = false
|
||||
}
|
||||
this.isSynced = false
|
||||
this.currentSyncTarget = null
|
||||
this.broadcastedHB = false
|
||||
this.findNextSyncTarget()
|
||||
}
|
||||
setUserId (userId) {
|
||||
if (this.userId == null) {
|
||||
this.userId = userId
|
||||
@@ -696,6 +706,41 @@ module.exports = function (Y /* :any */) {
|
||||
if (this.gcTimeout > 0) {
|
||||
garbageCollect()
|
||||
}
|
||||
this.repairCheckInterval = !opts.repairCheckInterval ? 6000 : opts.repairCheckInterval
|
||||
this.opsReceivedTimestamp = new Date()
|
||||
this.startRepairCheck()
|
||||
}
|
||||
startRepairCheck () {
|
||||
var os = this
|
||||
if (this.repairCheckInterval > 0) {
|
||||
this.repairCheckIntervalHandler = setInterval(function repairOnMissingOperations () {
|
||||
/*
|
||||
Case 1. No ops have been received in a while (new Date() - os.opsReceivedTimestamp > os.repairCheckInterval)
|
||||
- 1.1 os.listenersById is empty. Then the state was correct the whole time. -> Nothing to do (nor to update)
|
||||
- 1.2 os.listenersById is not empty.
|
||||
* Then the state was incorrect for at least {os.repairCheckInterval} seconds.
|
||||
* -> Remove everything in os.listenersById and sync again (connector.repair())
|
||||
Case 2. An op has been received in the last {os.repairCheckInterval } seconds.
|
||||
It is not yet necessary to check for faulty behavior. Everything can still resolve itself. Wait for more messages.
|
||||
If nothing was received for a while and os.listenersById is still not emty, we are in case 1.2
|
||||
-> Do nothing
|
||||
|
||||
Baseline here is: we really only have to catch case 1.2..
|
||||
*/
|
||||
if (
|
||||
new Date() - os.opsReceivedTimestamp > os.repairCheckInterval &&
|
||||
Object.keys(os.listenersById).length > 0 // os.listenersById is not empty
|
||||
) {
|
||||
// haven't received operations for over {os.repairCheckInterval} seconds, resend state vector
|
||||
os.listenersById = {}
|
||||
os.opsReceivedTimestamp = new Date() // update so you don't send repair several times in a row
|
||||
os.y.connector.repair()
|
||||
}
|
||||
}, this.repairCheckInterval)
|
||||
}
|
||||
}
|
||||
stopRepairCheck () {
|
||||
clearInterval(this.repairCheckIntervalHandler)
|
||||
}
|
||||
queueGarbageCollector (id) {
|
||||
if (this.y.isConnected()) {
|
||||
@@ -791,6 +836,7 @@ module.exports = function (Y /* :any */) {
|
||||
* destroy () {
|
||||
clearInterval(this.gcInterval)
|
||||
this.gcInterval = null
|
||||
this.stopRepairCheck()
|
||||
for (var key in this.initializedTypes) {
|
||||
var type = this.initializedTypes[key]
|
||||
if (type._destroy != null) {
|
||||
@@ -830,12 +876,14 @@ module.exports = function (Y /* :any */) {
|
||||
/*
|
||||
Apply a list of operations.
|
||||
|
||||
* we save a timestamp, because we received new operations that could resolve ops in this.listenersById (see this.startRepairCheck)
|
||||
* get a transaction
|
||||
* check whether all Struct.*.requiredOps are in the OS
|
||||
* check if it is an expected op (otherwise wait for it)
|
||||
* check if was deleted, apply a delete operation after op was applied
|
||||
*/
|
||||
apply (ops) {
|
||||
this.opsReceivedTimestamp = new Date()
|
||||
for (var i = 0; i < ops.length; i++) {
|
||||
var o = ops[i]
|
||||
if (o.id == null || o.id[0] !== this.y.connector.userId) {
|
||||
@@ -1089,6 +1137,48 @@ module.exports = function (Y /* :any */) {
|
||||
}, 0)
|
||||
}
|
||||
}
|
||||
/*
|
||||
Get a created/initialized type.
|
||||
*/
|
||||
getType (id) {
|
||||
return this.initializedTypes[JSON.stringify(id)]
|
||||
}
|
||||
/*
|
||||
Init type. This is called when a remote operation is retrieved, and transformed to a type
|
||||
TODO: delete type from store.initializedTypes[id] when corresponding id was deleted!
|
||||
*/
|
||||
* initType (id, args) {
|
||||
var sid = JSON.stringify(id)
|
||||
var t = this.store.initializedTypes[sid]
|
||||
if (t == null) {
|
||||
var op/* :MapStruct | ListStruct */ = yield* this.getOperation(id)
|
||||
if (op != null) {
|
||||
t = yield* Y[op.type].typeDefinition.initType.call(this, this.store, op, args)
|
||||
this.store.initializedTypes[sid] = t
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
/*
|
||||
Create type. This is called when the local user creates a type (which is a synchronous action)
|
||||
*/
|
||||
createType (typedefinition, id) {
|
||||
var structname = typedefinition[0].struct
|
||||
id = id || this.getNextOpId(1)
|
||||
var op = Y.Struct[structname].create(id)
|
||||
op.type = typedefinition[0].name
|
||||
|
||||
this.requestTransaction(function * () {
|
||||
if (op.id[0] === '_') {
|
||||
yield* this.setOperation(op)
|
||||
} else {
|
||||
yield* this.applyCreatedOperations([op])
|
||||
}
|
||||
})
|
||||
var t = Y[op.type].typeDefinition.createType(this, op, typedefinition[1])
|
||||
this.initializedTypes[JSON.stringify(op.id)] = t
|
||||
return t
|
||||
}
|
||||
}
|
||||
Y.AbstractDatabase = AbstractDatabase
|
||||
}
|
||||
@@ -1590,57 +1680,6 @@ module.exports = function (Y/* :any */) {
|
||||
os: Store;
|
||||
ss: Store;
|
||||
*/
|
||||
/*
|
||||
Get a type based on the id of its model.
|
||||
If it does not exist yes, create it.
|
||||
TODO: delete type from store.initializedTypes[id] when corresponding id was deleted!
|
||||
*/
|
||||
* getType (id, args) {
|
||||
var sid = JSON.stringify(id)
|
||||
var t = this.store.initializedTypes[sid]
|
||||
if (t == null) {
|
||||
var op/* :MapStruct | ListStruct */ = yield* this.getOperation(id)
|
||||
if (op != null) {
|
||||
t = yield* Y[op.type].typeDefinition.initType.call(this, this.store, op, args)
|
||||
this.store.initializedTypes[sid] = t
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
* createType (typedefinition, id) {
|
||||
var structname = typedefinition[0].struct
|
||||
id = id || this.store.getNextOpId(1)
|
||||
var op
|
||||
if (id[0] === '_') {
|
||||
op = yield* this.getOperation(id)
|
||||
} else {
|
||||
op = Y.Struct[structname].create(id)
|
||||
op.type = typedefinition[0].name
|
||||
}
|
||||
if (typedefinition[0].appendAdditionalInfo != null) {
|
||||
yield* typedefinition[0].appendAdditionalInfo.call(this, op, typedefinition[1])
|
||||
}
|
||||
if (op[0] === '_') {
|
||||
yield* this.setOperation(op)
|
||||
} else {
|
||||
yield* this.applyCreatedOperations([op])
|
||||
}
|
||||
return yield* this.getType(id, typedefinition[1])
|
||||
}
|
||||
/* createType (typedefinition, id) {
|
||||
var structname = typedefinition[0].struct
|
||||
id = id || this.store.getNextOpId(1)
|
||||
var op = Y.Struct[structname].create(id)
|
||||
op.type = typedefinition[0].name
|
||||
if (typedefinition[0].appendAdditionalInfo != null) {
|
||||
yield* typedefinition[0].appendAdditionalInfo.call(this, op, typedefinition[1])
|
||||
}
|
||||
// yield* this.applyCreatedOperations([op])
|
||||
yield* Y.Struct[op.struct].execute.call(this, op)
|
||||
yield* this.addOperation(op)
|
||||
yield* this.store.operationAdded(this, op)
|
||||
return yield* this.getType(id, typedefinition[1])
|
||||
}*/
|
||||
/*
|
||||
Apply operations that this user created (no remote ones!)
|
||||
* does not check for Struct.*.requiredOps()
|
||||
@@ -2234,7 +2273,7 @@ module.exports = function (Y/* :any */) {
|
||||
}
|
||||
if (this.store.forwardAppliedOperations) {
|
||||
var ops = []
|
||||
ops.push({struct: 'Delete', target: [d[0], d[1]], length: del[2]})
|
||||
ops.push({struct: 'Delete', target: [del[0], del[1]], length: del[2]})
|
||||
this.store.y.connector.broadcastOps(ops)
|
||||
}
|
||||
}
|
||||
@@ -2843,7 +2882,13 @@ module.exports = function (Y /* : any*/) {
|
||||
// finished with remaining operations
|
||||
self.waiting.push(d)
|
||||
}
|
||||
checkDelete(op)
|
||||
if (op.key == null) {
|
||||
// deletes in list
|
||||
checkDelete(op)
|
||||
} else {
|
||||
// deletes in map
|
||||
this.waiting.push(op)
|
||||
}
|
||||
} else {
|
||||
this.waiting.push(op)
|
||||
}
|
||||
@@ -2892,7 +2937,11 @@ module.exports = function (Y /* : any*/) {
|
||||
var o = this.waiting[i]
|
||||
if (o.struct === 'Insert') {
|
||||
var _o = yield* transaction.getInsertion(o.id)
|
||||
if (!Y.utils.compareIds(_o.id, o.id)) {
|
||||
if (_o.parentSub != null && _o.left != null) {
|
||||
// if o is an insertion of a map struc (parentSub is defined), then it shouldn't be necessary to compute left
|
||||
this.waiting.splice(i, 1)
|
||||
i-- // update index
|
||||
} else if (!Y.utils.compareIds(_o.id, o.id)) {
|
||||
// o got extended
|
||||
o.left = [o.id[0], o.id[1] - 1]
|
||||
} else if (_o.left == null) {
|
||||
@@ -3052,6 +3101,14 @@ module.exports = function (Y /* : any*/) {
|
||||
}
|
||||
Y.utils.EventHandler = EventHandler
|
||||
|
||||
/*
|
||||
Default class of custom types!
|
||||
*/
|
||||
class CustomType {
|
||||
|
||||
}
|
||||
Y.utils.CustomType = CustomType
|
||||
|
||||
/*
|
||||
A wrapper for the definition of a custom type.
|
||||
Every custom type must have three properties:
|
||||
@@ -3063,7 +3120,7 @@ module.exports = function (Y /* : any*/) {
|
||||
* class
|
||||
- the constructor of the custom type (e.g. in order to inherit from a type)
|
||||
*/
|
||||
class CustomType { // eslint-disable-line
|
||||
class CustomTypeDefinition { // eslint-disable-line
|
||||
/* ::
|
||||
struct: any;
|
||||
initType: any;
|
||||
@@ -3074,12 +3131,14 @@ module.exports = function (Y /* : any*/) {
|
||||
if (def.struct == null ||
|
||||
def.initType == null ||
|
||||
def.class == null ||
|
||||
def.name == null
|
||||
def.name == null ||
|
||||
def.createType == null
|
||||
) {
|
||||
throw new Error('Custom type was not initialized correctly!')
|
||||
}
|
||||
this.struct = def.struct
|
||||
this.initType = def.initType
|
||||
this.createType = def.createType
|
||||
this.class = def.class
|
||||
this.name = def.name
|
||||
if (def.appendAdditionalInfo != null) {
|
||||
@@ -3091,13 +3150,13 @@ module.exports = function (Y /* : any*/) {
|
||||
this.parseArguments.typeDefinition = this
|
||||
}
|
||||
}
|
||||
Y.utils.CustomType = CustomType
|
||||
Y.utils.CustomTypeDefinition = CustomTypeDefinition
|
||||
|
||||
Y.utils.isTypeDefinition = function isTypeDefinition (v) {
|
||||
if (v != null) {
|
||||
if (v instanceof Y.utils.CustomType) return [v]
|
||||
else if (v.constructor === Array && v[0] instanceof Y.utils.CustomType) return v
|
||||
else if (v instanceof Function && v.typeDefinition instanceof Y.utils.CustomType) return [v.typeDefinition]
|
||||
if (v instanceof Y.utils.CustomTypeDefinition) return [v]
|
||||
else if (v.constructor === Array && v[0] instanceof Y.utils.CustomTypeDefinition) return v
|
||||
else if (v instanceof Function && v.typeDefinition instanceof Y.utils.CustomTypeDefinition) return [v.typeDefinition]
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -3357,19 +3416,31 @@ module.exports = Y
|
||||
Y.requiringModules = requiringModules
|
||||
|
||||
Y.extend = function (name, value) {
|
||||
if (value instanceof Y.utils.CustomType) {
|
||||
Y[name] = value.parseArguments
|
||||
if (arguments.length === 2 && typeof name === 'string') {
|
||||
if (value instanceof Y.utils.CustomTypeDefinition) {
|
||||
Y[name] = value.parseArguments
|
||||
} else {
|
||||
Y[name] = value
|
||||
}
|
||||
if (requiringModules[name] != null) {
|
||||
requiringModules[name].resolve()
|
||||
delete requiringModules[name]
|
||||
}
|
||||
} else {
|
||||
Y[name] = value
|
||||
}
|
||||
if (requiringModules[name] != null) {
|
||||
requiringModules[name].resolve()
|
||||
delete requiringModules[name]
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
var f = arguments[i]
|
||||
if (typeof f === 'function') {
|
||||
f(Y)
|
||||
} else {
|
||||
throw new Error('Expected function!')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Y.requestModules = requestModules
|
||||
function requestModules (modules) {
|
||||
function requestModules (modules, sourceDir) {
|
||||
sourceDir = sourceDir || '/bower_components'
|
||||
// determine if this module was compiled for es5 or es6 (y.js vs. y.es6)
|
||||
// if Insert.execute is a Function, then it isnt a generator..
|
||||
// then load the es5(.js) files..
|
||||
@@ -3383,7 +3454,7 @@ function requestModules (modules) {
|
||||
// module does not exist
|
||||
if (typeof window !== 'undefined' && window.Y !== 'undefined') {
|
||||
var imported = document.createElement('script')
|
||||
imported.src = Y.sourceDir + '/' + modulename + '/' + modulename + extention
|
||||
imported.src = sourceDir + '/' + modulename + '/' + modulename + extention
|
||||
document.head.appendChild(imported)
|
||||
|
||||
let requireModule = {}
|
||||
@@ -3439,10 +3510,9 @@ function Y (opts/* :YOptions */) /* :Promise<YConfig> */ {
|
||||
for (var name in opts.share) {
|
||||
modules.push(opts.share[name])
|
||||
}
|
||||
Y.sourceDir = opts.sourceDir
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(function () {
|
||||
Y.requestModules(modules).then(function () {
|
||||
Y.requestModules(modules, opts.sourceDir).then(function () {
|
||||
if (opts == null) reject('An options object is expected! ')
|
||||
else if (opts.connector == null) reject('You must specify a connector! (missing connector property)')
|
||||
else if (opts.connector.name == null) reject('You must specify connector name! (missing connector.name property)')
|
||||
@@ -3483,6 +3553,9 @@ class YConfig {
|
||||
for (var propertyname in opts.share) {
|
||||
var typeConstructor = opts.share[propertyname].split('(')
|
||||
var typeName = typeConstructor.splice(0, 1)
|
||||
var type = Y[typeName]
|
||||
var typedef = type.typeDefinition
|
||||
var id = ['_', typedef.struct + '_' + typeName + '_' + propertyname + '_' + typeConstructor]
|
||||
var args = []
|
||||
if (typeConstructor.length === 1) {
|
||||
try {
|
||||
@@ -3490,11 +3563,13 @@ class YConfig {
|
||||
} catch (e) {
|
||||
throw new Error('Was not able to parse type definition! (share.' + propertyname + ')')
|
||||
}
|
||||
if (type.typeDefinition.parseArguments == null) {
|
||||
throw new Error(typeName + ' does not expect arguments!')
|
||||
} else {
|
||||
args = typedef.parseArguments(args[0])[1]
|
||||
}
|
||||
}
|
||||
var type = Y[typeName]
|
||||
var typedef = type.typeDefinition
|
||||
var id = ['_', typedef.struct + '_' + typeName + '_' + propertyname + '_' + typeConstructor]
|
||||
share[propertyname] = yield* this.createType(type.apply(typedef, args), id)
|
||||
share[propertyname] = yield* this.store.initType.call(this, id, args)
|
||||
}
|
||||
this.store.whenTransactionsFinished()
|
||||
.then(callback)
|
||||
|
||||
Reference in New Issue
Block a user