-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1074 from fabulous-dev/component-binding
Add support for Binding in Components
- Loading branch information
Showing
4 changed files
with
70 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Fabulous | ||
|
||
open System | ||
open System.ComponentModel | ||
open System.Runtime.CompilerServices | ||
|
||
type BindingRequest<'T> = delegate of unit -> StateValue<'T> | ||
|
||
[<Struct>] | ||
type BindingValue<'T> = | ||
[<EditorBrowsable(EditorBrowsableState.Never)>] | ||
val public SourceContext: ComponentContext | ||
|
||
[<EditorBrowsable(EditorBrowsableState.Never)>] | ||
val public SourceKey: int | ||
|
||
new(stateValue: StateValue<'T>) = | ||
{ SourceContext = stateValue.Context | ||
SourceKey = stateValue.Key } | ||
|
||
member this.Current = this.SourceContext.TryGetValue<'T>(this.SourceKey).Value | ||
|
||
member inline this.Set(value: 'T) = | ||
this.SourceContext.SetValue(this.SourceKey, value) | ||
|
||
[<AutoOpen>] | ||
module BindingBuilders = | ||
type Context with | ||
|
||
static member inline Binding(value: StateValue<'T>) = BindingRequest<'T>(fun () -> value) | ||
|
||
[<Extension>] | ||
type BindingExtensions = | ||
[<Extension>] | ||
static member inline Bind | ||
( | ||
_: ComponentBuilder<'parentMsg>, | ||
[<InlineIfLambda>] fn: BindingRequest<'T>, | ||
[<InlineIfLambda>] continuation: BindingValue<'T> -> ComponentBodyBuilder<'marker> | ||
) = | ||
ComponentBodyBuilder<'marker>(fun bindings ctx -> | ||
let key = int bindings | ||
let stateValue = fn.Invoke() | ||
|
||
// Dispose previous subscription | ||
match ctx.TryGetValue<IDisposable>(key) with | ||
| ValueNone -> () | ||
| ValueSome d -> d.Dispose() | ||
|
||
// Subscribe to source context changes | ||
let sourceKey = stateValue.Key | ||
|
||
let sub = | ||
stateValue.Context.RenderNeeded.Subscribe(fun k -> | ||
if k = sourceKey then | ||
ctx.NeedsRender(key)) | ||
|
||
ctx.SetValueInternal(key, sub) | ||
|
||
let bindingValue = BindingValue<'T>(stateValue) | ||
(continuation bindingValue).Invoke(bindings, ctx)) |
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