This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui/react: extract updater constructors
- Loading branch information
1 parent
24c0953
commit 179f9d2
Showing
2 changed files
with
39 additions
and
37 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,37 @@ | ||
import type { FullDB } from "../../../core"; | ||
import type { | ||
MassUpdater, | ||
MassUpdaterInternal, | ||
Updater, | ||
UpdaterInternal, | ||
} from "./types"; | ||
|
||
export function makeMassUpdater<Domain, K extends keyof Domain>( | ||
db: FullDB<Domain> | ||
): MassUpdater<Domain, K> { | ||
const updater: MassUpdaterInternal<Domain, K> = function ( | ||
_options, | ||
kind, | ||
id, | ||
delta | ||
) { | ||
db.update(kind, id, delta); | ||
}; | ||
return (a: any, b: any, c: any, d?: any) => | ||
d ? updater(a, b, c, d) : updater({}, a, b, c); | ||
} | ||
|
||
export function makeUpdater<Domain, K extends keyof Domain>( | ||
massUpdater: MassUpdater<Domain, K>, | ||
kind: K, | ||
id: string | ||
): Updater<Domain[K]> { | ||
const updater: UpdaterInternal<Domain[K]> = function (options, delta) { | ||
massUpdater(options, kind, id, delta); | ||
}; | ||
return ((a: any, b?: any) => { | ||
if (typeof a === "string") | ||
return updater({}, (prev) => ({ ...prev, [a]: b })); | ||
updater(b ? a : {}, b || a); | ||
}) as any; | ||
} |