Skip to content

Commit

Permalink
Merge pull request #31 from thoov/freezable
Browse files Browse the repository at this point in the history
Add Ember.Freezable support
  • Loading branch information
rwjblue authored Dec 19, 2017
2 parents 0f7344d + 555f9b7 commit ace1893
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = function(/* environment, appConfig */) {
_ENABLE_CURRENT_WHEN_SUPPORT: true,
_ENABLE_DEPRECATED_REGISTRY_SUPPORT: true,
_ENABLE_IMMEDIATE_OBSERVER_SUPPORT: true,
_ENABLE_STRING_FMT_SUPPORT: true
_ENABLE_STRING_FMT_SUPPORT: true,
_ENABLE_FREEZABLE_SUPPORT: true
}
};
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ module.exports = {
this.import('vendor/deprecated-registry.js');
this.import('vendor/immediate-observer.js');
this.import('vendor/string-fmt.js');
this.import('vendor/freezable.js');
}
};
53 changes: 53 additions & 0 deletions tests/unit/freezable-test.js
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');
});
58 changes: 58 additions & 0 deletions vendor/freezable.js
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`);
}
}
});
})();

0 comments on commit ace1893

Please sign in to comment.