native-document 1.0.89 → 1.0.90

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.
@@ -6,6 +6,7 @@ import PluginsManager from "../../core/utils/plugins-manager";
6
6
  import Validator from "../../core/utils/validator";
7
7
  import {ObservableWhen} from "./ObservableWhen";
8
8
  import {deepClone} from "../utils/helpers";
9
+ import {call} from "@babel/traverse/lib/path/context";
9
10
 
10
11
  /**
11
12
  *
@@ -57,7 +58,7 @@ ObservableItem.prototype.intercept = function(callback) {
57
58
  };
58
59
 
59
60
  ObservableItem.prototype.triggerFirstListener = function(operations) {
60
- this.$firstListener.callback(this.$currentValue, this.$previousValue, operations || {}, this.$firstListener.context);
61
+ this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
61
62
  };
62
63
 
63
64
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -67,25 +68,24 @@ ObservableItem.prototype.triggerListeners = function(operations) {
67
68
 
68
69
  operations = operations || DEFAULT_OPERATIONS;
69
70
  for(let i = 0, length = $listeners.length; i < length; i++) {
70
- const listener = $listeners[i];
71
- listener.callback($currentValue, $previousValue, operations, listener.context);
71
+ $listeners[i]($currentValue, $previousValue, operations);
72
72
  }
73
73
  };
74
74
 
75
75
  const handleWatcherCallback = function(callbacks, value) {
76
- if(callbacks.callback) {
77
- callbacks.callback(value, null, null, callbacks.context);
76
+ if(typeof callbacks === "function") {
77
+ callbacks(value);
78
78
  return;
79
79
  }
80
80
  if (callbacks.set) {
81
81
  callbacks.set(value);
82
82
  return;
83
83
  }
84
- callbacks.forEach(callback => {
85
- callback.callback
86
- ? callback.callback(value, null, null, callback.context)
87
- : callback.set(value);
88
- });
84
+ for(let i = 0, length = callbacks.length; i < length; i++) {
85
+ const callback = callbacks[i];
86
+ callback.set ? callback.set(value) : callback(value);
87
+
88
+ }
89
89
  };
90
90
 
91
91
  ObservableItem.prototype.triggerWatchers = function() {
@@ -97,12 +97,12 @@ ObservableItem.prototype.triggerWatchers = function() {
97
97
  const $previousValue = this.$previousValue;
98
98
  const $currentValue = this.$currentValue;
99
99
 
100
- if($watchers.has($currentValue)) {
101
- const $currentValueCallbacks = $watchers.get($currentValue);
100
+ const $currentValueCallbacks = $watchers.get($currentValue);
101
+ const $previousValueCallbacks = $watchers.get($previousValue);
102
+ if($currentValueCallbacks) {
102
103
  handleWatcherCallback($currentValueCallbacks, true);
103
104
  }
104
- if($watchers.has($previousValue)) {
105
- const $previousValueCallbacks = $watchers.get($previousValue);
105
+ if($previousValueCallbacks) {
106
106
  handleWatcherCallback($previousValueCallbacks, false);
107
107
  }
108
108
  };
@@ -114,7 +114,7 @@ ObservableItem.prototype.triggerAll = function(operations) {
114
114
 
115
115
  ObservableItem.prototype.triggerWatchersAndFirstListener = function(operations) {
116
116
  this.triggerWatchers();
117
- this.triggerListeners(operations);
117
+ this.triggerFirstListener(operations);
118
118
  };
119
119
 
120
120
  ObservableItem.prototype.assocTrigger = function() {
@@ -213,11 +213,10 @@ ObservableItem.prototype.cleanup = function() {
213
213
  /**
214
214
  *
215
215
  * @param {Function} callback
216
- * @param {?Object} context
217
216
  * @param {any} target
218
217
  * @returns {(function(): void)}
219
218
  */
220
- ObservableItem.prototype.subscribe = function(callback, context = null, target = null) {
219
+ ObservableItem.prototype.subscribe = function(callback, target = null) {
221
220
  this.$listeners = this.$listeners ?? [];
222
221
  if (this.$isCleanedUp) {
223
222
  DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
@@ -227,15 +226,13 @@ ObservableItem.prototype.subscribe = function(callback, context = null, target =
227
226
  throw new NativeDocumentError('Callback must be a function');
228
227
  }
229
228
 
230
-
231
- const finalCallback = { callback, context };
232
- this.$listeners.push(finalCallback);
229
+ this.$listeners.push(callback);
233
230
  this.assocTrigger();
234
231
  if(process.env.NODE_ENV === 'development') {
235
232
  PluginsManager.emit('ObservableSubscribe', this, target);
236
233
  }
237
234
  return () => {
238
- this.unsubscribe(finalCallback);
235
+ this.unsubscribe(callback);
239
236
  this.assocTrigger();
240
237
  if(process.env.NODE_ENV === 'development') {
241
238
  PluginsManager.emit('ObservableUnsubscribe', this);
@@ -243,24 +240,23 @@ ObservableItem.prototype.subscribe = function(callback, context = null, target =
243
240
  };
244
241
  };
245
242
 
246
- ObservableItem.prototype.on = function(value, callback, context = null) {
243
+ ObservableItem.prototype.on = function(value, callback) {
247
244
  this.$watchers = this.$watchers ?? new Map();
248
245
 
249
246
  let watchValueList = this.$watchers.get(value);
250
- const finalCallback = { callback, context };
251
247
 
252
248
  if(!watchValueList) {
253
- this.$watchers.set(value, finalCallback);
249
+ this.$watchers.set(value, callback);
254
250
  } else if(!Validator.isArray(watchValueList)) {
255
- watchValueList = [watchValueList, finalCallback];
251
+ watchValueList = [watchValueList, callback];
256
252
  this.$watchers.set(value, watchValueList);
257
253
  } else {
258
- watchValueList.push(finalCallback);
254
+ watchValueList.push(callback);
259
255
  }
260
256
 
261
257
  this.assocTrigger();
262
258
  return () => {
263
- const index = watchValueList.indexOf(finalCallback);
259
+ const index = watchValueList.indexOf(callback);
264
260
  watchValueList?.splice(index, 1);
265
261
  if(watchValueList.size === 1) {
266
262
  this.$watchers.set(value, watchValueList[0]);
@@ -315,12 +311,6 @@ ObservableItem.prototype.when = function(value) {
315
311
  return new ObservableWhen(this, value);
316
312
  };
317
313
 
318
- ObservableItem.prototype.toString = function() {
319
- if(!this.$memoryId) {
320
- MemoryManager.register(this);
321
- }
322
- return '{{#ObItem::(' +this.$memoryId+ ')}}';
323
- };
324
314
  ObservableItem.prototype.equals = function(other) {
325
315
  if(Validator.isObservable(other)) {
326
316
  return this.$currentValue === other.$currentValue;
@@ -6,8 +6,8 @@ export const ObservableWhen = function(observer, value) {
6
6
 
7
7
  ObservableWhen.prototype.__$isObservableWhen = true;
8
8
 
9
- ObservableWhen.prototype.subscribe = function(callback, context = null) {
10
- return this.$observer.on(this.$target, callback, context);
9
+ ObservableWhen.prototype.subscribe = function(callback) {
10
+ return this.$observer.on(this.$target, callback);
11
11
  };
12
12
 
13
13
  ObservableWhen.prototype.val = function() {
@@ -1,10 +1,8 @@
1
1
  import Validator from "../utils/validator";
2
+ import NativeDocumentError from "../errors/NativeDocumentError";
2
3
  import {BOOLEAN_ATTRIBUTES} from "./constants.js";
3
4
  import {Observable} from "../data/Observable";
4
5
 
5
- export const handleElementAttributeClass = function(shouldAdd, _, __, context) {
6
- context.element.classes.toggle(context.className, shouldAdd);
7
- };
8
6
  /**
9
7
  *
10
8
  * @param {HTMLElement} element
@@ -12,19 +10,17 @@ export const handleElementAttributeClass = function(shouldAdd, _, __, context) {
12
10
  */
13
11
  export function bindClassAttribute(element, data) {
14
12
  const classNames = Object.keys(data);
15
-
16
13
  for(let i = 0, length = classNames.length; i < length; i++) {
17
14
  const className = classNames[i];
18
15
  const value = data[className];
19
16
  if(value.__$isObservable) {
20
17
  element.classes.toggle(className, value.val());
21
- value.subscribe(handleElementAttributeClass, { element, className });
22
- // value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
18
+ value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
23
19
  continue;
24
20
  }
25
21
  if(value.__$isObservableWhen) {
26
22
  element.classes.toggle(className, value.isMath());
27
- value.subscribe(handleElementAttributeClass, { element, className });
23
+ value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
28
24
  continue;
29
25
  }
30
26
  if(value.$hydrate) {
@@ -33,6 +29,7 @@ export function bindClassAttribute(element, data) {
33
29
  }
34
30
  element.classes.toggle(className, value)
35
31
  }
32
+ data = null;
36
33
  }
37
34
 
38
35
  /**
@@ -41,7 +38,7 @@ export function bindClassAttribute(element, data) {
41
38
  * @param {Object} data
42
39
  */
43
40
  export function bindStyleAttribute(element, data) {
44
- const keys = Object.keys(data)
41
+ const keys = Object.keys(data);
45
42
  for(let i = 0, length = keys.length; i < length; i++) {
46
43
  const styleName = keys[i];
47
44
  const value = data[styleName];
@@ -63,8 +60,7 @@ export function bindStyleAttribute(element, data) {
63
60
  export function bindBooleanAttribute(element, attributeName, value) {
64
61
  const isObservable = value.__$isObservable;
65
62
  const defaultValue = isObservable? value.val() : value;
66
- const isBoolValue = typeof defaultValue === "boolean";
67
- if(isBoolValue) {
63
+ if(Validator.isBoolean(defaultValue)) {
68
64
  element[attributeName] = defaultValue;
69
65
  }
70
66
  else {
@@ -72,13 +68,13 @@ export function bindBooleanAttribute(element, attributeName, value) {
72
68
  }
73
69
  if(isObservable) {
74
70
  if(attributeName === 'checked') {
75
- if(isBoolValue) {
71
+ if(typeof defaultValue === 'boolean') {
76
72
  element.addEventListener('input', () => value.set(element[attributeName]));
77
- value.subscribe((newValue) => element[attributeName] = newValue);
78
- return;
79
73
  }
80
- element.addEventListener('input', () => value.set(element.value));
81
- value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
74
+ else {
75
+ element.addEventListener('input', () => value.set(element.value));
76
+ }
77
+ value.subscribe((newValue) => element[attributeName] = newValue);
82
78
  return;
83
79
  }
84
80
  value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
@@ -93,14 +89,14 @@ export function bindBooleanAttribute(element, attributeName, value) {
93
89
  * @param {Observable} value
94
90
  */
95
91
  export function bindAttributeWithObservable(element, attributeName, value) {
92
+ const applyValue = attributeName === 'value' ? (newValue) => element.value = newValue : (newValue) => element.setAttribute(attributeName, newValue);
93
+ value.subscribe(applyValue);
94
+
96
95
  if(attributeName === 'value') {
97
- value.subscribe((newValue) => element.value = newValue);
98
96
  element.value = value.val();
99
97
  element.addEventListener('input', () => value.set(element.value));
100
98
  return;
101
99
  }
102
-
103
- value.subscribe((newValue) => element.setAttribute(attributeName, newValue));
104
100
  element.setAttribute(attributeName, value.val());
105
101
  }
106
102
 
@@ -111,13 +107,11 @@ export function bindAttributeWithObservable(element, attributeName, value) {
111
107
  */
112
108
  export default function AttributesWrapper(element, attributes) {
113
109
 
114
- if(process.env.NODE_ENV === 'development') {
115
- Validator.validateAttributes(attributes);
116
- }
117
- const attributeNames = Object.keys(attributes);
110
+ Validator.validateAttributes(attributes);
111
+ const attributesKeys = Object.keys(attributes);
118
112
 
119
- for(let i = 0, length = attributeNames.length; i < length; i++) {
120
- const originalAttributeName = attributeNames[i];
113
+ for(let i = 0, length = attributesKeys.length; i < length; i++) {
114
+ const originalAttributeName = attributesKeys[i];
121
115
  const attributeName = originalAttributeName.toLowerCase();
122
116
  let value = attributes[originalAttributeName];
123
117
  if(value == null) {
@@ -142,10 +136,6 @@ export default function AttributesWrapper(element, attributes) {
142
136
  continue;
143
137
  }
144
138
 
145
- if(attributeName === 'value') {
146
- element.value = value;
147
- continue;
148
- }
149
139
  element.setAttribute(attributeName, value);
150
140
  }
151
141
  return element;
@@ -1,20 +1,16 @@
1
1
  import ObservableItem from "../../../core/data/ObservableItem";
2
+ import {toggleElementClass} from "../AttributesWrapper";
2
3
  import {ObservableWhen} from "../../data/ObservableWhen";
3
4
  import TemplateBinding from "../../../core/wrappers/TemplateBinding";
4
5
 
5
-
6
- export const handleElementAttributeClass = function(shouldAdd, _, __, context) {
7
- context.element.classes.toggle(context.className, shouldAdd);
8
- };
9
-
10
6
  ObservableItem.prototype.bindNdClass = function(element, className) {
11
7
  element.classes.toggle(className, this.val());
12
- this.subscribe(handleElementAttributeClass, { element, className });
8
+ this.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
13
9
  };
14
10
 
15
11
  ObservableWhen.prototype.bindNdClass = function(element, className) {
16
12
  element.classes.toggle(className, this.isMath());
17
- this.subscribe(handleElementAttributeClass, { element, className });
13
+ this.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
18
14
  };
19
15
 
20
16
  TemplateBinding.prototype.bindNdClass = function(element, className) {