Skip to content

Commit

Permalink
added an "edit" event in node.js-land emitted from documents (with ve…
Browse files Browse the repository at this point in the history
…rsion, location and change delta values)
  • Loading branch information
rsms committed Feb 1, 2011
1 parent 7d4592b commit 3a1644b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
21 changes: 21 additions & 0 deletions resources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ kod.on('openDocument', function(document) {
//console.log('openDocument: '+ util.inspect(document, 0, 4));
console.log('openDocument: #'+document.identifier+' '+document.type+
' '+(document.url || '*new*'));

// example of registering for the "edit" event, emitted after each edit to
// a document.
/*document.on('edit', function(version, location, changeDelta) {
console.log(this+':edit -> %j', {
version:version, location:location, changeDelta:changeDelta});
// if we are to manipulate the text, we need to compare versions since
// these things happen concurrently. Here, we wrap a "p" character in < & >:
if (document.version == version && changeDelta == 1) {
var text = document.text;
var changedChar = text.substr(location, 1);
if (changedChar == "p") {
document.text = text.substr(0,location)+'<'+changedChar+'>'+
text.substr(location+1);
if (document.version != version+1) {
console.log("another edit happened while we where running. "+
"The effect is undefined from our perspective.");
}
}
}
})*/
});

// example event listener for the "activateDocument" event, emitted when a
Expand Down
32 changes: 31 additions & 1 deletion src/KDocument.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ - (void)textStorageDidProcessEditing:(NSNotification *)notification {

// Increment our version
[self willChangeValueForKey:@"version"];
h_atomic_inc(&version_);
uint64_t version = h_atomic_inc(&version_);
[self didChangeValueForKey:@"version"];

// range that was affected by the edit
Expand All @@ -1382,6 +1382,36 @@ - (void)textStorageDidProcessEditing:(NSNotification *)notification {
});
}

// emit event in node-land
KNodePerformInNode(^(KNodeReturnBlock returnCallback){
v8::HandleScope scope;
v8::Local<v8::Object> doc = [self v8Value]->ToObject();
// TODO: refactor this to be reusable for emitting events on any object
v8::Local<v8::Value> emitV = doc->Get(v8::String::New("emit"));
if (!emitV.IsEmpty() && emitV->IsFunction()) {
v8::Local<v8::Value> argv[] = {
v8::String::New("edit"),
v8::Number::New((double)version),
v8::Number::New((double)editedRange.location),
v8::Integer::New(changeInLength)
};
static const int argc = sizeof(argv) / sizeof(argv[0]);
v8::TryCatch tryCatch;
v8::Local<v8::Value> returnValue =
v8::Local<v8::Function>::Cast(emitV)->Call(doc, argc, argv);
NSError *error = nil;
if (tryCatch.HasCaught()) {
v8::String::Utf8Value trace(tryCatch.StackTrace());
NSAutoreleasePool *pool1 = [NSAutoreleasePool new];
WLOG("Error while emitting event '%s' on %@: %s", "edit", self,
*trace ? *trace : "(no trace)");
[pool1 drain];
}
}
// must be called since this takes care of releasing some resources
returnCallback(nil, nil, nil);
});

// we do not process edits when we are loading
if (isLoading_) return;

Expand Down
2 changes: 2 additions & 0 deletions src/node-module/KObjectProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ @interface _KObjectProxyShelf : NSObject {} @end
@implementation _KObjectProxyShelf
- (void)_KObjectProxy_dealloc_associations {
//DLOG("_KObjectProxy_dealloc_associations for %p", self);
// TODO: assert that we are running in the nodejs thread, or we need to
// defer this to that thread since v8 will crash and burn unless so.

// clear wrapper
NSValue *v = objc_getAssociatedObject(self, &kPersistentWrapperKey);
Expand Down

0 comments on commit 3a1644b

Please sign in to comment.