@fluffjs/fluff 0.1.17 → 0.2.0

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.
@@ -0,0 +1 @@
1
+ export declare function InputProperty(): PropertyDecorator;
@@ -0,0 +1,4 @@
1
+ export function InputProperty() {
2
+ return (_target, _propertyKey) => {
3
+ };
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluffjs/fluff",
3
- "version": "0.1.17",
3
+ "version": "0.2.0",
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
+ }
@@ -5,15 +5,24 @@ export declare class Property<T> {
5
5
  readonly onChange: Publisher<T>;
6
6
  readonly onInboundChange: Publisher<T>;
7
7
  readonly onOutboundChange: Publisher<T>;
8
+ readonly onMetadataChange: Publisher<T>;
8
9
  private value?;
9
10
  private committed;
10
11
  private _isChanging;
11
12
  private readonly _propName?;
13
+ private _parentProperty?;
14
+ private _parentSubscription?;
12
15
  constructor(options: {
13
16
  initialValue: T;
14
17
  propertyName?: string;
15
18
  } | T);
16
- setValue(val: T, inbound?: boolean, commit?: boolean): void;
19
+ prop(): Property<T> | undefined;
20
+ setValue(val: T | Property<T>, inbound?: boolean, commit?: boolean): void;
21
+ private linkToParent;
22
+ private setValueInternal;
23
+ private pushToParent;
24
+ private clearParentLink;
25
+ reset(): void;
17
26
  triggerChange(direction?: Direction): void;
18
27
  subscribe(direction: Direction, cb: (val: T) => void): Subscription;
19
28
  getValue(): T | null;
package/utils/Property.js CHANGED
@@ -16,10 +16,13 @@ export class Property {
16
16
  onChange = new Publisher();
17
17
  onInboundChange = new Publisher();
18
18
  onOutboundChange = new Publisher();
19
+ onMetadataChange = new Publisher();
19
20
  value;
20
21
  committed = true;
21
22
  _isChanging = false;
22
23
  _propName;
24
+ _parentProperty;
25
+ _parentSubscription;
23
26
  constructor(options) {
24
27
  if (this.isOptionsObject(options)) {
25
28
  if (options.initialValue !== undefined) {
@@ -33,7 +36,14 @@ export class Property {
33
36
  this.value = options;
34
37
  }
35
38
  }
39
+ prop() {
40
+ return this._parentProperty;
41
+ }
36
42
  setValue(val, inbound = false, commit = true) {
43
+ if (val instanceof Property) {
44
+ this.linkToParent(val);
45
+ return;
46
+ }
37
47
  if (this._isChanging) {
38
48
  console.error((this._propName ? this._propName + ': ' : '') + 'Binding loop detected: setValue called while change is in progress');
39
49
  return;
@@ -58,11 +68,68 @@ export class Property {
58
68
  this.onOutboundChange.emit(this.value);
59
69
  }
60
70
  }
71
+ if (!inbound && this._parentProperty) {
72
+ this.pushToParent(val);
73
+ }
74
+ }
75
+ finally {
76
+ this._isChanging = false;
77
+ }
78
+ }
79
+ linkToParent(source) {
80
+ this.clearParentLink();
81
+ let root = source;
82
+ while (root._parentProperty) {
83
+ root = root._parentProperty;
84
+ }
85
+ this._parentProperty = root;
86
+ this._parentSubscription = root.subscribe(Direction.Any, (val) => {
87
+ this.setValueInternal(val, true);
88
+ });
89
+ const currentValue = root.getValue();
90
+ if (currentValue !== null) {
91
+ this.setValueInternal(currentValue, true);
92
+ }
93
+ }
94
+ setValueInternal(val, inbound) {
95
+ if (this._isChanging)
96
+ return;
97
+ const changed = val !== this.value && safeStringify(val) !== safeStringify(this.value);
98
+ if (!changed)
99
+ return;
100
+ this._isChanging = true;
101
+ try {
102
+ this.value = val;
103
+ this.onChange.emit(val);
104
+ if (inbound) {
105
+ this.onInboundChange.emit(val);
106
+ }
61
107
  }
62
108
  finally {
63
109
  this._isChanging = false;
64
110
  }
65
111
  }
112
+ pushToParent(val) {
113
+ if (!this._parentProperty)
114
+ return;
115
+ const sub = this._parentSubscription;
116
+ this._parentSubscription = undefined;
117
+ sub?.unsubscribe();
118
+ this._parentProperty.setValue(val);
119
+ this._parentSubscription = this._parentProperty.subscribe(Direction.Any, (v) => {
120
+ this.setValueInternal(v, true);
121
+ });
122
+ }
123
+ clearParentLink() {
124
+ if (this._parentSubscription) {
125
+ this._parentSubscription.unsubscribe();
126
+ this._parentSubscription = undefined;
127
+ }
128
+ this._parentProperty = undefined;
129
+ }
130
+ reset() {
131
+ this.clearParentLink();
132
+ }
66
133
  triggerChange(direction = Direction.Any) {
67
134
  if (this.value === undefined)
68
135
  return;