native-document 1.0.184 → 1.0.186

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": "native-document",
3
- "version": "1.0.184",
3
+ "version": "1.0.186",
4
4
  "description": "A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features",
5
5
  "author": "AfroCodeur <https://github.com/afrocodeur>",
6
6
  "license": "MIT",
@@ -18,6 +18,7 @@ export default function NodeCloner($element) {
18
18
  this.$attrs = null;
19
19
  this.$ndMethods = null;
20
20
  this.$content = null;
21
+ this.$uniqueCallbacks= new Map();
21
22
  }
22
23
 
23
24
  NodeCloner.prototype.shouldBeHydrate = function() {
@@ -56,10 +57,28 @@ const buildProperties = (cache, properties, data) => {
56
57
  };
57
58
 
58
59
  const getPropertyValue = (callbackOrProperty, data) => {
60
+ if(callbackOrProperty?.__$Observable) {
61
+ return callbackOrProperty;
62
+ }
59
63
  const value = (typeof callbackOrProperty ==='string') ? data[0][callbackOrProperty] : callbackOrProperty;
60
64
  return (typeof value === 'function') ? value.apply(this, data) : value;
61
65
  };
62
66
 
67
+
68
+ const $nodeClonedCallbackData = new WeakMap();
69
+
70
+ const $nodeClonerCallbackCaller = (callback, event) => {
71
+ const data = $nodeClonedCallbackData.get(event.target);
72
+ callback.apply(event.target, [...data, event]);
73
+ };
74
+
75
+ NodeCloner.prototype.resolveMethods = function() {
76
+ for(const methodName in this.$ndMethods) {
77
+ const callback = this.$ndMethods[methodName];
78
+ const uniqueCallback = $nodeClonerCallbackCaller.bind(null, callback);
79
+ this.$uniqueCallbacks.set(methodName, uniqueCallback);
80
+ }
81
+ };
63
82
  /**
64
83
  * Pre-compiles all registered bindings into a sequence of optimised steps.
65
84
  * Called once before the first clone operation. Subsequent calls are no-ops.
@@ -70,21 +89,24 @@ NodeCloner.prototype.resolve = function() {
70
89
  if(this.$content) {
71
90
  return;
72
91
  }
92
+ this.resolveMethods();
73
93
  const steps = [];
74
94
  if(this.$ndMethods) {
75
95
  const methods = Object.keys(this.$ndMethods);
76
96
  if(methods.length === 1) {
77
97
  const methodName = methods[0];
78
- const callback = this.$ndMethods[methodName];
79
- steps.push((clonedNode, data) => {
80
- clonedNode.nd[methodName](callback.length === 0 ? callback : callback.bind(clonedNode, ...data));
98
+ steps.push((clonedNode) => {
99
+ const uniqueCallback = this.$uniqueCallbacks.get(methodName);
100
+ clonedNode.nd[methodName](uniqueCallback);
81
101
  });
82
102
  } else {
83
- steps.push((clonedNode, data) => {
103
+ steps.push((clonedNode) => {
84
104
  const nd = clonedNode.nd;
85
105
  for(const methodName in this.$ndMethods) {
86
- const callback = this.$ndMethods[methodName];
87
- nd[methodName](callback.length === 0 ? callback : this.$ndMethods[methodName].bind(clonedNode, ...data));
106
+ const uniqueCallback = this.$uniqueCallbacks.get(methodName);
107
+
108
+ // callback.length === 0 ? callback : this.$ndMethods[methodName].bind(clonedNode, ...data)
109
+ nd[methodName](uniqueCallback);
88
110
  }
89
111
  });
90
112
  }
@@ -146,6 +168,7 @@ NodeCloner.prototype.resolve = function() {
146
168
 
147
169
  this.cloneNode = (data) => {
148
170
  const clonedNode = $element.cloneNode(false);
171
+ $nodeClonedCallbackData.set(clonedNode, data);
149
172
  for(let i = 0; i < stepsCount; i++) {
150
173
  steps[i](clonedNode, data);
151
174
  }
@@ -187,11 +210,7 @@ NodeCloner.prototype.attach = function(methodName, callback) {
187
210
  */
188
211
  NodeCloner.prototype.text = function(valueOrProperty) {
189
212
  this.$content = valueOrProperty;
190
- if(typeof valueOrProperty === 'function') {
191
- this.cloneNode = (data) => createTextNode(valueOrProperty.apply(null, data));
192
- return this;
193
- }
194
- this.cloneNode = (data) => createTextNode(data[0][valueOrProperty]);
213
+ this.cloneNode = (data) => createTextNode(getPropertyValue(valueOrProperty, data));
195
214
  return this;
196
215
  };
197
216
 
@@ -96,6 +96,7 @@ export function TemplateCloner($fn) {
96
96
  $node.nodeCloner = new NodeCloner($node);
97
97
  }
98
98
  assignClonerToNode($node);
99
+
99
100
  if(this.$scopeDataBuilder) {
100
101
  this.clone = (data) => {
101
102
  const scopeData = this.scopeData();