@reactunity/renderer 0.14.0 → 0.14.1

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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './src/helpers/dictionary-watcher';
2
+ export * from './src/helpers/hooks/use-globals';
3
+ export * from './src/helpers/hooks/use-watchable';
2
4
  export * from './src/helpers/icons';
3
- export * from './src/helpers/watcher';
4
5
  export * from './src/models/environment';
5
6
  export * from './src/models/generated';
6
7
  export * from './src/models/properties';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './src/helpers/dictionary-watcher';
2
+ export * from './src/helpers/hooks/use-globals';
3
+ export * from './src/helpers/hooks/use-watchable';
2
4
  export * from './src/helpers/icons';
3
- export * from './src/helpers/watcher';
4
5
  export * from './src/models/environment';
5
6
  export * from './src/models/generated';
6
7
  export * from './src/models/properties';
@@ -29,6 +29,19 @@ export interface DictionaryWatcher<T = Record<string, any>> {
29
29
  * ```
30
30
  */
31
31
  useContext: () => T;
32
+ /**
33
+ * React Hook for getting the value of this Dictionary. This one will subscribe only to the properties that are accessed.
34
+ *
35
+ * Usage:
36
+ *
37
+ * ```tsx
38
+ * function App() {
39
+ * const { a, b } = watcher.useValue();
40
+ * ...
41
+ * }
42
+ * ```
43
+ */
44
+ useValue: (subscribeToAllFields?: boolean, isEqual?: IsEqual) => T;
32
45
  /**
33
46
  * React Hook for getting a partial value from Context
34
47
  *
@@ -36,22 +49,18 @@ export interface DictionaryWatcher<T = Record<string, any>> {
36
49
  *
37
50
  * ```tsx
38
51
  * function App() {
39
- * const values = watcher.useSelector(st => st.count);
52
+ * const count = watcher.useSelector(st => st.count);
40
53
  * ...
41
54
  * }
42
55
  * ```
43
56
  */
44
- useSelector<Res>(selector: (store: T) => Res): Res;
57
+ useSelector<Res>(selector: (store: T) => Res, isEqual?: IsEqual<Res>): Res;
45
58
  }
59
+ type IsEqual<T = any> = (a: T, b: T) => boolean;
46
60
  /**
47
61
  * Creates a context that updates its value when the values in the dictionary change
48
62
  * @param dictionary The dictionary to be watched. Must implement the EventDictionary type in the C#
49
63
  * @param displayName A displayName to identify this context easier in case of problems
50
64
  */
51
65
  export declare function createDictionaryWatcher<ValueType = any, RecordType = Record<string, ValueType>>(dictionary: ReactUnity.Helpers.WatchableRecord<ValueType>, displayName?: string): DictionaryWatcher<RecordType>;
52
- export declare const globalsWatcher: DictionaryWatcher<DefaultGlobals>;
53
- export declare const useGlobals: () => DefaultGlobals;
54
- export declare const useGlobalsSelector: <Res>(selector: (store: DefaultGlobals) => Res) => Res;
55
- export declare const GlobalsProvider: (props: {
56
- children?: React.ReactNode;
57
- }) => import("react").FunctionComponentElement<import("react").ProviderProps<DefaultGlobals>>;
66
+ export {};
@@ -9,7 +9,7 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
- import { createContext, createElement, useContext } from 'react';
12
+ import { createContext, createElement, useContext, useMemo, useRef } from 'react';
13
13
  import { useSyncExternalStore } from 'use-sync-external-store/shim';
14
14
  import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
15
15
  /**
@@ -21,30 +21,43 @@ export function createDictionaryWatcher(dictionary, displayName) {
21
21
  var ctx = createContext(undefined);
22
22
  if (displayName)
23
23
  ctx.displayName = displayName;
24
- var snapshot = (__assign({}, dictionary));
25
- var subscribe = function (onStoreChange) {
26
- snapshot = (__assign({}, dictionary));
27
- var remove = dictionary === null || dictionary === void 0 ? void 0 : dictionary.AddListener(function (key, value, dic) {
28
- snapshot = (__assign({}, dictionary));
29
- onStoreChange();
30
- });
31
- if (!remove) {
32
- if (displayName)
33
- console.warn("".concat(displayName, " dictionary does not provide a change listener"));
34
- else
35
- console.warn('The dictionary does not provide a change listener');
36
- }
37
- return function () { return remove === null || remove === void 0 ? void 0 : remove(); };
24
+ var createSubscriber = function (fields, isEqual) {
25
+ var snapshot = (__assign({}, dictionary));
26
+ return {
27
+ subscribe: function (onStoreChange) {
28
+ snapshot = (__assign({}, dictionary));
29
+ var remove = dictionary === null || dictionary === void 0 ? void 0 : dictionary.AddListener(function (key, value, dic) {
30
+ var prev = snapshot;
31
+ snapshot = (__assign({}, dictionary));
32
+ if (!fields)
33
+ onStoreChange();
34
+ else {
35
+ for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) {
36
+ var field = fields_1[_i];
37
+ if (isEqual ? !isEqual(prev[field], snapshot[field]) : (prev[field] !== snapshot[field])) {
38
+ onStoreChange();
39
+ break;
40
+ }
41
+ }
42
+ }
43
+ });
44
+ if (!remove) {
45
+ if (displayName)
46
+ console.warn("".concat(displayName, " dictionary does not provide a change listener"));
47
+ else
48
+ console.warn('The dictionary does not provide a change listener');
49
+ }
50
+ return function () { return remove === null || remove === void 0 ? void 0 : remove(); };
51
+ },
52
+ getSnapshot: function () { return snapshot; },
53
+ };
38
54
  };
39
- var getSnapshot = function () { return snapshot; };
55
+ var defaultSubscriber = createSubscriber();
40
56
  var Provider = function GlobalsProvider(_a) {
41
57
  var children = _a.children;
42
- var value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
58
+ var value = useSyncExternalStore(defaultSubscriber.subscribe, defaultSubscriber.getSnapshot, defaultSubscriber.getSnapshot);
43
59
  return createElement(ctx.Provider, { value: value }, children);
44
60
  };
45
- function useSelector(selector, isEqual) {
46
- return useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, selector, isEqual);
47
- }
48
61
  function useDictionaryContext() {
49
62
  var context = useContext(ctx);
50
63
  if (context === undefined) {
@@ -55,9 +68,21 @@ export function createDictionaryWatcher(dictionary, displayName) {
55
68
  }
56
69
  return context;
57
70
  }
58
- return { context: ctx, Provider: Provider, useContext: useDictionaryContext, useSelector: useSelector };
71
+ function useValue(subscribeToAllFields, fieldEqual) {
72
+ if (subscribeToAllFields === void 0) { subscribeToAllFields = false; }
73
+ var fields = useRef([]);
74
+ var subscriber = useMemo(function () { return subscribeToAllFields ? defaultSubscriber : createSubscriber(fields.current, fieldEqual); }, [subscribeToAllFields, fieldEqual]);
75
+ var value = useSyncExternalStore(subscriber.subscribe, subscriber.getSnapshot, subscriber.getSnapshot);
76
+ var proxy = new Proxy(value, {
77
+ get: function (target, p, receiver) {
78
+ fields.current.push(p);
79
+ return value[p];
80
+ },
81
+ });
82
+ return proxy;
83
+ }
84
+ function useSelector(selector, isEqual) {
85
+ return useSyncExternalStoreWithSelector(defaultSubscriber.subscribe, defaultSubscriber.getSnapshot, defaultSubscriber.getSnapshot, selector, isEqual);
86
+ }
87
+ return { context: ctx, Provider: Provider, useValue: useValue, useContext: useDictionaryContext, useSelector: useSelector };
59
88
  }
60
- export var globalsWatcher = createDictionaryWatcher(Globals, 'globalsContext');
61
- export var useGlobals = globalsWatcher.useContext;
62
- export var useGlobalsSelector = globalsWatcher.useSelector;
63
- export var GlobalsProvider = globalsWatcher.Provider;
@@ -0,0 +1,6 @@
1
+ export declare const useGlobals: (subscribeToAllFields?: boolean, isEqual?: (a: any, b: any) => boolean) => DefaultGlobals;
2
+ export declare const useGlobalsContext: () => DefaultGlobals;
3
+ export declare const useGlobalsSelector: <Res>(selector: (store: DefaultGlobals) => Res, isEqual?: (a: Res, b: Res) => boolean) => Res;
4
+ export declare const GlobalsProvider: (props: {
5
+ children?: import("react").ReactNode;
6
+ }) => import("react").FunctionComponentElement<import("react").ProviderProps<DefaultGlobals>>;
@@ -0,0 +1,6 @@
1
+ import { createDictionaryWatcher } from "../dictionary-watcher";
2
+ var globalsWatcher = createDictionaryWatcher(Globals, 'globalsContext');
3
+ export var useGlobals = globalsWatcher.useValue;
4
+ export var useGlobalsContext = globalsWatcher.useContext;
5
+ export var useGlobalsSelector = globalsWatcher.useSelector;
6
+ export var GlobalsProvider = globalsWatcher.Provider;
@@ -0,0 +1,4 @@
1
+ import { ReactUnity } from '../../models/generated';
2
+ type IsEqual<T = any> = (a: T, b: T) => boolean;
3
+ export declare function useWatchable<T>(obj: ReactUnity.Helpers.IWatchable<T>, isEqual?: IsEqual<T>): T;
4
+ export {};
@@ -0,0 +1,26 @@
1
+ import { useMemo } from 'react';
2
+ import { useSyncExternalStore } from 'use-sync-external-store/shim';
3
+ var createSubscriber = function (obj, isEqual) {
4
+ var isWatchable = obj && typeof obj === 'object' && ('Value' in obj);
5
+ var snapshot = isWatchable ? obj.Value : undefined;
6
+ return {
7
+ subscribe: function (onStoreChange) {
8
+ snapshot = isWatchable ? obj.Value : undefined;
9
+ var remove = obj === null || obj === void 0 ? void 0 : obj.AddListener(function (key, value, dic) {
10
+ var prev = snapshot;
11
+ snapshot = isWatchable ? obj.Value : undefined;
12
+ if (isEqual ? !isEqual(prev, snapshot) : (prev !== snapshot)) {
13
+ onStoreChange();
14
+ }
15
+ });
16
+ if (!remove)
17
+ console.warn("The watchable does not provide a change listener");
18
+ return function () { return remove === null || remove === void 0 ? void 0 : remove(); };
19
+ },
20
+ getSnapshot: function () { return snapshot; },
21
+ };
22
+ };
23
+ export function useWatchable(obj, isEqual) {
24
+ var sb = useMemo(function () { return createSubscriber(obj, isEqual); }, [obj, isEqual]);
25
+ return useSyncExternalStore(sb.subscribe, sb.getSnapshot);
26
+ }
@@ -28,6 +28,9 @@ export interface RichTextElements {
28
28
  'nbsp': RichTextVoidTag;
29
29
  'zwsp': RichTextVoidTag;
30
30
  'br': RichTextVoidTag;
31
+ 'shy': RichTextVoidTag;
32
+ 'cr': RichTextVoidTag;
33
+ 'lf': RichTextVoidTag;
31
34
  'align': RichTextValueTag<'right' | 'center' | 'left'>;
32
35
  'alpha': RichTextValueTag<string>;
33
36
  'color': RichTextColorTag;
@@ -52,6 +55,7 @@ export interface RichTextElements {
52
55
  'nobr': RichTextVoidTag;
53
56
  'page': RichTextVoidTag;
54
57
  'pos': RichTextValueTag<number>;
58
+ 'scale': RichTextValueTag<number>;
55
59
  'size': RichTextValueTag<number>;
56
60
  'space': RichTextValueTag<number>;
57
61
  'sprite': RichTextSpriteTag;
@@ -1 +1 @@
1
- export declare const version = "0.14.0";
1
+ export declare const version = "0.14.1";
@@ -1 +1 @@
1
- export var version = '0.14.0';
1
+ export var version = '0.14.1';
@@ -1,8 +1,7 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { GlobalsProvider } from '../helpers/dictionary-watcher';
3
2
  import { ErrorBoundary } from './error-boundary';
4
3
  export function DefaultView(_a) {
5
4
  var children = _a.children, withHelpers = _a.withHelpers;
6
5
  return _jsx(_Fragment, { children: !withHelpers ? children :
7
- _jsx(ErrorBoundary, { children: _jsx(GlobalsProvider, { children: children }) }) });
6
+ _jsx(ErrorBoundary, { children: children }) });
8
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactunity/renderer",
3
- "version": "0.14.0",
3
+ "version": "0.14.1",
4
4
  "description": "React renderer for Unity3D",
5
5
  "types": "dist/index.d.ts",
6
6
  "typings": "dist/index.d.ts",
@@ -63,7 +63,7 @@
63
63
  "devDependencies": {
64
64
  "@reactunity/scripts": "^0.14.0",
65
65
  "@types/react-reconciler": "^0.28.2",
66
- "rimraf": "^4.0.6",
67
- "typescript": "^4.9.4"
66
+ "rimraf": "^4.1.2",
67
+ "typescript": "^4.9.5"
68
68
  }
69
69
  }
@@ -1,2 +0,0 @@
1
- import { ReactUnity } from '../models/generated';
2
- export declare function useWatchable<T>(obj: ReactUnity.Helpers.IWatchable<T>): T;
@@ -1,14 +0,0 @@
1
- import { useCallback, useMemo } from 'react';
2
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
3
- export function useWatchable(obj) {
4
- var isWatchable = useMemo(function () { return obj && typeof obj === 'object' && ('Value' in obj); }, [obj]);
5
- var subscribe = useCallback(function (onStoreChange) {
6
- if (isWatchable && 'AddListener' in obj) {
7
- var unsub_1 = obj.AddListener(function () { return onStoreChange(); });
8
- return function () { return unsub_1 === null || unsub_1 === void 0 ? void 0 : unsub_1(); };
9
- }
10
- return function () { };
11
- }, [obj, isWatchable]);
12
- var getSnapshot = useCallback(function () { return isWatchable ? obj.Value : undefined; }, [obj, isWatchable]);
13
- return useSyncExternalStore(subscribe, getSnapshot);
14
- }