Comparing version 8.0.1 to 9.0.0
# Changelog | ||
## `9.0.0` | ||
- Remove decorators | ||
- Added missing actions | ||
## `8.0.1` | ||
@@ -4,0 +9,0 @@ |
@@ -60,2 +60,5 @@ import { IObservableArray, ObservableMap } from 'mobx'; | ||
then(onFulfilled: (any: any) => Promise<any>, onRejected?: (any: any) => Promise<any>): Promise<any>; | ||
fulfill(): void; | ||
reject(): void; | ||
setProgress(progress: number | null): void; | ||
} | ||
@@ -66,2 +69,3 @@ | ||
requests: IObservableArray<Request>; | ||
constructor(); | ||
/** | ||
@@ -196,3 +200,3 @@ * Returns the resource's url. | ||
/** | ||
* Destroys the resurce on the client and | ||
* Destroys the resource on the client and | ||
* requests the backend to delete it there | ||
@@ -276,6 +280,2 @@ * too | ||
/** | ||
* Gets the ids of all the items in the collection | ||
*/ | ||
private get _ids(); | ||
/** | ||
* Get a resource at a given position | ||
@@ -282,0 +282,0 @@ */ |
280
lib/index.js
@@ -16,4 +16,2 @@ 'use strict'; | ||
var intersection = require('lodash/intersection'); | ||
var entries = require('lodash/entries'); | ||
var compact = require('lodash/compact'); | ||
@@ -32,4 +30,2 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection); | ||
var entries__default = /*#__PURE__*/_interopDefaultLegacy(entries); | ||
var compact__default = /*#__PURE__*/_interopDefaultLegacy(compact); | ||
@@ -60,2 +56,4 @@ /*! ***************************************************************************** | ||
function __extends(d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
@@ -89,9 +87,29 @@ function __() { this.constructor = d; } | ||
function __decorate(decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
function __read(o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
} | ||
function __spreadArray(to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || from); | ||
} | ||
var Request = /** @class */ (function () { | ||
@@ -104,7 +122,14 @@ function Request(promise, _a) { | ||
this.abort = abort; | ||
this.progress = progress = 0; | ||
this.progress = progress; | ||
this.promise = promise; | ||
this.promise | ||
.then(function () { _this.state = 'fulfilled'; }) | ||
.catch(function () { _this.state = 'rejected'; }); | ||
.then(function () { _this.fulfill(); }) | ||
.catch(function () { _this.reject(); }); | ||
mobx.makeObservable(this, { | ||
progress: mobx.observable, | ||
state: mobx.observable, | ||
fulfill: mobx.action, | ||
reject: mobx.action, | ||
setProgress: mobx.action | ||
}); | ||
} | ||
@@ -115,8 +140,11 @@ // This allows to use async/await on the request object | ||
}; | ||
__decorate([ | ||
mobx.observable | ||
], Request.prototype, "progress", void 0); | ||
__decorate([ | ||
mobx.observable | ||
], Request.prototype, "state", void 0); | ||
Request.prototype.fulfill = function () { | ||
this.state = 'fulfilled'; | ||
}; | ||
Request.prototype.reject = function () { | ||
this.state = 'rejected'; | ||
}; | ||
Request.prototype.setProgress = function (progress) { | ||
this.progress = progress; | ||
}; | ||
return Request; | ||
@@ -167,3 +195,9 @@ }()); | ||
function Base() { | ||
this.request = null; | ||
this.requests = mobx.observable.array([]); | ||
mobx.makeObservable(this, { | ||
request: mobx.observable, | ||
requests: mobx.observable.shallow, | ||
withRequest: mobx.action | ||
}); | ||
} | ||
@@ -185,13 +219,15 @@ /** | ||
.then(function (response) { | ||
if (_this.request === request) | ||
_this.request = null; | ||
mobx.runInAction(mobx.action('remove request', function () { | ||
mobx.action('remove request', function () { | ||
if (_this.request === request) | ||
_this.request = null; | ||
_this.requests.remove(request); | ||
})); | ||
})(); | ||
return response; | ||
}) | ||
.catch(function (error) { | ||
mobx.runInAction(mobx.action('remove request', function () { | ||
mobx.action('remove request', function () { | ||
if (_this.request === request) | ||
_this.request = null; | ||
_this.requests.remove(request); | ||
})); | ||
})(); | ||
throw new ErrorObject(error); | ||
@@ -231,11 +267,2 @@ }); | ||
}; | ||
__decorate([ | ||
mobx.observable | ||
], Base.prototype, "request", void 0); | ||
__decorate([ | ||
mobx.observable.shallow | ||
], Base.prototype, "requests", void 0); | ||
__decorate([ | ||
mobx.action | ||
], Base.prototype, "rpc", null); | ||
return Base; | ||
@@ -255,2 +282,14 @@ }()); | ||
_this.collection = null; | ||
mobx.makeObservable(_this, { | ||
isNew: mobx.computed, | ||
changedAttributes: mobx.computed, | ||
changes: mobx.computed, | ||
commitChanges: mobx.action, | ||
discardChanges: mobx.action, | ||
reset: mobx.action, | ||
set: mobx.action, | ||
fetch: mobx.action, | ||
save: mobx.action, | ||
destroy: mobx.action | ||
}); | ||
_this.defaultAttributes = defaultAttributes; | ||
@@ -365,3 +404,3 @@ var mergedAttributes = __assign(__assign({}, _this.defaultAttributes), attributes); | ||
get: function () { | ||
return getChangedAttributesBetween(Object.fromEntries(this.committedAttributes), Object.fromEntries(this.attributes)); | ||
return getChangedAttributesBetween(Object.fromEntries(this.committedAttributes), this.toJS()); | ||
}, | ||
@@ -376,3 +415,3 @@ enumerable: false, | ||
get: function () { | ||
return getChangesBetween(Object.fromEntries(this.committedAttributes), Object.fromEntries(this.attributes)); | ||
return getChangesBetween(Object.fromEntries(this.committedAttributes), this.toJS()); | ||
}, | ||
@@ -393,3 +432,3 @@ enumerable: false, | ||
Model.prototype.commitChanges = function () { | ||
this.committedAttributes.replace(Object.fromEntries(this.attributes)); | ||
this.committedAttributes.replace(this.toJS()); | ||
}; | ||
@@ -425,4 +464,6 @@ Model.prototype.discardChanges = function () { | ||
return; | ||
_this.set(data); | ||
_this.commitChanges(); | ||
mobx.action('fetch done', function () { | ||
_this.set(data); | ||
_this.commitChanges(); | ||
})(); | ||
}) | ||
@@ -475,4 +516,5 @@ .catch(function (_error) { }); // do nothing | ||
var onProgress = debounce__default['default'](function (progress) { | ||
if (optimistic && _this.request) | ||
_this.request.progress = progress; | ||
if (optimistic && _this.request) { | ||
_this.request.setProgress(progress); | ||
} | ||
}); | ||
@@ -484,4 +526,4 @@ var _e = apiClient()[method](path || this.url(), data, __assign({ onProgress: onProgress }, otherOptions)), promise = _e.promise, abort = _e.abort; | ||
return; | ||
var changes = getChangesBetween(currentAttributes, Object.fromEntries(_this.attributes)); | ||
mobx.runInAction(mobx.action('save success', function () { | ||
var changes = getChangesBetween(currentAttributes, _this.toJS()); | ||
mobx.action('save success', function () { | ||
_this.set(data); | ||
@@ -494,9 +536,11 @@ _this.commitChanges(); | ||
} | ||
})); | ||
})(); | ||
}) | ||
.catch(function (error) { | ||
_this.set(currentAttributes); | ||
if (optimistic && _this.isNew && collection) { | ||
collection.remove(_this); | ||
} | ||
mobx.action('save error', function () { | ||
_this.set(currentAttributes); | ||
if (optimistic && _this.isNew && collection) { | ||
collection.remove(_this); | ||
} | ||
})(); | ||
}); | ||
@@ -506,3 +550,3 @@ return this.withRequest(['saving', label], promise, abort); | ||
/** | ||
* Destroys the resurce on the client and | ||
* Destroys the resource on the client and | ||
* requests the backend to delete it there | ||
@@ -538,32 +582,2 @@ * too | ||
}; | ||
__decorate([ | ||
mobx.computed | ||
], Model.prototype, "isNew", null); | ||
__decorate([ | ||
mobx.computed | ||
], Model.prototype, "changedAttributes", null); | ||
__decorate([ | ||
mobx.computed | ||
], Model.prototype, "changes", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "commitChanges", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "discardChanges", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "reset", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "set", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "fetch", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "save", null); | ||
__decorate([ | ||
mobx.action | ||
], Model.prototype, "destroy", null); | ||
return Model; | ||
@@ -594,12 +608,2 @@ }(Base)); | ||
function getAttribute(resource, attribute) { | ||
if (resource instanceof Model) { | ||
return resource.has(attribute) | ||
? resource.get(attribute) | ||
: null; | ||
} | ||
else { | ||
return resource[attribute]; | ||
} | ||
} | ||
var Collection = /** @class */ (function (_super) { | ||
@@ -611,2 +615,13 @@ __extends(Collection, _super); | ||
_this.models = mobx.observable.array(data.map(function (m) { return _this.build(m); })); | ||
mobx.makeObservable(_this, { | ||
index: mobx.computed({ keepAlive: true }), | ||
length: mobx.computed, | ||
isEmpty: mobx.computed, | ||
add: mobx.action, | ||
reset: mobx.action, | ||
remove: mobx.action, | ||
set: mobx.action, | ||
create: mobx.action, | ||
fetch: mobx.action | ||
}); | ||
return _this; | ||
@@ -721,12 +736,2 @@ } | ||
}); | ||
Object.defineProperty(Collection.prototype, "_ids", { | ||
/** | ||
* Gets the ids of all the items in the collection | ||
*/ | ||
get: function () { | ||
return compact__default['default'](Array.from(this.index.get(this.primaryKey).keys())); | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
/** | ||
@@ -742,3 +747,3 @@ * Get a resource at a given position | ||
Collection.prototype.get = function (id, _a) { | ||
var _b = (_a === void 0 ? {} : _a).required, required = _b === void 0 ? false : _b; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.required, required = _c === void 0 ? false : _c; | ||
var models = this.index.get(this.primaryKey).get(id); | ||
@@ -764,7 +769,7 @@ var model = models && models[0]; | ||
// Sort the query to hit the indexes first | ||
var optimizedQuery = entries__default['default'](query).sort(function (A, B) { | ||
var optimizedQuery = Object.entries(query).sort(function (A, B) { | ||
return Number(_this.index.has(B[0])) - Number(_this.index.has(A[0])); | ||
}); | ||
return optimizedQuery.reduce(function (values, _a) { | ||
var attr = _a[0], value = _a[1]; | ||
var _b = __read(_a, 2), attr = _b[0], value = _b[1]; | ||
// Hitting index | ||
@@ -789,3 +794,3 @@ if (_this.index.has(attr)) { | ||
Collection.prototype.find = function (query, _a) { | ||
var _b = (_a === void 0 ? {} : _a).required, required = _b === void 0 ? false : _b; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.required, required = _c === void 0 ? false : _c; | ||
var model = typeof query === 'function' | ||
@@ -817,3 +822,3 @@ ? this.models.find(function (model) { return query(model); }) | ||
var models = difference__default['default'](data.map(function (m) { return _this.build(m); }), this.models); | ||
(_a = this.models).push.apply(_a, models); | ||
(_a = this.models).push.apply(_a, __spreadArray([], __read(models))); | ||
return models; | ||
@@ -836,2 +841,3 @@ }; | ||
} | ||
var toKeep = new Set(this.models); | ||
ids.forEach(function (id) { | ||
@@ -848,5 +854,6 @@ var model; | ||
} | ||
_this.models.splice(_this.models.indexOf(model), 1); | ||
toKeep.delete(model); | ||
model.collection = undefined; | ||
}); | ||
this.models.replace(Array.from(toKeep)); | ||
}; | ||
@@ -861,20 +868,23 @@ /** | ||
var _b = _a === void 0 ? {} : _a, _c = _b.add, add = _c === void 0 ? true : _c, _d = _b.change, change = _d === void 0 ? true : _d, _e = _b.remove, remove = _e === void 0 ? true : _e; | ||
var getPrimaryKey = function (resource) { return getAttribute(resource, _this.primaryKey); }; | ||
var idsToRemove = difference__default['default'](this._ids, resources.map(getPrimaryKey)); | ||
var resourcesToAdd = []; | ||
var idsToRemove = new Set(this.index.get(this.primaryKey).keys()); | ||
var resourcesToAdd = new Set([]); | ||
idsToRemove.delete(null); | ||
resources.forEach(function (resource) { | ||
var id = getPrimaryKey(resource); | ||
var id = resource[_this.primaryKey]; | ||
var model = id ? _this.get(id) : null; | ||
if (model && change) { | ||
model.set(resource instanceof Model ? resource.toJS() : resource); | ||
if (!model) { | ||
resourcesToAdd.add(resource); | ||
} | ||
else { | ||
resourcesToAdd.push(resource); | ||
idsToRemove.delete(id); | ||
if (change) { | ||
model.set(resource instanceof Model ? resource.toJS() : resource); | ||
} | ||
} | ||
}); | ||
if (remove && idsToRemove.length) { | ||
this.remove(idsToRemove); | ||
if (remove && idsToRemove.size) { | ||
this.remove(Array.from(idsToRemove)); | ||
} | ||
if (add && resourcesToAdd.length) { | ||
this.add(resourcesToAdd); | ||
if (add && resourcesToAdd.size) { | ||
this.add(Array.from(resourcesToAdd)); | ||
} | ||
@@ -908,10 +918,6 @@ }; | ||
this.requests.push(request); | ||
var promise = request.promise; | ||
promise | ||
.then(function (_response) { | ||
_this.requests.remove(request); | ||
}) | ||
.catch(function (error) { | ||
_this.requests.remove(request); | ||
var removeRequest = mobx.action('remove request', function () { | ||
return _this.requests.remove(request); | ||
}); | ||
request.promise.then(removeRequest).catch(removeRequest); | ||
return request; | ||
@@ -939,32 +945,2 @@ }; | ||
}; | ||
__decorate([ | ||
mobx.computed({ keepAlive: true }) | ||
], Collection.prototype, "index", null); | ||
__decorate([ | ||
mobx.computed | ||
], Collection.prototype, "length", null); | ||
__decorate([ | ||
mobx.computed | ||
], Collection.prototype, "isEmpty", null); | ||
__decorate([ | ||
mobx.computed | ||
], Collection.prototype, "_ids", null); | ||
__decorate([ | ||
mobx.action | ||
], Collection.prototype, "add", null); | ||
__decorate([ | ||
mobx.action | ||
], Collection.prototype, "reset", null); | ||
__decorate([ | ||
mobx.action | ||
], Collection.prototype, "remove", null); | ||
__decorate([ | ||
mobx.action | ||
], Collection.prototype, "set", null); | ||
__decorate([ | ||
mobx.action | ||
], Collection.prototype, "create", null); | ||
__decorate([ | ||
mobx.action | ||
], Collection.prototype, "fetch", null); | ||
return Collection; | ||
@@ -971,0 +947,0 @@ }(Base)); |
{ | ||
"name": "mobx-rest", | ||
"version": "8.0.1", | ||
"version": "9.0.0", | ||
"description": "REST conventions for mobx.", | ||
@@ -20,20 +20,21 @@ "jest": { | ||
"peerDependencies": { | ||
"mobx": "^6.0.2" | ||
"mobx": "^6.3.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "26.0.15", | ||
"@typescript-eslint/eslint-plugin": "4.6.1", | ||
"@typescript-eslint/parser": "4.6.1", | ||
"@types/jest": "26.0.23", | ||
"@types/lodash": "4.14.170", | ||
"@typescript-eslint/eslint-plugin": "4.26.1", | ||
"@typescript-eslint/parser": "4.26.1", | ||
"benchmark": "2.1.4", | ||
"eslint": "7.12.1", | ||
"eslint": "7.28.0", | ||
"husky": "4.3.0", | ||
"jest": "26.6.2", | ||
"lint-staged": "10.5.1", | ||
"jest": "27.0.4", | ||
"lint-staged": "11.0.0", | ||
"rimraf": "3.0.2", | ||
"rollup": "2.33.1", | ||
"rollup-plugin-node-resolve": "5.0.0", | ||
"rollup-plugin-typescript2": "^0.29.0", | ||
"ts-jest": "26.4.3", | ||
"tslib": "2.0.3", | ||
"typescript": "4.0.5" | ||
"rollup": "2.51.2", | ||
"rollup-plugin-node-resolve": "5.2.0", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"ts-jest": "27.0.3", | ||
"tslib": "2.3.0", | ||
"typescript": "4.3.2" | ||
}, | ||
@@ -54,3 +55,3 @@ "main": "lib", | ||
"linters": { | ||
"{src|__tests__}/**/*.js": [ | ||
"{src|__tests__}/**/*.ts": [ | ||
"git add" | ||
@@ -62,8 +63,7 @@ ] | ||
"dependencies": { | ||
"@types/lodash": "4.14.164", | ||
"deepmerge": "4.2.2", | ||
"lodash": "4.17.20", | ||
"mobx": "^6.0.2", | ||
"rollup-plugin-dts": "^3.0.1" | ||
"lodash": "4.17.21", | ||
"mobx": "^6.3.2", | ||
"rollup-plugin-dts": "^3.0.2" | ||
} | ||
} | ||
} |
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
5
71611
16
1263
+ Addedlodash@4.17.21(transitive)
- Removed@types/lodash@4.14.164
- Removed@types/lodash@4.14.164(transitive)
- Removedlodash@4.17.20(transitive)
Updatedlodash@4.17.21
Updatedmobx@^6.3.2
Updatedrollup-plugin-dts@^3.0.2