Added create-subscription example

This commit is contained in:
Brian Vaughn
2018-03-24 16:26:04 -07:00
parent ce060eb37a
commit e143823d7a
2 changed files with 41 additions and 2 deletions

View File

@@ -0,0 +1,33 @@
import {createSubscription} from 'create-subscription';
const Subscription = createSubscription({
getCurrentValue(source) {
// Return the current value of the subscription (source).
// highlight-next-line
return source.value;
},
subscribe(source, callback) {
function handleSubscriptionChange() {
callback(dataSource.value);
}
// Subscribe (e.g. add an event listener) to the subscription (source).
// Call callback(newValue) whenever a subscription changes.
// highlight-next-line
source.subscribe(handleSubscriptionChange);
// Return an unsubscribe method.
// highlight-range{1-3}
return function unsubscribe() {
source.unsubscribe(handleSubscriptionChange);
};
},
});
// Rather than passing the subscribable source to our ExampleComponent,
// We could just pass the subscribed value directly:
// highlight-range{1-3}
<Subscription source={dataSource}>
{value => <ExampleComponent subscribedValue={value} />}
</Subscription>;