Skip to content

Commit f4aee87

Browse files
authored
Merge pull request #23 from XbyOrange/release-v.1.2.0
Release v.1.2.0
2 parents 239a3e8 + b575d93 commit f4aee87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2934
-432
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"parser": "flow"
1717
}
1818
],
19+
"no-undef": "error",
1920
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
2021
},
2122
"extends": ["prettier"],

CHANGELOG.md

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
### Removed
1212
### BREAKING CHANGES
1313

14+
## [TO BE DEPRECATED]
15+
- Last argument of Selectors will stop being assigned as "defaultValue". To define default value, it will be mandatory to pass an options object as last argument, containing a "defaultValue" property.
16+
17+
## [1.2.0] - 2019-10-14
18+
### Added
19+
- Accept options object in Origin constructor as last argument.
20+
- Assign to the `_id` private property the value received in new option "uuid", when received.
21+
- Last argument in Selectors now can be an object containing "defaultValue" and/or "uuid" options.
22+
- Add "sources" handler for managing all instantiated mercury sources as a group.
23+
- Add "config" method to Origin and sources handlers. From now, all Origin implementations can define its own `_config` method, which will be called with the resultant config each time the "config" method is called.
24+
25+
### Changed
26+
- `_id` private property now is a hash of default id and default value (if no "uuid" option is received)
27+
- Objects without query now will emit "undefined" as "_queryId" property in "cleanAny" events, instead of "null".
28+
29+
### Fixed
30+
- Emit `_root` property on cleanAny events of Selectors.
31+
1432
## [1.1.0] - 2019-06-25
1533
### Added
1634
- Expose `_root` property in queried instances to allow identify the root instance.

README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Provides:
2727
* [Api][mercury-api-url]
2828
* [Memory Storage][mercury-memory-url]
2929
* [Browser Local Storage][mercury-browser-storage-url]
30-
* [Broswer Session Storage][mercury-browser-storage-url]
30+
* [Browser Session Storage][mercury-browser-storage-url]
3131
* [Prismic CMS][mercury-prismic-url]
3232
* __"Selector"__ constructor for combining or transforming the result of one or many origins.
3333
* __Declarative__. Declare which Origins your Selector needs to consume. Mercury will do the rest for you.
@@ -38,6 +38,8 @@ Provides:
3838
* __Switchable__. Consumed "source" can be changed programatically.
3939
* __Parallellizable__. Can fetch data from declared sources in series or in parallel.
4040
* __Queryable__. Queries can be applied as in Origins. You can pass the `query` data to sources, or use it in the `parser` function, after all related sources data have been fetched.
41+
* __"sources"__ singleton containing methods for managing all created mercury sources at a time.
42+
* __Tags__ Mercury instances can be tagged to create "management" groups. "Cleaning" cache of all mercury sources tagged with "api" tag calling to a single method is easy, for example.
4143

4244
## Install
4345

@@ -48,7 +50,7 @@ npm i @xbyorange/mercury --save
4850
## A simple example
4951

5052
```js
51-
import { Selector } from "@xbyorange/mercury";
53+
import { Selector, sources } from "@xbyorange/mercury";
5254
import { Api } from "@xbyorange/mercury-api";
5355

5456
const booksCollection = new Api("http://api.library.com/books");
@@ -67,6 +69,8 @@ const booksWithAuthors = new Selector(
6769
// Each book now includes an "authorName" property.
6870
const results = await booksWithAuthors.read();
6971

72+
// Clean cache of books, authors, and booksWithAuthors at a time.
73+
sources.clean();
7074
```
7175

7276
> This example uses the Api origin, which is not distributed in this package, but can clearly illustrate the usage of an Origin, and the intention of the library.
@@ -114,6 +118,28 @@ Read the full [Selector API documentation](docs/selector/api.md).
114118

115119
Some utilities are provided to make easier the task of testing Selectors. Please red the [testing selectors docs](docs/selector/testing.md).
116120

121+
## Sources
122+
123+
All created Mercury origins or selectors are registered in the "sources" object, which provides methods for managing them all together, or by groups based in tags.
124+
125+
### A simple example
126+
127+
```js
128+
import { sources } from "@xbyorange/mercury";
129+
130+
sources.clean();
131+
132+
sources.getByTag("need-auth").config({
133+
headers: {
134+
"authentication": "foo"
135+
}
136+
})
137+
```
138+
139+
### Api
140+
141+
Read the full [sources API documentation](docs/sources/api.md).
142+
117143
## Usage with frameworks
118144

119145
### React

docs/origin/api.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ To create a new `Origin`, you should extend from the Mercury `Origin` Class, def
66

77
If you don't define one of this methods, the correspondant CRUD method will not be available in the resultant Origin.
88

9-
The Constructor requires two arguments. Call `super` from your own constructor, passing:
10-
* `super([id, defaultValue])`
11-
* Arguments:
12-
* `id` Used for debugging purposes.
13-
* `defaultValue` Resultant origin will have this value in the `value` property until data is fetched.
9+
Call `super` from your own constructor, passing:
10+
* `super([id, defaultValue, options])`
11+
* Arguments:
12+
* `defaultId` Used for debugging purposes. The `_id` of the resultant source will be a hash calculated using this default id and default value.
13+
* `defaultValue` Resultant origin will have this value in the `value` property until data is fetched.
14+
* `options` Object containing another options, such as:
15+
* `uuid` If provided, the resultant instance will have this property as `_id`. It will not be "hashed".
16+
* `tags` Tags to be assigned to the instance when created. Tags are used by "sources" handler. For further info [read the `sources` documentation](../sources/api.md).
1417

1518
Crud methods will receive two arguments:
1619

1720
* `_read([query, extraParams])`
18-
* Arguments:
19-
* `query` If origin is queried, you will receive here the current query.
20-
* `extraParams` Extra data passed as argument when the method is invoked.
21-
* Returns: `<Promise>`
21+
* Arguments:
22+
* `query` If origin is queried, you will receive here the current query.
23+
* `extraParams` Extra data passed as argument when the method is invoked.
24+
* Returns: `<Promise>`
2225

2326
Available methods for internal usage:
2427

2528
* `this._cache` Use built-in cache to provide cache to your Origin.
26-
* `this._cache.set(query, value)`. Set cache for an specific query. Cached objects should be Promises, in order to provide cache too for parallel invokations.
27-
* `this._cache.get(query)`. Returns cache for an specific query.
28-
* `this._cache.clean([query])`. Clean cache of an specific query. Cleans all caches if no query is provided.
29+
* `this._cache.set(query, value)`. Set cache for an specific query. Cached objects should be Promises, in order to provide cache too for parallel invokations.
30+
* `this._cache.get(query)`. Returns cache for an specific query.
31+
* `this._cache.clean([query])`. Clean cache of an specific query. Cleans all caches if no query is provided.
2932

3033
#### Example
3134

@@ -34,8 +37,8 @@ import { Origin } from "@xbyorange/mercury";
3437
import requestPromise from "request-promise";
3538

3639
export class Request extends Origin {
37-
constructor(url, options) {
38-
super(url, options.defaultValue);
40+
constructor(url, options = {}) {
41+
super(url, options.defaultValue, options.uuid);
3942
this._url = url;
4043
}
4144

@@ -54,4 +57,4 @@ export class Request extends Origin {
5457
return request;
5558
}
5659
}
57-
```
60+
```

docs/origin/events.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ booksCollection.onCleanAny(cleanDetails => {
8585

8686
```
8787

88-
> Use the `removeCleanAnyListener` to remove an specific callback.
88+
> Use the `removeCleanAnyListener` to remove an specific callback.

docs/selector/api.md

+65-63
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,76 @@
22

33
#### Constructor
44

5-
* `const selector = new Selector(sources, parserCallback(sourcesResults[, currentQuery]), [defaultValue])`
6-
* Arguments
7-
* sources - `<source object or Array of sources objects>`
8-
* source object - `<source instance, or customSourceReader object>`
9-
* source instance - `<instance of an Origin or Selector>` Origin or Selector to read.
10-
* customSourceReader - `<Object>` with next properties:
11-
* `source`: `<instance of an Origin or Selector>` Origin or Selector to read.
12-
* `query`: `<Function>` `query: (currentQuery[, previousSourcesResults])`. Query callback.
13-
* Arguments:
14-
* currentQuery - `<Any>` Selector current query.
15-
* previousSourcesResults - `<Array of Any>` Array containing results of all previously fetched sources in series.
16-
* Returns:
17-
* `<Any>` Result of this callback will be passed as `query` to the source.
18-
* `catch`: `<Function>` `catch: (error[, currentQuery])`.
19-
* Arguments:
20-
* error - `<Error>` Error thrown by the read method of the source.
21-
* currentQuery - `<Any>` Selector current query.
22-
* Returns:
23-
* `<Error>` - Source will throw this error.
24-
* `<Any>` - Source will return this value instead of error.
25-
* `<source instance>` - The `read` method of the returned source instance will be called, and returned result will be assigned as value of the current source (Allows to switch to another source if first returns an error)
26-
* Array of source objects - `<Array of <source object>>` Provided sources will be fetched in parallel.
27-
* parserCallback - `<Function>`
28-
* Arguments:
29-
* sourcesResults - `<Any>` - Results returned by the `read` method of the sources.
30-
* currentQuery - `<Any>` Selector current query.
31-
* Returns:
32-
* Result data - `<Any>`
33-
* `<Promise>` - The result of the returned Promise will be returned as result data.
34-
* `<source instance>` - If another source instance is returned, it will be called with same method and data than Selector was.
35-
* defaultValue - `<Any>` Default value to return until real data is returned.
5+
* `const selector = new Selector(source, source, parserCallback(sourcesResults[, currentQuery]), [options])`
6+
* Arguments
7+
* sources - `<source object or Array of sources objects>`
8+
* source object - `<source instance, or customSourceReader object>`
9+
* source instance - `<instance of an Origin or Selector>` Origin or Selector to read.
10+
* customSourceReader - `<Object>` with next properties:
11+
* `source`: `<instance of an Origin or Selector>` Origin or Selector to read.
12+
* `query`: `<Function>` `query: (currentQuery[, previousSourcesResults])`. Query callback.
13+
* Arguments:
14+
* currentQuery - `<Any>` Selector current query.
15+
* previousSourcesResults - `<Array of Any>` Array containing results of all previously fetched sources in series.
16+
* Returns:
17+
* `<Any>` Result of this callback will be passed as `query` to the source.
18+
* `catch`: `<Function>` `catch: (error[, currentQuery])`.
19+
* Arguments:
20+
* error - `<Error>` Error thrown by the read method of the source.
21+
* currentQuery - `<Any>` Selector current query.
22+
* Returns:
23+
* `<Error>` - Source will throw this error.
24+
* `<Any>` - Source will return this value instead of error.
25+
* `<source instance>` - The `read` method of the returned source instance will be called, and returned result will be assigned as value of the current source (Allows to switch to another source if first returns an error)
26+
* Array of source objects - `<Array of <source object>>` Provided sources will be fetched in parallel.
27+
* parserCallback - `<Function>`
28+
* Arguments:
29+
* sourcesResults - `<Any>` - Results returned by the `read` method of the sources.
30+
* currentQuery - `<Any>` Selector current query.
31+
* Returns:
32+
* Result data - `<Any>`
33+
* `<Promise>` - The result of the returned Promise will be returned as result data.
34+
* `<source instance>` - If another source instance is returned, it will be called with same method and data than Selector was.
35+
* options - `<Object>`
36+
* defaultValue - `<Any>` Default value to return until real data is returned.
37+
* uuid - `<String>` Custom uuid to be defined as selector "_id"
3638

3739
#### Instance
3840

3941
* read `selector.read()`
40-
* Statics:
41-
* error - `<Error>` If read method returns an error, it will be accessible at this property.
42-
* loading - `<Boolean>` Will be true while Selector read is in progress.
43-
* value - `<Any>` Value returned by `read` method.
44-
* Returns
45-
* `<Any>` - Result of the parser function.
42+
* Statics:
43+
* error - `<Error>` If read method returns an error, it will be accessible at this property.
44+
* loading - `<Boolean>` Will be true while Selector read is in progress.
45+
* value - `<Any>` Value returned by `read` method.
46+
* Returns
47+
* `<Any>` - Result of the parser function.
4648
* create, update, delete `selector.create(data)` These methods can be used only when Selector returns another source.
47-
* Statics:
48-
* error - `<Error>` If read method returns an error, it will be accessible at this property.
49-
* loading - `<Boolean>` Will be true while Selector read is in progress.
50-
* value - `<Any>` Value returned by `read` method.
51-
* Arguments
52-
* data - `<Any>` Data that will be passed to the correspondant create, update or delete method of the returned source.
53-
* Returns
54-
* `<Any>` - Result of the correspondant method of returned source.
49+
* Statics:
50+
* error - `<Error>` If read method returns an error, it will be accessible at this property.
51+
* loading - `<Boolean>` Will be true while Selector read is in progress.
52+
* value - `<Any>` Value returned by `read` method.
53+
* Arguments
54+
* data - `<Any>` Data that will be passed to the correspondant create, update or delete method of the returned source.
55+
* Returns
56+
* `<Any>` - Result of the correspondant method of returned source.
5557
* clean `selector.clean([query])`
56-
* Arguments
57-
* query - `<Any>` Any object, string, array etc. for quering the source.
58-
* Returns
59-
* `<undefined>` - Selector instance cache corresponding to the provided query will be cleaned.
58+
* Arguments
59+
* query - `<Any>` Any object, string, array etc. for quering the source.
60+
* Returns
61+
* `<undefined>` - Selector instance cache corresponding to the provided query will be cleaned.
6062
* query `selector.query([query])`
61-
* Arguments
62-
* query - `<Any>` Any object, string, array etc. for quering the source.
63-
* Returns
64-
* `<selector instance>` - Will return a selector instance unique for the query provided. Returned instances will be the same if query is the same.
63+
* Arguments
64+
* query - `<Any>` Any object, string, array etc. for quering the source.
65+
* Returns
66+
* `<selector instance>` - Will return a selector instance unique for the query provided. Returned instances will be the same if query is the same.
6567
* addCustomQueries `selector.addCustomQueries(customQueryObject)`
66-
* Aliases
67-
* addCustomQuery `selector.addCustomQuery({ queryName: queryCallback(query)})`
68-
* Arguments
69-
* customQueryObject - `<Object>` containing properties:
70-
* [key] - `<String>` Key will be used as name of the new selector method that will execute the custom query.
71-
* queryCallback - `<Function>`
72-
* Arguments
73-
* query - `<Any>` Query provided by the user when using custom query method.
74-
* Returns
75-
* currentQuery - `<Any>` Query that will be used as current query, and passed to the CRUD methods when executed.
68+
* Aliases
69+
* addCustomQuery `selector.addCustomQuery({ queryName: queryCallback(query)})`
70+
* Arguments
71+
* customQueryObject - `<Object>` containing properties:
72+
* [key] - `<String>` Key will be used as name of the new selector method that will execute the custom query.
73+
* queryCallback - `<Function>`
74+
* Arguments
75+
* query - `<Any>` Query provided by the user when using custom query method.
76+
* Returns
77+
* currentQuery - `<Any>` Query that will be used as current query, and passed to the CRUD methods when executed.

docs/selector/asynchronous-mutable-properties.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ console.log(await booksWithAuthors.read()) // value returned by parser function.
1414
console.log(books.read.loading) // false
1515
console.log(books.read.value) // value returned by parser function.
1616

17-
```
17+
```

docs/selector/cache.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ booksWithAuthors.clean();
2424

2525
await booksWithAuthors.read(); // Selector will be executed again
2626

27-
```
27+
```

docs/selector/default-value.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Default values
22

3-
Define a default value for the Selector as last argument. Selector `value` property will have that value until real value is ready
3+
Define a default value for the Selector using options in last argument. Selector `value` property will have that value until real value is ready
44

55
```js
66
const booksAndAuthors = new Selector(
@@ -9,7 +9,9 @@ const booksAndAuthors = new Selector(
99
books: booksAndAuthors[0],
1010
authors: booksAndAuthors[1]
1111
}),
12-
[]
12+
{
13+
defaultValue: []
14+
}
1315
);
1416
console.log(booksAndAuthors.read.value); // []
15-
```
17+
```

docs/selector/parallel-sources.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ const booksWithAuthors = new Selector(
2222
// Now books and authors fetchs are executed in parallel
2323
const results = await booksWithAuthors.read();
2424

25-
```
25+
```

docs/selector/testing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ const mySelector = new Selector({
5454

5555
```js
5656
expect(mySelector.test.selector(["foo"])).toEqual("foo");
57-
```
57+
```

0 commit comments

Comments
 (0)