Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

(initial)

  • Loading branch information...
commit 8e2e7cb2e07420ddbf98f97c78c1da4f9890adf2 0 parents
@indutny authored
4 .gitignore
@@ -0,0 +1,4 @@
+node_modules/
+/build/
+npm-debug.log
+*.o
7 .travis.yml
@@ -0,0 +1,7 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.7
+ branches:
+ only:
+ - master
3  README.md
@@ -0,0 +1,3 @@
+# deferTick([function])
+
+...
8 binding.gyp
@@ -0,0 +1,8 @@
+{
+ "targets": [{
+ "target_name": "defer_tick",
+ "sources": [
+ "src/defer-tick.cc"
+ ]
+ }]
+}
5 lib/defer-tick.js
@@ -0,0 +1,5 @@
+var binding = require('../build/Release/defer_tick');
+
+module.exports = function deferTick(callback) {
+ binding.deferTick(callback);
+};
16 package.json
@@ -0,0 +1,16 @@
+{
+ "name": "defer-tick",
+ "version": "0.0.1",
+ "main": "./lib/defer-tick.js",
+ "dependencies": {
+ "node-gyp": "0.5.x"
+ },
+ "devDependencies": {
+ "mocha": "~1.2.0",
+ "async": "~0.1.22"
+ },
+ "scripts": {
+ "install": "node-gyp configure && node-gyp build",
+ "test": "mocha --reporter spec test/*-test.js"
+ }
+}
65 src/defer-tick.cc
@@ -0,0 +1,65 @@
+#include <node.h>
+
+namespace defer_tick {
+
+using namespace node;
+using namespace v8;
+
+#ifndef offset_of
+// g++ in strict mode complains loudly about the system offsetof() macro
+// because it uses NULL as the base address.
+# define offset_of(type, member) \
+ ((intptr_t) ((char *) &(((type *) 8)->member) - 8))
+#endif
+
+#ifndef container_of
+# define container_of(ptr, type, member) \
+ ((type *) ((char *) (ptr) - offset_of(type, member)))
+#endif
+
+struct DeferRequest {
+ Persistent<Function> callback;
+ uv_work_t work;
+};
+
+
+static void DeferNop(uv_work_t* work) {
+}
+
+
+static void DeferCallback(uv_work_t* work) {
+ HandleScope scope;
+
+ DeferRequest* req = container_of(work, DeferRequest, work);
+ MakeCallback(Context::GetCurrent()->Global(), req->callback, 0, NULL);
+
+ req->callback.Dispose();
+ req->callback.Clear();
+
+ delete req;
+}
+
+
+static Handle<Value> DeferTick(const Arguments& args) {
+ HandleScope scope;
+
+ if (args.Length() < 1 || !args[0]->IsFunction()) {
+ return ThrowException(String::New("First argument must be function"));
+ }
+
+ DeferRequest* req = new DeferRequest();
+ req->callback = Persistent<Function>::New(args[0].As<Function>());
+ uv_queue_work(uv_default_loop(), &req->work, DeferNop, DeferCallback);
+
+ return Null();
+}
+
+
+static void Init(Handle<Object> target) {
+ NODE_SET_METHOD(target, "deferTick", DeferTick);
+}
+
+
+NODE_MODULE(defer_tick, Init);
+
+} // namespace defer_tick
15 test/load-test.js
@@ -0,0 +1,15 @@
+var assert = require('assert'),
+ async = require('async'),
+ deferTick = require('..');
+
+describe('nextTick()', function() {
+ it('should fire one callback when queued', function(callback) {
+ deferTick(callback);
+ });
+
+ it('should fire tons of callback when queued', function(callback) {
+ async.map(new Array(2000).join('.').split('.'), function(_, callback) {
+ deferTick(callback);
+ }, callback);
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.