fix: add missing callback handlers, and simplify optional args handling

This commit is contained in:
Jonas Juselius
2024-09-23 13:48:51 +02:00
parent 9c433469f7
commit 99c46d8aac
2 changed files with 17 additions and 26 deletions

View File

@@ -208,16 +208,11 @@ type HookContext(host: HookContextHost) =
member this.useEffectOnce(effect) : unit =
this.setEffect(Effect.OnConnected effect)
member this.useContext(context: ContextType<'T>, ?subscribe: bool, ?callback: ContextCallback<'T>): ContextConsumer<_> =
let subscribe = subscribe |> Option.defaultValue false
match callback with
| Some f -> Lit.contextConsumer(this.host, context, subscribe, f)
| None -> Lit.contextConsumer(this.host, context, subscribe)
member this.useContext(context: ContextType<'T>, subscribe: bool, callback: ContextCallback<'T> option): ContextConsumer<_> =
Lit.contextConsumer(this.host, context, subscribe, callback)
member this.addProvider(context: ContextType<'T>, ?initialValue: 'T): ContextProvider<_> =
match initialValue with
| Some init -> Lit.contextProvider(this.host, context, init)
| None -> Lit.contextProvider(this.host, context)
member this.addProvider(context: ContextType<'T>, initialValue: 'T option): ContextProvider<_> =
Lit.contextProvider(this.host, context, initialValue)
[<AllowNullLiteral>]
type IHookProvider =
@@ -582,13 +577,10 @@ type Hook() =
/// </summary>
/// <param name="context">Context object handle</param>
/// <param name="subscribe">Subscribe to changes to the context provider state</param>
static member inline useContext(context: ContextType<'T>, ?subscribe: bool) =
match subscribe with
| Some sub -> Hook.getContext().useContext(context, sub)
| None -> Hook.getContext().useContext(context)
static member inline useContext<'T>(context: ContextType<'T>, ?subscribe: bool, ?callback: ContextCallback<'T>) =
let subscribe = subscribe |> Option.defaultValue false
Hook.getContext().useContext(context, subscribe, callback)
static member inline addProvider(context: ContextType<'T>, ?initalValue: 'T) =
match initalValue with
| Some value -> Hook.getContext().addProvider(context, value)
| None -> Hook.getContext().addProvider(context)
static member inline addProvider<'T>(context: ContextType<'T>, ?initialValue: 'T) =
Hook.getContext().addProvider(context, initialValue)

View File

@@ -72,6 +72,11 @@ type ContextConsumerOpts<'T>
member val subscribe: bool option = jsNative with get, set
member val callback: ContextCallback<'T> option = jsNative with get, set
[<ImportMember("@lit/context")>]
type ContextRoot() =
member _.attach(element: HTMLElement): unit = jsNative
member _.detach(element: HTMLElement): unit = jsNative
[<ImportMember("@lit/context")>]
type ContextProvider<'T>(this: obj, args: ContextProviderArgs<'T>) =
member _.setValue(v: 'T): unit = jsNative
@@ -81,12 +86,7 @@ type ContextConsumer<'T>(host: obj, options: ContextConsumerOpts<'T>) =
member _.value: 'T = jsNative
[<ImportMember("@lit/context")>]
type ContextRoot() =
member _.attach(element: HTMLElement): unit = jsNative
member _.detach(element: HTMLElement): unit = jsNative
[<ImportMember("@lit/context")>]
type ContextRequestEvent() =
type ContextRequestEvent<'T>() =
member val context: ContextType<'T> = jsNative with get
member val subscribe: bool option = jsNative with get
member val callback: ContextCallback<'T> option = jsNative with get
@@ -435,15 +435,14 @@ type Lit() =
let ctx = JsInterop.emitJsExpr contextId "$0"
LitBindings.createContext ctx
static member contextProvider<'T>(root: obj, context: ContextType<'T>, ?initialValue: 'T): ContextProvider<_> =
static member contextProvider<'T>(root: obj, context: ContextType<'T>, initialValue: 'T option): ContextProvider<_> =
match initialValue with
| Some initial ->
ContextProvider(root, ContextProviderArgs(context = context, initialValue = initial))
| None ->
ContextProvider(root, ContextProviderArgs(context = context))
static member contextConsumer<'T>(root: obj, context: ContextType<'T>, ?subscribe: bool, ?callback: ContextCallback<'T>): ContextConsumer<_> =
let subscribe = subscribe |> Option.defaultValue false
static member contextConsumer<'T>(root: obj, context: ContextType<'T>, subscribe: bool, callback: ContextCallback<'T> option): ContextConsumer<_> =
match callback with
| Some f ->
ContextConsumer(root, ContextConsumerOpts(context = context, subscribe = subscribe, callback = f))