Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] Use a symbol to detect if an object is a stream. #13290

Merged
merged 2 commits into from
Apr 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,43 @@ moduleFor('Components test: curly components', class extends RenderingTest {
this.assertElement(this.firstChild.childNodes[2], { tagName: 'b', content: 'bold' });
}

['@test can use isStream property without conflict (#13271)']() {
let component;
let FooBarComponent = Component.extend({
isStream: true,

init() {
this._super(...arguments);
component = this;
}
});

this.registerComponent('foo-bar', {
ComponentClass: FooBarComponent,

template: strip`
{{#if isStream}}
true
{{else}}
false
{{/if}}
`
});

this.render('{{foo-bar}}');

this.assertComponentElement(this.firstChild, { content: 'true' });

this.runTask(() => this.rerender());

this.assertComponentElement(this.firstChild, { content: 'true' });

this.runTask(() => set(component, 'isStream', false));

this.assertComponentElement(this.firstChild, { content: 'false' });

this.runTask(() => set(component, 'isStream', true));

this.assertComponentElement(this.firstChild, { content: 'true' });
}
});
7 changes: 5 additions & 2 deletions packages/ember-metal/lib/streams/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import Subscriber from 'ember-metal/streams/subscriber';
import Dependency from 'ember-metal/streams/dependency';
import { GUID_KEY } from 'ember-metal/utils';
import require from 'require';
import symbol from 'ember-metal/symbol';

export const IS_STREAM = symbol('IS_STREAM');

/**
@module ember-metal
*/
Expand All @@ -26,9 +30,8 @@ var KeyStream;
var ProxyMixin;

BasicStream.prototype = {
isStream: true,

_init(label) {
this[IS_STREAM] = true;
this.label = makeLabel(label);
this.isActive = false;
this.isDirty = true;
Expand Down
11 changes: 6 additions & 5 deletions packages/ember-metal/lib/streams/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assert } from 'ember-metal/debug';
import BasicStream, { Stream } from './stream';
import { IS_STREAM } from 'ember-metal/streams/stream';

/*
Check whether an object is a stream or not.
Expand All @@ -11,7 +12,7 @@ import BasicStream, { Stream } from './stream';
@return {Boolean} `true` if the object is a stream, `false` otherwise.
*/
export function isStream(object) {
return object && object.isStream;
return object && object[IS_STREAM];
}

/*
Expand All @@ -27,7 +28,7 @@ export function isStream(object) {
is provided.
*/
export function subscribe(object, callback, context) {
if (object && object.isStream) {
if (object && object[IS_STREAM]) {
return object.subscribe(callback, context);
}
}
Expand All @@ -44,7 +45,7 @@ export function subscribe(object, callback, context) {
@param {Object} [context] Object originally passed to `subscribe()`.
*/
export function unsubscribe(object, callback, context) {
if (object && object.isStream) {
if (object && object[IS_STREAM]) {
object.unsubscribe(callback, context);
}
}
Expand All @@ -60,7 +61,7 @@ export function unsubscribe(object, callback, context) {
@return The stream's current value, or the non-stream object itself.
*/
export function read(object) {
if (object && object.isStream) {
if (object && object[IS_STREAM]) {
return object.value();
} else {
return object;
Expand Down Expand Up @@ -343,7 +344,7 @@ export function chain(value, fn, label) {
}

export function setValue(object, value) {
if (object && object.isStream) {
if (object && object[IS_STREAM]) {
object.setValue(value);
}
}
5 changes: 3 additions & 2 deletions packages/ember-metal/tests/streams/concat_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Stream } from 'ember-metal/streams/stream';
import {
concat,
read
read,
isStream
} from 'ember-metal/streams/utils';


Expand All @@ -28,7 +29,7 @@ QUnit.test('returns a stream if a stream is in the array', function(assert) {
});
let result = concat(['foo', stream, 'baz'], ' ');

assert.ok(result.isStream, 'a stream is returned');
assert.ok(isStream(result), 'a stream is returned');
assert.equal(read(result), 'foo bar baz');
});

Expand Down