|
| 1 | +# Frequency of local storage save |
| 2 | + |
| 3 | +* Status: **accepted** <!-- optional --> |
| 4 | +* Deciders: Pietro <!-- optional --> |
| 5 | +* Date: 2019-01-24 <!-- optional --> |
| 6 | + |
| 7 | +<!-- Technical Story: [description | ticket/issue URL] --> |
| 8 | + |
| 9 | +## Context and Problem Statement |
| 10 | + |
| 11 | +<!-- [Describe the context and problem statement, e.g., in free form using two to three sentences. You may want to articulate the problem in form of a question.] --> |
| 12 | + |
| 13 | +The issue is how often to save to local storage when a user types, previous implementation saved every 5 characters. But that caused issues [#86](https://github.com/bbc/react-transcript-editor/issues/86) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Decision Drivers <!-- optional --> |
| 18 | + |
| 19 | +<!-- * [driver 1, e.g., a force, facing concern, …] --> |
| 20 | +* A simple and straight forward way to save to local storage |
| 21 | +* saving on a good frequency |
| 22 | +* without introducing performance issues especially on less performant devices |
| 23 | +* if possible without introducing third party dependencies |
| 24 | + |
| 25 | +## Considered Options |
| 26 | + |
| 27 | +* [loadash debounce](https://lodash.com/docs/4.17.11#debounce) |
| 28 | +* using a js timer |
| 29 | + |
| 30 | +## Notes on debounce option |
| 31 | + |
| 32 | +What is a debounce function? |
| 33 | + |
| 34 | +> debounce function does, it limits the rate at which a function can fire. |
| 35 | +> [...] You'll pass the debounce function the function to execute and the fire rate limit in milliseconds |
| 36 | +
|
| 37 | +from https://john-dugan.com/javascript-debounce/ |
| 38 | + |
| 39 | +In more detail |
| 40 | + |
| 41 | +https://davidwalsh.name/javascript-debounce-function |
| 42 | + |
| 43 | +with examples |
| 44 | + |
| 45 | +https://css-tricks.com/debouncing-throttling-explained-examples/ |
| 46 | + |
| 47 | + |
| 48 | +https://lodash.com/docs/4.17.11#debounce |
| 49 | + |
| 50 | +## Decision Outcome |
| 51 | + |
| 52 | +Chosen option: **using a js timer**. |
| 53 | + |
| 54 | +It uses a timer that can be consolidated into one final one rather then having a lot of saves being delayed, we just have one final save once after user has stopped typing for more then 5 seconds. |
| 55 | + |
| 56 | +The timer is cleared before being called so that there is only the final one left. Leaving only one final save at the end. As a performance optimization. |
| 57 | + |
| 58 | +```js |
| 59 | +if (this.saveTimer!== undefined) { |
| 60 | + clearTimeout(this.saveTimer); |
| 61 | +} |
| 62 | +this.saveTimer = setTimeout(() => { |
| 63 | + this.localSave(this.props.mediaUrl); |
| 64 | +}, 5000); |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | + <!-- because [justification. e.g., only option, which meets k.o. criterion decision driver | which resolves force force | … | comes out best (see below)]. --> |
| 69 | + |
| 70 | + |
| 71 | +<!-- |
| 72 | +### Positive Consequences |
| 73 | +
|
| 74 | +* [e.g., improvement of quality attribute satisfaction, follow-up decisions required, …] |
| 75 | +* … |
| 76 | +
|
| 77 | +### Negative consequences |
| 78 | +
|
| 79 | +* [e.g., compromising quality attribute, follow-up decisions required, …] |
| 80 | +* … |
| 81 | +
|
| 82 | +## Pros and Cons of the Options |
| 83 | +
|
| 84 | +### [option 1] |
| 85 | +
|
| 86 | +[example | description | pointer to more information | …] |
| 87 | +
|
| 88 | +* Good, because [argument a] |
| 89 | +* Good, because [argument b] |
| 90 | +* Bad, because [argument c] |
| 91 | +
|
| 92 | +### [option 2] |
| 93 | +
|
| 94 | +[example | description | pointer to more information | …] |
| 95 | +
|
| 96 | +* Good, because [argument a] |
| 97 | +* Good, because [argument b] |
| 98 | +* Bad, because [argument c] |
| 99 | +
|
| 100 | +
|
| 101 | +### [option 3] |
| 102 | +
|
| 103 | +[example | description | pointer to more information | …] |
| 104 | +
|
| 105 | +* Good, because [argument a] |
| 106 | +* Good, because [argument b] |
| 107 | +* Bad, because [argument c] |
| 108 | +
|
| 109 | +
|
| 110 | +## Links |
| 111 | +
|
| 112 | +* [Link type] [Link to ADR] --> |
0 commit comments