Skip to content

Commit

Permalink
Merge pull request #13290 from rwjblue/use-symbol-to-signify-stream
Browse files Browse the repository at this point in the history
[BUGFIX beta] Use a symbol to detect if an object is a stream.
  • Loading branch information
mmun committed Apr 10, 2016
2 parents 7026846 + 08e7df4 commit d262a69
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
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

0 comments on commit d262a69

Please sign in to comment.