diff options
Diffstat (limited to 'node_modules/once')
-rw-r--r-- | node_modules/once/README.md | 18 | ||||
-rw-r--r-- | node_modules/once/once.js | 11 | ||||
-rw-r--r-- | node_modules/once/package.json | 12 | ||||
-rw-r--r-- | node_modules/once/test/once.js | 4 |
4 files changed, 35 insertions, 10 deletions
diff --git a/node_modules/once/README.md b/node_modules/once/README.md index e833b83d2..a2981ea07 100644 --- a/node_modules/once/README.md +++ b/node_modules/once/README.md @@ -31,3 +31,21 @@ function load (file, cb) { Ironically, the prototype feature makes this module twice as complicated as necessary. + +To check whether you function has been called, use `fn.called`. Once the +function is called for the first time the return value of the original +function is saved in `fn.value` and subsequent calls will continue to +return this value. + +```javascript +var once = require('once') + +function load (cb) { + cb = once(cb) + var stream = createStream() + stream.once('data', cb) + stream.once('end', function () { + if (!cb.called) cb(new Error('not found')) + }) +} +``` diff --git a/node_modules/once/once.js b/node_modules/once/once.js index effc50a47..0770a73cd 100644 --- a/node_modules/once/once.js +++ b/node_modules/once/once.js @@ -10,10 +10,11 @@ once.proto = once(function () { }) function once (fn) { - var called = false - return function () { - if (called) return - called = true - return fn.apply(this, arguments) + var f = function () { + if (f.called) return f.value + f.called = true + return f.value = fn.apply(this, arguments) } + f.called = false + return f } diff --git a/node_modules/once/package.json b/node_modules/once/package.json index 22671632e..96af3dedf 100644 --- a/node_modules/once/package.json +++ b/node_modules/once/package.json @@ -1,6 +1,6 @@ { "name": "once", - "version": "1.1.1", + "version": "1.3.0", "description": "Run a function exactly one time", "main": "once.js", "directories": { @@ -29,7 +29,11 @@ "url": "http://blog.izs.me/" }, "license": "BSD", - "readme": "# once\n\nOnly call a function once.\n\n## usage\n\n```javascript\nvar once = require('once')\n\nfunction load (file, cb) {\n cb = once(cb)\n loader.load('file')\n loader.once('load', cb)\n loader.once('error', cb)\n}\n```\n\nOr add to the Function.prototype in a responsible way:\n\n```javascript\n// only has to be done once\nrequire('once').proto()\n\nfunction load (file, cb) {\n cb = cb.once()\n loader.load('file')\n loader.once('load', cb)\n loader.once('error', cb)\n}\n```\n\nIronically, the prototype feature makes this module twice as\ncomplicated as necessary.\n", - "_id": "once@1.1.1", - "_from": "once" + "readme": "# once\n\nOnly call a function once.\n\n## usage\n\n```javascript\nvar once = require('once')\n\nfunction load (file, cb) {\n cb = once(cb)\n loader.load('file')\n loader.once('load', cb)\n loader.once('error', cb)\n}\n```\n\nOr add to the Function.prototype in a responsible way:\n\n```javascript\n// only has to be done once\nrequire('once').proto()\n\nfunction load (file, cb) {\n cb = cb.once()\n loader.load('file')\n loader.once('load', cb)\n loader.once('error', cb)\n}\n```\n\nIronically, the prototype feature makes this module twice as\ncomplicated as necessary.\n\nTo check whether you function has been called, use `fn.called`. Once the\nfunction is called for the first time the return value of the original\nfunction is saved in `fn.value` and subsequent calls will continue to\nreturn this value.\n\n```javascript\nvar once = require('once')\n\nfunction load (cb) {\n cb = once(cb)\n var stream = createStream()\n stream.once('data', cb)\n stream.once('end', function () {\n if (!cb.called) cb(new Error('not found'))\n })\n}\n```\n", + "readmeFilename": "README.md", + "bugs": { + "url": "https://github.com/isaacs/once/issues" + }, + "_id": "once@1.3.0", + "_from": "once@latest" } diff --git a/node_modules/once/test/once.js b/node_modules/once/test/once.js index f0291a44f..a77951f11 100644 --- a/node_modules/once/test/once.js +++ b/node_modules/once/test/once.js @@ -8,10 +8,12 @@ test('once', function (t) { f ++ return f + g + this }) + t.notOk(foo.called) for (var i = 0; i < 1E3; i++) { t.same(f, i === 0 ? 0 : 1) var g = foo.call(1, 1) - t.same(g, i === 0 ? 3 : undefined) + t.ok(foo.called) + t.same(g, 3) t.same(f, 1) } t.end() |