Skip to content

Commit 2881530

Browse files
committed
docs: README corrections
1 parent f6ae7e9 commit 2881530

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

README.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,20 @@ let crystalizer = new Crystalizer({
8383

8484
## Samples
8585

86-
- **TODO** React TODO with undo/redo
87-
- **TODO** Small backend with event-driven frontend
88-
- **TODO**
86+
Sample apps, as built, will be placed here and linked to `./samples`.
87+
88+
- **TODO** React TODO app with undo/redo
89+
- **TODO** Time-based journal app
90+
- **TODO** Thin backend with seamless offline experience
8991

9092
## What are 'Crystals' and 'Shards'? And why?
9193

9294
A crystalizer is, in essence, a reducer. With default settings, you get something that closely resembles state management from things like Redux. Which, of course, is just a normal reduce function used in a particular way. So, you might wonder what the names are for, and why not just use the colloquial names 'actions' and 'reducers'?
9395

9496
I'll answer that now, and also give an introduction to Crystalize.js.
9597

98+
## Introduction
99+
96100
Crystalize.js, while it _is_ essentially a reducer, it serves a different purpose. A reducer simply _reduces_ a collection of elements into a single aggregate. But, what Crystalize.js sets out to do is a little bit different. What if you want to keep the collection you passed in? What if you want variable amounts of that collection aggregated, or to be able to rewind to different points of that aggregation to see what it was at that point?
97101

98102
It's fair to consider think of a 'crystal' as an accumulator, and a 'shard' as an element. And that's really what they are. But that doesn't capture the goal of Crystalize.js, either.
@@ -101,8 +105,6 @@ They could likewise be called 'state' and 'actions', and that's really what they
101105

102106
Thus, the names are chosen to better reflect what Crystalize.js is doing, in verb form. Shards are _crystalized_ into an accumulated state, and the name calls that out to reflect the control and choice you have in how that process takes place.
103107

104-
## Introduction
105-
106108
To illustrate, here's the flow of an action+reducer:
107109

108110
```
@@ -115,7 +117,7 @@ To illustrate, here's the flow of an action+reducer:
115117
readable
116118
```
117119

118-
You pass actions into the reducer, and then they're aggregated into the accumulator, in this case, your action state. You have your state, which is great, but your action is gone. It cannot be replayed, and timing data about that action is lost, unless you add _additional_ state in order to track that information.
120+
You pass actions into the reducer, and then they're aggregated into the accumulator, in this case, your app state. You have your state, which is great, but your action is gone. It cannot be replayed, and timing data about that action is lost, unless you add _additional state_ in order to track that information.
119121

120122
Here's the flow of Crystalize.js:
121123

@@ -138,9 +140,9 @@ Here's the flow of Crystalize.js:
138140
.take(N) .with(shards)
139141
```
140142

141-
You add shards (colloquially, 'actions'), via the `with()` method. You get the state via the `take()` method. But, you can also do more than just get the final state. You also get `N` count of the most recent shards that were added via `with`, and the crystal that is the aggregate of the shards you did _not_ take.
143+
You add shards (colloquially, 'actions'), via the `.with()` method. You get the state via the `take()` method. But, you can also do more than just get the final state. You also get `N` count of the most recent shards that were added via `.with`, and the crystal that is the aggregate of the shards you did _not_ take.
142144

143-
Putting this together, let's say you called `with()` and added 5 shards. Then, you called `.take(3)`. You'll get: 1) The final crystal, 2) The 3 most recently added shards, 3) The crystal that is the aggregate of the 2 oldest shards.
145+
Putting this together, let's say you called `.with()` and added 5 shards. Then, you called `.take(3)`. You'll get: 1) The final crystal, 2) The 3 most recently added shards, 3) The crystal that is the aggregate of the 2 oldest shards.
144146

145147
Let's bring that home with a code example:
146148

@@ -164,12 +166,12 @@ const [crystal, shards, base] = crystalizer.take(3);
164166

165167
console.log(crystal); // { total: 5 }
166168
console.log(shards); // [ { value: 1 }, { value: 1 }, { value: 1 } ]
167-
console.log(crystal); // { total: 2 }
169+
console.log(base); // { total: 2 }
168170
```
169171

170172
You can call this multiple times in a row without losing any data:
171173

172-
_(calling `take()` with no arguments is equilavent to `take(Infinity)`)_
174+
_(calling `take()` with no arguments is equivalent to `take(Infinity)`)_
173175

174176
```typescript
175177
crystalizer = crystalizer.with([
@@ -184,23 +186,23 @@ function logCrystalN(n?: number) {
184186
const [crystal, shards, base] = crystalizer.take(n);
185187
console.log(crystal);
186188
console.log(shards);
187-
console.log(crystal);
189+
console.log(base);
188190
}
189191

190-
logCrystalN();
192+
logCrystalN(1);
191193
// { total: 5 }
192-
// []
193-
// { total: 0 }
194+
// [{ value: 1 }]
195+
// { total: 4 }
194196

195197
logCrystalN(4);
196198
// { total: 5 }
197-
// [{ value: 1}, { value: 1}, { value: 1}, { value: 1}]
199+
// [{ value: 1 }, { value: 1 }, { value: 1 }, { value: 1 }]
198200
// { total: 1 }
199201

200-
logCrystalN(1);
202+
logCrystalN();
201203
// { total: 5 }
202-
// [{ value: 1}]
203-
// { total: 4 }
204+
// [{ value: 1 }, { value: 1 }, { value: 1 }, { value: 1 }, { value: 1 }]
205+
// { total: 0 }
204206
```
205207

206208
#### .without()
@@ -286,9 +288,9 @@ Let's step through what`s happening here.
286288
1. We called `.leave(2)`, so shards with id `4` and `5` are excluded from here on.
287289
2. We called `.take(1)`, so we're only interested in keeping the next most recend shard, id `3`
288290
3. `crystal` contains the aggregate of all the shards we didn't leave: 1, 2, and 3
289-
4. `base` contains only the aggregate of the shards we didn't `take`. In this case, that's 1 & 2.
291+
4. `base` contains only the aggregate of the shards we didn't take or leave. In this case, that's 1 & 2.
290292

291-
The value `L` is reset if you call `.with()` or `.without`, and all shards that were left will not be part of the next crystalizer object:
293+
The value `L` is reset if you call `.with()` or `.without()`, and all shards that were left will not be part of the next crystalizer object:
292294

293295
```typescript
294296
let crystalizer2 = crystalizer.leave(4).with([
@@ -320,7 +322,7 @@ crystalizer = crystalizer.leave((l) => l - 1);
320322

321323
The `.leave()` method is fine if you either know the historic index you want to backtrack to, or you simple want to increment the current one (undo/redo).
322324

323-
But, there might be time's where you want to focus on a specific shard and calculate both crystals as though that shard is the most recent shard.
325+
But, there might be times where you want to focus on a specific shard and calculate both crystals as though that shard is the most recent shard.
324326

325327
You can use `.focus()` to accomplish that.
326328

@@ -336,15 +338,15 @@ crystalizer = crystalizer.with([
336338
crystalizer = crystalizer.focus((shard) => shard.id == 3);
337339
```
338340

339-
Note that unlike `.leave()`, the internal pointer is _NOT_ reset when you call `.with()` or `.without()`. Instead, the pointer is updated for each call of `.with()` or `without()' per the seek function.
341+
Note that unlike `.leave()`, the internal pointer is _NOT_ reset when you call `.with()` or `.without()`. Instead, the pointer is updated for each call of `.with()` or `.without()` per the seek function.
340342

341343
You can also use `.focus()` for a chronological value, such as `T` timestamp.
342344

343345
```typescript
344346
crystalizer = crystalizer.focus((shard) => shard.ts >= Date.now() - WEEK);
345347
```
346348

347-
However, this relies on the shards being sorted by that value. We'll get into sorting as well in the next section, but there's also builtin ways to handle timestamps in Crystalize.js (see #TODO).
349+
However, this relies on the shards being sorted by that value. We'll get into sorting as well in the next section, but there's also builtin ways to handle timestamps in Crystalize.js (see [Timestamp](#timestamp)).
348350

349351
## Init options
350352

@@ -380,7 +382,7 @@ console.log(crystal);
380382
// { total: 22 }
381383

382384
console.log(shards);
383-
// Note that { timestamp: 3, value: 4 } is missing
385+
// Note that { timestamp: 3, value: 3 } is missing
384386
//
385387
// [{ timestamp: 1, value: 2 },
386388
// { timestamp: 1, value: 1 },
@@ -404,7 +406,7 @@ new Crystalizer<Crystal, Shard>({
404406

405407
### Map
406408

407-
You might wish to automatically add or change certain keys to every shard. Id`s are a great example of this. You can do so by specifying the `map` option, which takes a simple map function:
409+
You might wish to automatically add or change certain keys to every shard. Id's are a great example of this. You can do so by specifying the `map` option, which takes a simple map function:
408410

409411
```typescript
410412
import { ulid } from 'ulid';
@@ -416,11 +418,11 @@ let crystalizer = new Crystalizer<Crystal, Shard>({
416418
});
417419
```
418420

419-
Now, all your shards will have a unique id from `ulid` if they didn't already have an id.
421+
Now, all your shards will have a unique id from `ulid` if they didn't already have one.
420422

421423
### Timestamp
422424

423-
We have enough building blocks to ensure every shard has a timestamp, are ordered by those timestamps, and then revisit the `.focus()` example using them.
425+
We have enough building blocks to ensure every shard has a timestamp, and are ordered by those timestamps.
424426

425427
```typescript
426428
import { ulid } from 'ulid';
@@ -436,7 +438,7 @@ let crystalizer = new Crystalizer<Crystal, Shard>({
436438
});
437439
```
438440

439-
We can do this much more simply by specifying the `tsKey` option:
441+
But, we can do this much more simply by specifying the `tsKey` option:
440442

441443
```typescript
442444
import { ulid } from 'ulid';

0 commit comments

Comments
 (0)