@neovici/cosmoz-utils 3.27.0 → 3.29.1

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,87 @@
1
+ /* eslint-disable max-statements */
2
+ /**
3
+ * @license
4
+ * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5
+ * This code may only be used under the BSD style license found at
6
+ * http://polymer.github.io/LICENSE.txt
7
+ * The complete set of authors may be found at
8
+ * http://polymer.github.io/AUTHORS.txt
9
+ * The complete set of contributors may be found at
10
+ * http://polymer.github.io/CONTRIBUTORS.txt
11
+ * Code distributed by Google as part of the polymer project is also
12
+ * subject to an additional IP rights grant found at
13
+ * http://polymer.github.io/PATENTS.txt
14
+ */
15
+ import { isPrimitive } from 'lit-html/lib/parts';
16
+ import { directive } from 'lit-html';
17
+ const _state = new WeakMap(),
18
+ // Effectively infinity, but a SMI.
19
+ _infinity = 0x7fffffff;
20
+ /**
21
+ * Renders one of a series of values, including Promises, to a Part.
22
+ *
23
+ * This is a variant of until, which never falls back to lower priority values.
24
+ * Already rendered content is re-used, even when called with new Promises.
25
+ *
26
+ * Values are rendered in priority order, with the first argument having the
27
+ * highest priority and the last argument having the lowest priority. If a
28
+ * value is a Promise, low-priority values will be rendered until it resolves.
29
+ *
30
+ * The priority of values can be used to create placeholder content for async
31
+ * data. For example, a Promise with pending content can be the first,
32
+ * highest-priority, argument, and a non_promise loading indicator template can
33
+ * be used as the second, lower-priority, argument. The loading indicator will
34
+ * render immediately, and the primary content will render when the Promise
35
+ * resolves.
36
+ *
37
+ * Example:
38
+ *
39
+ * const content = fetch('./content.txt').then(r => r.text());
40
+ * html`${until(content, html`<span>Loading...</span>`)}`
41
+ */
42
+ export const lazyUntil = directive((...args) => part => {
43
+ let state = _state.get(part);
44
+ if (state === undefined) {
45
+ state = {
46
+ lastRenderedIndex: _infinity,
47
+ values: []
48
+ };
49
+ _state.set(part, state);
50
+ }
51
+ const previousValues = state.values,
52
+ previousLength = previousValues.length;
53
+ state.values = args;
54
+ for (let i = 0; i < args.length; i++) {
55
+ // If we've rendered a higher-priority value already, stop.
56
+ if (i > state.lastRenderedIndex) {
57
+ break;
58
+ }
59
+
60
+ const value = args[i];
61
+ // Render non-Promise values immediately
62
+ if (isPrimitive(value) || typeof value.then !== 'function') {
63
+ part.setValue(value);
64
+ state.lastRenderedIndex = i;
65
+ // Since a lower-priority value will never overwrite a higher-priority
66
+ // synchronous value, we can stop processing now.
67
+ break;
68
+ }
69
+ // If this is a Promise we've already handled, skip it.
70
+ if (i < previousLength && value === previousValues[i]) {
71
+ continue;
72
+ }
73
+
74
+ Promise.resolve(value).then(resolvedValue => {
75
+ const index = state.values.indexOf(value);
76
+ // If state.values doesn't contain the value, we've re-rendered without
77
+ // the value, so don't render it. Then, only render if the value is
78
+ // higher-priority than what's already been rendered.
79
+ if (index > -1 && index <= state.lastRenderedIndex) {
80
+ state.lastRenderedIndex = index;
81
+ part.setValue(resolvedValue);
82
+ part.commit();
83
+ }
84
+ });
85
+ }
86
+ });
87
+
@@ -87,7 +87,7 @@ export const hauntedPolymer = (outputPath, hook) => base => {
87
87
  // render in the next animation frame to allow the haunted scheduler to finish processing the current state update
88
88
  // otherwise hooks might be updated twice
89
89
  cancelAnimationFrame(outlet.__renderAF);
90
- outlet.__renderAF = requestAnimationFrame(() => render(part, outlet));
90
+ outlet.__renderAF = requestAnimationFrame(() => render(typeof part === 'function' ? part() : part, outlet));
91
91
  }
92
92
  };
93
93
  };
package/lib/object.js CHANGED
@@ -3,7 +3,7 @@ import { identity } from './function';
3
3
  export const prop = key => key && (object => object[key]) || identity,
4
4
  strProp = key => {
5
5
  const p = prop(key);
6
- return o => p(o)?.toString() || '';
6
+ return o => typeof o === 'string' ? o : p(o)?.toString() || '';
7
7
  },
8
8
  isObject = obj => Object.prototype.toString.call(obj) === '[object Object]',
9
9
  merge = (...objs) => objs.reduce((acc, obj) => {
package/lib/promise.js CHANGED
@@ -1,4 +1,14 @@
1
- export const timeout$ = ms => new Promise(res => setTimeout(res, ms)),
1
+ class ManagedPromise extends Promise {
2
+ constructor(callback) {
3
+ const handles = {};
4
+ super((resolve, reject) => Object.assign(handles, { resolve, reject }));
5
+ Object.assign(this, handles);
6
+ callback?.(handles.resolve, handles.reject);
7
+ }
8
+ }
9
+
10
+
11
+ const timeout$ = ms => new Promise(res => setTimeout(res, ms)),
2
12
  event$ = (target, type, predicate = () => true, timeout = 300000) =>
3
13
  new Promise((resolve, reject) => {
4
14
  let handler;
@@ -68,3 +78,11 @@ export const timeout$ = ms => new Promise(res => setTimeout(res, ms)),
68
78
  pending.push({ resolve: res, reject: rej });
69
79
  });
70
80
  };
81
+
82
+ export {
83
+ ManagedPromise,
84
+ timeout$,
85
+ event$,
86
+ limit$,
87
+ debounce$
88
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "3.27.0",
3
+ "version": "3.29.1",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",