southpaw 0.2.7

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.
Files changed (169) hide show
  1. package/README.md +51 -0
  2. package/api/client.ts +83 -0
  3. package/api/helpers/fs.ts +98 -0
  4. package/api/helpers/stream.ts +6 -0
  5. package/api/index.ts +50 -0
  6. package/api/server.ts +566 -0
  7. package/constants/fonts.ts +1583 -0
  8. package/constants/icons.ts +2124 -0
  9. package/constants/langs.ts +2592 -0
  10. package/constants/ssr-loading.tsx +17 -0
  11. package/contexts/dev.tsx +124 -0
  12. package/hooks/useDebounceCb.ts +21 -0
  13. package/modules/app.tsx +102 -0
  14. package/modules/behaviors.ts +161 -0
  15. package/modules/component.tsx +483 -0
  16. package/modules/config/index.ts +8 -0
  17. package/modules/database.ts +25 -0
  18. package/modules/document.tsx +23 -0
  19. package/modules/error.tsx +16 -0
  20. package/modules/page/autozoom.tsx +92 -0
  21. package/modules/page/browser.tsx +166 -0
  22. package/modules/page/index.tsx +327 -0
  23. package/modules/page/insert-modal.tsx +225 -0
  24. package/modules/page/inspect/index.tsx +237 -0
  25. package/modules/page/inspect/wrapper.tsx +88 -0
  26. package/modules/page/knobs/action.tsx +58 -0
  27. package/modules/page/knobs/color.tsx +78 -0
  28. package/modules/page/knobs/helpers.ts +71 -0
  29. package/modules/page/knobs/icon.tsx +85 -0
  30. package/modules/page/knobs/linker.tsx +188 -0
  31. package/modules/page/knobs/list-json.tsx +149 -0
  32. package/modules/page/knobs/list-text.tsx +129 -0
  33. package/modules/page/knobs/number.tsx +84 -0
  34. package/modules/page/knobs/segmented.tsx +81 -0
  35. package/modules/page/knobs/select.tsx +89 -0
  36. package/modules/page/knobs/size.tsx +106 -0
  37. package/modules/page/knobs/switch.tsx +75 -0
  38. package/modules/page/knobs/text.tsx +93 -0
  39. package/modules/page/left-tabs.tsx +36 -0
  40. package/modules/page/right-tabs.tsx +31 -0
  41. package/modules/page/seo.tsx +69 -0
  42. package/modules/page/tree.tsx +860 -0
  43. package/modules/state.ts +787 -0
  44. package/modules/store.ts +37 -0
  45. package/modules/theme.ts +105 -0
  46. package/modules/translations.ts +15 -0
  47. package/package.json +68 -0
  48. package/scripts/bin.js +607 -0
  49. package/scripts/test.js +145 -0
  50. package/themes/base.tsx +8962 -0
  51. package/themes/index.ts +4 -0
  52. package/themes/m2.ts +39 -0
  53. package/themes/newspaper.ts +39 -0
  54. package/types/api.ts +57 -0
  55. package/types/fiber_node.ts +69 -0
  56. package/types/react_element.ts +14 -0
  57. package/ui/accordion.tsx +70 -0
  58. package/ui/actionicon.tsx +41 -0
  59. package/ui/affix.tsx +14 -0
  60. package/ui/alert.tsx +37 -0
  61. package/ui/anchor.tsx +52 -0
  62. package/ui/arrow.tsx +116 -0
  63. package/ui/aspectratio.tsx +21 -0
  64. package/ui/autocomplete.tsx +34 -0
  65. package/ui/avatar.tsx +36 -0
  66. package/ui/backgroundimage.tsx +16 -0
  67. package/ui/badge.tsx +28 -0
  68. package/ui/blockquote.tsx +15 -0
  69. package/ui/blocks/faq.tsx +145 -0
  70. package/ui/blocks/features.tsx +167 -0
  71. package/ui/blocks/footer.tsx +182 -0
  72. package/ui/blocks/herobanner.tsx +391 -0
  73. package/ui/blocks/index.ts +44 -0
  74. package/ui/blocks/leftnav.tsx +141 -0
  75. package/ui/blocks/pricing.tsx +210 -0
  76. package/ui/blocks/testimonials.tsx +149 -0
  77. package/ui/blocks/uidocs.tsx +1429 -0
  78. package/ui/box.tsx +27 -0
  79. package/ui/breadcrumbs.tsx +33 -0
  80. package/ui/burger.tsx +23 -0
  81. package/ui/button.tsx +85 -0
  82. package/ui/card.tsx +28 -0
  83. package/ui/center.tsx +26 -0
  84. package/ui/checkbox.tsx +35 -0
  85. package/ui/chip.tsx +32 -0
  86. package/ui/closebutton.tsx +15 -0
  87. package/ui/code.tsx +104 -0
  88. package/ui/collapse.tsx +16 -0
  89. package/ui/colorinput.tsx +32 -0
  90. package/ui/colorpicker.tsx +26 -0
  91. package/ui/colorswatch.tsx +17 -0
  92. package/ui/combobox.tsx +80 -0
  93. package/ui/container.tsx +27 -0
  94. package/ui/dialog.tsx +15 -0
  95. package/ui/divider.tsx +58 -0
  96. package/ui/drawer.tsx +28 -0
  97. package/ui/fieldset.tsx +15 -0
  98. package/ui/filebutton.tsx +23 -0
  99. package/ui/fileinput.tsx +36 -0
  100. package/ui/flex.tsx +139 -0
  101. package/ui/focustrap.tsx +15 -0
  102. package/ui/form.tsx +26 -0
  103. package/ui/grid.tsx +69 -0
  104. package/ui/highlight.tsx +15 -0
  105. package/ui/hoverable.tsx +66 -0
  106. package/ui/hovercard.tsx +45 -0
  107. package/ui/icon.tsx +76 -0
  108. package/ui/image.tsx +32 -0
  109. package/ui/index.ts +1355 -0
  110. package/ui/indicator.tsx +42 -0
  111. package/ui/infobox.tsx +200 -0
  112. package/ui/input.tsx +18 -0
  113. package/ui/inviewport.tsx +23 -0
  114. package/ui/jsoninput.tsx +35 -0
  115. package/ui/kbd.tsx +20 -0
  116. package/ui/list.tsx +33 -0
  117. package/ui/loader.tsx +26 -0
  118. package/ui/loadingoverlay.tsx +22 -0
  119. package/ui/mark.tsx +15 -0
  120. package/ui/menu.tsx +89 -0
  121. package/ui/modal.tsx +27 -0
  122. package/ui/multiselect.tsx +42 -0
  123. package/ui/nativeselect.tsx +17 -0
  124. package/ui/navlink.tsx +40 -0
  125. package/ui/notification.tsx +25 -0
  126. package/ui/numberformatter.tsx +15 -0
  127. package/ui/numberinput.tsx +37 -0
  128. package/ui/overlay.tsx +15 -0
  129. package/ui/pagination.tsx +21 -0
  130. package/ui/paper.tsx +33 -0
  131. package/ui/passwordinput.tsx +36 -0
  132. package/ui/pill.tsx +17 -0
  133. package/ui/pillsinput.tsx +37 -0
  134. package/ui/pininput.tsx +30 -0
  135. package/ui/popover.tsx +43 -0
  136. package/ui/portal.tsx +15 -0
  137. package/ui/progress.tsx +49 -0
  138. package/ui/props.ts +91 -0
  139. package/ui/propsDef.ts +38 -0
  140. package/ui/radio.tsx +30 -0
  141. package/ui/rating.tsx +25 -0
  142. package/ui/ringprogress.tsx +17 -0
  143. package/ui/scrollarea.tsx +27 -0
  144. package/ui/segmentedcontrol.tsx +38 -0
  145. package/ui/select.tsx +43 -0
  146. package/ui/simplegrid.tsx +23 -0
  147. package/ui/skeleton.tsx +15 -0
  148. package/ui/slider.tsx +31 -0
  149. package/ui/space.tsx +19 -0
  150. package/ui/spoiler.tsx +23 -0
  151. package/ui/stepper.tsx +63 -0
  152. package/ui/switch.tsx +29 -0
  153. package/ui/table.tsx +93 -0
  154. package/ui/tabs.tsx +80 -0
  155. package/ui/tagsinput.tsx +19 -0
  156. package/ui/terminal.tsx +44 -0
  157. package/ui/text.tsx +168 -0
  158. package/ui/textarea.tsx +36 -0
  159. package/ui/textinput.tsx +34 -0
  160. package/ui/timeline.tsx +30 -0
  161. package/ui/title.tsx +42 -0
  162. package/ui/tooltip.tsx +32 -0
  163. package/ui/transition.tsx +21 -0
  164. package/ui/tree.tsx +42 -0
  165. package/ui/typographystylesprovider.tsx +20 -0
  166. package/ui/unstyledbutton.tsx +18 -0
  167. package/ui/video.tsx +16 -0
  168. package/ui/visuallyhidden.tsx +15 -0
  169. package/utils/index.ts +76 -0
@@ -0,0 +1,787 @@
1
+ /* TODO: refactor typings */
2
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
+ //@ts-nocheck
4
+ const ObservableSlim = (function () {
5
+ var paths = [];
6
+ // An array that stores all of the observables created through the public create() method below.
7
+ var observables: unknown[] = [];
8
+ // An array of all the objects that we have assigned Proxies to
9
+ var targets: unknown[] = [];
10
+
11
+ // An array of arrays containing the Proxies created for each target object. targetsProxy is index-matched with
12
+ // 'targets' -- together, the pair offer a Hash table where the key is not a string nor number, but the actual target object
13
+ var targetsProxy: (string | unknown[])[] = [];
14
+
15
+ // this variable tracks duplicate proxies assigned to the same target.
16
+ // the 'set' handler below will trigger the same change on all other Proxies tracking the same target.
17
+ // however, in order to avoid an infinite loop of Proxies triggering and re-triggering one another, we use dupProxy
18
+ // to track that a given Proxy was modified from the 'set' handler
19
+ var dupProxy: null = null;
20
+
21
+ var _getProperty = function (obj: unknown, path: string) {
22
+ return path.split(".").reduce(function (
23
+ prev: { [x: string]: unknown },
24
+ curr: string | number
25
+ ) {
26
+ return prev ? prev[curr] : undefined;
27
+ }, obj || self);
28
+ };
29
+
30
+ /**
31
+ * Create a new ES6 `Proxy` whose changes we can observe through the `observe()` method.
32
+ * @param {object} target Plain object that we want to observe for changes.
33
+ * @param {boolean|number} domDelay If `true`, then the observed changes to `target` will be batched up on a 10ms delay (via `setTimeout()`).
34
+ * If `false`, then the `observer` function will be immediately invoked after each individual change made to `target`. It is helpful to set
35
+ * `domDelay` to `true` when your `observer` function makes DOM manipulations (fewer DOM redraws means better performance). If a number greater
36
+ * than zero, then it defines the DOM delay in milliseconds.
37
+ * @param {function(ObservableSlimChange[])} [observer] Function that will be invoked when a change is made to the proxy of `target`.
38
+ * When invoked, this function is passed a single argument: an array of `ObservableSlimChange` detailing each change that has been made.
39
+ * @param {object} originalObservable The original observable created by the user, exists for recursion purposes, allows one observable to observe
40
+ * change on unknown nested/child objects.
41
+ * @param {{target: object, property: string}[]} originalPath Array of objects, each object having the properties `target` and `property`:
42
+ * `target` is referring to the observed object itself and `property` referring to the name of that object in the nested structure.
43
+ * The path of the property in relation to the target on the original observable, exists for recursion purposes, allows one observable to observe
44
+ * change on unknown nested/child objects.
45
+ * @returns {ProxyConstructor} Proxy of the target object.
46
+ */
47
+ var _create = function (
48
+ target: unknown[],
49
+ domDelay: number | boolean | undefined,
50
+ originalObservable: null | undefined,
51
+ originalPath: { target: unknown; property: string }[] | undefined
52
+ ) {
53
+ var observable = originalObservable || null;
54
+
55
+ // if the target object is not extensible (e.g. frozen, sealed, preventExtensions), then we cannot create a proxy for it
56
+ // because we cannot set the __targetPosition property on it.
57
+ if (!Object.isExtensible(target)) return target;
58
+
59
+ // record the nested path taken to access this object -- if there was no path then we provide the first empty entry
60
+ var path = originalPath || [{ target: target, property: "" }];
61
+ paths.push(path);
62
+
63
+ // in order to accurately report the "previous value" of the "length" property on an Array
64
+ // we must use a helper property because intercepting a length change is not always possible as of 8/13/2018 in
65
+ // Chrome -- the new `length` value is already set by the time the `set` handler is invoked
66
+ if (target instanceof Array) {
67
+ if (!target.hasOwnProperty("__length"))
68
+ Object.defineProperty(target, "__length", {
69
+ enumerable: false,
70
+ value: target.length,
71
+ writable: true,
72
+ });
73
+ else target.__length = target.length;
74
+ }
75
+
76
+ var changes: { previousValue: unknown }[] = [];
77
+
78
+ /**
79
+ * Returns a string of the nested path (in relation to the top-level observed object) of the property being modified or deleted.
80
+ * @param {object} target Plain object that we want to observe for changes.
81
+ * @param {string} property Property name.
82
+ * @param {boolean} [jsonPointer] Set to `true` if the string path should be formatted as a JSON pointer rather than with the dot notation
83
+ * (`false` as default).
84
+ * @returns {string} Nested path (e.g., `hello.testing.1.bar` or, if JSON pointer, `/hello/testing/1/bar`).
85
+ */
86
+ var _getPath = function (
87
+ target: unknown,
88
+ property: string,
89
+ jsonPointer: boolean | undefined
90
+ ) {
91
+ var fullPath = "";
92
+ var lastTarget = null;
93
+
94
+ // loop over each item in the path and append it to full path
95
+ for (var i = 0; i < path.length; i++) {
96
+ // if the current object was a member of an array, it's possible that the array was at one point
97
+ // mutated and would cause the position of the current object in that array to change. we perform an indexOf
98
+ // lookup here to determine the current position of that object in the array before we add it to fullPath
99
+ if (lastTarget instanceof Array && !isNaN(path[i].property)) {
100
+ path[i].property = lastTarget.indexOf(path[i].target);
101
+ }
102
+
103
+ fullPath = fullPath + "." + path[i].property;
104
+ lastTarget = path[i].target;
105
+ }
106
+
107
+ // add the current property
108
+ fullPath = fullPath + "." + property;
109
+
110
+ // remove the beginning two dots -- ..foo.bar becomes foo.bar (the first item in the nested chain doesn't have a property name)
111
+ fullPath = fullPath.substring(2);
112
+
113
+ if (jsonPointer === true) fullPath = "/" + fullPath.replace(/\./g, "/");
114
+
115
+ return fullPath;
116
+ };
117
+
118
+ var _notifyObservers = function (numChanges: number) {
119
+ // if the observable is paused, then we don't want to execute unknown of the observer functions
120
+ if (observable.paused === true) return;
121
+
122
+ var domDelayIsNumber = typeof domDelay === "number";
123
+
124
+ // execute observer functions on a 10ms setTimeout, this prevents the observer functions from being executed
125
+ // separately on every change -- this is necessary because the observer functions will often trigger UI updates
126
+ if (domDelayIsNumber || domDelay === true) {
127
+ setTimeout(
128
+ function () {
129
+ if (numChanges === changes.length) {
130
+ // we create a copy of changes before passing it to the observer functions because even if the observer function
131
+ // throws an error, we still need to ensure that changes is reset to an empty array so that old changes don't persist
132
+ var changesCopy = changes.slice(0);
133
+ changes = [];
134
+
135
+ // invoke unknown functions that are observing changes
136
+ for (var i = 0; i < observable.observers.length; i++)
137
+ observable.observers[i](changesCopy);
138
+ }
139
+ },
140
+ domDelayIsNumber && domDelay > 0 ? domDelay : 10
141
+ );
142
+ } else {
143
+ // we create a copy of changes before passing it to the observer functions because even if the observer function
144
+ // throws an error, we still need to ensure that changes is reset to an empty array so that old changes don't persist
145
+ var changesCopy = changes.slice(0);
146
+ changes = [];
147
+
148
+ // invoke unknown functions that are observing changes
149
+ for (var i = 0; i < observable.observers.length; i++)
150
+ observable.observers[i](changesCopy);
151
+ }
152
+ };
153
+
154
+ var handler = {
155
+ get: function (
156
+ target: {
157
+ [x: string]: unknown;
158
+ hasOwnProperty: (arg0: unknown) => unknown;
159
+ },
160
+ property: string
161
+ ) {
162
+ // implement a simple check for whether or not the object is a proxy, this helps the .create() method avoid
163
+ // creating Proxies of Proxies.
164
+ if (property === "__getTarget") {
165
+ return target;
166
+ } else if (property === "__isProxy") {
167
+ return true;
168
+ // from the perspective of a given observable on a parent object, return the parent object of the given nested object
169
+ } else if (property === "__getParent") {
170
+ return function (i: number) {
171
+ if (typeof i === "undefined") var i = 1;
172
+ var parentPath = _getPath(target, "__getParent").split(".");
173
+ parentPath.splice(-(i + 1), i + 1);
174
+ return _getProperty(observable.parentProxy, parentPath.join("."));
175
+ };
176
+ // return the full path of the current object relative to the parent observable
177
+ } else if (property === "__getPath") {
178
+ // strip off the 12 characters for ".__getParent"
179
+ var parentPath = _getPath(target, "__getParent");
180
+ return parentPath.slice(0, -12);
181
+ }
182
+
183
+ // for performance improvements, we assign this to a variable so we do not have to lookup the property value again
184
+ var targetProp = target[property];
185
+ if (
186
+ target instanceof Date &&
187
+ targetProp instanceof Function &&
188
+ targetProp !== null
189
+ ) {
190
+ return targetProp.bind(target);
191
+ }
192
+
193
+ // if we are traversing into a new object, then we want to record path to that object and return a new observable.
194
+ // recursively returning a new observable allows us a single Observable.observe() to monitor all changes on
195
+ // the target object and unknown objects nested within.
196
+ if (
197
+ targetProp instanceof Object &&
198
+ targetProp !== null &&
199
+ target.hasOwnProperty(property)
200
+ ) {
201
+ // if we've found a proxy nested on the object, then we want to retrieve the original object behind that proxy
202
+ if (targetProp.__isProxy === true)
203
+ targetProp = targetProp.__getTarget;
204
+
205
+ // if the object accessed by the user (targetProp) already has a __targetPosition AND the object
206
+ // stored at target[targetProp.__targetPosition] is not null, then that means we are already observing this object
207
+ // we might be able to return a proxy that we've already created for the object
208
+ if (
209
+ targetProp.__targetPosition > -1 &&
210
+ targets[targetProp.__targetPosition] !== null
211
+ ) {
212
+ // loop over the proxies that we've created for this object
213
+ var ttp = targetsProxy[targetProp.__targetPosition];
214
+ for (var i = 0, l = ttp.length; i < l; i++) {
215
+ // if we find a proxy that was setup for this particular observable, then return that proxy
216
+ if (observable === ttp[i].observable) {
217
+ return ttp[i].proxy;
218
+ }
219
+ }
220
+ }
221
+
222
+ // if we're arrived here, then that means there is no proxy for the object the user just accessed, so we
223
+ // have to create a new proxy for it
224
+
225
+ // create a shallow copy of the path array -- if we didn't create a shallow copy then all nested objects would share the same path array and the path wouldn't be accurate
226
+ var newPath = path.slice(0);
227
+ newPath.push({ target: targetProp, property: property });
228
+ return _create(targetProp, domDelay, observable, newPath);
229
+ } else {
230
+ return targetProp;
231
+ }
232
+ },
233
+ deleteProperty: function (
234
+ target: { [x: string]: unknown },
235
+ property: string | number
236
+ ) {
237
+ // was this change an original change or was it a change that was re-triggered below
238
+ var originalChange = true;
239
+ if (dupProxy === proxy) {
240
+ originalChange = false;
241
+ dupProxy = null;
242
+ }
243
+
244
+ // in order to report what the previous value was, we must make a copy of it before it is deleted
245
+ var previousValue = Object.assign({}, target);
246
+
247
+ // record the deletion that just took place
248
+ changes.push({
249
+ type: "delete",
250
+ target: target,
251
+ property: property,
252
+ newValue: null,
253
+ previousValue: previousValue[property],
254
+ currentPath: _getPath(target, property),
255
+ jsonPointer: _getPath(target, property, true),
256
+ proxy: proxy,
257
+ });
258
+
259
+ if (originalChange === true) {
260
+ // perform the delete that we've trapped if changes are not paused for this observable
261
+ if (!observable.changesPaused) delete target[property];
262
+
263
+ for (var a = 0, l = targets.length; a < l; a++)
264
+ if (target === targets[a]) break;
265
+
266
+ // loop over each proxy and see if the target for this change has unknown other proxies
267
+ var currentTargetProxy = targetsProxy[a] || [];
268
+
269
+ var b = currentTargetProxy.length;
270
+ while (b--) {
271
+ // if the same target has a different proxy
272
+ if (currentTargetProxy[b].proxy !== proxy) {
273
+ // !!IMPORTANT!! store the proxy as a duplicate proxy (dupProxy) -- this will adjust the behavior above appropriately (that is,
274
+ // prevent a change on dupProxy from re-triggering the same change on other proxies)
275
+ dupProxy = currentTargetProxy[b].proxy;
276
+
277
+ // make the same delete on the different proxy for the same target object. it is important that we make this change *after* we invoke the same change
278
+ // on unknown other proxies so that the previousValue can show up correct for the other proxies
279
+ delete currentTargetProxy[b].proxy[property];
280
+ }
281
+ }
282
+ }
283
+
284
+ _notifyObservers(changes.length);
285
+
286
+ return true;
287
+ },
288
+ set: function (
289
+ target: {
290
+ [x: string]: unknown;
291
+ __length?: unknown;
292
+ __targetPosition?: unknown;
293
+ },
294
+ property: string,
295
+ value: { __isProxy: unknown; __getTarget: unknown },
296
+ receiver: { [x: string]: unknown }
297
+ ) {
298
+ // if the value we're assigning is an object, then we want to ensure
299
+ // that we're assigning the original object, not the proxy, in order to avoid mixing
300
+ // the actual targets and proxies -- creates issues with path logging if we don't do this
301
+ if (value && value.__isProxy) value = value.__getTarget;
302
+
303
+ // was this change an original change or was it a change that was re-triggered below
304
+ var originalChange = true;
305
+ if (dupProxy === proxy) {
306
+ originalChange = false;
307
+ dupProxy = null;
308
+ }
309
+
310
+ // improve performance by saving direct references to the property
311
+ var targetProp = target[property];
312
+
313
+ // Only record this change if:
314
+ // 1. the new value differs from the old one
315
+ // 2. OR if this proxy was not the original proxy to receive the change
316
+ // 3. OR the modified target is an array and the modified property is "length" and our helper property __length indicates that the array length has changed
317
+ //
318
+ // Regarding #3 above: mutations of arrays via .push or .splice actually modify the .length before the set handler is invoked
319
+ // so in order to accurately report the correct previousValue for the .length, we have to use a helper property.
320
+ if (
321
+ targetProp !== value ||
322
+ originalChange === false ||
323
+ (property === "length" &&
324
+ target instanceof Array &&
325
+ target.__length !== value)
326
+ ) {
327
+ var foundObservable = true;
328
+
329
+ var typeOfTargetProp = typeof targetProp;
330
+
331
+ // determine if we're adding something new or modifying some that already existed
332
+ var type = "update";
333
+ if (typeOfTargetProp === "undefined") type = "add";
334
+
335
+ // store the change that just occurred. it is important that we store the change before invoking the other proxies so that the previousValue is correct
336
+ changes.push({
337
+ type: type,
338
+ target: target,
339
+ property: property,
340
+ newValue: value,
341
+ previousValue: receiver[property],
342
+ currentPath: _getPath(target, property),
343
+ jsonPointer: _getPath(target, property, true),
344
+ proxy: proxy,
345
+ });
346
+
347
+ // mutations of arrays via .push or .splice actually modify the .length before the set handler is invoked
348
+ // so in order to accurately report the correct previousValue for the .length, we have to use a helper property.
349
+ if (
350
+ property === "length" &&
351
+ target instanceof Array &&
352
+ target.__length !== value
353
+ ) {
354
+ changes[changes.length - 1].previousValue = target.__length;
355
+ target.__length = value;
356
+ }
357
+
358
+ // !!IMPORTANT!! if this proxy was the first proxy to receive the change, then we need to go check and see
359
+ // if there are other proxies for the same project. if there are, then we will modify those proxies as well so the other
360
+ // observers can be modified of the change that has occurred.
361
+ if (originalChange === true) {
362
+ // because the value actually differs than the previous value
363
+ // we need to store the new value on the original target object,
364
+ // but only as long as changes have not been paused
365
+ if (!observable.changesPaused) target[property] = value;
366
+
367
+ foundObservable = false;
368
+
369
+ var targetPosition = target.__targetPosition;
370
+ var z = targetsProxy[targetPosition].length;
371
+
372
+ // find the parent target for this observable -- if the target for that observable has not been removed
373
+ // from the targets array, then that means the observable is still active and we should notify the observers of this change
374
+ while (z--) {
375
+ if (observable === targetsProxy[targetPosition][z].observable) {
376
+ if (
377
+ targets[
378
+ targetsProxy[targetPosition][z].observable.parentTarget
379
+ .__targetPosition
380
+ ] !== null
381
+ ) {
382
+ foundObservable = true;
383
+ break;
384
+ }
385
+ }
386
+ }
387
+
388
+ // if we didn't find an observable for this proxy, then that means .remove(proxy) was likely invoked
389
+ // so we no longer need to notify unknown observer function about the changes, but we still need to update the
390
+ // value of the underlying original objects see below: target[property] = value;
391
+ if (foundObservable) {
392
+ // loop over each proxy and see if the target for this change has unknown other proxies
393
+ var currentTargetProxy = targetsProxy[targetPosition];
394
+ for (var b = 0, l = currentTargetProxy.length; b < l; b++) {
395
+ // if the same target has a different proxy
396
+ if (currentTargetProxy[b].proxy !== proxy) {
397
+ // !!IMPORTANT!! store the proxy as a duplicate proxy (dupProxy) -- this will adjust the behavior above appropriately (that is,
398
+ // prevent a change on dupProxy from re-triggering the same change on other proxies)
399
+ dupProxy = currentTargetProxy[b].proxy;
400
+
401
+ // invoke the same change on the different proxy for the same target object. it is important that we make this change *after* we invoke the same change
402
+ // on unknown other proxies so that the previousValue can show up correct for the other proxies
403
+ currentTargetProxy[b].proxy[property] = value;
404
+ }
405
+ }
406
+
407
+ // if the property being overwritten is an object, then that means this observable
408
+ // will need to stop monitoring this object and unknown nested objects underneath the overwritten object else they'll become
409
+ // orphaned and grow memory usage. we execute this on a setTimeout so that the clean-up process does not block
410
+ // the UI rendering -- there's no need to execute the clean up immediately
411
+ setTimeout(function () {
412
+ if (typeOfTargetProp === "object" && targetProp !== null) {
413
+ // check if the to-be-overwritten target property still exists on the target object
414
+ // if it does still exist on the object, then we don't want to stop observing it. this resolves
415
+ // an issue where array .sort() triggers objects to be overwritten, but instead of being overwritten
416
+ // and discarded, they are shuffled to a new position in the array
417
+ var keys = Object.keys(target);
418
+ for (var i = 0, l = keys.length; i < l; i++) {
419
+ if (target[keys[i]] === targetProp) return;
420
+ }
421
+
422
+ var stillExists = false;
423
+
424
+ // now we perform the more expensive search recursively through the target object.
425
+ // if we find the targetProp (that was just overwritten) still exists somewhere else
426
+ // further down in the object, then we still need to observe the targetProp on this observable.
427
+ (function iterate(target) {
428
+ var keys = Object.keys(target);
429
+ for (var i = 0, l = keys.length; i < l; i++) {
430
+ var property = keys[i];
431
+ var nestedTarget = target[property];
432
+
433
+ if (
434
+ nestedTarget instanceof Object &&
435
+ nestedTarget !== null
436
+ )
437
+ iterate(nestedTarget);
438
+ if (nestedTarget === targetProp) {
439
+ stillExists = true;
440
+ return;
441
+ }
442
+ }
443
+ })(target);
444
+
445
+ // even though targetProp was overwritten, if it still exists somewhere else on the object,
446
+ // then we don't want to remove the observable for that object (targetProp)
447
+ if (stillExists === true) return;
448
+
449
+ // loop over each property and recursively invoke the `iterate` function for unknown
450
+ // objects nested on targetProp
451
+ (function iterate(obj) {
452
+ var keys = Object.keys(obj);
453
+ for (var i = 0, l = keys.length; i < l; i++) {
454
+ var objProp = obj[keys[i]];
455
+ if (objProp instanceof Object && objProp !== null)
456
+ iterate(objProp);
457
+ }
458
+
459
+ // if there are unknown existing target objects (objects that we're already observing)...
460
+ var c = -1;
461
+ for (var i = 0, l = targets.length; i < l; i++) {
462
+ if (obj === targets[i]) {
463
+ c = i;
464
+ break;
465
+ }
466
+ }
467
+ if (c > -1) {
468
+ // ...then we want to determine if the observables for that object match our current observable
469
+ var currentTargetProxy = targetsProxy[c];
470
+ var d = currentTargetProxy.length;
471
+
472
+ while (d--) {
473
+ // if we do have an observable monitoring the object thats about to be overwritten
474
+ // then we can remove that observable from the target object
475
+ if (observable === currentTargetProxy[d].observable) {
476
+ currentTargetProxy.splice(d, 1);
477
+ break;
478
+ }
479
+ }
480
+
481
+ // if there are no more observables assigned to the target object, then we can remove
482
+ // the target object altogether. this is necessary to prevent growing memory consumption particularly with large data sets
483
+ if (currentTargetProxy.length == 0) {
484
+ // targetsProxy.splice(c,1);
485
+ targets[c] = null;
486
+ }
487
+ }
488
+ })(targetProp);
489
+ }
490
+ }, 10000);
491
+ }
492
+
493
+ // TO DO: the next block of code resolves test case #29, but it results in poor IE11 performance with very large objects.
494
+ // UPDATE: need to re-evaluate IE11 performance due to major performance overhaul from 12/23/2018.
495
+ //
496
+ // if the value we've just set is an object, then we'll need to iterate over it in order to initialize the
497
+ // observers/proxies on all nested children of the object
498
+ /* if (value instanceof Object && value !== null) {
499
+ (function iterate(proxy) {
500
+ var target = proxy.__getTarget;
501
+ var keys = Object.keys(target);
502
+ for (var i = 0, l = keys.length; i < l; i++) {
503
+ var property = keys[i];
504
+ if (target[property] instanceof Object && target[property] !== null) iterate(proxy[property]);
505
+ };
506
+ })(proxy[property]);
507
+ }; */
508
+ }
509
+
510
+ if (foundObservable) {
511
+ // notify the observer functions that the target has been modified
512
+ _notifyObservers(changes.length);
513
+ }
514
+ }
515
+ return true;
516
+ },
517
+ };
518
+
519
+ var __targetPosition = target.__targetPosition;
520
+ if (!(__targetPosition > -1)) {
521
+ Object.defineProperty(target, "__targetPosition", {
522
+ value: targets.length,
523
+ writable: false,
524
+ enumerable: false,
525
+ configurable: false,
526
+ });
527
+ }
528
+
529
+ // create the proxy that we'll use to observe unknown changes
530
+ var proxy = new Proxy(target, handler);
531
+
532
+ // we don't want to create a new observable if this function was invoked recursively
533
+ if (observable === null) {
534
+ observable = {
535
+ parentTarget: target,
536
+ domDelay: domDelay,
537
+ parentProxy: proxy,
538
+ observers: [],
539
+ paused: false,
540
+ path: path,
541
+ changesPaused: false,
542
+ };
543
+ observables.push(observable);
544
+ }
545
+
546
+ // store the proxy we've created so it isn't re-created unnecessarily via get handler
547
+ var proxyItem = { target: target, proxy: proxy, observable: observable };
548
+
549
+ // if we have already created a Proxy for this target object then we add it to the corresponding array
550
+ // on targetsProxy (targets and targetsProxy work together as a Hash table indexed by the actual target object).
551
+ if (__targetPosition > -1) {
552
+ // the targets array is set to null for the position of this particular object, then we know that
553
+ // the observable was removed some point in time for this object -- so we need to set the reference again
554
+ if (targets[__targetPosition] === null) {
555
+ targets[__targetPosition] = target;
556
+ }
557
+
558
+ targetsProxy[__targetPosition].push(proxyItem);
559
+
560
+ // else this is a target object that we had not yet created a Proxy for, so we must add it to targets,
561
+ // and push a new array on to targetsProxy containing the new Proxy
562
+ } else {
563
+ targets.push(target);
564
+ targetsProxy.push([proxyItem]);
565
+ }
566
+
567
+ return proxy;
568
+ };
569
+
570
+ /**
571
+ * @typedef {object} ObservableSlimChange Observed change.
572
+ * @property {"add"|"update"|"delete"} type Change type.
573
+ * @property {string} property Property name.
574
+ * @property {string} currentPath Property path with the dot notation (e.g. `foo.0.bar`).
575
+ * @property {string} jsonPointer Property path with the JSON pointer syntax (e.g. `/foo/0/bar`). See https://datatracker.ietf.org/doc/html/rfc6901.
576
+ * @property {object} target Target object.
577
+ * @property {ProxyConstructor} proxy Proxy of the target object.
578
+ * @property {*} newValue New value of the property.
579
+ * @property {*} [previousValue] Previous value of the property
580
+ */
581
+
582
+ return {
583
+ /**
584
+ * Create a new ES6 `Proxy` whose changes we can observe through the `observe()` method.
585
+ * @param {object} target Plain object that we want to observe for changes.
586
+ * @param {boolean|number} domDelay If `true`, then the observed changes to `target` will be batched up on a 10ms delay (via `setTimeout()`).
587
+ * If `false`, then the `observer` function will be immediately invoked after each individual change made to `target`. It is helpful to set
588
+ * `domDelay` to `true` when your `observer` function makes DOM manipulations (fewer DOM redraws means better performance). If a number greater
589
+ * than zero, then it defines the DOM delay in milliseconds.
590
+ * @param {function(ObservableSlimChange[])} [observer] Function that will be invoked when a change is made to the proxy of `target`.
591
+ * When invoked, this function is passed a single argument: an array of `ObservableSlimChange` detailing each change that has been made.
592
+ * @returns {ProxyConstructor} Proxy of the target object.
593
+ */
594
+ create: function (
595
+ target: { __isProxy: boolean; __getTarget: unknown },
596
+ domDelay: unknown,
597
+ observer: unknown
598
+ ) {
599
+ // test if the target is a Proxy, if it is then we need to retrieve the original object behind the Proxy.
600
+ // we do not allow creating proxies of proxies because -- given the recursive design of ObservableSlim -- it would lead to sharp increases in memory usage
601
+ if (target.__isProxy === true) {
602
+ var target = target.__getTarget;
603
+ //if it is, then we should throw an error. we do not allow creating proxies of proxies
604
+ // because -- given the recursive design of ObservableSlim -- it would lead to sharp increases in memory usage
605
+ //throw new Error("ObservableSlim.create() cannot create a Proxy for a target object that is also a Proxy.");
606
+ }
607
+
608
+ // fire off the _create() method -- it will create a new observable and proxy and return the proxy
609
+ var proxy = _create(target, domDelay);
610
+
611
+ // assign the observer function
612
+ if (typeof observer === "function") this.observe(proxy, observer);
613
+
614
+ // recursively loop over all nested objects on the proxy we've just created
615
+ // this will allow the top observable to observe unknown changes that occur on a nested object
616
+ (function iterate(proxy) {
617
+ var target = proxy.__getTarget;
618
+ if (
619
+ typeof target === "undefined" ||
620
+ target === null ||
621
+ Array.isArray(target)
622
+ )
623
+ return;
624
+ var keys = Object.keys(target);
625
+ for (var i = 0, l = keys.length; i < l; i++) {
626
+ var property = keys[i];
627
+ if (target[property] instanceof Object && target[property] !== null)
628
+ iterate(proxy[property]);
629
+ }
630
+ })(proxy);
631
+
632
+ return proxy;
633
+ },
634
+
635
+ /**
636
+ * Add a new observer function to an existing proxy.
637
+ * @param {ProxyConstructor} proxy An ES6 `Proxy` created by the `create()` method.
638
+ * @param {function(ObservableSlimChange[])} observer Function that will be invoked when a change is made to the proxy of `target`.
639
+ * When invoked, this function is passed a single argument: an array of `ObservableSlimChange` detailing each change that has been made.
640
+ * @returns {void} Does not return unknown value.
641
+ */
642
+ observe: function (proxy: unknown, observer: unknown) {
643
+ // loop over all the observables created by the _create() function
644
+ var i = observables.length;
645
+ while (i--) {
646
+ if (observables[i].parentProxy === proxy) {
647
+ observables[i].observers.push(observer);
648
+ break;
649
+ }
650
+ }
651
+ },
652
+
653
+ /**
654
+ * Prevent unknown observer functions from being invoked when a change occurs to a proxy.
655
+ * @param {ProxyConstructor} proxy An ES6 `Proxy` created by the `create()` method.
656
+ * @returns {void} Does not return unknown value.
657
+ */
658
+ pause: function (proxy: unknown) {
659
+ var i = observables.length;
660
+ var foundMatch = false;
661
+ while (i--) {
662
+ if (observables[i].parentProxy === proxy) {
663
+ observables[i].paused = true;
664
+ foundMatch = true;
665
+ break;
666
+ }
667
+ }
668
+
669
+ if (foundMatch == false)
670
+ throw new Error(
671
+ "ObseravableSlim could not pause observable -- matching proxy not found."
672
+ );
673
+ },
674
+
675
+ /**
676
+ * Resume execution of unknown observer functions when a change is made to a proxy.
677
+ * @param {ProxyConstructor} proxy An ES6 `Proxy` created by the `create()` method.
678
+ * @returns {void} Does not return unknown value.
679
+ */
680
+ resume: function (proxy: unknown) {
681
+ var i = observables.length;
682
+ var foundMatch = false;
683
+ while (i--) {
684
+ if (observables[i].parentProxy === proxy) {
685
+ observables[i].paused = false;
686
+ foundMatch = true;
687
+ break;
688
+ }
689
+ }
690
+
691
+ if (foundMatch == false)
692
+ throw new Error(
693
+ "ObseravableSlim could not resume observable -- matching proxy not found."
694
+ );
695
+ },
696
+
697
+ /**
698
+ * Prevent unknown changes (i.e., `set`, and `deleteProperty`) from being written to the target object.
699
+ * However, the observer functions will still be invoked to let you know what changes **WOULD** have been made.
700
+ * This can be useful if the changes need to be approved by an external source before the changes take effect.
701
+ * @param {ProxyConstructor} proxy An ES6 `Proxy` created by the `create()` method.
702
+ * @returns {void} Does not return unknown value.
703
+ */
704
+ pauseChanges: function (proxy: unknown) {
705
+ var i = observables.length;
706
+ var foundMatch = false;
707
+ while (i--) {
708
+ if (observables[i].parentProxy === proxy) {
709
+ observables[i].changesPaused = true;
710
+ foundMatch = true;
711
+ break;
712
+ }
713
+ }
714
+
715
+ if (foundMatch == false)
716
+ throw new Error(
717
+ "ObseravableSlim could not pause changes on observable -- matching proxy not found."
718
+ );
719
+ },
720
+
721
+ /**
722
+ * Resume the changes that were taking place prior to the call to `pauseChanges()` method.
723
+ * @param {ProxyConstructor} proxy An ES6 `Proxy` created by the `create()` method.
724
+ * @returns {void} Does not return unknown value.
725
+ */
726
+ resumeChanges: function (proxy: unknown) {
727
+ var i = observables.length;
728
+ var foundMatch = false;
729
+ while (i--) {
730
+ if (observables[i].parentProxy === proxy) {
731
+ observables[i].changesPaused = false;
732
+ foundMatch = true;
733
+ break;
734
+ }
735
+ }
736
+
737
+ if (foundMatch == false)
738
+ throw new Error(
739
+ "ObseravableSlim could not resume changes on observable -- matching proxy not found."
740
+ );
741
+ },
742
+
743
+ /**
744
+ * Remove the observable and proxy thereby preventing unknown further callback observers for changes occurring to the target object.
745
+ * @param {ProxyConstructor} proxy An ES6 `Proxy` created by the `create()` method.
746
+ * @returns {void} Does not return unknown value.
747
+ */
748
+ remove: function (proxy: unknown) {
749
+ var matchedObservable = null;
750
+ var foundMatch = false;
751
+
752
+ var c = observables.length;
753
+ while (c--) {
754
+ if (observables[c].parentProxy === proxy) {
755
+ matchedObservable = observables[c];
756
+ foundMatch = true;
757
+ break;
758
+ }
759
+ }
760
+
761
+ var a = targetsProxy.length;
762
+ while (a--) {
763
+ var b = targetsProxy[a].length;
764
+ while (b--) {
765
+ if (targetsProxy[a][b].observable === matchedObservable) {
766
+ targetsProxy[a].splice(b, 1);
767
+
768
+ // if there are no more proxies for this target object
769
+ // then we null out the position for this object on the targets array
770
+ // since we are essentially no longer observing this object.
771
+ // we do not splice it off the targets array, because if we re-observe the same
772
+ // object at a later time, the property __targetPosition cannot be redefined.
773
+ if (targetsProxy[a].length === 0) {
774
+ targets[a] = null;
775
+ }
776
+ }
777
+ }
778
+ }
779
+
780
+ if (foundMatch === true) {
781
+ observables.splice(c, 1);
782
+ }
783
+ },
784
+ };
785
+ })();
786
+
787
+ export default ObservableSlim;