Skip to content

Commit 0c790b3

Browse files
author
Vlad Balin
committed
Hotfix for collection.reset ownership problem
1 parent 81673b5 commit 0c790b3

File tree

10 files changed

+41
-18
lines changed

10 files changed

+41
-18
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/collection/commons.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/collection/index.js

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/collection/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "type-r",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Reactive serializable data layer for modern JS applications",
55
"main": "./dist/index.js",
66
"jsnext:main": "./lib/index.js",

src/collection/commons.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type Comparator = ( a : Record, b : Record ) => number;
3434

3535
/** @private */
3636
export function dispose( collection : CollectionCore ) : Record[]{
37-
const models = collection.models;
37+
const { models } = collection;
3838

3939
collection.models = [];
4040
collection._byId = {};

src/collection/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,17 +410,28 @@ export class Collection< R extends Record = Record> extends Transactional implem
410410

411411
reset( a_elements? : ElementsArg, options : TransactionOptions = {} ) : R[] {
412412
const isRoot = begin( this ),
413-
previousModels = dispose( this );
413+
previousModels = this.models;
414414

415415
// Make all changes required, but be silent.
416416
if( a_elements ){
417417
emptySetTransaction( this, toElements( this, a_elements, options ), options, true );
418418
}
419+
else{
420+
this._byId = {};
421+
this.models = [];
422+
}
419423

420424
markAsDirty( this, options );
421425

422426
options.silent || trigger2( this, 'reset', this, defaults( { previousModels : previousModels }, options ) );
423427

428+
// Dispose models which are not in the updated collection.
429+
const { _byId } = this;
430+
431+
for( let toDispose of previousModels ){
432+
_byId[ toDispose.cid ] || free( this, toDispose );
433+
}
434+
424435
isRoot && commit( this );
425436
return this.models;
426437
}

tests/typescript/dist/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,13 +2473,7 @@ var off$6 = off$2;
24732473
var commit$2 = transactionApi.commit;
24742474
var _aquire = transactionApi.aquire;
24752475
var _free = transactionApi.free;
2476-
function dispose(collection) {
2477-
var models = collection.models;
2478-
collection.models = [];
2479-
collection._byId = {};
2480-
freeAll(collection, models);
2481-
return models;
2482-
}
2476+
24832477
function convertAndAquire(collection, attrs, options) {
24842478
var model = collection.model;
24852479
var record;
@@ -3092,12 +3086,21 @@ var Collection = (function (_super) {
30923086
};
30933087
Collection.prototype.reset = function (a_elements, options) {
30943088
if (options === void 0) { options = {}; }
3095-
var isRoot = begin(this), previousModels = dispose(this);
3089+
var isRoot = begin(this), previousModels = this.models;
30963090
if (a_elements) {
30973091
emptySetTransaction(this, toElements(this, a_elements, options), options, true);
30983092
}
3093+
else {
3094+
this._byId = {};
3095+
this.models = [];
3096+
}
30993097
markAsDirty(this, options);
31003098
options.silent || trigger2$2(this, 'reset', this, defaults$1({ previousModels: previousModels }, options));
3099+
var _byId = this._byId;
3100+
for (var _i = 0, previousModels_1 = previousModels; _i < previousModels_1.length; _i++) {
3101+
var toDispose = previousModels_1[_i];
3102+
_byId[toDispose.cid] || free$2(this, toDispose);
3103+
}
31013104
isRoot && commit(this);
31023105
return this.models;
31033106
};

tests/typescript/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)