@xh/hoist 73.0.0-SNAPSHOT.1746826954718 → 73.0.0-SNAPSHOT.1746829743606

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/CHANGELOG.md CHANGED
@@ -36,6 +36,9 @@
36
36
  * Fixed drag-and-drop usability issues with the mobile `ColChooser`.
37
37
  * Made `GridModel.defaultGroupSortFn` null-safe and improved type signature.
38
38
  * Disable `dashCanvasAddViewButton` if there are no `menuItems` to show.
39
+ * Improvements to `@bindable` and `@persist` to handle lifecycle-related bugs. Note that previously
40
+ `@bindable` would work even if `makeObservable()` was not called, but this is no longer the case.
41
+ Please ensure that `makeObservable()` is called in your model's constructor when using `@bindable`.
39
42
 
40
43
  ### ⚙️ Typescript API Adjustments
41
44
 
@@ -6,4 +6,4 @@ export declare function makeObservable(target: any, annotations?: AnnotationsMap
6
6
  /**
7
7
  * An enhanced version of the native mobx isObservableProp
8
8
  */
9
- export declare function isObservableProp(target: any, propertyKey: PropertyKey): any;
9
+ export declare function isObservableProp(target: any, propertyKey: PropertyKey): boolean;
@@ -4,6 +4,8 @@
4
4
  *
5
5
  * Copyright © 2025 Extremely Heavy Industries Inc.
6
6
  */
7
+ import {wait} from '@xh/hoist/promise';
8
+ import {observable} from 'mobx';
7
9
  import {logError, throwIf} from '../utils/js';
8
10
  import {HoistBaseClass, PersistableState, PersistenceProvider, PersistOptions} from './';
9
11
 
@@ -71,38 +73,38 @@ function createPersistDescriptor(
71
73
  );
72
74
  return descriptor;
73
75
  }
74
- const codeValue = descriptor.initializer;
75
- let hasInitialized = false,
76
- ret;
77
- const initializer = function () {
78
- // Initializer can be called multiple times when stacking decorators.
79
- if (hasInitialized) return ret;
76
+ const codeValue = descriptor.initializer,
77
+ initializer = function () {
78
+ // codeValue undefined if no initial in-code value provided, otherwise call to get initial value.
79
+ let ret = codeValue?.call(this);
80
80
 
81
- // codeValue undefined if no initial in-code value provided, otherwise call to get initial value.
82
- ret = codeValue?.call(this);
83
-
84
- const persistOptions = {
85
- path: property,
86
- ...PersistenceProvider.mergePersistOptions(this.persistWith, options)
87
- };
88
- PersistenceProvider.create({
89
- persistOptions,
90
- owner: this,
91
- target: {
92
- getPersistableState: () =>
93
- new PersistableState(hasInitialized ? this[property] : ret),
94
- setPersistableState: state => {
95
- if (!hasInitialized) {
96
- ret = state.value;
97
- } else {
98
- this[property] = state.value;
81
+ // Property is not available on the instance until after the next tick.
82
+ const propertyAvailable = observable.box(false),
83
+ persistOptions = {
84
+ path: property,
85
+ ...PersistenceProvider.mergePersistOptions(this.persistWith, options)
86
+ };
87
+ PersistenceProvider.create({
88
+ persistOptions,
89
+ owner: this,
90
+ target: {
91
+ getPersistableState: () =>
92
+ new PersistableState(propertyAvailable.get() ? this[property] : ret),
93
+ setPersistableState: state => {
94
+ if (!propertyAvailable.get()) {
95
+ ret = state.value;
96
+ } else {
97
+ this[property] = state.value;
98
+ }
99
99
  }
100
100
  }
101
- }
102
- });
101
+ });
103
102
 
104
- hasInitialized = true;
105
- return ret;
106
- };
103
+ // Wait for next tick to ensure construction has completed and property has been made
104
+ // observable via makeObservable.
105
+ wait().thenAction(() => propertyAvailable.set(true));
106
+
107
+ return ret;
108
+ };
107
109
  return {...descriptor, initializer};
108
110
  }
@@ -5,8 +5,6 @@
5
5
  * Copyright © 2025 Extremely Heavy Industries Inc.
6
6
  */
7
7
  import {upperFirst} from 'lodash';
8
- import {observable, runInAction} from 'mobx';
9
- import {getOrCreate} from '../utils/js';
10
8
 
11
9
  /**
12
10
  * Decorator to mark a property as observable and also provide a simple MobX action of the
@@ -40,40 +38,14 @@ function createBindable(target, name, descriptor, isRef) {
40
38
  Object.defineProperty(target, setterName, {value});
41
39
  }
42
40
 
43
- // 2) Place a hidden getter on prototype that wraps the backing observable.
44
- const {initializer} = descriptor,
45
- propName = `_${name}_bindable`,
46
- valName = `_${name}_bindable_value`;
47
- Object.defineProperty(target, propName, {
48
- get() {
49
- return getOrCreate(this, valName, () => {
50
- const initVal = initializer?.call(this);
51
- return isRef ? observable.box(initVal, {deep: false}) : observable.box(initVal);
52
- });
53
- }
54
- });
55
-
56
- // 3) Create the descriptor for a getter/setter pair..
57
- descriptor = {
58
- get() {
59
- return this[propName].get();
60
- },
61
- set(v) {
62
- runInAction(() => this[propName].set(v));
63
- },
64
- enumerable: true,
65
- configurable: true
66
- };
67
-
68
- // 4) Record on class, so we can later create on *instance* in makeObservable.
69
- // (Be sure to create cloned list for *this* particular class.)
41
+ // 2) Record on class, so we can later create on *instance* in makeObservable.
42
+ // (Be sure to create cloned list since this will exist on prototype superclasses of this class)
70
43
  const key = '_xhBindableProperties';
71
44
  if (!target.hasOwnProperty(key)) {
72
45
  target[key] = {...target[key]};
73
46
  }
74
- target[key][name] = descriptor;
47
+ target[key][name] = {isRef};
75
48
 
76
- // 5) Return the get/set to be placed on prototype. (If makeObservable() never called, or called
77
- // late, the non-enumerable property will still be available.)
49
+ // 3) Return original descriptor.
78
50
  return descriptor;
79
51
  }
package/mobx/overrides.ts CHANGED
@@ -9,7 +9,9 @@ import {
9
9
  AnnotationsMap,
10
10
  CreateObservableOptions,
11
11
  makeObservable as baseMakeObservable,
12
- isObservableProp as baseIsObservableProp
12
+ isObservableProp as baseIsObservableProp,
13
+ observable,
14
+ runInAction
13
15
  } from 'mobx';
14
16
 
15
17
  /**
@@ -21,10 +23,21 @@ export function makeObservable(
21
23
  options?: CreateObservableOptions
22
24
  ) {
23
25
  // Finish creating 'bindable' properties for this instance.
24
- // Do here to ensure it's enumerable on *instance*
25
26
  const bindables = target._xhBindableProperties;
26
- forEach(bindables, (descriptor, name) => {
27
- Object.defineProperty(target, name, descriptor);
27
+ forEach(bindables, ({isRef}, name) => {
28
+ const propName = `_${name}_bindable`,
29
+ initVal = target[name];
30
+ target[propName] = isRef ? observable.box(initVal, {deep: false}) : observable.box(initVal);
31
+ Object.defineProperty(target, name, {
32
+ get() {
33
+ return this[propName].get();
34
+ },
35
+ set(v) {
36
+ runInAction(() => this[propName].set(v));
37
+ },
38
+ enumerable: true,
39
+ configurable: true
40
+ });
28
41
  });
29
42
 
30
43
  return baseMakeObservable(target, annotations, options);
@@ -33,8 +46,8 @@ export function makeObservable(
33
46
  /**
34
47
  * An enhanced version of the native mobx isObservableProp
35
48
  */
36
- export function isObservableProp(target: any, propertyKey: PropertyKey) {
49
+ export function isObservableProp(target: any, propertyKey: PropertyKey): boolean {
37
50
  return (
38
- baseIsObservableProp(target, propertyKey) || target?._xhBindableProperties?.[propertyKey]
51
+ baseIsObservableProp(target, propertyKey) || !!target?._xhBindableProperties?.[propertyKey]
39
52
  );
40
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xh/hoist",
3
- "version": "73.0.0-SNAPSHOT.1746826954718",
3
+ "version": "73.0.0-SNAPSHOT.1746829743606",
4
4
  "description": "Hoist add-on for building and deploying React Applications.",
5
5
  "repository": "github:xh/hoist-react",
6
6
  "homepage": "https://xh.io",