@fluffjs/fluff 0.1.17 → 0.1.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluffjs/fluff",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "module": "./index.js",
@@ -191,9 +191,6 @@ export class FluffElement extends FluffBase {
191
191
  this.__bindOutputOnElement(el, outputName, handler);
192
192
  }
193
193
  __setChildProperty(el, propName, value) {
194
- if (value instanceof Property) {
195
- value = value.getValue();
196
- }
197
194
  if (el instanceof HTMLElement && el.hasAttribute('x-fluff-component')) {
198
195
  const tagName = el.tagName.toLowerCase();
199
196
  if (customElements.get(tagName) === undefined) {
@@ -0,0 +1,9 @@
1
+ import { Property } from '../../utils/Property.js';
2
+ import { FluffElement } from '../FluffElementImpl.js';
3
+ interface ChildComponentInstance extends FluffElement {
4
+ theProp: number;
5
+ __theProp: Property<number>;
6
+ }
7
+ type ChildComponentConstructor = new () => ChildComponentInstance;
8
+ export declare function createPropBindChildComponent(): ChildComponentConstructor;
9
+ export {};
@@ -0,0 +1,20 @@
1
+ import { Property } from '../../utils/Property.js';
2
+ import { FluffElement } from '../FluffElementImpl.js';
3
+ export function createPropBindChildComponent() {
4
+ class ChildComponent extends FluffElement {
5
+ __theProp = new Property({ initialValue: 0, propertyName: 'theProp' });
6
+ get theProp() {
7
+ return this.__theProp.getValue() ?? 0;
8
+ }
9
+ set theProp(val) {
10
+ this.__theProp.setValue(val);
11
+ }
12
+ __render() {
13
+ this.__getShadowRoot().innerHTML = '<span>Child value: </span>';
14
+ }
15
+ __setupBindings() {
16
+ super.__setupBindings();
17
+ }
18
+ }
19
+ return ChildComponent;
20
+ }
@@ -0,0 +1,8 @@
1
+ import { Property } from '../../utils/Property.js';
2
+ import { FluffElement } from '../FluffElementImpl.js';
3
+ interface ParentComponentInstance extends FluffElement {
4
+ sourceProperty: Property<number>;
5
+ }
6
+ type ParentComponentConstructor = new () => ParentComponentInstance;
7
+ export declare function createPropBindParentComponent(childTag: string): ParentComponentConstructor;
8
+ export {};
@@ -0,0 +1,19 @@
1
+ import { Property } from '../../utils/Property.js';
2
+ import { FluffElement } from '../FluffElementImpl.js';
3
+ export function createPropBindParentComponent(childTag) {
4
+ class ParentComponent extends FluffElement {
5
+ sourceProperty = new Property({ initialValue: 2, propertyName: 'sourceProperty' });
6
+ __render() {
7
+ this.__getShadowRoot().innerHTML = `
8
+ <${childTag} x-fluff-component data-lid="l0"></${childTag}>
9
+ `;
10
+ }
11
+ __setupBindings() {
12
+ super.__setupBindings();
13
+ }
14
+ }
15
+ ParentComponent.__bindings = {
16
+ l0: [{ n: 'theProp', b: 'property', e: 0, d: [] }]
17
+ };
18
+ return ParentComponent;
19
+ }
@@ -9,11 +9,18 @@ export declare class Property<T> {
9
9
  private committed;
10
10
  private _isChanging;
11
11
  private readonly _propName?;
12
+ private _parentProperty?;
13
+ private _parentSubscription?;
12
14
  constructor(options: {
13
15
  initialValue: T;
14
16
  propertyName?: string;
15
17
  } | T);
16
- setValue(val: T, inbound?: boolean, commit?: boolean): void;
18
+ setValue(val: T | Property<T>, inbound?: boolean, commit?: boolean): void;
19
+ private linkToParent;
20
+ private setValueInternal;
21
+ private pushToParent;
22
+ private clearParentLink;
23
+ reset(): void;
17
24
  triggerChange(direction?: Direction): void;
18
25
  subscribe(direction: Direction, cb: (val: T) => void): Subscription;
19
26
  getValue(): T | null;
package/utils/Property.js CHANGED
@@ -20,6 +20,8 @@ export class Property {
20
20
  committed = true;
21
21
  _isChanging = false;
22
22
  _propName;
23
+ _parentProperty;
24
+ _parentSubscription;
23
25
  constructor(options) {
24
26
  if (this.isOptionsObject(options)) {
25
27
  if (options.initialValue !== undefined) {
@@ -34,6 +36,10 @@ export class Property {
34
36
  }
35
37
  }
36
38
  setValue(val, inbound = false, commit = true) {
39
+ if (val instanceof Property) {
40
+ this.linkToParent(val);
41
+ return;
42
+ }
37
43
  if (this._isChanging) {
38
44
  console.error((this._propName ? this._propName + ': ' : '') + 'Binding loop detected: setValue called while change is in progress');
39
45
  return;
@@ -58,11 +64,68 @@ export class Property {
58
64
  this.onOutboundChange.emit(this.value);
59
65
  }
60
66
  }
67
+ if (!inbound && this._parentProperty) {
68
+ this.pushToParent(val);
69
+ }
61
70
  }
62
71
  finally {
63
72
  this._isChanging = false;
64
73
  }
65
74
  }
75
+ linkToParent(source) {
76
+ this.clearParentLink();
77
+ let root = source;
78
+ while (root._parentProperty) {
79
+ root = root._parentProperty;
80
+ }
81
+ this._parentProperty = root;
82
+ this._parentSubscription = root.subscribe(Direction.Any, (val) => {
83
+ this.setValueInternal(val, true);
84
+ });
85
+ const currentValue = root.getValue();
86
+ if (currentValue !== null) {
87
+ this.setValueInternal(currentValue, true);
88
+ }
89
+ }
90
+ setValueInternal(val, inbound) {
91
+ if (this._isChanging)
92
+ return;
93
+ const changed = val !== this.value && safeStringify(val) !== safeStringify(this.value);
94
+ if (!changed)
95
+ return;
96
+ this._isChanging = true;
97
+ try {
98
+ this.value = val;
99
+ this.onChange.emit(val);
100
+ if (inbound) {
101
+ this.onInboundChange.emit(val);
102
+ }
103
+ }
104
+ finally {
105
+ this._isChanging = false;
106
+ }
107
+ }
108
+ pushToParent(val) {
109
+ if (!this._parentProperty)
110
+ return;
111
+ const sub = this._parentSubscription;
112
+ this._parentSubscription = undefined;
113
+ sub?.unsubscribe();
114
+ this._parentProperty.setValue(val);
115
+ this._parentSubscription = this._parentProperty.subscribe(Direction.Any, (v) => {
116
+ this.setValueInternal(v, true);
117
+ });
118
+ }
119
+ clearParentLink() {
120
+ if (this._parentSubscription) {
121
+ this._parentSubscription.unsubscribe();
122
+ this._parentSubscription = undefined;
123
+ }
124
+ this._parentProperty = undefined;
125
+ }
126
+ reset() {
127
+ this.clearParentLink();
128
+ }
66
129
  triggerChange(direction = Direction.Any) {
67
130
  if (this.value === undefined)
68
131
  return;