data-lackey
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -87,3 +87,3 @@ "use strict"; | ||
componentWillMount() { | ||
componentDidMount() { | ||
this.setResources(resourceCreators, this.props); | ||
@@ -96,3 +96,3 @@ } | ||
componentWillReceiveProps(nextProps) { | ||
UNSAFE_componentWillReceiveProps(nextProps) { | ||
this.setResources(resourceCreators, nextProps); | ||
@@ -99,0 +99,0 @@ } |
{ | ||
"name": "data-lackey", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "orchestrate your data loading", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
101
README.md
# Data Lackey | ||
> __Tired of having to build promise chains to coordinate the loading of your pages? | ||
Tired of having pages break because a user linked from a different page? | ||
Tired of overloading your server with duplicate requests for the same data? | ||
Give Data Lackey a spin....__ | ||
* Tired of building complex views that manage data loading? | ||
* Tired of promise chains to coordinate the loading of your pages? | ||
* Tired of pages breaking because some data wasn't loaded first? | ||
* Tired of overloading your server with duplicate requests for the same data? | ||
Give Data Lackey a spin! _Data Lackey orchestrates data loading for rich front-end JS applications._ | ||
Data Lackey orchestrates data loading for rich front-end JS applications. | ||
With Data Lackey: | ||
* declaratively express what data is needed | ||
* separate data loading from views | ||
With Data Lackey: | ||
* declaratively express which data is needed by components | ||
* automatically track which data is not loaded, being loaded and already loaded | ||
* configure dependencies between data, and be guaranteed data is loaded before other data | ||
At a basic level, Data Lackey keeps track of the loading of your data, so it's not | ||
buried in or distributed throughout components or middleware: | ||
``` | ||
rules | ||
& | ||
loaders | ||
⬇ | ||
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯→ load ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯→ Configured Data Lackey ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯→ promise for data | ||
(resource uri) | ||
``` | ||
It layers on top of this : | ||
* automatically track which data is not loaded, being loaded and already loaded. At any | ||
point you can ask whether a given piece of data is loaded. | ||
* configure dependencies between data, and be guaranteed data is loaded before other data. | ||
This eliminates bugs and race conditions from view loading. | ||
* reload data at periodic intervals (poll) | ||
@@ -23,23 +37,37 @@ * expire data / support a ttl (time to live) for individual pieces of data | ||
```bash | ||
shell> yarn add data-lackey | ||
$ yarn add data-lackey | ||
``` | ||
### Create and Configure your lackey | ||
You'll need create a "data lackey" to track your data loading. Creating a file for this: | ||
```js | ||
// File: myLackey.js -- or whatever you want | ||
// File: lackey.js -- or whatever you want | ||
import { DataLackey } from 'data-lackey' | ||
export const myLackey = new DataLackey() | ||
export const lackey = new DataLackey() | ||
myLackey.rule('/books', | ||
{ | ||
loader: () => fetch('/api/books') | ||
}) | ||
myLackey.rule('/book :bookId', | ||
{ | ||
loader: ({bookId}) => fetch(`/api/books/${bookId}`), | ||
dependsOn: 'books' | ||
}) | ||
lackey.rule('/books', { loader: () => fetch('/api/books') }) | ||
lackey.rule('/book :bookId', { loader: ({bookId}) => fetch(`/api/books/${bookId}`), | ||
dependsOn: 'books' }) | ||
``` | ||
And then, configure your component with a new wrapping method `mapPropsToDataRsrcs`: | ||
More details on [patterns](./docs/patterns.md) and [rules](./docs/rules.md). | ||
### Load the Data | ||
Very simply, you can call `lackey.load('/books')` to call the underlying `fetch`. This is | ||
nice because you can hide some details in the loader that you don't want in the load call-- | ||
but probably not worth restructuring your code for. But when you have dependencies | ||
between different pieces of data-- the above the code wants the books index to be called before | ||
an individual book-- it is convenient. Calling `load('/book 8')` will return a promise that | ||
includes the loading of the `/books` endpoint. These dependencies (promise chains) can become | ||
quite complex if you have a larger webapp. | ||
This direct usage [is outlined here.](./docs/direct_usage.md) | ||
### Configure your React Component | ||
Data Lackey understands React components, and offers a HOC to manage the loading of | ||
data. Individual components can be completely free of data loading responsibilities. | ||
Configure your component with a new wrapping method `mapPropsToDataRsrcs`: | ||
```js | ||
@@ -58,20 +86,11 @@ // File: myComponent.js | ||
that is dependent on the `books` data as well, that will be loaded first. | ||
Usage within React is 100% configuration driven and [is outlined here.](./docs/react.md) | ||
## Advanced Usage | ||
Data Lackey works great with React, and removes tedious and error prone data loading | ||
code, replacing it with declarations of data depedencies. Usage within React is 100% configuration driven and [is outlined here.](./docs/react.md) | ||
You can also use it directly, to isolate the load orchestration details. This is called | ||
["direct usage" and outlined here.](./direct_usage.md) | ||
## Testing with Data Lackey | ||
Data Lackey itself is well tested. As most of the configuration of Data Lackey is declarative, there's | ||
less of a need to test this configuration. Given that, though, `loader` functions can be unit tested, | ||
as any data loading function can be tested. They are conveniently isolated from any component code. | ||
Data Lackey itself is well tested. One of the benefits of Data Lackey is that it separates the loading | ||
of data from the component itself. The loaders can be unit tested (they are functional in nature), | ||
and should simplify your component tests. | ||
@@ -81,4 +100,5 @@ # API | ||
## Terminology: | ||
* `load`: queue up a data resource to load. | ||
* `data resource`: a single set of data that can be loaded. | ||
* `data uri`: a string identifying a single resource. | ||
* `load`: request the loading of a data resource. | ||
@@ -90,12 +110,3 @@ #### Data Resource states: | ||
* `failed to load` | ||
### TODO | ||
* `unload` callback should pass in params from matcher, not just URL | ||
* ttl | ||
* Load in batches | ||
### Related Projects | ||
@@ -102,0 +113,0 @@ |
35566
113