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

create-subscription #12325

Merged
merged 54 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d5d8bf6
POC for create-component-with-subscriptions
bvaughn Mar 5, 2018
7b1e8c2
Updated README
bvaughn Mar 5, 2018
f8743b3
Added Rollup bundle
bvaughn Mar 5, 2018
4304b55
Expanded tests
bvaughn Mar 5, 2018
30eb16a
Updated bundle comment
bvaughn Mar 5, 2018
eb1372c
Added a test for "cold" observable
bvaughn Mar 5, 2018
88e7e22
Updated inline comments
bvaughn Mar 5, 2018
c395051
Updated README examples
bvaughn Mar 5, 2018
78c9a4c
Added a test (and README docs) for Promises
bvaughn Mar 5, 2018
d1fc6e8
Added a caveat about Promises to README
bvaughn Mar 5, 2018
2e98ca9
Use a HOC for functional components and a mixin for ES6 components
bvaughn Mar 6, 2018
5093ab4
Added tests for create-react-class
bvaughn Mar 6, 2018
54468e8
Flow fix
bvaughn Mar 6, 2018
6b16b7c
Added a ref test
bvaughn Mar 6, 2018
d05ffa3
Added a test for react-lifecycles-compat
bvaughn Mar 6, 2018
bd36fb4
Updated README to show class component
bvaughn Mar 6, 2018
a11164e
Added docs for default values
bvaughn Mar 6, 2018
31edf59
Improved README examples
bvaughn Mar 6, 2018
256c5e5
Simplified Promise docs and added additional test
bvaughn Mar 6, 2018
6dcac15
Swapped functional/class component usage in examples
bvaughn Mar 6, 2018
39d7ba8
Split internal and public API tests
bvaughn Mar 6, 2018
b5571c1
Tweaks
bvaughn Mar 6, 2018
2192fd5
Changed impl to only support one subscription per component
bvaughn Mar 6, 2018
fdfa22b
Docs tweak
bvaughn Mar 6, 2018
afeb6cd
Docs tweaks
bvaughn Mar 6, 2018
7532184
Refactored create-subscription to more closely mimic context API
bvaughn Mar 7, 2018
0f936ba
Renamed create-component-with-subscriptions => create-subscription
bvaughn Mar 7, 2018
2d824c2
Renamed references to create-subscription
bvaughn Mar 7, 2018
3edff49
Replaced .toThrow with .toWarnDev
bvaughn Mar 7, 2018
e056172
Disable render-phase side effects
bvaughn Mar 7, 2018
9bdc6d6
Updated docs
bvaughn Mar 7, 2018
9ffe079
README and naming tweaks
bvaughn Mar 7, 2018
629f145
README tweaks
bvaughn Mar 7, 2018
48b4a1b
Wording tweak
bvaughn Mar 7, 2018
ee2ae93
Inline comments tweak
bvaughn Mar 7, 2018
64d80b8
Minor test tidying up
bvaughn Mar 7, 2018
ad190fb
Added more context to README intro
bvaughn Mar 7, 2018
3288726
Wordsmith nit picking
bvaughn Mar 7, 2018
81f2695
Wordsmith nit picking
bvaughn Mar 7, 2018
267a76b
Replaced Value with Value | void type
bvaughn Mar 7, 2018
db7b84f
Tweaks in response to Flarnie's feedback
bvaughn Mar 7, 2018
32d6d40
Added RxJS for tests instead of fake impls
bvaughn Mar 7, 2018
5557120
Improved children Flow type slightly
bvaughn Mar 7, 2018
ee3dfcc
Added Flow <> around config
bvaughn Mar 7, 2018
a2f43a5
Fixed example imports in README
bvaughn Mar 8, 2018
4e57ed7
Replaced createComponent() references with createSubscription() in RE…
bvaughn Mar 8, 2018
f0c68b8
Changed subscribe() to return an unsubscribe method (or false)
bvaughn Mar 8, 2018
63a65e6
Flow type tweak
bvaughn Mar 12, 2018
e6740aa
Merge branch 'master' into create-component-with-subscriptions
bvaughn Mar 13, 2018
c116528
Responded to Andrew's PR feedback
bvaughn Mar 13, 2018
c1dd9a7
Docs updatE
bvaughn Mar 13, 2018
e10e2fc
Flow tweak
bvaughn Mar 13, 2018
f03dfa9
Addressed PR feedback from Flarnie
bvaughn Mar 13, 2018
6f740d9
Removed contradictory references to Flux stores in README
bvaughn Mar 13, 2018
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
81 changes: 81 additions & 0 deletions packages/create-component-with-subscriptions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# create-component-with-subscriptions

Below is an example showing how the container can be used:

```js
// This is an example functional component that subscribes to some values.
function ExampleComponent({
examplePassThroughProperty,
friendsList,
userProfile
}) {
// The rendered output of this component is not very important.
// It just exists to show how the observed values are provided.
// Properties not related to subscriptions are passed through as-is,
// (e.g. examplePassThroughProperty).
}

// In the below example, "friendsList" mimics an RxJS BehaviorSubject,
// and "userProfile" mimics an event dispatcher (like a DOM element).
function getDataFor(subscribable, propertyName) {
switch (propertyName) {
case "friendsListSubject":
return subscribable.getValue();
case "userProfile":
return subscribable.value;
default:
throw Error(`Invalid subscribable, "${propertyName}", specified.`);
}
}

function subscribeTo(valueChangedCallback, subscribable, propertyName) {
switch (propertyName) {
case "friendsListSubject":
// Return the subscription in this case; it's necessary to unsubscribe.
return subscribable.subscribe(valueChangedCallback);
case "userProfile":
const onChange = () => valueChangedCallback(subscribable.value);
subscribable.addEventListener(onChange);
// Return the event handling callback, since it's required to unsubscribe.
return onChange;
default:
throw Error(`Invalid subscribable, "${propertyName}", specified.`);
}
}

function unsubscribeFrom(subscribable, propertyName, subscription) {
switch (propertyName) {
case "friendsListSubject":
// Unsubscribe using the subscription rather than the subscribable.
subscription.unsubscribe();
case "userProfile":
// In this case, 'subscription', is the event handler/function.
subscribable.removeEventListener(subscription);
break;
default:
throw Error(`Invalid subscribable, "${propertyName}", specified.`);
}
}

// Map incoming subscriptions property names (e.g. friendsListSubject)
// to property names expected by our functional component (e.g. friendsList).
const subscribablePropertiesMap = {
friendsListSubject: "friendsList",
userProfile: "userProfile"
};

// Decorate our functional component with a subscriber component.
// This HOC will automatically manage subscriptions to the incoming props,
// and map them to subscribed values to be passed to the inner component.
// All other props will be passed through as-is.
export default createSubscribable(
{
getDataFor,
subscribablePropertiesMap,
subscribeTo,
unsubscribeFrom
},
ExampleComponent
);

```
12 changes: 12 additions & 0 deletions packages/create-component-with-subscriptions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

export * from './src/createComponentWithSubscriptions';
7 changes: 7 additions & 0 deletions packages/create-component-with-subscriptions/npm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/create-component-with-subscriptions.production.min.js');
} else {
module.exports = require('./cjs/create-component-with-subscriptions.development.js');
}
13 changes: 13 additions & 0 deletions packages/create-component-with-subscriptions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "create-component-with-subscriptions",
"description": "HOC for creating async-safe React components with subscriptions",
"version": "0.0.1",
"repository": "facebook/react",
"files": ["LICENSE", "README.md", "index.js", "cjs/"],
"dependencies": {
"fbjs": "^0.8.16"
},
"peerDependencies": {
"react": "16.3.0-alpha.1"
}
}
Loading