-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from thoov/freezable
Add Ember.Freezable support
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Ember from 'ember'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Ember.Freezable'); | ||
|
||
const { get, set } = Ember; | ||
|
||
test('frozenCopy should be deprecated', function(assert) { | ||
assert.expectDeprecation(() => { | ||
let Obj = Ember.Object.extend(Ember.Freezable, Ember.Copyable, { | ||
copy() { | ||
return Obj.create(); | ||
} | ||
}); | ||
|
||
Obj.create().frozenCopy(); | ||
}, '`frozenCopy` is deprecated, use `Object.freeze` instead.'); | ||
}); | ||
|
||
test('Ember.Freezable should be deprecated', function(assert) { | ||
assert.expectDeprecation(() => { | ||
Ember.Object.extend(Ember.Freezable).create(); | ||
}, '`Ember.Freezable` is deprecated, use `Object.freeze` instead.'); | ||
}); | ||
|
||
test('frozen objects should return same instance', function(assert) { | ||
let obj, copy; | ||
|
||
const CopyableObject = Ember.Object.extend(Ember.Freezable, Ember.Copyable, { | ||
id: null, | ||
|
||
init() { | ||
this._super(...arguments); | ||
set(this, 'id', Ember.generateGuid()); | ||
}, | ||
|
||
copy() { | ||
let ret = new CopyableObject(); | ||
set(ret, 'id', get(this, 'id')); | ||
return ret; | ||
} | ||
}); | ||
|
||
obj = new CopyableObject(); | ||
assert.ok(!Ember.Freezable || Ember.Freezable.detect(obj), 'object should be freezable'); | ||
copy = obj.frozenCopy(); | ||
assert.equal(Ember.get(obj, 'id'), Ember.get(copy, 'id'), 'new copy should be equal'); | ||
assert.ok(get(copy, 'isFrozen'), 'returned value should be frozen'); | ||
|
||
copy = obj.freeze().frozenCopy(); | ||
assert.deepEqual(copy, obj, 'returns frozen object should be same'); | ||
assert.ok(get(copy, 'isFrozen'), 'returned object should be frozen'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(function() { | ||
var _Ember; | ||
if (typeof Ember !== 'undefined') { | ||
_Ember = Ember; | ||
} else { | ||
_Ember = require('ember').default; | ||
} | ||
|
||
if (EmberENV && EmberENV._ENABLE_FREEZABLE_SUPPORT !== true) { | ||
return false; | ||
} | ||
|
||
_Ember.Freezable = _Ember.Mixin.create({ | ||
init() { | ||
_Ember.deprecate( | ||
'`Ember.Freezable` is deprecated, use `Object.freeze` instead.', | ||
false, | ||
{ id: 'ember-runtime.freezable-init', until: '3.0.0' } | ||
); | ||
this._super(...arguments); | ||
}, | ||
|
||
isFrozen: false, | ||
|
||
freeze() { | ||
if (_Ember.get(this, 'isFrozen')) { | ||
return this; | ||
} | ||
|
||
_Ember.set(this, 'isFrozen', true); | ||
return this; | ||
} | ||
}); | ||
|
||
_Ember.FROZEN_ERROR = 'Frozen object cannot be modified.'; | ||
|
||
_Ember.NativeArray.reopen({ | ||
replace() { | ||
_Ember.assert(_Ember.FROZEN_ERROR, !this.isFrozen); | ||
this._super.apply(this, arguments); | ||
} | ||
}); | ||
|
||
_Ember.Copyable.reopen({ | ||
frozenCopy() { | ||
_Ember.deprecate( | ||
'`frozenCopy` is deprecated, use `Object.freeze` instead.', | ||
false, | ||
{ id: 'ember-runtime.frozen-copy', until: '3.0.0' } | ||
); | ||
if (_Ember.Freezable && _Ember.Freezable.detect(this)) { | ||
return _Ember.get(this, 'isFrozen') ? this : this.copy().freeze(); | ||
} else { | ||
throw new _Ember.Error(`${this} does not support freezing`); | ||
} | ||
} | ||
}); | ||
})(); |