Socket
Socket
Sign inDemoInstall

mobx-utils

Package Overview
Dependencies
Maintainers
4
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-utils - npm Package Compare versions

Comparing version 6.0.0 to 6.0.1

4

CHANGELOG.md

@@ -0,1 +1,5 @@

# 6.0.1
* Fixed build issue causing decorators in the final build version not to be picked up correctly. Fixes [#279](https://github.com/mobxjs/mobx-utils/issues/279)
# 6.0.0

@@ -2,0 +6,0 @@

95

lib/create-view-model.js

@@ -24,8 +24,28 @@ var __assign = (this && this.__assign) || function () {

var _this = this;
this.model = model;
this.localValues = observable.map({});
this.localComputedValues = observable.map({});
this.isPropertyDirty = function (key) {
return _this.localValues.has(key);
};
Object.defineProperty(this, "model", {
enumerable: true,
configurable: true,
writable: true,
value: model
});
Object.defineProperty(this, "localValues", {
enumerable: true,
configurable: true,
writable: true,
value: observable.map({})
});
Object.defineProperty(this, "localComputedValues", {
enumerable: true,
configurable: true,
writable: true,
value: observable.map({})
});
Object.defineProperty(this, "isPropertyDirty", {
enumerable: true,
configurable: true,
writable: true,
value: function (key) {
return _this.localValues.has(key);
}
});
makeObservable(this);

@@ -76,26 +96,41 @@ invariant(isObservableObject(model), "createViewModel expects an observable object");

});
ViewModel.prototype.submit = function () {
var _this = this;
keys(this.localValues).forEach(function (key) {
var source = _this.localValues.get(key);
var destination = _this.model[key];
if (isObservableArray(destination)) {
destination.replace(source);
}
else if (isObservableMap(destination)) {
destination.clear();
destination.merge(source);
}
else if (!isComputed(source)) {
_this.model[key] = source;
}
});
this.localValues.clear();
};
ViewModel.prototype.reset = function () {
this.localValues.clear();
};
ViewModel.prototype.resetProperty = function (key) {
this.localValues.delete(key);
};
Object.defineProperty(ViewModel.prototype, "submit", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
var _this = this;
keys(this.localValues).forEach(function (key) {
var source = _this.localValues.get(key);
var destination = _this.model[key];
if (isObservableArray(destination)) {
destination.replace(source);
}
else if (isObservableMap(destination)) {
destination.clear();
destination.merge(source);
}
else if (!isComputed(source)) {
_this.model[key] = source;
}
});
this.localValues.clear();
}
});
Object.defineProperty(ViewModel.prototype, "reset", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
this.localValues.clear();
}
});
Object.defineProperty(ViewModel.prototype, "resetProperty", {
enumerable: false,
configurable: true,
writable: true,
value: function (key) {
this.localValues.delete(key);
}
});
__decorate([

@@ -102,0 +137,0 @@ computed

@@ -6,6 +6,38 @@ /**

function DeepMapEntry(base, args) {
this.base = base;
this.args = args;
this.closestIdx = 0;
this.isDisposed = false;
Object.defineProperty(this, "base", {
enumerable: true,
configurable: true,
writable: true,
value: base
});
Object.defineProperty(this, "args", {
enumerable: true,
configurable: true,
writable: true,
value: args
});
Object.defineProperty(this, "root", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "closest", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "closestIdx", {
enumerable: true,
configurable: true,
writable: true,
value: 0
});
Object.defineProperty(this, "isDisposed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
var current = (this.closest = this.root = base);

@@ -22,51 +54,76 @@ var i = 0;

}
DeepMapEntry.prototype.exists = function () {
this.assertNotDisposed();
var l = this.args.length;
return this.closestIdx >= l - 1 && this.closest.has(this.args[l - 1]);
};
DeepMapEntry.prototype.get = function () {
this.assertNotDisposed();
if (!this.exists())
throw new Error("Entry doesn't exist");
return this.closest.get(this.args[this.args.length - 1]);
};
DeepMapEntry.prototype.set = function (value) {
this.assertNotDisposed();
var l = this.args.length;
var current = this.closest;
// create remaining maps
for (var i = this.closestIdx; i < l - 1; i++) {
var m = new Map();
current.set(this.args[i], m);
current = m;
Object.defineProperty(DeepMapEntry.prototype, "exists", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
this.assertNotDisposed();
var l = this.args.length;
return this.closestIdx >= l - 1 && this.closest.has(this.args[l - 1]);
}
this.closestIdx = l - 1;
this.closest = current;
current.set(this.args[l - 1], value);
};
DeepMapEntry.prototype.delete = function () {
this.assertNotDisposed();
if (!this.exists())
throw new Error("Entry doesn't exist");
var l = this.args.length;
this.closest.delete(this.args[l - 1]);
// clean up remaining maps if needed (reconstruct stack first)
var c = this.root;
var maps = [c];
for (var i = 0; i < l - 1; i++) {
c = c.get(this.args[i]);
maps.push(c);
});
Object.defineProperty(DeepMapEntry.prototype, "get", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
this.assertNotDisposed();
if (!this.exists())
throw new Error("Entry doesn't exist");
return this.closest.get(this.args[this.args.length - 1]);
}
for (var i = maps.length - 1; i > 0; i--) {
if (maps[i].size === 0)
maps[i - 1].delete(this.args[i - 1]);
});
Object.defineProperty(DeepMapEntry.prototype, "set", {
enumerable: false,
configurable: true,
writable: true,
value: function (value) {
this.assertNotDisposed();
var l = this.args.length;
var current = this.closest;
// create remaining maps
for (var i = this.closestIdx; i < l - 1; i++) {
var m = new Map();
current.set(this.args[i], m);
current = m;
}
this.closestIdx = l - 1;
this.closest = current;
current.set(this.args[l - 1], value);
}
this.isDisposed = true;
};
DeepMapEntry.prototype.assertNotDisposed = function () {
// TODO: once this becomes annoying, we should introduce a reset method to re-run the constructor logic
if (this.isDisposed)
throw new Error("Concurrent modification exception");
};
});
Object.defineProperty(DeepMapEntry.prototype, "delete", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
this.assertNotDisposed();
if (!this.exists())
throw new Error("Entry doesn't exist");
var l = this.args.length;
this.closest.delete(this.args[l - 1]);
// clean up remaining maps if needed (reconstruct stack first)
var c = this.root;
var maps = [c];
for (var i = 0; i < l - 1; i++) {
c = c.get(this.args[i]);
maps.push(c);
}
for (var i = maps.length - 1; i > 0; i--) {
if (maps[i].size === 0)
maps[i - 1].delete(this.args[i - 1]);
}
this.isDisposed = true;
}
});
Object.defineProperty(DeepMapEntry.prototype, "assertNotDisposed", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
// TODO: once this becomes annoying, we should introduce a reset method to re-run the constructor logic
if (this.isDisposed)
throw new Error("Concurrent modification exception");
}
});
return DeepMapEntry;

@@ -80,16 +137,37 @@ }());

function DeepMap() {
this.store = new Map();
this.argsLength = -1;
Object.defineProperty(this, "store", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
Object.defineProperty(this, "argsLength", {
enumerable: true,
configurable: true,
writable: true,
value: -1
});
Object.defineProperty(this, "last", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
DeepMap.prototype.entry = function (args) {
if (this.argsLength === -1)
this.argsLength = args.length;
else if (this.argsLength !== args.length)
throw new Error("DeepMap should be used with functions with a consistent length, expected: " + this.argsLength + ", got: " + args.length);
if (this.last)
this.last.isDisposed = true;
return (this.last = new DeepMapEntry(this.store, args));
};
Object.defineProperty(DeepMap.prototype, "entry", {
enumerable: false,
configurable: true,
writable: true,
value: function (args) {
if (this.argsLength === -1)
this.argsLength = args.length;
else if (this.argsLength !== args.length)
throw new Error("DeepMap should be used with functions with a consistent length, expected: " + this.argsLength + ", got: " + args.length);
if (this.last)
this.last.isDisposed = true;
return (this.last = new DeepMapEntry(this.store, args));
}
});
return DeepMap;
}());
export { DeepMap };

@@ -69,2 +69,14 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

var _this = this;
Object.defineProperty(this, "current", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "subscription", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
makeObservable(this);

@@ -76,17 +88,37 @@ runInAction(function () {

}
StreamListener.prototype.dispose = function () {
if (this.subscription) {
this.subscription.unsubscribe();
Object.defineProperty(StreamListener.prototype, "dispose", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
};
StreamListener.prototype.next = function (value) {
this.current = value;
};
StreamListener.prototype.complete = function () {
this.dispose();
};
StreamListener.prototype.error = function (value) {
this.current = value;
this.dispose();
};
});
Object.defineProperty(StreamListener.prototype, "next", {
enumerable: false,
configurable: true,
writable: true,
value: function (value) {
this.current = value;
}
});
Object.defineProperty(StreamListener.prototype, "complete", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
this.dispose();
}
});
Object.defineProperty(StreamListener.prototype, "error", {
enumerable: false,
configurable: true,
writable: true,
value: function (value) {
this.current = value;
this.dispose();
}
});
__decorate([

@@ -93,0 +125,0 @@ observable.ref

@@ -5,3 +5,3 @@ var __extends = (this && this.__extends) || (function () {

({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);

@@ -54,2 +54,45 @@ };

var _this = _super.call(this) || this;
/**
* Base observable array which is being sorted into groups.
*/
Object.defineProperty(_this, "_base", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The ObservableGroupMap needs to track some state per-item. This is the name/symbol of the
* property used to attach the state.
*/
Object.defineProperty(_this, "_ogmInfoKey", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The function used to group the items.
*/
Object.defineProperty(_this, "_groupBy", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* This function is used to generate the mobx debug names of the observable group arrays.
*/
Object.defineProperty(_this, "_keyToName", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(_this, "_disposeBaseObserver", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
_this._keyToName = keyToName;

@@ -87,11 +130,26 @@ _this._groupBy = groupBy;

}
ObservableGroupMap.prototype.clear = function () {
throw new Error("not supported");
};
ObservableGroupMap.prototype.delete = function (_key) {
throw new Error("not supported");
};
ObservableGroupMap.prototype.set = function (_key, _value) {
throw new Error("not supported");
};
Object.defineProperty(ObservableGroupMap.prototype, "clear", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
throw new Error("not supported");
}
});
Object.defineProperty(ObservableGroupMap.prototype, "delete", {
enumerable: false,
configurable: true,
writable: true,
value: function (_key) {
throw new Error("not supported");
}
});
Object.defineProperty(ObservableGroupMap.prototype, "set", {
enumerable: false,
configurable: true,
writable: true,
value: function (_key, _value) {
throw new Error("not supported");
}
});
/**

@@ -101,67 +159,92 @@ * Disposes all observers created during construction and removes state added to base array

*/
ObservableGroupMap.prototype.dispose = function () {
this._disposeBaseObserver();
for (var i = 0; i < this._base.length; i++) {
var item = this._base[i];
var grouperItemInfo = item[this._ogmInfoKey];
grouperItemInfo.reaction();
delete item[this._ogmInfoKey];
Object.defineProperty(ObservableGroupMap.prototype, "dispose", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
this._disposeBaseObserver();
for (var i = 0; i < this._base.length; i++) {
var item = this._base[i];
var grouperItemInfo = item[this._ogmInfoKey];
grouperItemInfo.reaction();
delete item[this._ogmInfoKey];
}
}
};
ObservableGroupMap.prototype._getGroupArr = function (key) {
var result = _super.prototype.get.call(this, key);
if (undefined === result) {
result = observable([], { name: "GroupArray[" + this._keyToName(key) + "]", deep: false });
_super.prototype.set.call(this, key, result);
});
Object.defineProperty(ObservableGroupMap.prototype, "_getGroupArr", {
enumerable: false,
configurable: true,
writable: true,
value: function (key) {
var result = _super.prototype.get.call(this, key);
if (undefined === result) {
result = observable([], { name: "GroupArray[" + this._keyToName(key) + "]", deep: false });
_super.prototype.set.call(this, key, result);
}
return result;
}
return result;
};
ObservableGroupMap.prototype._removeFromGroupArr = function (key, itemIndex) {
var arr = _super.prototype.get.call(this, key);
if (1 === arr.length) {
_super.prototype.delete.call(this, key);
});
Object.defineProperty(ObservableGroupMap.prototype, "_removeFromGroupArr", {
enumerable: false,
configurable: true,
writable: true,
value: function (key, itemIndex) {
var arr = _super.prototype.get.call(this, key);
if (1 === arr.length) {
_super.prototype.delete.call(this, key);
}
else if (itemIndex === arr.length - 1) {
// last position in array
arr.length--;
}
else {
arr[itemIndex] = arr[arr.length - 1];
arr[itemIndex][this._ogmInfoKey].groupArrIndex = itemIndex;
arr.length--;
}
}
else if (itemIndex === arr.length - 1) {
// last position in array
arr.length--;
});
Object.defineProperty(ObservableGroupMap.prototype, "_addItem", {
enumerable: false,
configurable: true,
writable: true,
value: function (item) {
var _this = this;
var groupByValue = this._groupBy(item);
var groupArr = this._getGroupArr(groupByValue);
var value = {
groupByValue: groupByValue,
groupArrIndex: groupArr.length,
reaction: reaction(function () { return _this._groupBy(item); }, function (newGroupByValue, _r) {
console.log("new group by value ", newGroupByValue);
var grouperItemInfo = item[_this._ogmInfoKey];
_this._removeFromGroupArr(grouperItemInfo.groupByValue, grouperItemInfo.groupArrIndex);
var newGroupArr = _this._getGroupArr(newGroupByValue);
var newGroupArrIndex = newGroupArr.length;
newGroupArr.push(item);
grouperItemInfo.groupByValue = newGroupByValue;
grouperItemInfo.groupArrIndex = newGroupArrIndex;
}),
};
Object.defineProperty(item, this._ogmInfoKey, {
configurable: true,
enumerable: false,
value: value,
});
groupArr.push(item);
}
else {
arr[itemIndex] = arr[arr.length - 1];
arr[itemIndex][this._ogmInfoKey].groupArrIndex = itemIndex;
arr.length--;
});
Object.defineProperty(ObservableGroupMap.prototype, "_removeItem", {
enumerable: false,
configurable: true,
writable: true,
value: function (item) {
var grouperItemInfo = item[this._ogmInfoKey];
this._removeFromGroupArr(grouperItemInfo.groupByValue, grouperItemInfo.groupArrIndex);
grouperItemInfo.reaction();
delete item[this._ogmInfoKey];
}
};
ObservableGroupMap.prototype._addItem = function (item) {
var _this = this;
var groupByValue = this._groupBy(item);
var groupArr = this._getGroupArr(groupByValue);
var value = {
groupByValue: groupByValue,
groupArrIndex: groupArr.length,
reaction: reaction(function () { return _this._groupBy(item); }, function (newGroupByValue, _r) {
console.log("new group by value ", newGroupByValue);
var grouperItemInfo = item[_this._ogmInfoKey];
_this._removeFromGroupArr(grouperItemInfo.groupByValue, grouperItemInfo.groupArrIndex);
var newGroupArr = _this._getGroupArr(newGroupByValue);
var newGroupArrIndex = newGroupArr.length;
newGroupArr.push(item);
grouperItemInfo.groupByValue = newGroupByValue;
grouperItemInfo.groupArrIndex = newGroupArrIndex;
}),
};
Object.defineProperty(item, this._ogmInfoKey, {
configurable: true,
enumerable: false,
value: value,
});
groupArr.push(item);
};
ObservableGroupMap.prototype._removeItem = function (item) {
var grouperItemInfo = item[this._ogmInfoKey];
this._removeFromGroupArr(grouperItemInfo.groupByValue, grouperItemInfo.groupArrIndex);
grouperItemInfo.reaction();
delete item[this._ogmInfoKey];
};
});
return ObservableGroupMap;
}(ObservableMap));
export { ObservableGroupMap };
{
"name": "mobx-utils",
"version": "6.0.0",
"version": "6.0.1",
"description": "Utility functions and common patterns for MobX",

@@ -48,6 +48,6 @@ "main": "mobx-utils.umd.js",

"rollup": "^2.10.8",
"rxjs": "^6.5.5",
"rxjs": "^6.6.3",
"shelljs": "^0.8.4",
"ts-jest": "^26.0.0",
"typescript": "^3.9.3"
"typescript": "^4.0.3"
},

@@ -54,0 +54,0 @@ "dependencies": {},

@@ -74,4 +74,8 @@ # MobX-utils

- [Examples](#examples-13)
- [dispose](#dispose)
- [ObservableMap](#observablemap)
- [defineProperty](#defineproperty)
- [defineProperty](#defineproperty-1)
- [defineProperty](#defineproperty-2)
- [defineProperty](#defineproperty-3)
- [defineProperty](#defineproperty-4)
- [computedFn](#computedfn)

@@ -619,9 +623,26 @@ - [Parameters](#parameters-15)

### dispose
## ObservableMap
## defineProperty
Base observable array which is being sorted into groups.
## defineProperty
The ObservableGroupMap needs to track some state per-item. This is the name/symbol of the
property used to attach the state.
## defineProperty
The function used to group the items.
## defineProperty
This function is used to generate the mobx debug names of the observable group arrays.
## defineProperty
Disposes all observers created during construction and removes state added to base array
items.
## ObservableMap
## computedFn

@@ -628,0 +649,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc