added support for Object.observe in supported browsers
This commit is contained in:
parent
d4bb2dc173
commit
9b582fc795
@ -23,5 +23,8 @@
|
||||
"Gruntfile.coffee",
|
||||
"extras",
|
||||
"tests"
|
||||
]
|
||||
],
|
||||
"dependencies": {
|
||||
"peerjs": "~0.3.14",
|
||||
}
|
||||
}
|
||||
|
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
4
build/browser/Frameworks/XmlFramework.min.js
vendored
4
build/browser/Frameworks/XmlFramework.min.js
vendored
File diff suppressed because one or more lines are too long
@ -61,6 +61,10 @@
|
||||
return _results;
|
||||
};
|
||||
|
||||
Operation.prototype.deleteAllListeners = function() {
|
||||
return this.event_listeners = [];
|
||||
};
|
||||
|
||||
Operation.prototype.callEvent = function() {
|
||||
return this.forwardEvent.apply(this, [this].concat(__slice.call(arguments)));
|
||||
};
|
||||
@ -97,7 +101,8 @@
|
||||
};
|
||||
|
||||
Operation.prototype.cleanup = function() {
|
||||
return HB.removeOperation(this);
|
||||
HB.removeOperation(this);
|
||||
return this.deleteAllListeners();
|
||||
};
|
||||
|
||||
Operation.prototype.setParent = function(parent) {
|
||||
|
File diff suppressed because one or more lines are too long
@ -87,25 +87,74 @@
|
||||
};
|
||||
|
||||
JsonType.prototype.toJson = function() {
|
||||
var json, name, o, val;
|
||||
val = this.val();
|
||||
json = {};
|
||||
for (name in val) {
|
||||
o = val[name];
|
||||
if (o === null) {
|
||||
json[name] = o;
|
||||
} else if (o.constructor === {}.constructor) {
|
||||
json[name] = this.val(name).toJson();
|
||||
} else if (o instanceof types.Operation) {
|
||||
while (o instanceof types.Operation) {
|
||||
o = o.val();
|
||||
var json, name, o, that, val;
|
||||
if ((this.bound_json == null) || (Object.observe == null) || true) {
|
||||
val = this.val();
|
||||
json = {};
|
||||
for (name in val) {
|
||||
o = val[name];
|
||||
if (o === null) {
|
||||
json[name] = o;
|
||||
} else if (o.constructor === {}.constructor) {
|
||||
json[name] = this.val(name).toJson();
|
||||
} else if (o instanceof types.Operation) {
|
||||
while (o instanceof types.Operation) {
|
||||
o = o.val();
|
||||
}
|
||||
json[name] = o;
|
||||
} else {
|
||||
json[name] = o;
|
||||
}
|
||||
json[name] = o;
|
||||
} else {
|
||||
json[name] = o;
|
||||
}
|
||||
this.bound_json = json;
|
||||
if ((Object.observe != null) && false) {
|
||||
that = this;
|
||||
Object.observe(this.bound_json, function(events) {
|
||||
var event, _i, _len, _results;
|
||||
_results = [];
|
||||
for (_i = 0, _len = events.length; _i < _len; _i++) {
|
||||
event = events[_i];
|
||||
if (event.type === "add" || (event.type = "update")) {
|
||||
_results.push(that.val(event.name, event.object[event.name]));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
});
|
||||
that.on('change', function(event_name, property_name, op) {
|
||||
var notifier, oldVal;
|
||||
if (this === that) {
|
||||
notifier = Object.getNotifier(that.bound_json);
|
||||
oldVal = that.bound_json[property_name];
|
||||
if (oldVal != null) {
|
||||
notifier.performChange('update', function() {
|
||||
return that.bound_json[property_name] = that.val(property_name);
|
||||
}, that.bound_json);
|
||||
return notifier.notify({
|
||||
object: that.bound_json,
|
||||
type: 'update',
|
||||
name: property_name,
|
||||
oldValue: oldVal,
|
||||
changed_by: op.creator
|
||||
});
|
||||
} else {
|
||||
notifier.performChange('add', function() {
|
||||
return that.bound_json[property_name] = that.val(property_name);
|
||||
}, that.bound_json);
|
||||
return notifier.notify({
|
||||
object: that.bound_json,
|
||||
type: 'add',
|
||||
name: property_name,
|
||||
oldValue: oldVal,
|
||||
changed_by: op.creator
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return json;
|
||||
return this.bound_json;
|
||||
};
|
||||
|
||||
JsonType.prototype.setReplaceManager = function(replace_manager) {
|
||||
|
File diff suppressed because one or more lines are too long
@ -255,7 +255,7 @@
|
||||
}
|
||||
});
|
||||
addPropertyListener = function(event, op) {
|
||||
repl_manager.deleteListener('addProperty', addPropertyListener);
|
||||
repl_manager.deleteListener('insert', addPropertyListener);
|
||||
return repl_manager.parent.callEvent('addProperty', property_name, op);
|
||||
};
|
||||
this.on('insert', addPropertyListener);
|
||||
@ -318,6 +318,9 @@
|
||||
|
||||
Replaceable.prototype.applyDelete = function() {
|
||||
if (this.content != null) {
|
||||
if (this.next_cl.type !== "Delimiter") {
|
||||
this.content.deleteAllListeners();
|
||||
}
|
||||
this.content.applyDelete();
|
||||
this.content.dontSync();
|
||||
}
|
||||
|
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
@ -222,7 +222,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -335,7 +335,7 @@ JsonFramework was initialized (Depending on the HistoryBuffer implementation).</
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -116,7 +116,7 @@ if (x.type === "JsonType") {
|
||||
</a>
|
||||
</span>
|
||||
<span class='desc'>
|
||||
Transform this to a Json and loose all the sharing-abilities (the new object will be a deep clone)!
|
||||
Transform this to a Json.
|
||||
</span>
|
||||
</li>
|
||||
<li>
|
||||
@ -253,7 +253,8 @@ if (x.type === "JsonType") {
|
||||
<br>
|
||||
</p>
|
||||
<div class='docstring'>
|
||||
<p>Transform this to a Json and loose all the sharing-abilities (the new object will be a deep clone)!</p>
|
||||
<p>Transform this to a Json. If your browser supports Object.observe it will be transformed automatically when a change arrives.
|
||||
Otherwise you will loose all the sharing-abilities (the new object will be a deep clone)!</p>
|
||||
</div>
|
||||
<div class='tags'>
|
||||
<h3>Returns:</h3>
|
||||
@ -466,7 +467,7 @@ if (x.type === "JsonType") {
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -139,7 +139,7 @@ console.log(w.newProperty == "Awesome") # true!</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -356,7 +356,7 @@ JsonFramework was initialized (Depending on the HistoryBuffer implementation).</
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -455,7 +455,7 @@ yatta.bind(textbox);</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -303,7 +303,7 @@ JsonFramework was initialized (Depending on the HistoryBuffer implementation).</
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -38,7 +38,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -76,7 +76,7 @@ But I would become really motivated if you gave me some feedback :) (<a href="ht
|
||||
</ul>
|
||||
<h2 id="support">Support</h2><p>Please report <em>any</em> issues to the <a href="https://github.com/DadaMonad/Yatta/issues">Github issue page</a>!
|
||||
I would appreciate if developers gave me feedback on how <em>convenient</em> the framework is, and if it is easy to use. Particularly the XML-support may not support every DOM-methods - if you encounter a method that does not cause any change on other peers,
|
||||
please state function name, and sample parameters. However, there are browser-specific features, that Yatta won't support.</p><h2 id="license">License</h2><p>Yatta! is licensed under the <a href="./LICENSE.txt">MIT License</a>.</p><a href="mailto:kevin.jahns@rwth-aachen.de">kevin.jahns@rwth-aachen.de</a>
|
||||
please state function name, and sample parameters. However, there are browser-specific features, that Yatta won't support.</p><h2 id="license">License</h2><p>Yatta! is licensed under the <a href="./LICENSE.txt">MIT License</a>.</p><a href="mailto:kevin.jahns@rwth-aachen.de">kevin.jahns@rwth-aachen.de</a>
|
||||
|
||||
|
||||
|
||||
@ -85,7 +85,7 @@ please state function name, and sample parameters. However, there are browser-sp
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -106,7 +106,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -48,7 +48,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -60,7 +60,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -60,7 +60,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -60,7 +60,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -48,7 +48,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -39,7 +39,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -39,7 +39,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -39,7 +39,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -39,7 +39,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -37,7 +37,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
December 01, 14 10:47:17 by
|
||||
December 02, 14 09:37:37 by
|
||||
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
|
||||
Codo
|
||||
</a>
|
||||
|
@ -0,0 +1,333 @@
|
||||
## PeerJs + JSON Example
|
||||
Here, I will give a short overview on how to enable collaborative json with the
|
||||
[PeerJs](http://peerjs.com/) Connector and the Json Framework. Open
|
||||
[index.html](http://dadamonad.github.io/Yatta/examples/PeerJs-Json/index.html) in your Browser and
|
||||
use the console to explore Yatta!
|
||||
|
||||
[PeerJs](http://peerjs.com) is a Framework that enables you to connect to other peers. You just need the
|
||||
user-id of the peer (browser/client). And then you can connect to it.
|
||||
|
||||
First you have to include the following libraries in your html file:
|
||||
```
|
||||
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
|
||||
<script src="../../build/browser/Frameworks/JsonFramework.js"></script>
|
||||
<script src="../../build/browser/Connectors/PeerJsConnector.js"></script>
|
||||
<script src="./index.js"></script>
|
||||
```
|
||||
### Create Connector
|
||||
|
||||
The PeerJs Framework requires an API key, or you need to set up your own PeerJs server.
|
||||
Get an API key from the [Website](http://peerjs.com/peerserver).
|
||||
The first parameter of `createPeerJsConnector` is forwarded as the options object in PeerJs.
|
||||
Therefore, you may also specify the server/port here, if you have set up your own server.
|
||||
|
||||
|
||||
```js
|
||||
var yatta, yattaHandler;
|
||||
```
|
||||
|
||||
|
||||
This will connect to the server owned by the peerjs team.
|
||||
For now, you can use my API key.
|
||||
|
||||
|
||||
```js
|
||||
//var options = {key: 'h7nlefbgavh1tt9'};
|
||||
```
|
||||
|
||||
|
||||
This will connect to one of my peerjs instances.
|
||||
I can't guaranty that this will be always up. This is why you should use the previous method with the api key,
|
||||
or set up your own server.
|
||||
|
||||
|
||||
```js
|
||||
var options = {
|
||||
host: "terrific-peerjs.herokuapp.com",
|
||||
port: "", // this works because heroku can forward to the right port.
|
||||
// debug: true,
|
||||
};
|
||||
|
||||
var user_id = Math.ceil(Math.random()*100);
|
||||
connector = new PeerJsConnector(user_id,options);
|
||||
//var connector = new TestConnector(user_id);
|
||||
```
|
||||
|
||||
|
||||
### Yatta
|
||||
yatta is the shared json object. If you change something on this object,
|
||||
it will be instantly shared with all the other collaborators.
|
||||
|
||||
|
||||
```js
|
||||
yatta = new Y.JsonFramework(user_id, connector);
|
||||
```
|
||||
|
||||
|
||||
Next, you may want to connect to another peer. Therefore you have to receive his
|
||||
user_id. If the other peer is connected to other peers, the PeerJsConnector
|
||||
will automatically connect to them too.
|
||||
|
||||
Transmitting the user_id is your job.
|
||||
See [TextEditing](../../examples/TextEditing/) for a nice example
|
||||
on how to do that with urls.
|
||||
|
||||
|
||||
```js
|
||||
console.log("Copy your user-id: " + user_id);
|
||||
|
||||
// yatta.connector.connectToPeer(peer_user_id);
|
||||
```
|
||||
|
||||
|
||||
Add a integer-property like this
|
||||
|
||||
|
||||
```js
|
||||
yatta.val('x', 7);
|
||||
```
|
||||
|
||||
|
||||
Get the value of property x like this
|
||||
|
||||
|
||||
```js
|
||||
console.log(yatta.val('x') === 7); // true
|
||||
```
|
||||
|
||||
|
||||
A string property can be either mutable or immutable.
|
||||
|
||||
|
||||
```js
|
||||
yatta.val('mutable_string', "text", "mutable");
|
||||
yatta.val('immutable_string', "text", "immutable");
|
||||
|
||||
console.log(yatta.val('immutable_string') === "text"); // true
|
||||
yatta.val('mutable_string').insertText(2,"XXX"); // position, string
|
||||
yatta.val('mutable_string').deleteText(0,1); // position, deletion length
|
||||
console.log(yatta.val('mutable_string').val() === "eXXXxt"); // true
|
||||
```
|
||||
|
||||
|
||||
Did you recognize that we use anoter `.val()` for mutable strings?
|
||||
Thats because `yatta.val('mutable_string')` is of type *WordType*.
|
||||
Since WordType implements `toString`, you can use it like a string:
|
||||
|
||||
|
||||
```js
|
||||
console.log("" + yatta.val("mutable_string") === "eXXXxt") // true, concatenating it with a string will implicitly invoke toString()
|
||||
```
|
||||
|
||||
|
||||
You can omit the mutable - parameter. In that case the default will be used.
|
||||
Initially the default is 'mutable'. You can set it like this:
|
||||
|
||||
|
||||
```js
|
||||
yatta.setMutableDefault('mutable');
|
||||
// or
|
||||
yatta.setMutableDefault('immutable');
|
||||
|
||||
yatta.val('new_string', "string");
|
||||
console.log(yatta.val('new_string') === "string"); // true
|
||||
```
|
||||
|
||||
|
||||
Yatta is [chainable](http://schier.co/post/method-chaining-in-javascript):
|
||||
|
||||
|
||||
```js
|
||||
yatta.val('a', 4).val('b',5);
|
||||
console.log(yatta.val('a') === 4); // true
|
||||
console.log(yatta.val('b') === 5); // true
|
||||
```
|
||||
|
||||
|
||||
You can alse set objects.
|
||||
|
||||
|
||||
```js
|
||||
yatta.val('object', {a : {b : "b"}, c : { d : 5 }});
|
||||
console.log(yatta.val('object').val('c').val('d') === 5); // true
|
||||
```
|
||||
|
||||
|
||||
Lists are always immutable.
|
||||
|
||||
|
||||
```js
|
||||
yatta.val('list', [0,1,2]);
|
||||
console.log(yatta.val('list')[2] === 2); // true
|
||||
```
|
||||
|
||||
|
||||
### Check Types
|
||||
Certainly you want to check types!
|
||||
You get the type of an YattaType with the `.type` property.
|
||||
|
||||
Here, we create a function that parses a Yatta type to a string.
|
||||
|
||||
|
||||
```js
|
||||
function show(o){
|
||||
if (o.type === "JsonType"){
|
||||
return JSON.stringify(o.toJson());
|
||||
} else if (o.type === "WordType") {
|
||||
return o.val();
|
||||
} else if (o.constructor === {}.constructor) { // It's an Object
|
||||
return JSON.stringify(o);
|
||||
} else { // It's a primitive data type (E.g. string, int)
|
||||
return o;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Add listeners
|
||||
Apply a 'addProperty' - listener to a JsonType.
|
||||
|
||||
|
||||
```js
|
||||
function addProperty(event_name, property_name, op){
|
||||
// op is the operation that changed the objects value. In addProperty it is most likely to be a 'Replaceable' (see doc).
|
||||
console.log("Property '" + property_name + "' was created by '"+op.creator+"'!");
|
||||
console.log("Value: " + show(this.val(property_name))); // 'this' is the object on which the property was created.
|
||||
};
|
||||
yatta.on('addProperty', addProperty);
|
||||
yatta.val('new', {z: 7}); // Property 'new' was created!
|
||||
```
|
||||
|
||||
|
||||
Apply a 'change' - listener to a JsonType.
|
||||
|
||||
|
||||
```js
|
||||
function change(event_name, property_name, op){
|
||||
// Check who made this property change!
|
||||
if(op.creator == yatta.getUserId()){
|
||||
console.log("You changed the value of property '" + property_name + "'!");
|
||||
}else{
|
||||
console.log("User '"+op.creator+"' changed the value of property '" + property_name + "'!");
|
||||
}
|
||||
|
||||
console.log("New value: " + show(this.val(property_name)) + ""); // 'this' is the object on which the property changed.
|
||||
};
|
||||
yatta.on('change', change);
|
||||
yatta.val('mutable_string', "text", 'mutable'); // You changed the value of property 'mutable_string'!
|
||||
```
|
||||
|
||||
|
||||
'change' and 'addProperty' do also fire for nested properties.
|
||||
|
||||
|
||||
```js
|
||||
yatta.val('new').val('z', {'replace' : "x"}); // Property 'z' was replaced or changed!
|
||||
yatta.val('new').val('z').val('new', {"true": true}); // Property 'z' was replaced or changed! + Property 'new' was created!
|
||||
```
|
||||
|
||||
|
||||
Delete the listeners
|
||||
|
||||
|
||||
```js
|
||||
yatta.deleteListener('addProperty', addProperty);
|
||||
yatta.deleteListener('change', change);
|
||||
```
|
||||
|
||||
|
||||
Apply 'insert' and 'delete' - listeners to Words.
|
||||
|
||||
|
||||
```js
|
||||
function insert_delete(event_name, op){
|
||||
if (event_name === "insert"){
|
||||
console.log("Inserted '" + op.content + "' at position " + op.getPosition());
|
||||
} else if (event_name === "delete"){
|
||||
console.log("Deleted character at position " + op.getPosition());
|
||||
}
|
||||
};
|
||||
yatta.val('mutable_string').on(['insert', 'delete'], insert_delete);
|
||||
yatta.val('mutable_string').insertText(0, 'a'); // Inserted 'a' at position 0
|
||||
yatta.val('mutable_string').deleteText(0, 1); // Deleted character at position 0
|
||||
|
||||
|
||||
yatta.val('mutable_string').deleteListener('insert_delete', insert_delete);
|
||||
```
|
||||
|
||||
|
||||
### Experimental method
|
||||
Nah.. this is only for the cool kids.
|
||||
|
||||
|
||||
```js
|
||||
console.log(yatta.value.list[2] === 2) // true
|
||||
yatta.value.list = [3,4,5]
|
||||
console.log(yatta.val('list')[2] === 5) // true
|
||||
yatta.value.object = {c : 4}
|
||||
console.log(yatta.value.object.c === 4) // true
|
||||
```
|
||||
|
||||
|
||||
How did I do that? ^^
|
||||
|
||||
In Javascript it is possible set setter- and getter- for properties. This is
|
||||
why this method feels much more natural.
|
||||
The downside is that you are only allowed to overwrite existing properties.
|
||||
|
||||
|
||||
```js
|
||||
yatta.value.newProperty = "Awesome"
|
||||
console.log(yatta.value.newProperty !== "Awesome") // true, yatta.value.newProperty is undefined.
|
||||
```
|
||||
|
||||
|
||||
So, how do we create new properties?
|
||||
|
||||
|
||||
```js
|
||||
yatta.value = {newProperty : "Awesome"}
|
||||
console.log(yatta.value.newProperty === "Awesome") // true, it's awesome ;)
|
||||
```
|
||||
|
||||
|
||||
This is stupid! I don't want to overwrite all my existing properties!
|
||||
Very well.. The solution is that we merge yatta.value with the new assignment.
|
||||
For example: assuming we want to overwrite yatta.value with some object o.
|
||||
Then these two rules apply:
|
||||
* The result has all properties of o
|
||||
* The result has all properties of yatta.value if they don't occur under the same property-name in o
|
||||
|
||||
|
||||
```js
|
||||
yatta.value = {newProperty : {Awesome : true }}
|
||||
console.log(yatta.value.list[2] === 5) // true, old value list still exists.
|
||||
console.log(yatta.value.newProperty.Awesome === true) // true, newProperty is overwritten.
|
||||
```
|
||||
|
||||
|
||||
Consider this case.
|
||||
|
||||
|
||||
```js
|
||||
yatta.value = {newProperty : { x : 4} }
|
||||
console.log(yatta.value.newProperty.Awesome == null) // true, newProperty was replaced, therefore it is now undefined
|
||||
```
|
||||
|
||||
|
||||
Did you notice that you always set immutable objects if you set properties like this?
|
||||
Even if the default is 'mutable'. If you want to work with mutable objects you have to work with .val().
|
||||
|
||||
One last thing. You are only allowed to set properties like this `yatta.value = o`.
|
||||
Yatta can't observe if you overwrite object references `yatta = "Awesome"`.
|
||||
|
||||
|
||||
```js
|
||||
w = yatta.value.newProperty
|
||||
w = "Awesome"
|
||||
console.log(yatta.value.newProperty !== "Awesome") // true, still not awesome..
|
||||
```
|
||||
|
||||
|
||||
Please also read [JsonWrapper](https://rawgit.com/DadaMonad/Yatta/master/doc/class/JsonWrapper.html).
|
||||
I really want to hear what you think about this method :)
|
@ -3,17 +3,17 @@
|
||||
<head>
|
||||
<meta charset=utf-8 />
|
||||
<title>PeerJs Json Example</title>
|
||||
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
|
||||
<script src="../../build/browser/Frameworks/JsonFramework.js"></script>
|
||||
<script src="../../bower_components/peerjs/peer.js"></script>
|
||||
<script src="../../bower_components/connector/peerjs-connector/peerjs-connector.js"></script>
|
||||
<script src="../../bower_components/connector/test-connector/test-connector.js"></script>
|
||||
<script src="../../build/browser/Frameworks/JsonFramework.js"></script>
|
||||
<script src="./index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1> PeerJs + Json Tutorial</h1>
|
||||
<p> Collaborative Json editing with <a href="https://github.com/DadaMonad/Yatta/">Yatta</a>
|
||||
and <a href="http://peerjs.com/">PeerJs</a> (WebRTC). </p>
|
||||
|
||||
|
||||
|
||||
<p> <a href="https://github.com/DadaMonad/Yatta/">Yatta</a> is a Framework for Real-Time collaboration on arbitrary data structures.
|
||||
You can find the code for this example <a href="https://github.com/DadaMonad/Yatta/tree/master/examples/PeerJs-Json">here</a>.
|
||||
</p>
|
||||
|
@ -29,22 +29,23 @@ var yatta, yattaHandler;
|
||||
This will connect to the server owned by the peerjs team.
|
||||
For now, you can use my API key.
|
||||
*/
|
||||
var options = {key: 'h7nlefbgavh1tt9'};
|
||||
//var options = {key: 'h7nlefbgavh1tt9'};
|
||||
|
||||
/**
|
||||
This will connect to one of my peerjs instances.
|
||||
I can't guaranty that this will be always up. This is why you should use the previous method with the api key,
|
||||
or set up your own server.
|
||||
*/
|
||||
/*
|
||||
|
||||
var options = {
|
||||
host: "terrific-peerjs.herokuapp.com",
|
||||
port: "", // this works because heroku can forward to the right port.
|
||||
// debug: true,
|
||||
};*/
|
||||
};
|
||||
|
||||
var user_id = Math.ceil(Math.random()*100);
|
||||
connector = new PeerJsConnector(user_id,options);
|
||||
//var connector = new TestConnector(user_id);
|
||||
|
||||
/**
|
||||
### Yatta
|
||||
@ -160,15 +161,12 @@ connector = new PeerJsConnector(user_id,options);
|
||||
*/
|
||||
function change(event_name, property_name, op){
|
||||
// Check who made this property change!
|
||||
if(op != null){
|
||||
if(op.creator == yatta.getUserId()){
|
||||
console.log("You changed the value of property '" + property_name + "'!");
|
||||
}else{
|
||||
console.log("User '"+op.creator+"' changed the value of property '" + property_name + "'!");
|
||||
}
|
||||
} else {
|
||||
console.log("The value of property '"+property_name+"' changed!")
|
||||
if(op.creator == yatta.getUserId()){
|
||||
console.log("You changed the value of property '" + property_name + "'!");
|
||||
}else{
|
||||
console.log("User '"+op.creator+"' changed the value of property '" + property_name + "'!");
|
||||
}
|
||||
|
||||
console.log("New value: " + show(this.val(property_name)) + ""); // 'this' is the object on which the property changed.
|
||||
};
|
||||
yatta.on('change', change);
|
||||
@ -179,6 +177,12 @@ connector = new PeerJsConnector(user_id,options);
|
||||
*/
|
||||
yatta.val('new').val('z', {'replace' : "x"}); // Property 'z' was replaced or changed!
|
||||
yatta.val('new').val('z').val('new', {"true": true}); // Property 'z' was replaced or changed! + Property 'new' was created!
|
||||
|
||||
/**
|
||||
Delete the listeners
|
||||
*/
|
||||
yatta.deleteListener('addProperty', addProperty);
|
||||
yatta.deleteListener('change', change);
|
||||
|
||||
/**
|
||||
Apply 'insert' and 'delete' - listeners to Words.
|
||||
@ -194,8 +198,7 @@ connector = new PeerJsConnector(user_id,options);
|
||||
yatta.val('mutable_string').insertText(0, 'a'); // Inserted 'a' at position 0
|
||||
yatta.val('mutable_string').deleteText(0, 1); // Deleted character at position 0
|
||||
|
||||
yatta.deleteListener('addProperty', addProperty);
|
||||
yatta.deleteListener('change', change);
|
||||
|
||||
yatta.val('mutable_string').deleteListener('insert_delete', insert_delete);
|
||||
|
||||
|
||||
|
@ -130,11 +130,12 @@ module.exports = (HB)->
|
||||
super()
|
||||
|
||||
#
|
||||
# Transform this to a Json and loose all the sharing-abilities (the new object will be a deep clone)!
|
||||
# Transform this to a Json. If your browser supports Object.observe it will be transformed automatically when a change arrives.
|
||||
# Otherwise you will loose all the sharing-abilities (the new object will be a deep clone)!
|
||||
# @return {Json}
|
||||
#
|
||||
toJson: ()->
|
||||
if not @bound_json?
|
||||
if not @bound_json? or not Object.observe?
|
||||
val = @val()
|
||||
json = {}
|
||||
for name, o of val
|
||||
@ -149,6 +150,37 @@ module.exports = (HB)->
|
||||
else
|
||||
json[name] = o
|
||||
@bound_json = json
|
||||
if Object.observe?
|
||||
that = @
|
||||
Object.observe @bound_json, (events)->
|
||||
for event in events
|
||||
if not event.changed_by? and (event.type is "add" or event.type = "update")
|
||||
# this event is not created by Yatta.
|
||||
that.val(event.name, event.object[event.name])
|
||||
that.on 'change', (event_name, property_name, op)->
|
||||
if this is that and op.creator isnt HB.getUserId()
|
||||
notifier = Object.getNotifier(that.bound_json)
|
||||
oldVal = that.bound_json[property_name]
|
||||
if oldVal?
|
||||
notifier.performChange 'update', ()->
|
||||
that.bound_json[property_name] = that.val(property_name)
|
||||
, that.bound_json
|
||||
notifier.notify
|
||||
object: that.bound_json
|
||||
type: 'update'
|
||||
name: property_name
|
||||
oldValue: oldVal
|
||||
changed_by: op.creator
|
||||
else
|
||||
notifier.performChange 'add', ()->
|
||||
that.bound_json[property_name] = that.val(property_name)
|
||||
, that.bound_json
|
||||
notifier.notify
|
||||
object: that.bound_json
|
||||
type: 'add'
|
||||
name: property_name
|
||||
oldValue: oldVal
|
||||
changed_by: op.creator
|
||||
@bound_json
|
||||
|
||||
#
|
||||
|
Loading…
x
Reference in New Issue
Block a user