Skip to content

Commit c118735

Browse files
committed
rename: onMutation to watch
1 parent 6e9b88a commit c118735

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@
5757
```
5858
- you can make a substrata of another substrata
5959

60-
### onMutation events
60+
### watch mutations
6161
- you can listen to global mutations on the strata
6262
```ts
63-
strata.onMutation(s => console.log(s.count))
63+
strata.watch(s => console.log(s.count))
6464
```
6565
- substrata listeners don't care about outside changes
6666
```ts
67-
snacks.onMutation(s => console.log(s.peanuts))
67+
snacks.watch(s => console.log(s.peanuts))
6868
```
69-
- onMutation returns a fn to stop listening
69+
- watch returns a fn to stop listening
7070
```ts
71-
const stop = strata.onMutation(s => console.log(s.count))
71+
const stop = strata.watch(s => console.log(s.count))
7272
stop() // stop listening
7373
```
7474

s/parts/chronstrata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class Chronstrata<ParentState extends Substate, S extends Substate> imple
2626
return this.#substrata.state.future.length
2727
}
2828

29-
onMutation(fn: (state: S) => void) {
30-
return this.#substrata.onMutation(chronicle => fn(chronicle.present))
29+
watch(fn: (state: S) => void) {
30+
return this.#substrata.watch(chronicle => fn(chronicle.present))
3131
}
3232

3333
/** progress forwards in history */

s/parts/strata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export class Strata<S extends State> implements Stratum<S> {
1414
})
1515

1616
options: Options
17-
onMutation = sub<[state: S]>()
17+
watch = sub<[state: S]>()
1818

1919
#mutable: S
2020
#immutable: S
2121
#mutationLock = 0
2222
#dispatchMutation = debounce(0, async(state: S) => {
2323
this.#mutationLock++
24-
try { await this.onMutation.pub(state) }
24+
try { await this.watch.pub(state) }
2525
finally { this.#mutationLock-- }
2626
})
2727

s/parts/substrata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {Chronicle, Mutator, Options, Selector, Stratum, Substate} from "./types.
55

66
export class Substrata<ParentState extends Substate, S extends Substate> implements Stratum<S> {
77
dispose: () => void
8-
onMutation = sub<[state: S]>()
8+
watch = sub<[state: S]>()
99

1010
#immutable: S
11-
#dispatchMutation = debounce(0, (state: S) => this.onMutation.pub(state))
11+
#dispatchMutation = debounce(0, (state: S) => this.watch.pub(state))
1212

1313
constructor(
1414
private parent: Stratum<ParentState>,
@@ -19,7 +19,7 @@ export class Substrata<ParentState extends Substate, S extends Substate> impleme
1919
const state = this.selector(this.parent.state)
2020
this.#immutable = deep.freeze(this.options.clone(state))
2121

22-
this.dispose = this.parent.onMutation(async parentState => {
22+
this.dispose = this.parent.watch(async parentState => {
2323
const oldState = this.#immutable
2424
const newState = this.selector(parentState)
2525
const isChanged = !deep.equal(newState, oldState)

s/parts/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type Substate = {} | null | undefined
1313

1414
export type Stratum<S extends Substate> = {
1515
readonly state: S
16-
onMutation(fn: (s: S) => void): () => void
16+
watch(fn: (s: S) => void): () => void
1717
mutate(mutator: Mutator<S>): Promise<S>
1818
substrata<Sub extends Substate>(selector: Selector<S, Sub>): Substrata<S, Sub>
1919
}

s/tests.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@ await Science.run({
4040
expect(() => strata.state.x = 3).throws()
4141
}),
4242

43-
"onMutation is published": Science.test(async() => {
43+
"watch is published": Science.test(async() => {
4444
const strata = new Strata({count: 0})
4545
let mutationCount = 0
46-
strata.onMutation.sub(() => {mutationCount++})
46+
strata.watch.sub(() => {mutationCount++})
4747
await strata.mutate(state => state.count++)
4848
expect(mutationCount).is(1)
4949
}),
5050

51-
"onMutation is debounced": Science.test(async() => {
51+
"watch is debounced": Science.test(async() => {
5252
const strata = new Strata({count: 0})
5353
let mutationCount = 0
54-
strata.onMutation.sub(() => {mutationCount++})
54+
strata.watch.sub(() => {mutationCount++})
5555
const promise = strata.mutate(state => state.count++)
5656
expect(mutationCount).is(0)
5757
await promise
5858
expect(mutationCount).is(1)
5959
}),
6060

61-
"onMutation is fired when array item is pushed": Science.test(async() => {
61+
"watch is fired when array item is pushed": Science.test(async() => {
6262
const strata = new Strata({items: ["hello", "world"]})
6363
let mutationCount = 0
64-
strata.onMutation.sub(() => {mutationCount++})
64+
strata.watch.sub(() => {mutationCount++})
6565
await strata.mutate(state => state.items.push("lol"))
6666
expect(mutationCount).is(1)
6767
expect(strata.state.items.length).is(3)
@@ -70,7 +70,7 @@ await Science.run({
7070
"prevent mutation loops": Science.test(async() => {
7171
const strata = new Strata({count: 0})
7272
let mutationCount = 0
73-
strata.onMutation.sub(async() => {
73+
strata.watch.sub(async() => {
7474
mutationCount++
7575
if (mutationCount > 100)
7676
return
@@ -131,12 +131,12 @@ await Science.run({
131131
expect(b.state.c).is(103)
132132
}),
133133

134-
"onMutation ignores outside mutations": Science.test(async() => {
134+
"watch ignores outside mutations": Science.test(async() => {
135135
const strata = new Strata({a: {x: 0}, b: {x: 0}})
136136
const a = strata.substrata(s => s.a)
137137
const b = strata.substrata(s => s.b)
138138
let counted = 0
139-
b.onMutation.sub(() => {counted++})
139+
b.watch.sub(() => {counted++})
140140
await a.mutate(a => a.x = 1)
141141
expect(counted).is(0)
142142
}),

0 commit comments

Comments
 (0)