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

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,9 +36,6 @@
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`.
42
39
 
43
40
  ### ⚙️ Typescript API Adjustments
44
41
 
@@ -4,8 +4,6 @@
4
4
  *
5
5
  * Copyright © 2025 Extremely Heavy Industries Inc.
6
6
  */
7
- import {wait} from '@xh/hoist/promise';
8
- import {observable} from 'mobx';
9
7
  import {logError, throwIf} from '../utils/js';
10
8
  import {HoistBaseClass, PersistableState, PersistenceProvider, PersistOptions} from './';
11
9
 
@@ -73,38 +71,38 @@ function createPersistDescriptor(
73
71
  );
74
72
  return descriptor;
75
73
  }
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);
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;
80
80
 
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
- }
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;
99
99
  }
100
100
  }
101
- });
102
-
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));
101
+ }
102
+ });
106
103
 
107
- return ret;
108
- };
104
+ hasInitialized = true;
105
+ return ret;
106
+ };
109
107
  return {...descriptor, initializer};
110
108
  }
@@ -5,6 +5,8 @@
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';
8
10
 
9
11
  /**
10
12
  * Decorator to mark a property as observable and also provide a simple MobX action of the
@@ -38,14 +40,40 @@ function createBindable(target, name, descriptor, isRef) {
38
40
  Object.defineProperty(target, setterName, {value});
39
41
  }
40
42
 
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)
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.)
43
70
  const key = '_xhBindableProperties';
44
71
  if (!target.hasOwnProperty(key)) {
45
72
  target[key] = {...target[key]};
46
73
  }
47
- target[key][name] = isRef;
74
+ target[key][name] = descriptor;
48
75
 
49
- // 3) Return original descriptor.
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.)
50
78
  return descriptor;
51
79
  }
package/mobx/overrides.ts CHANGED
@@ -9,9 +9,7 @@ import {
9
9
  AnnotationsMap,
10
10
  CreateObservableOptions,
11
11
  makeObservable as baseMakeObservable,
12
- isObservableProp as baseIsObservableProp,
13
- observable,
14
- runInAction
12
+ isObservableProp as baseIsObservableProp
15
13
  } from 'mobx';
16
14
 
17
15
  /**
@@ -23,21 +21,10 @@ export function makeObservable(
23
21
  options?: CreateObservableOptions
24
22
  ) {
25
23
  // Finish creating 'bindable' properties for this instance.
24
+ // Do here to ensure it's enumerable on *instance*
26
25
  const bindables = target._xhBindableProperties;
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
- });
26
+ forEach(bindables, (descriptor, name) => {
27
+ Object.defineProperty(target, name, descriptor);
41
28
  });
42
29
 
43
30
  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.1746826299049",
3
+ "version": "73.0.0-SNAPSHOT.1746826691467",
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",