@neovici/cosmoz-utils 5.35.0 → 5.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ type Init<T> = T | (() => T);
2
+ type UpdateFn<T> = (prev: T) => T;
3
+ type Update<T> = T | UpdateFn<T>;
4
+ type Setter<T> = (update: Update<T>) => void;
5
+ type Result<T> = [T, Setter<T>];
6
+ export declare function useProperty<T>(prop: string): Result<T | undefined>;
7
+ export declare function useProperty<T>(prop: string, init: Init<T>): Result<T>;
8
+ export {};
@@ -0,0 +1,25 @@
1
+ import { useEffect, useCallback } from 'haunted';
2
+ import { useMeta } from './use-meta';
3
+ import { useHost } from './use-host';
4
+ import { invoke } from '../function';
5
+ import { notifyProperty } from './use-notify-property';
6
+ export function useProperty(prop, init) {
7
+ const host = useHost();
8
+ const value = host[prop];
9
+ const setValue = useCallback((update) => {
10
+ const val = host[prop];
11
+ const newVal = typeof update === 'function' ? update(val) : update;
12
+ if (Object.is(val, newVal)) {
13
+ return;
14
+ }
15
+ notifyProperty(host, prop, newVal);
16
+ }, []);
17
+ const meta = useMeta({ init });
18
+ useEffect(() => {
19
+ const { init } = meta;
20
+ if (init == null)
21
+ return;
22
+ setValue(invoke(init));
23
+ }, []);
24
+ return [value, setValue];
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "5.35.0",
3
+ "version": "5.36.0",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",