express-state
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -23,2 +23,7 @@ /* | ||
// Defines a "hidden" property that stores with the value of | ||
// `options.isJSON` that `add()` was called with. This allows for a | ||
// hot-path to be taken during serialization. | ||
__json__: {value: {}}, | ||
// Defines a "hidden" property that stores serializations of data by | ||
@@ -32,2 +37,3 @@ // namespace that was exposed and deemed cacheable; e.g. won't be | ||
Exposed.create = function (exposed) { | ||
// If we're not inheriting, return a new instance. | ||
if (!Exposed.isExposed(exposed)) { | ||
@@ -39,8 +45,8 @@ return new Exposed(); | ||
// prototype. This allows the new object to inherit from, *and* shadow the | ||
// existing object. A prototype relationship is also setup for the | ||
// serialized cached state, aggregation of applicable namespaces happens at | ||
// existing object. Aggregation of applicable namespaces happens at | ||
// `toString()` time. | ||
return Object.create(exposed, { | ||
__namespaces__: {value: []}, | ||
__serialized__: {value: Object.create(exposed.__serialized__)} | ||
__json__ : {value: {}}, | ||
__serialized__: {value: {}} | ||
}); | ||
@@ -54,11 +60,15 @@ }; | ||
Exposed.prototype.add = function (namespace, value, options) { | ||
options || (options = {}); | ||
var nsRegex = new RegExp('^' + namespace + '(?:$|\\..+)'), | ||
namespaces = this.__namespaces__, | ||
oldNamespaces = namespaces.filter(nsRegex.test.bind(nsRegex)), | ||
json = this.__json__, | ||
serialized = this.__serialized__; | ||
// Removes previously exposed namespaces, values, and serialized state which | ||
// no longer apply and have become noops. | ||
// Removes previously exposed namespaces, values, isJSON setting, and | ||
// serialized state which no longer apply and have become noops. | ||
oldNamespaces.forEach(function (namespace) { | ||
namespaces.splice(namespaces.indexOf(namespace), 1); | ||
delete json[namespace]; | ||
delete serialized[namespace]; | ||
@@ -72,7 +82,12 @@ delete this[namespace]; | ||
// Stores the value of `options.isJSON` so it can be passed along to | ||
// `serialize()`. When set, this option speeds up serialization. | ||
var isJSON = !!options.isJSON; | ||
json[namespace] = isJSON; | ||
// When it's deemed safe to cache the serialized form of the `value` because | ||
// it won't change, run the serialization process once, eagerly. The result | ||
// is cached to greatly optimize to speed of the `toString()` method. | ||
if (options && options.cache) { | ||
serialized[namespace] = serialize(value); | ||
if (options.cache) { | ||
serialized[namespace] = serialize(value, {isJSON: isJSON}); | ||
} | ||
@@ -164,7 +179,8 @@ }; | ||
Exposed.prototype._getSerializedValue = function (namespace) { | ||
var serialized = this.__serialized__, | ||
var json = this.__json__, | ||
serialized = this.__serialized__, | ||
proto; | ||
// Own pre-serialized value. | ||
if (serialized.hasOwnProperty(namespace)) { | ||
if (serialized[namespace]) { | ||
return serialized[namespace]; | ||
@@ -175,3 +191,3 @@ } | ||
if (this.hasOwnProperty(namespace)) { | ||
return serialize(this[namespace]); | ||
return serialize(this[namespace], {isJSON: json[namespace]}); | ||
} | ||
@@ -178,0 +194,0 @@ |
{ | ||
"name": "express-state", | ||
"description": "Share server-side state with the client-side of an Express app via JavaScript.", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"licenses": [ | ||
@@ -50,3 +50,3 @@ { | ||
"dependencies": { | ||
"serialize-javascript": "~1.1.0" | ||
"serialize-javascript": "^1.1.0" | ||
}, | ||
@@ -53,0 +53,0 @@ "devDependencies": { |
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
20663
243
+ Addedserialize-javascript@1.9.1(transitive)
- Removedserialize-javascript@1.1.2(transitive)
Updatedserialize-javascript@^1.1.0