How to use the idom.core._hooks.current_hook function in idom

To help you get started, we’ve selected a few idom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rmorshea / idom / idom / core / hooks.py View on Github external
async def use_frame_rate(rate: float = 0) -> None:
    await current_hook().use_state(FramePacer, rate).wait()
github rmorshea / idom / idom / core / hooks.py View on Github external
def use_lru_cache(
    function: _LruFunc, maxsize: Optional[int] = 128, typed: bool = False
) -> _LruFunc:
    return current_hook().use_state(lru_cache(maxsize, typed), function)
github rmorshea / idom / idom / core / hooks.py View on Github external
def _use_state(
    value: _StateType, should_update: Callable[[_StateType, _StateType], bool],
) -> Tuple[_StateType, Callable[[_StateType], None]]:
    hook = current_hook()
    state: Dict[str, Any] = hook.use_state(dict)
    update = hook.use_update()

    if "value" not in state:
        state["value"] = value

    def set_state(new: _StateType) -> None:
        old = state["value"]
        if should_update(new, old):
            state["value"] = new
            update()

    return state["value"], set_state
github rmorshea / idom / idom / core / hooks.py View on Github external
def use_context(
    context_type: Type[Context[_StateType]],
    should_update: Callable[[_StateType, _StateType], bool] = _new_is_not_old,
) -> Tuple[_StateType, Callable[[_StateType], None]]:
    return current_hook().use_context(context_type, should_update)
github rmorshea / idom / idom / core / hooks.py View on Github external
def _use_shared(
    shared: Shared[_StateType], should_update: Callable[[_StateType, _StateType], bool],
) -> [_StateType, Callable[[_StateType], None]]:
    hook = current_hook()
    element_id = hook.element().id
    update = hook.use_update()

    hook.use_finalizer(shared._callbacks.pop, element_id, None)
    old = shared._value
    shared._callbacks[element_id] = (
        lambda new: update() if should_update(new, old) else None
    )

    def set_state(new: _StateType) -> None:
        shared.update(new)

    return shared._value, set_state
github rmorshea / idom / idom / core / hooks.py View on Github external
def use_update() -> Callable[[], None]:
    return current_hook().use_update()
github rmorshea / idom / idom / core / hooks.py View on Github external
def use_memo(
    function: Callable[..., _MemoValue], *args: Any, **kwargs: Any
) -> _MemoValue:
    hook = current_hook()
    cache: Dict[int, _MemoValue] = hook.use_state(dict)

    key = hash((args, tuple(kwargs.items())))
    if key in cache:
        result = cache[key]
    else:
        cache.clear()
        result = cache[key] = function(*args, **kwargs)

    return result