-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add observable source from property changes
Closes #1
- Loading branch information
1 parent
678432e
commit 6b66b92
Showing
3 changed files
with
69 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
export { default as Route } from "./route"; | ||
|
||
export { default as subscribe } from "./decorators/subscribe"; | ||
|
||
export { | ||
default as fromPropertyChange | ||
} from "./observables/from-property-change"; |
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,16 @@ | ||
import { fromEventPattern } from "rxjs"; | ||
import { get } from "@ember/object"; | ||
|
||
export default function fromPropertyChange(subject, property) { | ||
return fromEventPattern( | ||
handler => { | ||
subject.addObserver(property, subject, handler); | ||
}, | ||
handler => { | ||
subject.removeObserver(property, subject, handler); | ||
}, | ||
(instance, prop) => { | ||
return get(instance, prop); | ||
} | ||
); | ||
} |
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,48 @@ | ||
import { module, test } from "qunit"; | ||
import { setupScheduler } from "ember-observable/test-support"; | ||
import td from "testdouble"; | ||
import EmberObject from "@ember/object"; | ||
import { fromPropertyChange } from "ember-observable"; | ||
|
||
class Dummy extends EmberObject { | ||
foo = "foo"; | ||
} | ||
|
||
module("Unit | Observables | fromPropertyChange", function(hooks) { | ||
setupScheduler(hooks); | ||
|
||
test("it emits events when a property changes", function(assert) { | ||
const instance = Dummy.create(); | ||
|
||
const observer = td.function(); | ||
fromPropertyChange(instance, "foo").subscribe(observer); | ||
|
||
instance.set("foo", "bar"); | ||
instance.set("foo", "bax"); | ||
|
||
assert.verify(observer("bar"), "Called for the first change"); | ||
assert.verify(observer("bax"), "Called for the second change"); | ||
}); | ||
|
||
test("no longer emits events for changes after unsubscribe", function(assert) { | ||
const instance = Dummy.create(); | ||
|
||
const observer = td.function(); | ||
const subscription = fromPropertyChange(instance, "foo").subscribe( | ||
observer | ||
); | ||
|
||
instance.set("foo", "bar"); | ||
|
||
subscription.unsubscribe(); | ||
|
||
instance.set("foo", "bax"); | ||
|
||
assert.verify(observer("bar"), "Called for the first change"); | ||
assert.verify( | ||
observer("bax"), | ||
{ times: 0 }, | ||
"Not called for the second change" | ||
); | ||
}); | ||
}); |