pax_global_header00006660000000000000000000000064127636300450014520gustar00rootroot0000000000000052 comment=0e614d9f5a7e6f0305c625f6b581f6d80b33b8a6 once-1.4.0/000077500000000000000000000000001276363004500124465ustar00rootroot00000000000000once-1.4.0/.travis.yml000066400000000000000000000002061276363004500145550ustar00rootroot00000000000000language: node_js language: node_js node_js: - '0.8' - '0.10' - '0.12' - 'iojs' before_install: - npm install -g npm@latest once-1.4.0/LICENSE000066400000000000000000000013751276363004500134610ustar00rootroot00000000000000The ISC License Copyright (c) Isaac Z. Schlueter and Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. once-1.4.0/README.md000066400000000000000000000033541276363004500137320ustar00rootroot00000000000000# once Only call a function once. ## usage ```javascript var once = require('once') function load (file, cb) { cb = once(cb) loader.load('file') loader.once('load', cb) loader.once('error', cb) } ``` Or add to the Function.prototype in a responsible way: ```javascript // only has to be done once require('once').proto() function load (file, cb) { cb = cb.once() loader.load('file') loader.once('load', cb) loader.once('error', 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')) }) } ``` ## `once.strict(func)` Throw an error if the function is called twice. Some functions are expected to be called only once. Using `once` for them would potentially hide logical errors. In the example below, the `greet` function has to call the callback only once: ```javascript function greet (name, cb) { // return is missing from the if statement // when no name is passed, the callback is called twice if (!name) cb('Hello anonymous') cb('Hello ' + name) } function log (msg) { console.log(msg) } // this will print 'Hello anonymous' but the logical error will be missed greet(null, once(msg)) // once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time greet(null, once.strict(msg)) ``` once-1.4.0/once.js000066400000000000000000000016471276363004500137400ustar00rootroot00000000000000var wrappy = require('wrappy') module.exports = wrappy(once) module.exports.strict = wrappy(onceStrict) once.proto = once(function () { Object.defineProperty(Function.prototype, 'once', { value: function () { return once(this) }, configurable: true }) Object.defineProperty(Function.prototype, 'onceStrict', { value: function () { return onceStrict(this) }, configurable: true }) }) function once (fn) { var f = function () { if (f.called) return f.value f.called = true return f.value = fn.apply(this, arguments) } f.called = false return f } function onceStrict (fn) { var f = function () { if (f.called) throw new Error(f.onceError) f.called = true return f.value = fn.apply(this, arguments) } var name = fn.name || 'Function wrapped with `once`' f.onceError = name + " shouldn't be called more than once" f.called = false return f } once-1.4.0/package.json000066400000000000000000000010761276363004500147400ustar00rootroot00000000000000{ "name": "once", "version": "1.4.0", "description": "Run a function exactly one time", "main": "once.js", "directories": { "test": "test" }, "dependencies": { "wrappy": "1" }, "devDependencies": { "tap": "^7.0.1" }, "scripts": { "test": "tap test/*.js" }, "files": [ "once.js" ], "repository": { "type": "git", "url": "git://github.com/isaacs/once" }, "keywords": [ "once", "function", "one", "single" ], "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "license": "ISC" } once-1.4.0/test/000077500000000000000000000000001276363004500134255ustar00rootroot00000000000000once-1.4.0/test/once.js000066400000000000000000000035131276363004500147110ustar00rootroot00000000000000var test = require('tap').test var once = require('../once.js') test('once', function (t) { var f = 0 function fn (g) { t.equal(f, 0) f ++ return f + g + this } fn.ownProperty = {} var foo = once(fn) t.equal(fn.ownProperty, foo.ownProperty) 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.ok(foo.called) t.same(g, 3) t.same(f, 1) } t.end() }) test('once.strict with named function', function (t) { var f = 0 function fn (g) { t.equal(f, 0) f ++ return f + g + this } fn.ownProperty = {} var foo = once.strict(fn) t.equal(fn.ownProperty, foo.ownProperty) t.notOk(foo.called) var g = foo.call(1, 1) t.ok(foo.called) t.same(g, 3) t.same(f, 1) try { foo.call(2, 2) t.fail('strict once should throw exception on second call') } catch (err) { t.ok(err instanceof Error) t.equal(err.message, "fn shouldn't be called more than once") t.end() } }) test('once.strict with anonymous function', function (t) { var foo = once.strict(function (g) { return g + 1 }) t.notOk(foo.called) var g = foo(1) t.ok(foo.called) t.same(g, 2) try { foo(2) t.fail('strict once should throw exception on second call') } catch (err) { t.ok(err instanceof Error) t.equal(err.message, "Function wrapped with `once` shouldn't be called more than once") t.end() } }) test('once.strict with custom error message', function (t) { var foo = once.strict(function (g) { return g + 1 }) foo.onceError = 'foo error' t.notOk(foo.called) var g = foo(1) t.ok(foo.called) t.same(g, 2) try { foo(2) t.fail('strict once should throw exception on second call') } catch (err) { t.ok(err instanceof Error) t.equal(err.message, 'foo error') t.end() } })