@xh/hoist 73.0.0-SNAPSHOT.1746798376611 → 73.0.0-SNAPSHOT.1746826299049

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,7 +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
- * Fixed `@persist` decorator.
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`.
40
42
 
41
43
  ### ⚙️ Typescript API Adjustments
42
44
 
@@ -74,42 +74,37 @@ function createPersistDescriptor(
74
74
  return descriptor;
75
75
  }
76
76
  const codeValue = descriptor.initializer,
77
- // True once the property is available on the instance (after other decorators have run).
78
- propertyAvailable = observable.box(false);
79
- let hasInitialized = false,
80
- ret;
81
- const initializer = function () {
82
- // Initializer can be called multiple times when stacking decorators.
83
- if (hasInitialized) return ret;
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);
84
80
 
85
- // codeValue undefined if no initial in-code value provided, otherwise call to get initial value.
86
- ret = codeValue?.call(this);
87
-
88
- const persistOptions = {
89
- path: property,
90
- ...PersistenceProvider.mergePersistOptions(this.persistWith, options)
91
- };
92
- PersistenceProvider.create({
93
- persistOptions,
94
- owner: this,
95
- target: {
96
- getPersistableState: () =>
97
- new PersistableState(propertyAvailable.get() ? this[property] : ret),
98
- setPersistableState: state => {
99
- if (!hasInitialized) {
100
- ret = state.value;
101
- } else {
102
- 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
+ }
103
99
  }
104
100
  }
105
- }
106
- });
101
+ });
107
102
 
108
- // Wait for next tick to ensure property is available on the instance before observing.
109
- wait().then(() => propertyAvailable.set(true));
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));
110
106
 
111
- hasInitialized = true;
112
- return ret;
113
- };
107
+ return ret;
108
+ };
114
109
  return {...descriptor, initializer};
115
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xh/hoist",
3
- "version": "73.0.0-SNAPSHOT.1746798376611",
3
+ "version": "73.0.0-SNAPSHOT.1746826299049",
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",