@kjots/geheugen
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -7,65 +7,95 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.kjotsgeheugen = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
}); | ||
class Memo { | ||
constructor({ | ||
Q = Promise, | ||
singleton = true, | ||
dependencies = [], | ||
onSet = () => {}, | ||
onReset = () => {}, | ||
factory = () => {}, | ||
promise, | ||
value | ||
} = {}) { | ||
dependencies.forEach(dependency => { | ||
dependency.dependants.push(this); | ||
}); | ||
return Object.assign(this, { Q, singleton, dependencies, onSet, onReset, factory, promise, value, dependants: [] }); | ||
} | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
resolve() { | ||
if (this.singleton && this.value !== undefined) { | ||
return this.Q.resolve(this.value); | ||
} else if (this.promise !== undefined) { | ||
return this.promise; | ||
} else { | ||
this.promise = this.Q.all(this.dependencies.map(dependency => dependency.resolve())).then(this.factory).then(value => { | ||
this.value = value; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
return value; | ||
}).finally(() => { | ||
delete this.promise; | ||
}); | ||
var Memo = exports.Memo = function () { | ||
function Memo() { | ||
var _this = this; | ||
return this.promise; | ||
} | ||
} | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref$Q = _ref.Q, | ||
Q = _ref$Q === undefined ? Promise : _ref$Q, | ||
_ref$singleton = _ref.singleton, | ||
singleton = _ref$singleton === undefined ? true : _ref$singleton, | ||
_ref$dependencies = _ref.dependencies, | ||
dependencies = _ref$dependencies === undefined ? [] : _ref$dependencies, | ||
_ref$onSet = _ref.onSet, | ||
onSet = _ref$onSet === undefined ? function () {} : _ref$onSet, | ||
_ref$onReset = _ref.onReset, | ||
onReset = _ref$onReset === undefined ? function () {} : _ref$onReset, | ||
_ref$factory = _ref.factory, | ||
factory = _ref$factory === undefined ? function () {} : _ref$factory, | ||
promise = _ref.promise, | ||
value = _ref.value; | ||
get() { | ||
return this.value; | ||
_classCallCheck(this, Memo); | ||
dependencies.forEach(function (dependency) { | ||
dependency.dependants.push(_this); | ||
}); | ||
return Object.assign(this, { Q: Q, singleton: singleton, dependencies: dependencies, onSet: onSet, onReset: onReset, factory: factory, promise: promise, value: value, dependants: [] }); | ||
} | ||
set(value) { | ||
this.onSet(value); | ||
_createClass(Memo, [{ | ||
key: "resolve", | ||
value: function resolve() { | ||
var _this2 = this; | ||
this.resetDependants(); | ||
if (this.singleton && this.value !== undefined) { | ||
return this.Q.resolve(this.value); | ||
} else if (this.promise !== undefined) { | ||
return this.promise; | ||
} else { | ||
this.promise = this.Q.all(this.dependencies.map(function (dependency) { | ||
return dependency.resolve(); | ||
})).then(this.factory).then(function (value) { | ||
_this2.value = value; | ||
this.value = value; | ||
} | ||
return value; | ||
}).finally(function () { | ||
delete _this2.promise; | ||
}); | ||
reset() { | ||
this.onReset(); | ||
return this.promise; | ||
} | ||
} | ||
}, { | ||
key: "get", | ||
value: function get() { | ||
return this.value; | ||
} | ||
}, { | ||
key: "set", | ||
value: function set(value) { | ||
this.onSet(value); | ||
this.resetDependants(); | ||
this.resetDependants(); | ||
delete this.value; | ||
} | ||
this.value = value; | ||
} | ||
}, { | ||
key: "reset", | ||
value: function reset() { | ||
this.onReset(); | ||
resetDependants() { | ||
this.dependants.forEach(dependant => dependant.reset()); | ||
} | ||
} | ||
exports.Memo = Memo; | ||
this.resetDependants(); | ||
delete this.value; | ||
} | ||
}, { | ||
key: "resetDependants", | ||
value: function resetDependants() { | ||
this.dependants.forEach(function (dependant) { | ||
return dependant.reset(); | ||
}); | ||
} | ||
}]); | ||
return Memo; | ||
}(); | ||
},{}],2:[function(require,module,exports){ | ||
@@ -84,3 +114,3 @@ 'use strict'; | ||
enumerable: true, | ||
get: function () { | ||
get: function get() { | ||
return _Memo[key]; | ||
@@ -91,4 +121,8 @@ } | ||
exports.memoise = memoise; | ||
function memoise(...args) { | ||
return new _Memo.Memo(...args); | ||
function memoise() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return new (Function.prototype.bind.apply(_Memo.Memo, [null].concat(args)))(); | ||
} | ||
@@ -95,0 +129,0 @@ |
@@ -13,3 +13,3 @@ 'use strict'; | ||
enumerable: true, | ||
get: function () { | ||
get: function get() { | ||
return _Memo[key]; | ||
@@ -20,5 +20,9 @@ } | ||
exports.memoise = memoise; | ||
function memoise(...args) { | ||
return new _Memo.Memo(...args); | ||
function memoise() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return new (Function.prototype.bind.apply(_Memo.Memo, [null].concat(args)))(); | ||
} | ||
//# sourceMappingURL=index.js.map |
126
lib/Memo.js
@@ -6,63 +6,93 @@ "use strict"; | ||
}); | ||
class Memo { | ||
constructor({ | ||
Q = Promise, | ||
singleton = true, | ||
dependencies = [], | ||
onSet = () => {}, | ||
onReset = () => {}, | ||
factory = () => {}, | ||
promise, | ||
value | ||
} = {}) { | ||
dependencies.forEach(dependency => { | ||
dependency.dependants.push(this); | ||
}); | ||
return Object.assign(this, { Q, singleton, dependencies, onSet, onReset, factory, promise, value, dependants: [] }); | ||
} | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
resolve() { | ||
if (this.singleton && this.value !== undefined) { | ||
return this.Q.resolve(this.value); | ||
} else if (this.promise !== undefined) { | ||
return this.promise; | ||
} else { | ||
this.promise = this.Q.all(this.dependencies.map(dependency => dependency.resolve())).then(this.factory).then(value => { | ||
this.value = value; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
return value; | ||
}).finally(() => { | ||
delete this.promise; | ||
}); | ||
var Memo = exports.Memo = function () { | ||
function Memo() { | ||
var _this = this; | ||
return this.promise; | ||
} | ||
} | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref$Q = _ref.Q, | ||
Q = _ref$Q === undefined ? Promise : _ref$Q, | ||
_ref$singleton = _ref.singleton, | ||
singleton = _ref$singleton === undefined ? true : _ref$singleton, | ||
_ref$dependencies = _ref.dependencies, | ||
dependencies = _ref$dependencies === undefined ? [] : _ref$dependencies, | ||
_ref$onSet = _ref.onSet, | ||
onSet = _ref$onSet === undefined ? function () {} : _ref$onSet, | ||
_ref$onReset = _ref.onReset, | ||
onReset = _ref$onReset === undefined ? function () {} : _ref$onReset, | ||
_ref$factory = _ref.factory, | ||
factory = _ref$factory === undefined ? function () {} : _ref$factory, | ||
promise = _ref.promise, | ||
value = _ref.value; | ||
get() { | ||
return this.value; | ||
_classCallCheck(this, Memo); | ||
dependencies.forEach(function (dependency) { | ||
dependency.dependants.push(_this); | ||
}); | ||
return Object.assign(this, { Q: Q, singleton: singleton, dependencies: dependencies, onSet: onSet, onReset: onReset, factory: factory, promise: promise, value: value, dependants: [] }); | ||
} | ||
set(value) { | ||
this.onSet(value); | ||
_createClass(Memo, [{ | ||
key: "resolve", | ||
value: function resolve() { | ||
var _this2 = this; | ||
this.resetDependants(); | ||
if (this.singleton && this.value !== undefined) { | ||
return this.Q.resolve(this.value); | ||
} else if (this.promise !== undefined) { | ||
return this.promise; | ||
} else { | ||
this.promise = this.Q.all(this.dependencies.map(function (dependency) { | ||
return dependency.resolve(); | ||
})).then(this.factory).then(function (value) { | ||
_this2.value = value; | ||
this.value = value; | ||
} | ||
return value; | ||
}).finally(function () { | ||
delete _this2.promise; | ||
}); | ||
reset() { | ||
this.onReset(); | ||
return this.promise; | ||
} | ||
} | ||
}, { | ||
key: "get", | ||
value: function get() { | ||
return this.value; | ||
} | ||
}, { | ||
key: "set", | ||
value: function set(value) { | ||
this.onSet(value); | ||
this.resetDependants(); | ||
this.resetDependants(); | ||
delete this.value; | ||
} | ||
this.value = value; | ||
} | ||
}, { | ||
key: "reset", | ||
value: function reset() { | ||
this.onReset(); | ||
resetDependants() { | ||
this.dependants.forEach(dependant => dependant.reset()); | ||
} | ||
} | ||
exports.Memo = Memo; | ||
this.resetDependants(); | ||
delete this.value; | ||
} | ||
}, { | ||
key: "resetDependants", | ||
value: function resetDependants() { | ||
this.dependants.forEach(function (dependant) { | ||
return dependant.reset(); | ||
}); | ||
} | ||
}]); | ||
return Memo; | ||
}(); | ||
//# sourceMappingURL=Memo.js.map |
{ | ||
"name": "@kjots/geheugen", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "A memoisation toolkit", | ||
@@ -33,4 +33,4 @@ "main": "lib/index.js", | ||
"babel-core": "^6.21.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0", | ||
"babel-polyfill": "^6.20.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-preset-es2016": "^6.16.0", | ||
@@ -37,0 +37,0 @@ "babel-preset-es2017": "^6.16.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51843
604