| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,37 +12,48 @@ | |||
| 12 | 12 | // or field properties of configuration objects whose values are callbacks) | |
| 13 | 13 | struct AsyncBaton { | |
| 14 | 14 | uv_sem_t semaphore; | |
| 15 | + | ||
| 16 | + virtual ~AsyncBaton() {} | ||
| 15 | 17 | }; | |
| 16 | 18 | ||
| 19 | + void deleteBaton(AsyncBaton *baton); | ||
| 20 | + | ||
| 17 | 21 | template<typename ResultT> | |
| 18 | 22 | struct AsyncBatonWithResult : public AsyncBaton { | |
| 19 | 23 | ResultT result; | |
| 20 | 24 | ResultT defaultResult; // result returned if the callback doesn't return anything valid | |
| 25 | + void (*onCompletion)(AsyncBaton *); | ||
| 21 | 26 | ||
| 22 | 27 | AsyncBatonWithResult(const ResultT &defaultResult) | |
| 23 | 28 | : defaultResult(defaultResult) { | |
| 24 | - uv_sem_init(&semaphore, 0); | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - ~AsyncBatonWithResult() { | ||
| 28 | - uv_sem_destroy(&semaphore); | ||
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | 31 | void Done() { | |
| 32 | - // signal completion | ||
| 33 | - uv_sem_post(&semaphore); | ||
| 32 | + if (onCompletion) { | ||
| 33 | + onCompletion(this); | ||
| 34 | + } else { | ||
| 35 | + // signal completion | ||
| 36 | + uv_sem_post(&semaphore); | ||
| 37 | + } | ||
| 34 | 38 | } | |
| 35 | 39 | ||
| 36 | - ResultT ExecuteAsync(ThreadPool::Callback asyncCallback) { | ||
| 40 | + ResultT ExecuteAsync(ThreadPool::Callback asyncCallback, void (*onCompletion)(AsyncBaton *) = NULL) { | ||
| 37 | 41 | result = 0; | |
| 42 | + this->onCompletion = onCompletion; | ||
| 43 | + if (!onCompletion) { | ||
| 44 | + uv_sem_init(&semaphore, 0); | ||
| 45 | + } | ||
| 38 | 46 | ||
| 39 | 47 | { | |
| 40 | 48 | LockMaster::TemporaryUnlock temporaryUnlock; | |
| 41 | 49 | ||
| 42 | 50 | libgit2ThreadPool.ExecuteReverseCallback(asyncCallback, this); | |
| 43 | 51 | ||
| 44 | - // wait for completion | ||
| 45 | - uv_sem_wait(&semaphore); | ||
| 52 | + if (!onCompletion) { | ||
| 53 | + // wait for completion | ||
| 54 | + uv_sem_wait(&semaphore); | ||
| 55 | + uv_sem_destroy(&semaphore); | ||
| 56 | + } | ||
| 46 | 57 | } | |
| 47 | 58 | ||
| 48 | 59 | return result; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + #include "../include/async_baton.h" | ||
| 2 | + | ||
| 3 | + void deleteBaton(AsyncBaton *baton) { | ||
| 4 | + delete baton; | ||
| 5 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,19 +119,28 @@ | |||
| 119 | 119 | {{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %} | |
| 120 | 120 | {% endeach %} | |
| 121 | 121 | ) { | |
| 122 | - {{ field.name|titleCase }}Baton baton({{ field.return.noResults }}); | ||
| 122 | + {{ field.name|titleCase }}Baton *baton = | ||
| 123 | + new {{ field.name|titleCase }}Baton({{ field.return.noResults }}); | ||
| 123 | 124 | ||
| 124 | 125 | {% each field.args|argsInfo as arg %} | |
| 125 | - baton.{{ arg.name }} = {{ arg.name }}; | ||
| 126 | + baton->{{ arg.name }} = {{ arg.name }}; | ||
| 126 | 127 | {% endeach %} | |
| 127 | 128 | ||
| 128 | - {{ cppClassName }}* instance = {{ field.name }}_getInstanceFromBaton(&baton); | ||
| 129 | + {{ cppClassName }}* instance = {{ field.name }}_getInstanceFromBaton(baton); | ||
| 130 | + | ||
| 131 | + {{ field.return.type }} result; | ||
| 129 | 132 | ||
| 130 | 133 | if (instance->{{ field.name }}.WillBeThrottled()) { | |
| 131 | - return baton.defaultResult; | ||
| 134 | + result = baton->defaultResult; | ||
| 135 | + delete baton; | ||
| 136 | + } else if (instance->{{ field.name }}.ShouldWaitForResult()) { | ||
| 137 | + result = baton->ExecuteAsync({{ field.name }}_async); | ||
| 138 | + delete baton; | ||
| 139 | + } else { | ||
| 140 | + result = baton->defaultResult; | ||
| 141 | + baton->ExecuteAsync({{ field.name }}_async, deleteBaton); | ||
| 132 | 142 | } | |
| 133 | - | ||
| 134 | - return baton.ExecuteAsync({{ field.name }}_async); | ||
| 143 | + return result; | ||
| 135 | 144 | } | |
| 136 | 145 | ||
| 137 | 146 | void {{ cppClassName }}::{{ field.name }}_async(void *untypedBaton) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ | |||
| 12 | 12 | }, | |
| 13 | 13 | ||
| 14 | 14 | "sources": [ | |
| 15 | + "src/async_baton.cc", | ||
| 15 | 16 | "src/lock_master.cc", | |
| 16 | 17 | "src/nodegit.cc", | |
| 17 | 18 | "src/init_ssh2.cc", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -166,6 +166,37 @@ describe("Clone", function() { | |||
| 166 | 166 | }); | |
| 167 | 167 | }); | |
| 168 | 168 | ||
| 169 | + it("can clone without waiting for callback results", function() { | ||
| 170 | + var test = this; | ||
| 171 | + var url = "https://github.com/nodegit/test.git"; | ||
| 172 | + var lastReceivedObjects = 0; | ||
| 173 | + var cloneFinished = false; | ||
| 174 | + var opts = { | ||
| 175 | + fetchOpts: { | ||
| 176 | + callbacks: { | ||
| 177 | + transferProgress: { | ||
| 178 | + waitForResult: false, | ||
| 179 | + callback: function(progress) { | ||
| 180 | + var receivedObjects = progress.receivedObjects(); | ||
| 181 | + assert.false( | ||
| 182 | + cloneFinished, | ||
| 183 | + "callback running after clone completion" | ||
| 184 | + ); | ||
| 185 | + assert.gt(receivedObjects, lastReceivedObjects); | ||
| 186 | + lastReceivedObjects = receivedObjects; | ||
| 187 | + } | ||
| 188 | + } | ||
| 189 | + } | ||
| 190 | + } | ||
| 191 | + }; | ||
| 192 | + | ||
| 193 | + return Clone(url, clonePath, opts).then(function(repo) { | ||
| 194 | + assert.ok(repo instanceof Repository); | ||
| 195 | + cloneFinished = true; | ||
| 196 | + test.repository = repo; | ||
| 197 | + }); | ||
| 198 | + }); | ||
| 199 | + | ||
| 169 | 200 | it("can clone using nested function", function() { | |
| 170 | 201 | var test = this; | |
| 171 | 202 | var url = "https://github.com/nodegit/test.git"; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments