Skip to content

Commit 55194d9

Browse files
author
Vlad Balin
committed
added transactional.assign and fixed bug with merging shared attrs
1 parent 76222cf commit 55194d9

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

dist/index.js

Lines changed: 5 additions & 8 deletions
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.

dist/transactions.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export declare abstract class Transactional implements Messenger, Validatable, T
4848
transaction(fun: (self: this) => void, options?: TransactionOptions): void;
4949
updateEach(iteratee: (val: any, key: string) => void, options?: TransactionOptions): void;
5050
set(values: any, options?: TransactionOptions): this;
51+
assign(source: Transactional): this;
5152
abstract _createTransaction(values: any, options?: TransactionOptions): Transaction;
5253
parse(data: any, options?: TransactionOptions): any;
5354
abstract toJSON(): {};

src/record/attributes/owned.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export class AggregatedType extends AnyType {
3737
this._log( 'error', 'aggregated attribute is assigned with shared collection type', value, record );
3838
}
3939

40-
return options.merge ? value.clone() : value; // TODO: looks like clone is never called. Remove.
40+
// With explicit 'merge' option we need to clone an object if its previous value was 'null'.
41+
// This is an only case we could be here when merge === true.
42+
return options.merge ? value.clone() : value;
4143
}
4244

4345
return <any>this.type.create( value, options );

src/record/attributes/shared.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@ export class SharedType extends AnyType {
3535

3636
canBeUpdated( prev : Transactional, next : any, options : TransactionOptions ) : any {
3737
// If an object already exists, and new value is of incompatible type, let object handle the update.
38-
if( prev && next != null ){
39-
if( next instanceof this.type ){
40-
// In case if merge option explicitly specified, force merge.
41-
if( options.merge ) return next.__inner_state__;
42-
}
43-
else{
44-
return next;
45-
}
38+
if( prev && next != null && !( next instanceof this.type ) ){
39+
return next;
4640
}
4741
}
4842

src/transactions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ export abstract class Transactional implements Messenger, Validatable, Traversab
155155
return this;
156156
}
157157

158+
// Assign transactional object "by value", copying aggregated fields.
159+
assign( source : Transactional ) : this {
160+
return this.set( this.__inner_state__, { merge : true } );
161+
}
162+
158163
// Apply bulk object update without any notifications, and return open transaction.
159164
// Used internally to implement two-phase commit.
160165
// Returns null if there are no any changes.

0 commit comments

Comments
 (0)