Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "object"

Index

Classes

Functions

Functions

Const $object

  • $object<T, K>(src: T, opts: Partial<StreamObjOpts<T, K>>, inner: Fn<StreamObj<T, K>["streams"], Promise<ComponentLike>>): $Object<T, K>
  • Creates a control component wrapper with an internal stream setup for user defined keys in the given object. When this component is mounted, it will call inner with an object of key-value streams and then $compiles that function's return value as component body.

    remarks

    Uses {@link @thi.ng/rstream#fromObject} for creating the internal key-value streams. These can then be used by inner to produce reactive child elements. The given src object is only used to seed those streams with initial values. The component wrapper can be updated with new values, using the .update() life cycle method with a new object.

    By default the value streams will only trigger updates if their values have changed. See {@link @thi.ng/rstream#StreamObjOpts} for more details and options.

    Also see $subObject.

    example
    const obj = $object(
      // source object (for seeding)
      { id: "a", name: "foo", ignore: 23 },
      // create subscriptions for given keys
      { keys: ["id", "name"] }
      // component factory
      async (obj) => <ComponentLike>["div", {}, "id: ", obj.id, " name: ", obj.name]
    );
    
    obj.mount(document.body);
    
    obj.update({ id: "b", name: "bar" });

    Type parameters

    • T

    • K: Keys<T>

    Parameters

    • src: T
    • opts: Partial<StreamObjOpts<T, K>>

      options for fromObject() stream setup

    • inner: Fn<StreamObj<T, K>["streams"], Promise<ComponentLike>>

    Returns $Object<T, K>

Const $subObject

  • $subObject<T, K>(src: ISubscribable<T>, opts: Partial<StreamObjOpts<T, K>>, inner: Fn<StreamObj<T, K>["streams"], Promise<ComponentLike>>): IComponent<T>
  • Syntax sugar for a combination of $sub and $object to allow reactive updates of $object() components themselves.

    example
    interface Foo {
      id: string;
      name: string;
    }
    
    const state = reactive<Foo>({ id: "a", name: "foo" });
    
    $subObject<Foo, keyof Foo>(
      state,
      { keys: ["id", "name"] },
      // component factory
      // only executed once, but `obj.id` and `obj.name` are reactive values
      async (obj) => <ComponentLike>["div", {}, "id: ", obj.id, " name: ", obj.name]
    ).mount(document.body);
    
    // update
    state.next({ id: "b", name: "bar" });

    Type parameters

    • T

    • K: Keys<T>

    Parameters

    • src: ISubscribable<T>
    • opts: Partial<StreamObjOpts<T, K>>
    • inner: Fn<StreamObj<T, K>["streams"], Promise<ComponentLike>>

    Returns IComponent<T>

Generated using TypeDoc