UNPKG

react-scoped-model

Version:
53 lines (52 loc) 2.52 kB
/** * @license * MIT License * * Copyright (c) 2021 Alexis Munsayac * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * * @author Alexis Munsayac <alexis.munsayac@gmail.com> * @copyright Alexis Munsayac 2021 */ import React, { Context, WeakValidationMap, PropsWithChildren } from 'react'; import Notifier from './notifier'; export declare type ScopedModelHook<Model, Props = unknown> = (props: Props) => Model; export declare type ScopedModelMemo<Props = unknown> = (prev: Props, next: Props) => boolean; export declare type ScopedModelBailout<T> = (prev: T, next: T) => boolean; export interface ScopedModelOptions<Model, Props = unknown> { displayName?: string; propTypes?: WeakValidationMap<Props>; defaultProps?: Partial<Props>; shouldUpdate?: ScopedModelMemo<Props>; shouldNotify?: ScopedModelBailout<Model>; } export interface ScopedModel<Model, Props = unknown> { context: Context<Notifier<Model> | null>; Provider: React.FC<PropsWithChildren<Props>>; displayName: string; } /** * Creates a scoped model instance that generates a state from a given * React-based hook function which allows fine-grained control on injected * contextual state within the component tree. * @param useModelHook * @param options */ export default function createModel<Model, Props = unknown>(useModelHook: ScopedModelHook<Model, Props>, options?: ScopedModelOptions<Model, Props>): ScopedModel<Model, Props>;