native-document 1.0.104 → 1.0.106

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.
@@ -4,6 +4,9 @@ import Validator from "../../utils/validator";
4
4
  import { ElementCreator } from "../../wrappers/ElementCreator";
5
5
  import NativeDocumentError from "../../errors/NativeDocumentError";
6
6
 
7
+
8
+ const CREATE_AND_CACHE_ACTIONS = new Set(['clear', 'push', 'unshift', 'replace']);
9
+
7
10
  /**
8
11
  * Renders items from an ObservableArray with optimized array-specific updates.
9
12
  * Provides index observables and handles array mutations efficiently.
@@ -64,50 +67,67 @@ export function ForEachArray(data, callback, configs = {}) {
64
67
  if(removeChild) {
65
68
  const child = cacheItem.child;
66
69
  child?.remove();
67
- cache.delete(cacheItem.keyId);
70
+ cache.delete(item);
68
71
  }
69
72
  cacheItem.indexObserver?.cleanup();
70
73
  };
71
74
 
72
- const cleanCache = (items) => {
73
- if(configs.shouldKeepItemsInCache) {
74
- return;
75
- }
76
- if(!isIndexRequired) {
77
- cache.clear();
78
- return;
75
+ const createAndCache = (item) => {
76
+ const child = ElementCreator.getChild(callback(item, null));
77
+ if(process.env.NODE_ENV === 'development') {
78
+ if(!child) {
79
+ throw new NativeDocumentError("ForEachArray child can't be null or undefined!");
80
+ }
79
81
  }
80
- for (const [itemAsKey, _] of cache.entries()) {
81
- if(items && items.contains(itemAsKey)) {
82
- continue;
82
+ cache.set(item, { child, indexObserver: null });
83
+ return child;
84
+ };
85
+
86
+ const createWithIndexAndCache = (item, indexKey) => {
87
+ const indexObserver = Observable(indexKey);
88
+ const child = ElementCreator.getChild(callback(item, indexObserver));
89
+ if(process.env.NODE_ENV === 'development') {
90
+ if(!child) {
91
+ throw new NativeDocumentError("ForEachArray child can't be null or undefined!");
83
92
  }
84
- removeCacheItem(itemAsKey, false);
85
93
  }
86
- cache.clear();
87
- }
94
+ cache.set(item, { child, indexObserver });
95
+ return child;
96
+ };
88
97
 
89
- const buildItem = (item, indexKey) => {
98
+ const getOrCreate = (item, indexKey) => {
90
99
  const cacheItem = cache.get(item);
91
100
  if(cacheItem) {
92
101
  cacheItem.indexObserver?.set(indexKey);
93
- const child = cacheItem.child;
94
- if(child) {
95
- return child;
96
- }
97
- cache.delete(item);
102
+ return cacheItem.child;
98
103
  }
104
+ return createAndCache(item, indexKey);
105
+ };
99
106
 
100
- const indexObserver = isIndexRequired ? Observable(indexKey) : null;
101
- let child = ElementCreator.getChild(callback(item, indexObserver));
102
- if(child) {
103
- cache.set(item, {
104
- child,
105
- indexObserver
106
- });
107
- return child;
107
+ let buildItem = createAndCache;
108
+ const selectBuildStrategy = (action = null) => {
109
+ if(CREATE_AND_CACHE_ACTIONS.has(action)) {
110
+ buildItem = isIndexRequired ? createWithIndexAndCache : createAndCache;
111
+ return;
108
112
  }
113
+ buildItem = cache.size ? getOrCreate : (isIndexRequired ? createWithIndexAndCache : createAndCache);
114
+ };
115
+
109
116
 
110
- throw new NativeDocumentError("ForEachArray child can't be null or undefined!");
117
+ const cleanCache = (items) => {
118
+ if(!isIndexRequired) {
119
+ cache.clear();
120
+ return;
121
+ }
122
+ if(configs.shouldKeepItemsInCache) {
123
+ return;
124
+ }
125
+ for (const [itemAsKey, _] of cache.entries()) {
126
+ if(items && items.includes(itemAsKey)) {
127
+ continue;
128
+ }
129
+ removeCacheItem(itemAsKey, false);
130
+ }
111
131
  };
112
132
 
113
133
  const removeByItem = function(item, fragment) {
@@ -125,10 +145,10 @@ export function ForEachArray(data, callback, configs = {}) {
125
145
  return;
126
146
  }
127
147
  child.remove();
128
- }
148
+ };
129
149
 
130
150
  const Actions = {
131
- toFragment(items, startIndexFrom = 0){
151
+ toFragment(items){
132
152
  const fragment = document.createDocumentFragment();
133
153
  for(let i = 0, length = items.length; i < length; i++) {
134
154
  fragment.appendChild(buildItem(items[i], lastNumberOfItems));
@@ -136,7 +156,7 @@ export function ForEachArray(data, callback, configs = {}) {
136
156
  }
137
157
  return fragment;
138
158
  },
139
- add(items, delay = 2) {
159
+ add(items) {
140
160
  element.appendElement(Actions.toFragment(items));
141
161
  },
142
162
  replace(items) {
@@ -251,6 +271,7 @@ export function ForEachArray(data, callback, configs = {}) {
251
271
  clear();
252
272
  return;
253
273
  }
274
+ selectBuildStrategy(operations?.action);
254
275
 
255
276
  if(!operations?.action) {
256
277
  if(lastNumberOfItems === 0) {
@@ -11,6 +11,13 @@ const COMMON_NODE_TYPES = {
11
11
  DOCUMENT_FRAGMENT: 11
12
12
  };
13
13
 
14
+ const VALID_TYPES = {
15
+ [COMMON_NODE_TYPES.ELEMENT]: true,
16
+ [COMMON_NODE_TYPES.TEXT]: true,
17
+ [COMMON_NODE_TYPES.DOCUMENT_FRAGMENT]: true,
18
+ [COMMON_NODE_TYPES.COMMENT]: true
19
+ };
20
+
14
21
  const Validator = {
15
22
  isObservable(value) {
16
23
  return value?.__$isObservable;
@@ -61,12 +68,10 @@ const Validator = {
61
68
  return !(typeof value !== 'object' || value === null || Array.isArray(value) || value.constructor.name !== 'Object')
62
69
  },
63
70
  isElement(value) {
64
- return value && (
65
- value.nodeType === COMMON_NODE_TYPES.ELEMENT ||
66
- value.nodeType === COMMON_NODE_TYPES.TEXT ||
67
- value.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT ||
68
- value.nodeType === COMMON_NODE_TYPES.COMMENT
69
- );
71
+ return value && VALID_TYPES[value.nodeType];
72
+ },
73
+ isDOMNode(value) {
74
+ return VALID_TYPES[value.nodeType];
70
75
  },
71
76
  isFragment(value) {
72
77
  return value?.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT;
@@ -176,6 +176,7 @@ export function TemplateCloner($fn) {
176
176
  ElementCreator.bindTextNode(textNode, callbackOrProperty(...data));
177
177
  }, 'value');
178
178
  };
179
+ this.text = this.value;
179
180
  this.attr = (fn) => {
180
181
  return createBinding(fn, 'attributes');
181
182
  };
@@ -7,11 +7,7 @@ import Validator from "../../utils/validator";
7
7
  import ObservableChecker from "../../data/ObservableChecker";
8
8
 
9
9
  String.prototype.toNdElement = function () {
10
- const formattedChild = this.resolveObservableTemplate ? this.resolveObservableTemplate() : this;
11
- if(Validator.isString(formattedChild)) {
12
- return ElementCreator.createStaticTextNode(null, formattedChild);
13
- }
14
- return ElementCreator.getChild(null, formattedChild);
10
+ return ElementCreator.createStaticTextNode(null, this);
15
11
  };
16
12
 
17
13
  Element.prototype.toNdElement = function () {
@@ -77,13 +77,13 @@ export function Route($path, $component, $options = {}) {
77
77
  *
78
78
  * @param {string} path
79
79
  */
80
- this.match = function(path) {
80
+ this.match = function(path) {
81
81
  path = '/'+trim(path, '/');
82
82
  const match = getPattern().exec(path);
83
83
  if(!match) return false;
84
84
  const params = {};
85
85
 
86
- match.forEach((value, index) => {
86
+ getPattern().exec(path).forEach((value, index) => {
87
87
  if(index < 1) return;
88
88
  const name = $paramsNames[index - 1];
89
89
  params[name] = value;
@@ -22,7 +22,7 @@ export function RouterComponent(router, container) {
22
22
  }
23
23
 
24
24
  let anchor = node;
25
- if(!Validator.isAnchor(node) || Validator.isFragment(node)) {
25
+ if(!Validator.isAnchor(node)) {
26
26
  anchor = Anchor(path);
27
27
  anchor.appendChild(node);
28
28
  }
@@ -20,4 +20,4 @@ interface ServiceStatic {
20
20
  ): MemoizeProxy<(...args: Args) => T>;
21
21
  }
22
22
 
23
- export const Cache: ServiceStatic;
23
+ export const Service: ServiceStatic;