1
0
Fork 0

chore: Updating final Store docs for v3

This commit is contained in:
Joost De Cock 2022-09-29 23:03:28 +02:00
parent 3391859fb7
commit 098d23c3b7
7 changed files with 230 additions and 28 deletions

View file

@ -2,26 +2,44 @@
title: Store
---
The **Store** object holds a simple key/value store with
methods for storing and retrieving information.
A single store per pattern is shared by all pattern parts.
A **Store** object holds a simple key/value store with methods for storing and
retrieving data.
## Signature
```js
Store Store.extend(Array methods=[])
```
The constructor takes a single argument, an Array of methods to add to the
store. Each entry in the array should be an array itself holding a path in
dot notation and a method. See below for an example.
## Example
```js
function myCustomMethod() {
// Do something clever
}
const store = new Store([
["path.to.the.method", myCustomMethod ]
])
```
With the configuration above, you can call `store.path.to.the.method()` and it
will run `myCustomMethod()`.
## Methods
A Store object exposes the following methods:
<ReadMore list />
## Notes
A store is typically used to share information between parts. For example
the length of the neck opening in one part can be used to calculate the
length for the collar in another part.
The `Store` object exposes the following methods:
<ReadMore list />
<Tip>
###### The store is available as shorthand
You can access the store instance from the [Part.shorthand](/reference/api/part/shorthand/) method;
```js
let { store } = part.shorthand();
```
</Tip>
<Fixme compact>Include link to guide</Fixme>

View file

@ -0,0 +1,48 @@
---
title: Store.extend()
---
The `Store.extend()` method can be used to extend the store with new
functionality by passing an array of methods and the path to the location in
the store where they should be attached. It is typically not used directly, but
rather through a plugin.
## Signature
```js
Store Store.extend(Array methods=[])
```
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
The single argument should be an Array of methods to add to the
store. Each entry in the array should be an array itself holding a path in
dot notation and a method, as such:
```js
function myCustomMethod() {
// Do something clever
}
const store = new Store([
["path.to.the.method", myCustomMethod ]
])
```
With the configuration above, you can call `store.path.to.the.method()` and it
will run `myCustomMethod()`.
Stores the value of `value` in the store under key `key`.
## Notes
The Store will not allow you to extend any of the following keys:
- `set`
- `setIfUnset`
- `push`
- `unset`
- `get`
- `extend`

View file

@ -1,9 +1,46 @@
---
title: get()
title: Store.get()
---
The `Store.get()` method retrieves the data available under `key`.
## Signature
```js
mixed store.get(string key)
mixed store.get(mixed key, mixed dflt)
```
Returnes the value stored under `key`.
If `key` is not available, the Store will return the optional second parameter
(a default value).
## Example
```js
const store = new Store()
store.set('example', 'Hi there')
const value = store.get('example')
// value now holds 'Hi there'
```
## Notes
You can get/set nested keys either through dot-notation, or by passing an
array:
```js
const store = new Store()
store.set('my.nested.example', 'Hi there')
store.set(['my', 'other', 'nested', 'example'], 'Oh hi again')
let value
// Dot notation
value = store.get('my.nested.example') // works
value = store.get('my.other.nested.example') // works
// Using an array
value = store.get(['my', 'nested', 'example']) // works
value = store.get(['my', 'other', 'nested', 'example']) // works
// Direct access to the store object
value = store.my.nested.example // Also works
value = store.my.other.nested.example // Also works
```

View file

@ -0,0 +1,29 @@
---
title: Store.push()
---
The `Store.push()` method adds the parameters you pass it to the array stored under `key`.
If `key` does not hold and Array, the Store will log a warning, but nothing will happen.
## Signature
```js
Store store.push(mixed value1, mixed value2, ...)
```
<Note compact>This method is [variadic](https://en.wikipedia.org/wiki/Variadic_function)</Note>
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
## Example
```js
const store = new Store()
store.set('example', ['Hi there'])
store.push(
'How are you doing',
'How are YOU doing'
)
```

View file

@ -1,9 +1,45 @@
---
title: set()
title: Store.set()
---
The `Store.set()` method stores the value of `value` in the store under key
`key`.
## Signature
```js
void store.set(string key, mixed value)
Store store.set(mixed key, mixed value)
```
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
## Example
```js
const store = new Store()
store.set('example', 'Hi there')
```
## Notes
You can get/set nested keys either through dot-notation, or by passing an
array:
```js
const store = new Store()
store.set('my.nested.example', 'Hi there')
store.set(['my', 'other', 'nested', 'example'], 'Oh hi again')
let value
// Dot notation
value = store.get('my.nested.example') // works
value = store.get('my.other.nested.example') // works
// Using an array
value = store.get(['my', 'nested', 'example']) // works
value = store.get(['my', 'other', 'nested', 'example']) // works
// Direct access to the store object
value = store.my.nested.example // Also works
value = store.my.other.nested.example // Also works
```
Stores the value of `value` in the store under key `key`.

View file

@ -1,10 +1,23 @@
---
title: setIfUnset
title: Store.setIfUnset()
---
The `Store.set()` method stores the value of `value` in the store under key
`key`, but only if that key does not already hold a value.
## Signature
```js
void store.setIfUnset(string key, mixed value)
Store store.set(mixed key, mixed value)
```
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
## Example
```js
const store = new Store()
store.set('example', 'Hi there')
store.setIfUnset('example', 'Hi again') // This has no effect
```
Stores the value of `value` in the store under key `key`,
but only if that key does not already hold a value.

View file

@ -0,0 +1,21 @@
---
title: Store.unset()
---
The `Store.unset()` value removes a `key` from the store.
## Signature
```js
Store store.unset(string key)
```
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
## Example
```js
const store = new Store()
.set('example', 'I will be gone before you know it')
.unset('example')
```