-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This updates the sensor interface to no longer require a state reference as input, but simply return a subscribable for changes on a specific path. This makes the interface simpler and moves the responsibility of updating the state to sensor subscribers. This is however a breaking change, even though the contructor interface does not changes. Change-type: major
- Loading branch information
Showing
5 changed files
with
114 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Ref } from '../ref'; | ||
import type { Operation } from '../operation'; | ||
import { View } from '../view'; | ||
import type { Path } from '../path'; | ||
|
||
export function patch<S>( | ||
r: Ref<S>, | ||
changes: Operation<S, Path> | Array<Operation<S, Path>>, | ||
) { | ||
changes = Array.isArray(changes) ? changes : [changes]; | ||
changes.forEach((change) => { | ||
const view = View.from(r, change.path); | ||
switch (change.op) { | ||
case 'create': | ||
case 'update': | ||
view._ = change.target as any; | ||
break; | ||
case 'delete': | ||
view.delete(); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
export type Patcher<S> = ( | ||
changes: Operation<S, Path> | Array<Operation<S, Path>>, | ||
) => void; | ||
|
||
export function Patcher<S>(s: S): Patcher<S> { | ||
const r = Ref.of(s); | ||
return (changes: Operation<S, Path> | Array<Operation<S, Path>>) => | ||
patch(r, changes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters