Skip to content

Commit d81a61f

Browse files
authored
[items] Improve JSDoc and README for ItemPersistence#persist (#338)
Closes #335. Signed-off-by: Florian Hotze <[email protected]>
1 parent 9c1f3ef commit d81a61f

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,15 @@ Calling `Item.persistence` returns an `ItemPersistence` object with the followin
520520
- .getAllStatesBetween(begin, end, serviceId) ⇒ `Array[PersistedItem]`
521521
- .lastUpdate(serviceId) ⇒ `ZonedDateTime | null`
522522
- .nextUpdate(serviceId) ⇒ `ZonedDateTime | null`
523-
- .maximumSince(timestamp,serviceId) ⇒ `PersistedItem | null`
524-
- .maximumUntil(timestamp,serviceId) ⇒ `PersistedItem | null`
523+
- .maximumSince(timestamp, serviceId) ⇒ `PersistedItem | null`
524+
- .maximumUntil(timestamp, serviceId) ⇒ `PersistedItem | null`
525525
- .maximumBetween(begin, end, serviceId) ⇒ `PersistedItem | null`
526526
- .minimumSince(timestamp, serviceId) ⇒ `PersistedItem | null`
527527
- .minimumUntil(timestamp, serviceId) ⇒ `PersistedItem | null`
528528
- .minimumBetween(begin, end, serviceId) ⇒ `PersistedItem | null`
529529
- .persist(serviceId): Tells the persistence service to store the current Item state, which is then done asynchronously.
530530
**Warning:** This has the side effect, that if the Item state changes shortly after `.persist` has been called, the new Item state will be persisted. See [JSDoc](https://openhab.github.io/openhab-js/items.ItemPersistence.html#persist) for a possible work-around.
531+
- .persist(timestamp, state, serviceId): Tells the persistence service to store the given state at the given timestamp, which is then done asynchronously.
531532
- .persistedState(timestamp, serviceId) ⇒ `PersistedItem | null`
532533
- .previousState(skipEqual, serviceId) ⇒ `PersistedItem | null`
533534
- .nextState(skipEqual, serviceId) ⇒ `PersistedItem | null`

src/items/item-persistence.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,25 @@ class ItemPersistence {
137137
/**
138138
* Persists the state of a given Item.
139139
*
140-
* Tells the persistence service to store the current state of the Item, which is then performed asynchronously.
141-
* This has the side effect, that if the Item state changes shortly after `.persist` has been called, the new state will be persisted.
140+
* There are four ways to use this method:
141+
* ```js
142+
* // Tell persistence to store the current Item state
143+
* items.MyItem.persistence.persist();
144+
* items.MyItem.persistence.persist('influxdb'); // using the InfluxDB persistence service
145+
*
146+
* // Tell persistence to store the state 'ON' at 2021-01-01 00:00:00
147+
* items.MyItem.persistence.persist(time.toZDT('2021-01-01T00:00:00'), 'ON');
148+
* items.MyItem.persistence.persist(time.toZDT('2021-01-01T00:00:00'), 'ON', 'influxdb'); // using the InfluxDB persistence service
149+
* ```
150+
*
151+
* **Note:** The persistence service will store the state asynchronously in the background, this method will return immediately.
152+
* When storing the current state, this has the side effect, that if the Item state changes shortly the method call, the new state will be persisted.
142153
* To work around that side effect, you might add `java.lang.Thread.sleep` to your code:
143-
* @example
154+
* ```js
144155
* items.MyItem.persistence.persist(); // Tell persistence to store the current Item state
145156
* java.lang.Thread.sleep(100); // Wait 100 ms to make sure persistence has enough time to store the current Item state
146157
* items.MyItem.postUpdate(0); // Now set the Item state to a new value
158+
* ```
147159
*
148160
* @param {(time.ZonedDateTime | Date)} [timestamp] the date for the item state to be stored
149161
* @param {string} [state] the state to be stored

types/items/item-persistence.d.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@ declare class ItemPersistence {
1717
/**
1818
* Persists the state of a given Item.
1919
*
20-
* Tells the persistence service to store the current state of the Item, which is then performed asynchronously.
21-
* This has the side effect, that if the Item state changes shortly after `.persist` has been called, the new state will be persisted.
20+
* There are four ways to use this method:
21+
* ```js
22+
* // Tell persistence to store the current Item state
23+
* items.MyItem.persistence.persist();
24+
* items.MyItem.persistence.persist('influxdb'); // using the InfluxDB persistence service
25+
*
26+
* // Tell persistence to store the state 'ON' at 2021-01-01 00:00:00
27+
* items.MyItem.persistence.persist(time.toZDT('2021-01-01T00:00:00'), 'ON');
28+
* items.MyItem.persistence.persist(time.toZDT('2021-01-01T00:00:00'), 'ON', 'influxdb'); // using the InfluxDB persistence service
29+
* ```
30+
*
31+
* **Note:** The persistence service will store the state asynchronously in the background, this method will return immediately.
32+
* When storing the current state, this has the side effect, that if the Item state changes shortly the method call, the new state will be persisted.
2233
* To work around that side effect, you might add `java.lang.Thread.sleep` to your code:
23-
* @example
34+
* ```js
2435
* items.MyItem.persistence.persist(); // Tell persistence to store the current Item state
2536
* java.lang.Thread.sleep(100); // Wait 100 ms to make sure persistence has enough time to store the current Item state
2637
* items.MyItem.postUpdate(0); // Now set the Item state to a new value
38+
* ```
2739
*
2840
* @param {(time.ZonedDateTime | Date)} [timestamp] the date for the item state to be stored
2941
* @param {string} [state] the state to be stored

types/items/item-persistence.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

0 commit comments

Comments
 (0)