module-invalidate
Advanced tools
Comparing version 0.9.4 to 0.9.5
40
index.js
@@ -7,2 +7,7 @@ 'use strict'; | ||
const boundCachedSym = Symbol(); | ||
const invalidateCallbacksSym = Symbol(); | ||
const validateCallbacksSym = Symbol(); | ||
Module.invalidate = function() { | ||
@@ -32,6 +37,13 @@ | ||
if ( '_invalidateCallbacks' in this ) { | ||
if ( invalidateCallbacksSym in this ) { | ||
var validateCallbacks = this[validateCallbacksSym] || (this[validateCallbacksSym] = new Set); | ||
this._invalidateCallbacks.forEach(callback => callback(this._exports)); | ||
this._invalidateCallbacks.clear(); | ||
this[invalidateCallbacksSym].forEach(callback => { | ||
var validateCallback = callback(this._exports); | ||
if ( typeof(validateCallback) === 'function' ) | ||
validateCallbacks.add(validateCallback); | ||
}); | ||
this[invalidateCallbacksSym].clear(); | ||
} | ||
@@ -44,3 +56,3 @@ | ||
var invalidateCallbacks = this._invalidateCallbacks || (this._invalidateCallbacks = new Set); | ||
var invalidateCallbacks = this[invalidateCallbacksSym] || (this[invalidateCallbacksSym] = new Set); | ||
return invalidateCallbacks.add(callback).delete.bind(invalidateCallbacks, callback); | ||
@@ -54,5 +66,10 @@ } | ||
mod.load(mod.filename); | ||
if ( validateCallbacksSym in mod ) { | ||
mod[validateCallbacksSym].forEach(callback => callback(mod._exports) ); | ||
mod[validateCallbacksSym].clear(); | ||
} | ||
} | ||
const boundCached = Symbol(); | ||
@@ -123,7 +140,7 @@ function createProxy(mod) { | ||
// needed for native function, like Promise.resolve().then, ... | ||
if ( boundCached in val ) | ||
return val[boundCached]; | ||
if ( boundCachedSym in val ) | ||
return val[boundCachedSym]; | ||
var bound = val.bind(mod._exports); | ||
Object.setPrototypeOf(bound, val); // see test "exports property on function" | ||
val[boundCached] = bound; | ||
val[boundCachedSym] = bound; | ||
return bound; | ||
@@ -151,5 +168,6 @@ } | ||
// see https://tc39.github.io/ecma262/#sec-invariants-of-the-essential-internal-methods | ||
//throw new TypeError('ownKeys not implemented'); | ||
return Reflect.ownKeys(target).concat(Reflect.ownKeys(mod._exports)); | ||
//return Reflect.ownKeys(mod._exports); | ||
var ownKeys = Reflect.ownKeys(mod._exports); | ||
if ( typeof mod._exports !== 'function' ) | ||
ownKeys.push('prototype'); | ||
return ownKeys; | ||
}, | ||
@@ -156,0 +174,0 @@ |
{ | ||
"name": "module-invalidate", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"description": "invalidate required modules", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -236,4 +236,33 @@ # module-invalidate | ||
Gives you the opportunity to free resources created in the module. | ||
eg. temporary files, timers, web routes, ... | ||
The `callback` can return function that is called after the module is reloaded. This can help you restore your module state. | ||
##### Example: | ||
```JavaScript | ||
module.invalidable = true; | ||
this.connectedUsers = []; | ||
exports.connectUser = function(name) { | ||
this.connectedUsers.push(name); | ||
} | ||
exports.getConnectedUsers = function() { | ||
return this.connectedUsers; | ||
} | ||
module.onInvalidate(function(oldExports) { | ||
return function(newExports) { | ||
newExports.connectedUsers = oldExports.connectedUsers; | ||
} | ||
}); | ||
``` | ||
## How it works | ||
@@ -240,0 +269,0 @@ |
@@ -180,2 +180,19 @@ var assert = require('assert'); | ||
it('exports json', function() { | ||
var val = 1; | ||
var mod = new utils.TmpModule(_ =>`{ "a":${val} }`, { ext: 'json' }); | ||
mod.module.invalidable = true; | ||
assert.equal(mod.module.exports.a, 1); | ||
val++; | ||
mod.set(); | ||
mod.module.invalidate(); | ||
assert.equal(mod.module.exports.a, 2); | ||
}); | ||
}); |
@@ -89,3 +89,43 @@ var assert = require('assert'); | ||
}); | ||
it('onInvalidate callback all', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
this.connectedUsers = []; | ||
exports.connectUser = function(name) { | ||
this.connectedUsers.push(name); | ||
} | ||
exports.getConnectedUsers = function() { | ||
return this.connectedUsers; | ||
} | ||
module.onInvalidate(function(immutableExports) { | ||
return function(newExports) { | ||
newExports.connectedUsers = immutableExports.connectedUsers; | ||
} | ||
}); | ||
`); | ||
mod.module.exports.connectUser('foo'); | ||
mod.module.exports.connectUser('bar'); | ||
assert.equal(mod.module.exports.getConnectedUsers().join(), 'foo,bar'); | ||
module.invalidateByPath(mod.module.filename); | ||
assert.equal(mod.module.exports.getConnectedUsers().join(), 'foo,bar'); | ||
}); | ||
}); |
@@ -23,3 +23,3 @@ const fs = require('fs'); | ||
exports.TmpModule = function(moduleContent, opts) { | ||
exports.TmpModule = function(moduleContent, opts = {}) { | ||
@@ -29,3 +29,4 @@ moduleIndex++; | ||
this.filename = path.join(__dirname, `_tmp_mod${moduleIndex}.js`); | ||
var ext = opts.ext || 'js'; | ||
this.filename = path.join(__dirname, `_tmp_mod${moduleIndex}.${ext}`); | ||
@@ -32,0 +33,0 @@ Object.defineProperty(this, 'module', { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
35104
772
334
0
26