grainjs 1.0.1 → 1.1.0

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 (146) hide show
  1. package/README.md +23 -26
  2. package/dist/cjs/index.js +28 -17
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/lib/PriorityQueue.d.ts +2 -2
  5. package/dist/cjs/lib/PriorityQueue.js +1 -0
  6. package/dist/cjs/lib/PriorityQueue.js.map +1 -1
  7. package/dist/cjs/lib/_computed_queue.js +4 -3
  8. package/dist/cjs/lib/_computed_queue.js.map +1 -1
  9. package/dist/cjs/lib/binding.d.ts +11 -4
  10. package/dist/cjs/lib/binding.js +6 -5
  11. package/dist/cjs/lib/binding.js.map +1 -1
  12. package/dist/cjs/lib/browserGlobals.d.ts +4 -1
  13. package/dist/cjs/lib/browserGlobals.js +2 -0
  14. package/dist/cjs/lib/browserGlobals.js.map +1 -1
  15. package/dist/cjs/lib/computed.d.ts +49 -28
  16. package/dist/cjs/lib/computed.js +38 -51
  17. package/dist/cjs/lib/computed.js.map +1 -1
  18. package/dist/cjs/lib/dispose.d.ts +109 -96
  19. package/dist/cjs/lib/dispose.js +106 -79
  20. package/dist/cjs/lib/dispose.js.map +1 -1
  21. package/dist/cjs/lib/dom.d.ts +40 -18
  22. package/dist/cjs/lib/dom.js +63 -29
  23. package/dist/cjs/lib/dom.js.map +1 -1
  24. package/dist/cjs/lib/domComponent.d.ts +56 -51
  25. package/dist/cjs/lib/domComponent.js +46 -44
  26. package/dist/cjs/lib/domComponent.js.map +1 -1
  27. package/dist/cjs/lib/domComputed.d.ts +50 -20
  28. package/dist/cjs/lib/domComputed.js +37 -7
  29. package/dist/cjs/lib/domComputed.js.map +1 -1
  30. package/dist/cjs/lib/domDispose.d.ts +27 -12
  31. package/dist/cjs/lib/domDispose.js +27 -11
  32. package/dist/cjs/lib/domDispose.js.map +1 -1
  33. package/dist/cjs/lib/domForEach.d.ts +5 -4
  34. package/dist/cjs/lib/domForEach.js +41 -41
  35. package/dist/cjs/lib/domForEach.js.map +1 -1
  36. package/dist/cjs/lib/domImpl.d.ts +33 -10
  37. package/dist/cjs/lib/domImpl.js +29 -9
  38. package/dist/cjs/lib/domImpl.js.map +1 -1
  39. package/dist/cjs/lib/domMethods.d.ts +93 -47
  40. package/dist/cjs/lib/domMethods.js +91 -47
  41. package/dist/cjs/lib/domMethods.js.map +1 -1
  42. package/dist/cjs/lib/domevent.d.ts +87 -62
  43. package/dist/cjs/lib/domevent.js +85 -59
  44. package/dist/cjs/lib/domevent.js.map +1 -1
  45. package/dist/cjs/lib/emit.d.ts +62 -32
  46. package/dist/cjs/lib/emit.js +68 -53
  47. package/dist/cjs/lib/emit.js.map +1 -1
  48. package/dist/cjs/lib/kowrap.d.ts +6 -3
  49. package/dist/cjs/lib/kowrap.js +7 -3
  50. package/dist/cjs/lib/kowrap.js.map +1 -1
  51. package/dist/cjs/lib/obsArray.d.ts +91 -53
  52. package/dist/cjs/lib/obsArray.js +87 -54
  53. package/dist/cjs/lib/obsArray.js.map +1 -1
  54. package/dist/cjs/lib/observable.d.ts +25 -15
  55. package/dist/cjs/lib/observable.js +31 -19
  56. package/dist/cjs/lib/observable.js.map +1 -1
  57. package/dist/cjs/lib/pureComputed.d.ts +12 -15
  58. package/dist/cjs/lib/pureComputed.js +16 -18
  59. package/dist/cjs/lib/pureComputed.js.map +1 -1
  60. package/dist/cjs/lib/styled.d.ts +78 -61
  61. package/dist/cjs/lib/styled.js +27 -79
  62. package/dist/cjs/lib/styled.js.map +1 -1
  63. package/dist/cjs/lib/subscribe.d.ts +41 -37
  64. package/dist/cjs/lib/subscribe.js +31 -39
  65. package/dist/cjs/lib/subscribe.js.map +1 -1
  66. package/dist/cjs/lib/util.js +2 -0
  67. package/dist/cjs/lib/util.js.map +1 -1
  68. package/dist/cjs/lib/widgets/input.d.ts +3 -1
  69. package/dist/cjs/lib/widgets/input.js +7 -4
  70. package/dist/cjs/lib/widgets/input.js.map +1 -1
  71. package/dist/cjs/lib/widgets/select.d.ts +4 -2
  72. package/dist/cjs/lib/widgets/select.js +8 -5
  73. package/dist/cjs/lib/widgets/select.js.map +1 -1
  74. package/dist/esm/lib/_computed_queue.js +3 -3
  75. package/dist/esm/lib/_computed_queue.js.map +1 -1
  76. package/dist/esm/lib/binding.js +2 -2
  77. package/dist/esm/lib/binding.js.map +1 -1
  78. package/dist/esm/lib/browserGlobals.js +1 -0
  79. package/dist/esm/lib/browserGlobals.js.map +1 -1
  80. package/dist/esm/lib/computed.js +36 -50
  81. package/dist/esm/lib/computed.js.map +1 -1
  82. package/dist/esm/lib/dispose.js +104 -78
  83. package/dist/esm/lib/dispose.js.map +1 -1
  84. package/dist/esm/lib/dom.js +40 -18
  85. package/dist/esm/lib/dom.js.map +1 -1
  86. package/dist/esm/lib/domComponent.js +45 -44
  87. package/dist/esm/lib/domComponent.js.map +1 -1
  88. package/dist/esm/lib/domComputed.js +32 -5
  89. package/dist/esm/lib/domComputed.js.map +1 -1
  90. package/dist/esm/lib/domDispose.js +26 -11
  91. package/dist/esm/lib/domDispose.js.map +1 -1
  92. package/dist/esm/lib/domForEach.js +40 -41
  93. package/dist/esm/lib/domForEach.js.map +1 -1
  94. package/dist/esm/lib/domImpl.js +26 -7
  95. package/dist/esm/lib/domImpl.js.map +1 -1
  96. package/dist/esm/lib/domMethods.js +77 -35
  97. package/dist/esm/lib/domMethods.js.map +1 -1
  98. package/dist/esm/lib/domevent.js +84 -59
  99. package/dist/esm/lib/domevent.js.map +1 -1
  100. package/dist/esm/lib/emit.js +67 -53
  101. package/dist/esm/lib/emit.js.map +1 -1
  102. package/dist/esm/lib/kowrap.js +5 -2
  103. package/dist/esm/lib/kowrap.js.map +1 -1
  104. package/dist/esm/lib/obsArray.js +82 -50
  105. package/dist/esm/lib/obsArray.js.map +1 -1
  106. package/dist/esm/lib/observable.js +26 -15
  107. package/dist/esm/lib/observable.js.map +1 -1
  108. package/dist/esm/lib/pureComputed.js +15 -18
  109. package/dist/esm/lib/pureComputed.js.map +1 -1
  110. package/dist/esm/lib/styled.js +24 -77
  111. package/dist/esm/lib/styled.js.map +1 -1
  112. package/dist/esm/lib/subscribe.js +27 -36
  113. package/dist/esm/lib/subscribe.js.map +1 -1
  114. package/dist/esm/lib/util.js +1 -0
  115. package/dist/esm/lib/util.js.map +1 -1
  116. package/dist/esm/lib/widgets/input.js +3 -1
  117. package/dist/esm/lib/widgets/input.js.map +1 -1
  118. package/dist/esm/lib/widgets/select.js +3 -1
  119. package/dist/esm/lib/widgets/select.js.map +1 -1
  120. package/dist/grain-full.debug.js +2138 -3052
  121. package/dist/grain-full.debug.js.map +7 -0
  122. package/dist/grain-full.min.js +6 -2
  123. package/dist/grain-full.min.js.map +7 -1
  124. package/lib/binding.ts +9 -2
  125. package/lib/browserGlobals.ts +3 -1
  126. package/lib/computed.ts +56 -56
  127. package/lib/dispose.ts +110 -85
  128. package/lib/dom.ts +41 -20
  129. package/lib/domComponent.ts +68 -70
  130. package/lib/domComputed.ts +66 -21
  131. package/lib/domDispose.ts +28 -11
  132. package/lib/domForEach.ts +13 -12
  133. package/lib/domImpl.ts +30 -7
  134. package/lib/domMethods.ts +101 -46
  135. package/lib/domevent.ts +86 -61
  136. package/lib/emit.ts +64 -50
  137. package/lib/kowrap.ts +5 -2
  138. package/lib/obsArray.ts +89 -54
  139. package/lib/observable.ts +26 -15
  140. package/lib/pureComputed.ts +16 -22
  141. package/lib/styled.ts +85 -71
  142. package/lib/subscribe.ts +41 -45
  143. package/lib/util.ts +1 -0
  144. package/lib/widgets/input.ts +3 -1
  145. package/lib/widgets/select.ts +3 -1
  146. package/package.json +48 -38
@@ -1,34 +1,28 @@
1
1
  "use strict";
2
- /**
3
- * dom.js provides a way to build a DOM tree easily.
4
- *
5
- * E.g.
6
- * import {dom} from 'grainjs';
7
- * dom('a#link.c1.c2', {'href': url}, 'Hello ', dom('span', 'world'));
8
- * creates Node <a id="link" class="c1 c2" href={{url}}Hello <span>world</span></a>.
9
- *
10
- * dom.frag(dom('span', 'Hello'), ['blah', dom('div', 'world')])
11
- * creates document fragment with <span>Hello</span>blah<div>world</div>.
12
- *
13
- * DOM can also be created and modified inline during creation:
14
- * dom('a#id.c1',
15
- * dom.cls('c2'), dom.attr('href', url),
16
- * dom.text('Hello '), dom('span', dom.text('world')))
17
- * creates Node <a id="link" class="c1 c2" href={{url}}Hello <span>world</span></a>,
18
- * identical to the first example above.
19
- */
20
- function __export(m) {
21
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
22
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
23
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.dom = void 0;
24
18
  // We keep various dom-related functions organized in private modules, but they are exposed here.
25
- __export(require("./domImpl"));
26
- __export(require("./domComponent"));
27
- __export(require("./domComputed"));
28
- __export(require("./domDispose"));
29
- __export(require("./domForEach"));
30
- __export(require("./domMethods"));
31
- __export(require("./domevent"));
19
+ __exportStar(require("./domImpl"), exports);
20
+ __exportStar(require("./domComponent"), exports);
21
+ __exportStar(require("./domComputed"), exports);
22
+ __exportStar(require("./domDispose"), exports);
23
+ __exportStar(require("./domForEach"), exports);
24
+ __exportStar(require("./domMethods"), exports);
25
+ __exportStar(require("./domevent"), exports);
32
26
  const _domComponent = require("./domComponent");
33
27
  const _domComputed = require("./domComputed");
34
28
  const _domDispose = require("./domDispose");
@@ -37,10 +31,48 @@ const _domImpl = require("./domImpl");
37
31
  const _domMethods = require("./domMethods");
38
32
  const domevent = require("./domevent");
39
33
  const domImpl_1 = require("./domImpl");
34
+ /**
35
+ * `dom()` provides a way to build a DOM tree easily.
36
+ *
37
+ * The first argument is a string consisting of a lowercase tag name (e.g. `"div"`), with optional
38
+ * `"#foo"` suffix to add the ID `'foo'`, and zero or more `".bar"` suffixes to add a CSS class
39
+ * `'bar'`.
40
+ *
41
+ * The rest of the arguments are optional and may be any number, of these types:
42
+ *
43
+ * @param Nodes - become children of the created element
44
+ * @param strings - become text node children
45
+ * @param objects - Literal objects to set string attributes on the element.
46
+ * E.g. `{title: "foo"}`.
47
+ * @param null - The values `null` and `undefined` values are ignored completely.
48
+ * @param Arrays - flattened with each item processed recursively
49
+ * @param functions - called with the element being built as the argument, for a chance to modify
50
+ * the element as it's being created. Return values are processed recursively.
51
+ * @param functions - "dom methods" are a expressions such as `dom.attr('href', url)` or
52
+ * `dom.hide(obs)`, which are special cases of the "functions" category.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * import {dom} from 'grainjs';
57
+ * dom('a', {href: url, className: 'myclass'}, 'Hello ', dom('strong', 'world'));
58
+ * ```
59
+ * creates HTML element `<a href={{url}} class="myclass">Hello <strong>world</strong></a>`.
60
+ *
61
+ * @example
62
+ * Here's an example equivalent to the one above, using dom methods `dom.cls`, `dom.attr`,
63
+ * `dom.text`. In reality, these methods are useful with observable values rather than constant
64
+ * strings.
65
+ * ```ts
66
+ * dom('a', dom.attr('href', url), dom.cls('myclass'),
67
+ * dom.text('Hello '), dom('strong', dom.text('world')));
68
+ * ```
69
+ *
70
+ * @see [DOM & Observables](/basics).
71
+ */
40
72
  // We just want to re-export _domImpl.dom, but to allow adding methods to it in a typesafe way,
41
73
  // TypeScript wants us to declare a real function in the same file.
42
74
  function dom(tagString, ...args) {
43
- return domImpl_1.dom(tagString, ...args);
75
+ return (0, domImpl_1.dom)(tagString, ...args);
44
76
  }
45
77
  exports.dom = dom;
46
78
  // Additionally export all methods as properties of dom() function.
@@ -79,7 +111,9 @@ exports.dom = dom;
79
111
  dom.getData = _domMethods.getData;
80
112
  dom.replaceContent = _domComputed.replaceContent;
81
113
  dom.domComputed = _domComputed.domComputed;
114
+ dom.domComputedOwned = _domComputed.domComputedOwned;
82
115
  dom.maybe = _domComputed.maybe;
116
+ dom.maybeOwned = _domComputed.maybeOwned;
83
117
  dom.forEach = _domForEach.forEach;
84
118
  dom.create = _domComponent.create;
85
119
  dom.onElem = domevent.onElem;
@@ -1 +1 @@
1
- {"version":3,"file":"dom.js","sourceRoot":"","sources":["../../../lib/dom.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;;;AAEH,iGAAiG;AACjG,+BAA0B;AAC1B,oCAA+B;AAC/B,mCAA8B;AAC9B,kCAA6B;AAC7B,kCAA6B;AAC7B,kCAA6B;AAC7B,gCAA2B;AAE3B,gDAAgD;AAChD,8CAA8C;AAC9C,4CAA4C;AAC5C,4CAA4C;AAC5C,sCAAsC;AACtC,4CAA4C;AAE5C,uCAAuC;AAEvC,uCAAkE;AAElE,+FAA+F;AAC/F,mEAAmE;AACnE,SAAgB,GAAG,CAAsB,SAAc,EAAE,GAAG,IAA4B;IACtF,OAAO,aAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;AAClC,CAAC;AAFD,kBAEC;AAED,mEAAmE;AACnE,WAAiB,GAAG;IACL,OAAG,GAAe,QAAQ,CAAC,GAAG,CAAC;IAC/B,QAAI,GAAc,QAAQ,CAAC,IAAI,CAAC;IAChC,UAAM,GAAY,QAAQ,CAAC,MAAM,CAAC;IAClC,QAAI,GAAc,QAAQ,CAAC,IAAI,CAAC;IAChC,WAAO,GAAW,QAAQ,CAAC,OAAO,CAAC;IAEnC,cAAU,GAAQ,WAAW,CAAC,UAAU,CAAC;IACzC,iBAAa,GAAK,WAAW,CAAC,aAAa,CAAC;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O,YAAY,CAAC,WAAW,CAAC;IAC3C,oBAAgB,GAAG,YAAY,CAAC,gBAAgB,CAAC;IACjD,SAAK,GAAa,YAAY,CAAC,KAAK,CAAC;IACrC,cAAU,GAAQ,YAAY,CAAC,UAAU,CAAC;IAE1C,WAAO,GAAW,WAAW,CAAC,OAAO,CAAC;IAEtC,UAAM,GAAY,aAAa,CAAC,MAAM,CAAC;IAEvC,UAAM,GAAY,QAAQ,CAAC,MAAM,CAAC;IAClC,MAAE,GAAgB,QAAQ,CAAC,EAAE,CAAC;IAC9B,eAAW,GAAO,QAAQ,CAAC,WAAW,CAAC;IACvC,WAAO,GAAW,QAAQ,CAAC,OAAO,CAAC;IACnC,aAAS,GAAS,QAAQ,CAAC,SAAS,CAAC;IACrC,cAAU,GAAQ,QAAQ,CAAC,UAAU,CAAC;IACtC,aAAS,GAAS,QAAQ,CAAC,SAAS,CAAC;AACpD,CAAC,EApDgB,GAAG,GAAH,WAAG,KAAH,WAAG,QAoDnB"}
@@ -1,74 +1,79 @@
1
+ import { MultiHolder } from './dispose';
2
+ import { DomContents } from './domComputed';
3
+ export interface IDomComponent {
4
+ buildDom(): DomContents;
5
+ }
6
+ export type DomComponentReturn = DomContents | IDomComponent;
7
+ export type IDomCreateFunc<Args extends any[]> = (owner: MultiHolder, ...args: Args) => DomComponentReturn;
8
+ export interface IDomCreateClass<Args extends any[]> {
9
+ create: IDomCreateFunc<Args>;
10
+ new (...args: Args): DomComponentReturn;
11
+ }
12
+ export type IDomCreator<Args extends any[]> = IDomCreateFunc<Args> | IDomCreateClass<Args>;
13
+ export type DomCreatorArgs<T> = T extends (owner: MultiHolder, ...args: infer P) => any ? P : (T extends new (...args: infer P) => any ? P : never);
1
14
  /**
2
- * UI components that can be inserted into dom().
3
- *
4
- * Components are created and inserted using dom.create():
15
+ * UI components that can be inserted into `dom()`.
5
16
  *
6
- * dom('div',
7
- * dom.create(MyWidget, ...myArgs), // Calls MyWidget.create(owner, ...myArgs)
8
- * dom.create(createMyWidget, ...myArgs), // Calls createMyWidget(owner, ...myArgs)
9
- * )
17
+ * Components are created and inserted using `dom.create()`:
18
+ * ```ts
19
+ * dom('div',
20
+ * dom.create(MyWidget, ...myArgs), // Calls MyWidget.create(owner, ...myArgs)
21
+ * dom.create(createMyWidget, ...myArgs), // Calls createMyWidget(owner, ...myArgs)
22
+ * )
23
+ * ```
10
24
  *
11
- * The first argument may be a function, which is called directly, or a class with a .create()
25
+ * The first argument may be a function, which is called directly, or a class with a `.create()`
12
26
  * static method, in which case that's what gets called.
13
27
  *
14
28
  * In both cases, the call gets a first argument of `owner` followed by the rest of the arguments
15
- * to dom.create(). The `owner` is a MultiHolder that will own this component. This works
29
+ * to `dom.create()`. The `owner` is a `MultiHolder` that will own this component. This works
16
30
  * naturally with any class that derives from Disposable, since it then has a suitable static
17
- * create() method.
31
+ * `create()` method.
18
32
  *
19
33
  * Function-based components may use owner to easily handle disposal. For example:
20
- *
21
- * dom.create(createMyWidget)
22
- * function createMyWidget(owner) {
23
- * const foo = Foo.create(owner);
24
- * return dom('div', foo.getTitle());
25
- * }
26
- *
27
- * The `owner` argument is the main benefit of dom.create(). Logically, the owner is the DOM where
34
+ * ```ts
35
+ * dom.create(createMyWidget)
36
+ * function createMyWidget(owner) {
37
+ * const foo = Foo.create(owner);
38
+ * return dom('div', foo.getTitle());
39
+ * }
40
+ * ```
41
+ *
42
+ * The `owner` argument is the main benefit of `dom.create()`. Logically, the owner is the DOM where
28
43
  * the component is attached. When the parent DOM element is disposed, so is the component.
29
44
  *
30
- * [Explanation] To understand why the syntax is such, consider a potential alternative such as:
45
+ * :::info Explanation
46
+ *
47
+ * To understand why the syntax is such, consider a potential alternative such as:
48
+ * ```ts
49
+ * dom('div', _insert_(new Comp1()), _insert_(new Comp2(...args)))
50
+ * ```
31
51
  *
32
- * dom('div', _insert_(new Comp1()), _insert_(new Comp2(...args)))
52
+ * In both cases, the constructor for Comp1 runs before the constructor for Comp2. What happens
53
+ * when Comp2's constructor throws an exception? In the second case, nothing yet owns the
54
+ * created Comp1 component, and it will never get cleaned up. With `dom.create()`, the DOM
55
+ * gets ownership of Comp1 early enough and will dispose it.
33
56
  *
34
- * In both cases, the constructor for Comp1 runs before the constructor for Comp2. What happens
35
- * when Comp2's constructor throws an exception? In the second case, nothing yet owns the
36
- * created Comp1 component, and it will never get cleaned up. With dom.create(), the DOM
37
- * gets ownership of Comp1 early enough and will dispose it.
57
+ * :::
38
58
  *
39
59
  * A function component may return DOM directly. A class component returns the class instance,
40
- * which must have a .buildDom() method which will be called right after the constructor to get
60
+ * which must have a `.buildDom()` method which will be called right after the constructor to get
41
61
  * the DOM. Note that buildDom is only called once.
42
62
  *
43
- * A function component may also return an object with .buildDom(). So these are equivalent:
44
- *
45
- * dom.create(MyWidget)
46
- * dom.create((owner) => MyWidget.create(owner))
63
+ * A function component may also return an object with `.buildDom()`. So these are equivalent:
64
+ * ```ts
65
+ * dom.create(MyWidget)
66
+ * dom.create((owner) => MyWidget.create(owner))
67
+ * ```
47
68
  *
48
69
  * Note that ownership should be handled using the `owner` argument. Don't do this:
70
+ * ```ts
71
+ * // NON-EXAMPLE: Nothing will dispose the created object:
72
+ * // dom.create(() => new MyWidget());
73
+ * ```
49
74
  *
50
- * // NON-EXAMPLE: Nothing will dispose the created object:
51
- * // dom.create(() => new MyWidget());
52
- *
53
- * The returned DOM may includes Nodes, strings, and domComputed() values, as well as arrays of
54
- * any of these. In other words, any DomArg goes except DomMethods. All the DOM returned will be
75
+ * The returned DOM may includes Nodes, strings, and `domComputed()` values, as well as arrays of
76
+ * any of these. In other words, any `DomArg` goes except `DomMethods`. All the DOM returned will be
55
77
  * disposed when the containing element is disposed, followed by the `owner` itself.
56
78
  */
57
- import { MultiHolder } from './dispose';
58
- import { DomComputed } from './domComputed';
59
- export declare type DomContents = Node | string | DomComputed | void | null | undefined | IDomContentsArray;
60
- export interface IDomContentsArray extends Array<DomContents> {
61
- }
62
- export interface IDomComponent {
63
- buildDom(): DomContents;
64
- }
65
- export declare type DomComponentReturn = DomContents | IDomComponent;
66
- export declare type IDomCreateFunc<Args extends any[]> = (owner: MultiHolder, ...args: Args) => DomComponentReturn;
67
- export interface IDomCreateClass<Args extends any[]> {
68
- create: IDomCreateFunc<Args>;
69
- new (...args: Args): DomComponentReturn;
70
- }
71
- export declare type IDomCreator<Args extends any[]> = IDomCreateFunc<Args> | IDomCreateClass<Args>;
72
- declare type DomCreatorArgs<T> = T extends (owner: MultiHolder, ...args: infer P) => any ? P : (T extends new (...args: infer P) => any ? P : never);
73
79
  export declare function create<Fn extends IDomCreator<any[]>>(fn: Fn, ...args: DomCreatorArgs<Fn>): DomContents;
74
- export {};
@@ -1,78 +1,80 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = void 0;
4
+ const domComputed_1 = require("./domComputed");
3
5
  /**
4
- * UI components that can be inserted into dom().
5
- *
6
- * Components are created and inserted using dom.create():
6
+ * UI components that can be inserted into `dom()`.
7
7
  *
8
- * dom('div',
9
- * dom.create(MyWidget, ...myArgs), // Calls MyWidget.create(owner, ...myArgs)
10
- * dom.create(createMyWidget, ...myArgs), // Calls createMyWidget(owner, ...myArgs)
11
- * )
8
+ * Components are created and inserted using `dom.create()`:
9
+ * ```ts
10
+ * dom('div',
11
+ * dom.create(MyWidget, ...myArgs), // Calls MyWidget.create(owner, ...myArgs)
12
+ * dom.create(createMyWidget, ...myArgs), // Calls createMyWidget(owner, ...myArgs)
13
+ * )
14
+ * ```
12
15
  *
13
- * The first argument may be a function, which is called directly, or a class with a .create()
16
+ * The first argument may be a function, which is called directly, or a class with a `.create()`
14
17
  * static method, in which case that's what gets called.
15
18
  *
16
19
  * In both cases, the call gets a first argument of `owner` followed by the rest of the arguments
17
- * to dom.create(). The `owner` is a MultiHolder that will own this component. This works
20
+ * to `dom.create()`. The `owner` is a `MultiHolder` that will own this component. This works
18
21
  * naturally with any class that derives from Disposable, since it then has a suitable static
19
- * create() method.
22
+ * `create()` method.
20
23
  *
21
24
  * Function-based components may use owner to easily handle disposal. For example:
22
- *
23
- * dom.create(createMyWidget)
24
- * function createMyWidget(owner) {
25
- * const foo = Foo.create(owner);
26
- * return dom('div', foo.getTitle());
27
- * }
28
- *
29
- * The `owner` argument is the main benefit of dom.create(). Logically, the owner is the DOM where
25
+ * ```ts
26
+ * dom.create(createMyWidget)
27
+ * function createMyWidget(owner) {
28
+ * const foo = Foo.create(owner);
29
+ * return dom('div', foo.getTitle());
30
+ * }
31
+ * ```
32
+ *
33
+ * The `owner` argument is the main benefit of `dom.create()`. Logically, the owner is the DOM where
30
34
  * the component is attached. When the parent DOM element is disposed, so is the component.
31
35
  *
32
- * [Explanation] To understand why the syntax is such, consider a potential alternative such as:
36
+ * :::info Explanation
33
37
  *
34
- * dom('div', _insert_(new Comp1()), _insert_(new Comp2(...args)))
38
+ * To understand why the syntax is such, consider a potential alternative such as:
39
+ * ```ts
40
+ * dom('div', _insert_(new Comp1()), _insert_(new Comp2(...args)))
41
+ * ```
35
42
  *
36
- * In both cases, the constructor for Comp1 runs before the constructor for Comp2. What happens
37
- * when Comp2's constructor throws an exception? In the second case, nothing yet owns the
38
- * created Comp1 component, and it will never get cleaned up. With dom.create(), the DOM
39
- * gets ownership of Comp1 early enough and will dispose it.
43
+ * In both cases, the constructor for Comp1 runs before the constructor for Comp2. What happens
44
+ * when Comp2's constructor throws an exception? In the second case, nothing yet owns the
45
+ * created Comp1 component, and it will never get cleaned up. With `dom.create()`, the DOM
46
+ * gets ownership of Comp1 early enough and will dispose it.
47
+ *
48
+ * :::
40
49
  *
41
50
  * A function component may return DOM directly. A class component returns the class instance,
42
- * which must have a .buildDom() method which will be called right after the constructor to get
51
+ * which must have a `.buildDom()` method which will be called right after the constructor to get
43
52
  * the DOM. Note that buildDom is only called once.
44
53
  *
45
- * A function component may also return an object with .buildDom(). So these are equivalent:
46
- *
47
- * dom.create(MyWidget)
48
- * dom.create((owner) => MyWidget.create(owner))
54
+ * A function component may also return an object with `.buildDom()`. So these are equivalent:
55
+ * ```ts
56
+ * dom.create(MyWidget)
57
+ * dom.create((owner) => MyWidget.create(owner))
58
+ * ```
49
59
  *
50
60
  * Note that ownership should be handled using the `owner` argument. Don't do this:
61
+ * ```ts
62
+ * // NON-EXAMPLE: Nothing will dispose the created object:
63
+ * // dom.create(() => new MyWidget());
64
+ * ```
51
65
  *
52
- * // NON-EXAMPLE: Nothing will dispose the created object:
53
- * // dom.create(() => new MyWidget());
54
- *
55
- * The returned DOM may includes Nodes, strings, and domComputed() values, as well as arrays of
56
- * any of these. In other words, any DomArg goes except DomMethods. All the DOM returned will be
66
+ * The returned DOM may includes Nodes, strings, and `domComputed()` values, as well as arrays of
67
+ * any of these. In other words, any `DomArg` goes except `DomMethods`. All the DOM returned will be
57
68
  * disposed when the containing element is disposed, followed by the `owner` itself.
58
69
  */
59
- const dispose_1 = require("./dispose");
60
- const domComputed_1 = require("./domComputed");
61
- const domDispose_1 = require("./domDispose");
62
70
  function create(fn, ...args) {
63
- const [markerPre, markerPost, func] = domComputed_1.domComputed(null, () => {
64
- // Note that the callback to domComputed() is not called until the markers have been attached
65
- // to the parent element. We attach the MultiHolder's disposal to markerPost the way
66
- // domComputed() normally attaches its own bindings.
67
- const owner = dispose_1.MultiHolder.create(null);
68
- domDispose_1.autoDisposeElem(markerPost, owner);
71
+ return (0, domComputed_1.domComputedOwned)(null, (owner) => {
69
72
  const value = ('create' in fn) ?
70
73
  fn.create(owner, ...args) :
71
74
  fn(owner, ...args);
72
75
  return (value && typeof value === 'object' && 'buildDom' in value) ?
73
76
  value.buildDom() : value;
74
77
  });
75
- return [markerPre, markerPost, func];
76
78
  }
77
79
  exports.create = create;
78
80
  //# sourceMappingURL=domComponent.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"domComponent.js","sourceRoot":"","sources":["../../../lib/domComponent.ts"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;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AE,GAAG,IAAI,CAAC,CAAC,CAAC;YACtD,EAA4B,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;QAChD,OAAO,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,UAAU,IAAI,KAAK,CAAC,CAAC,CAAC;YAClE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IAC7B,CAAC,CAAC,CAAC;AACL,CAAC;AARD,wBAQC"}
@@ -1,12 +1,16 @@
1
1
  import { BindableValue } from './binding';
2
+ import { MultiHolder } from './dispose';
2
3
  import { DomArg, DomMethod } from './domImpl';
3
- export declare type DomComputed = [Node, Node, DomMethod];
4
+ export type DomComputed = [Node, Node, DomMethod];
5
+ export type DomContents = Node | string | DomComputed | void | null | undefined | IDomContentsArray;
6
+ export interface IDomContentsArray extends Array<DomContents> {
7
+ }
4
8
  /**
5
9
  * Replaces the content between nodeBefore and nodeAfter, which should be two siblings within the
6
10
  * same parent node. New content may be anything allowed as an argument to dom(), including null
7
11
  * to insert nothing. Runs disposers, if any, on all removed content.
8
12
  */
9
- export declare function replaceContent(nodeBefore: Node, nodeAfter: Node, content: DomArg): void;
13
+ export declare function replaceContent(nodeBefore: Node, nodeAfter: Node, content: DomContents): void;
10
14
  /**
11
15
  * Appends dynamic DOM content to an element. The value may be an observable or function (from
12
16
  * which a computed is created), whose value will be passed to `contentFunc` which should return
@@ -17,33 +21,48 @@ export declare function replaceContent(nodeBefore: Node, nodeAfter: Node, conten
17
21
  * changes, previous content is disposed and removed, and new content added in its place.
18
22
  *
19
23
  * The following are roughly equivalent:
20
- * (A) domComputed(nlinesObs, nlines => nlines > 1 ? dom('textarea') : dom('input'));
21
- * (B) domComputed(use => use(nlinesObs) > 1 ? dom('textarea') : dom('input'));
22
- * (C) domComputed(use => use(nlinesObs) > 1, isTall => isTall ? dom('textarea') : dom('input'));
24
+ * ```ts
25
+ * // (A)
26
+ * domComputed(nlinesObs, nlines => nlines > 1 ? dom('textarea') : dom('input'))
27
+ * // (B)
28
+ * domComputed(use => use(nlinesObs) > 1 ? dom('textarea') : dom('input'))
29
+ * // (C)
30
+ * domComputed(use => use(nlinesObs) > 1, isTall => isTall ? dom('textarea') : dom('input'))
31
+ * ```
23
32
  *
24
33
  * Here, (C) is best. Both (A) and (B) would rebuild DOM for any change in nlinesObs, but (C)
25
34
  * encapsulates meaningful changes in the observable, and only recreates DOM when necessary.
26
35
  *
27
36
  * Syntax (B), without the second argument, may be useful in cases of DOM depending on several
28
37
  * observables, e.g.
38
+ * ```ts
39
+ * domComputed(use => use(readonlyObs) ? dom('div') :
40
+ * (use(nlinesObs) > 1 ? dom('textarea') : dom('input')))
41
+ * ```
29
42
  *
30
- * domComputed(use => use(readonlyObs) ? dom('div') :
31
- * (use(nlinesObs) > 1 ? dom('textarea') : dom('input')));
32
- *
33
- * If the argument is not an observable, domComputed() may but should not be used. The following
43
+ * If the argument is not an observable, `domComputed()` may but should not be used. The following
34
44
  * are equivalent:
35
- *
36
- * dom(..., domComputed(listValue, list => `Have ${list.length} items`), ...)
37
- * dom(..., `Have ${listValue.length} items`, ...)
45
+ * ```ts
46
+ * dom(..., domComputed(listValue, list => `Have ${list.length} items`), ...);
47
+ * dom(..., `Have ${listValue.length} items`, ...);
48
+ * ```
38
49
  *
39
50
  * In this case, the latter is preferred as the clearly simpler one.
40
51
  *
41
- * @param valueObs: Observable or function for a computed.
42
- * @param contentFunc: Function called with the result of valueObs as the input, and
52
+ * @param valueObs - Observable or function for a computed.
53
+ * @param contentFunc - Function called with the result of valueObs as the input, and
43
54
  * returning DOM as output. If omitted, defaults to the identity function.
44
55
  */
45
56
  export declare function domComputed(valueObs: BindableValue<Exclude<DomArg, DomMethod>>): DomComputed;
46
- export declare function domComputed<T>(valueObs: BindableValue<T>, contentFunc: (val: T) => DomArg): DomComputed;
57
+ export declare function domComputed<T>(valueObs: BindableValue<T>, contentFunc: (val: T) => DomContents): DomComputed;
58
+ /**
59
+ * Like domComputed(), but the callback gets an additional first argument, owner, which may be
60
+ * used to take ownership of objects created by the callback. These will be disposed before each
61
+ * new call to the callback, and when the containing DOM is disposed.
62
+ *
63
+ * `domComputedOwned(valueObs, (owner, value) => Editor.create(owner, value).renderSomething())`
64
+ */
65
+ export declare function domComputedOwned<T>(valueObs: BindableValue<T>, contentFunc: (owner: MultiHolder, val: T) => DomContents): DomComputed;
47
66
  /**
48
67
  * Conditionally appends DOM to an element. The value may be an observable or function (from which
49
68
  * a computed is created), whose value -- if truthy -- will be passed to `contentFunc` which
@@ -52,18 +71,29 @@ export declare function domComputed<T>(valueObs: BindableValue<T>, contentFunc:
52
71
  * Note that if the observable changes between different truthy values, contentFunc gets called
53
72
  * for each value, and previous content gets destroyed. To consider all truthy values the same,
54
73
  * use an observable that returns a proper boolean, e.g.
55
- *
74
+ * ```ts
56
75
  * dom.maybe(use => Boolean(use(fooObs)), () => dom(...));
76
+ * ```
57
77
  *
58
78
  * As with domComputed(), dom.maybe() may but should not be used when the argument is not an
59
79
  * observable or function. The following are equivalent:
60
- *
80
+ * ```ts
61
81
  * dom(..., dom.maybe(myValue, () => dom(...)));
62
82
  * dom(..., myValue ? dom(...) : null);
83
+ * ```
63
84
  *
64
85
  * The latter is preferred for being simpler.
65
86
  *
66
- * @param boolValueObs: Observable or function for a computed.
67
- * @param contentFunc: Called with the result of boolValueObs when it is truthy. Should return DOM.
87
+ * @param boolValueObs - Observable or function for a computed.
88
+ * @param contentFunc - Called with the result of boolValueObs when it is truthy. Should return DOM.
89
+ */
90
+ export declare function maybe<T>(boolValueObs: BindableValue<T>, contentFunc: (val: NonNullable<T>) => DomContents): DomComputed;
91
+ /**
92
+ * Like maybe(), but the callback gets an additional first argument, owner, which may be used to
93
+ * take ownership of objects created by the callback. These will be disposed before each new call
94
+ * to the callback, and when the condition becomes false or the containing DOM gets disposed.
95
+ * ```ts
96
+ * maybeOwned(showEditor, (owner) => Editor.create(owner).renderSomething())
97
+ * ```
68
98
  */
69
- export declare function maybe<T>(boolValueObs: BindableValue<T>, contentFunc: (val: NonNullable<T>) => DomArg): DomComputed;
99
+ export declare function maybeOwned<T>(boolValueObs: BindableValue<T>, contentFunc: (owner: MultiHolder, val: NonNullable<T>) => DomContents): DomComputed;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.maybeOwned = exports.maybe = exports.domComputedOwned = exports.domComputed = exports.replaceContent = void 0;
3
4
  const binding_1 = require("./binding");
5
+ const dispose_1 = require("./dispose");
4
6
  const domDispose_1 = require("./domDispose");
5
7
  const domImpl_1 = require("./domImpl");
6
8
  // Use the browser globals in a way that allows replacing them with mocks in tests.
@@ -16,11 +18,11 @@ function replaceContent(nodeBefore, nodeAfter, content) {
16
18
  let next;
17
19
  for (let n = nodeBefore.nextSibling; n && n !== nodeAfter; n = next) {
18
20
  next = n.nextSibling;
19
- domDispose_1.domDispose(n);
21
+ (0, domDispose_1.domDispose)(n);
20
22
  elem.removeChild(n);
21
23
  }
22
24
  if (content) {
23
- elem.insertBefore(content instanceof browserGlobals_1.G.Node ? content : domImpl_1.frag(content), nodeAfter);
25
+ elem.insertBefore(content instanceof browserGlobals_1.G.Node ? content : (0, domImpl_1.frag)(content), nodeAfter);
24
26
  }
25
27
  }
26
28
  }
@@ -31,10 +33,24 @@ function domComputed(valueObs, contentFunc = identity) {
31
33
  // Function is added after markerPre and markerPost, so that it runs once they have already been
32
34
  // attached to elem (the parent element).
33
35
  return [markerPre, markerPost, (elem) => {
34
- binding_1.subscribeElem(markerPost, valueObs, (value) => replaceContent(markerPre, markerPost, contentFunc(value)));
36
+ (0, binding_1.subscribeElem)(markerPost, valueObs, (value) => replaceContent(markerPre, markerPost, contentFunc(value)));
35
37
  }];
36
38
  }
37
39
  exports.domComputed = domComputed;
40
+ /**
41
+ * Like domComputed(), but the callback gets an additional first argument, owner, which may be
42
+ * used to take ownership of objects created by the callback. These will be disposed before each
43
+ * new call to the callback, and when the containing DOM is disposed.
44
+ *
45
+ * `domComputedOwned(valueObs, (owner, value) => Editor.create(owner, value).renderSomething())`
46
+ */
47
+ function domComputedOwned(valueObs, contentFunc) {
48
+ const holder = dispose_1.Holder.create(null);
49
+ const [markerPre, markerPost, func] = domComputed(valueObs, (val) => contentFunc(dispose_1.MultiHolder.create(holder), val));
50
+ (0, domDispose_1.autoDisposeElem)(markerPost, holder);
51
+ return [markerPre, markerPost, func];
52
+ }
53
+ exports.domComputedOwned = domComputedOwned;
38
54
  function identity(arg) { return arg; }
39
55
  /**
40
56
  * Conditionally appends DOM to an element. The value may be an observable or function (from which
@@ -44,22 +60,36 @@ function identity(arg) { return arg; }
44
60
  * Note that if the observable changes between different truthy values, contentFunc gets called
45
61
  * for each value, and previous content gets destroyed. To consider all truthy values the same,
46
62
  * use an observable that returns a proper boolean, e.g.
47
- *
63
+ * ```ts
48
64
  * dom.maybe(use => Boolean(use(fooObs)), () => dom(...));
65
+ * ```
49
66
  *
50
67
  * As with domComputed(), dom.maybe() may but should not be used when the argument is not an
51
68
  * observable or function. The following are equivalent:
52
- *
69
+ * ```ts
53
70
  * dom(..., dom.maybe(myValue, () => dom(...)));
54
71
  * dom(..., myValue ? dom(...) : null);
72
+ * ```
55
73
  *
56
74
  * The latter is preferred for being simpler.
57
75
  *
58
- * @param boolValueObs: Observable or function for a computed.
59
- * @param contentFunc: Called with the result of boolValueObs when it is truthy. Should return DOM.
76
+ * @param boolValueObs - Observable or function for a computed.
77
+ * @param contentFunc - Called with the result of boolValueObs when it is truthy. Should return DOM.
60
78
  */
61
79
  function maybe(boolValueObs, contentFunc) {
62
80
  return domComputed(boolValueObs, (value) => value ? contentFunc(value) : null);
63
81
  }
64
82
  exports.maybe = maybe;
83
+ /**
84
+ * Like maybe(), but the callback gets an additional first argument, owner, which may be used to
85
+ * take ownership of objects created by the callback. These will be disposed before each new call
86
+ * to the callback, and when the condition becomes false or the containing DOM gets disposed.
87
+ * ```ts
88
+ * maybeOwned(showEditor, (owner) => Editor.create(owner).renderSomething())
89
+ * ```
90
+ */
91
+ function maybeOwned(boolValueObs, contentFunc) {
92
+ return domComputedOwned(boolValueObs, (owner, value) => value ? contentFunc(owner, value) : null);
93
+ }
94
+ exports.maybeOwned = maybeOwned;
65
95
  //# sourceMappingURL=domComputed.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"domComputed.js","sourceRoot":"","sources":["../../../lib/domComputed.ts"],"names":[],"mappings":";;AAAA,uCAAuD;AACvD,6CAAwC;AACxC,uCAAkD;AAElD,mFAAmF;AACnF,qDAAmC;AAMnC;;;;GAIG;AACH,SAAgB,cAAc,CAAC,UAAgB,EAAE,SAAe,EAAE,OAAe;IAC/E,MAAM,IAAI,GAAG,UAAU,CAAC,UAAU,CAAC;IACnC,IAAI,IAAI,EAAE;QACR,IAAI,IAAI,CAAC;QACT,KAAK,IAAI,CAAC,GAAG,UAAU,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC,GAAG,IAAI,EAAE;YACnE,IAAI,GAAG,CAAC,CAAC,WAAW,CAAC;YACrB,uBAAU,CAAC,CAAC,CAAC,CAAC;YACd,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;SACrB;QACD,IAAI,OAAO,EAAE;YACX,IAAI,CAAC,YAAY,CAAC,OAAO,YAAY,kBAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,cAAI,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,CAAC;SACnF;KACF;AACH,CAAC;AAbD,wCAaC;AAyCD,SAAgB,WAAW,CACzB,QAA0B,EAAE,cAAkC,QAAe;IAE7E,MAAM,SAAS,GAAG,kBAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAChD,MAAM,UAAU,GAAG,kBAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAEjD,gGAAgG;IAChG,yCAAyC;IACzC,OAAO,CAAC,SAAS,EAAE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