grainjs 1.0.2 → 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.
- package/README.md +23 -71
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lib/PriorityQueue.d.ts +1 -1
- package/dist/cjs/lib/_computed_queue.js +3 -3
- package/dist/cjs/lib/_computed_queue.js.map +1 -1
- package/dist/cjs/lib/binding.d.ts +11 -4
- package/dist/cjs/lib/binding.js +5 -5
- package/dist/cjs/lib/binding.js.map +1 -1
- package/dist/cjs/lib/computed.d.ts +49 -28
- package/dist/cjs/lib/computed.js +38 -52
- package/dist/cjs/lib/computed.js.map +1 -1
- package/dist/cjs/lib/dispose.d.ts +109 -96
- package/dist/cjs/lib/dispose.js +106 -80
- package/dist/cjs/lib/dispose.js.map +1 -1
- package/dist/cjs/lib/dom.d.ts +38 -18
- package/dist/cjs/lib/dom.js +44 -20
- package/dist/cjs/lib/dom.js.map +1 -1
- package/dist/cjs/lib/domComponent.d.ts +56 -48
- package/dist/cjs/lib/domComponent.js +66 -1
- package/dist/cjs/lib/domComponent.js.map +1 -1
- package/dist/cjs/lib/domComputed.d.ts +31 -21
- package/dist/cjs/lib/domComputed.js +14 -11
- package/dist/cjs/lib/domComputed.js.map +1 -1
- package/dist/cjs/lib/domDispose.d.ts +27 -12
- package/dist/cjs/lib/domDispose.js +26 -11
- package/dist/cjs/lib/domDispose.js.map +1 -1
- package/dist/cjs/lib/domForEach.d.ts +4 -3
- package/dist/cjs/lib/domForEach.js +10 -9
- package/dist/cjs/lib/domForEach.js.map +1 -1
- package/dist/cjs/lib/domImpl.d.ts +33 -10
- package/dist/cjs/lib/domImpl.js +28 -9
- package/dist/cjs/lib/domImpl.js.map +1 -1
- package/dist/cjs/lib/domMethods.d.ts +93 -47
- package/dist/cjs/lib/domMethods.js +88 -46
- package/dist/cjs/lib/domMethods.js.map +1 -1
- package/dist/cjs/lib/domevent.d.ts +87 -62
- package/dist/cjs/lib/domevent.js +84 -59
- package/dist/cjs/lib/domevent.js.map +1 -1
- package/dist/cjs/lib/emit.d.ts +62 -32
- package/dist/cjs/lib/emit.js +67 -53
- package/dist/cjs/lib/emit.js.map +1 -1
- package/dist/cjs/lib/kowrap.d.ts +6 -3
- package/dist/cjs/lib/kowrap.js +6 -3
- package/dist/cjs/lib/kowrap.js.map +1 -1
- package/dist/cjs/lib/obsArray.d.ts +91 -53
- package/dist/cjs/lib/obsArray.js +87 -55
- package/dist/cjs/lib/obsArray.js.map +1 -1
- package/dist/cjs/lib/observable.d.ts +25 -15
- package/dist/cjs/lib/observable.js +29 -18
- package/dist/cjs/lib/observable.js.map +1 -1
- package/dist/cjs/lib/pureComputed.d.ts +12 -15
- package/dist/cjs/lib/pureComputed.js +15 -18
- package/dist/cjs/lib/pureComputed.js.map +1 -1
- package/dist/cjs/lib/styled.d.ts +78 -61
- package/dist/cjs/lib/styled.js +26 -79
- package/dist/cjs/lib/styled.js.map +1 -1
- package/dist/cjs/lib/subscribe.d.ts +41 -37
- package/dist/cjs/lib/subscribe.js +31 -40
- package/dist/cjs/lib/subscribe.js.map +1 -1
- package/dist/cjs/lib/util.js +1 -0
- package/dist/cjs/lib/util.js.map +1 -1
- package/dist/cjs/lib/widgets/input.d.ts +3 -1
- package/dist/cjs/lib/widgets/input.js +6 -4
- package/dist/cjs/lib/widgets/input.js.map +1 -1
- package/dist/cjs/lib/widgets/select.d.ts +4 -2
- package/dist/cjs/lib/widgets/select.js +7 -5
- package/dist/cjs/lib/widgets/select.js.map +1 -1
- package/dist/esm/lib/_computed_queue.js +3 -3
- package/dist/esm/lib/_computed_queue.js.map +1 -1
- package/dist/esm/lib/binding.js +2 -2
- package/dist/esm/lib/binding.js.map +1 -1
- package/dist/esm/lib/computed.js +36 -50
- package/dist/esm/lib/computed.js.map +1 -1
- package/dist/esm/lib/dispose.js +104 -78
- package/dist/esm/lib/dispose.js.map +1 -1
- package/dist/esm/lib/dom.js +38 -18
- package/dist/esm/lib/dom.js.map +1 -1
- package/dist/esm/lib/domComponent.js +65 -0
- package/dist/esm/lib/domComponent.js.map +1 -1
- package/dist/esm/lib/domComputed.js +10 -7
- package/dist/esm/lib/domComputed.js.map +1 -1
- package/dist/esm/lib/domDispose.js +26 -11
- package/dist/esm/lib/domDispose.js.map +1 -1
- package/dist/esm/lib/domForEach.js +3 -2
- package/dist/esm/lib/domForEach.js.map +1 -1
- package/dist/esm/lib/domImpl.js +26 -7
- package/dist/esm/lib/domImpl.js.map +1 -1
- package/dist/esm/lib/domMethods.js +77 -35
- package/dist/esm/lib/domMethods.js.map +1 -1
- package/dist/esm/lib/domevent.js +84 -59
- package/dist/esm/lib/domevent.js.map +1 -1
- package/dist/esm/lib/emit.js +67 -53
- package/dist/esm/lib/emit.js.map +1 -1
- package/dist/esm/lib/kowrap.js +5 -2
- package/dist/esm/lib/kowrap.js.map +1 -1
- package/dist/esm/lib/obsArray.js +82 -50
- package/dist/esm/lib/obsArray.js.map +1 -1
- package/dist/esm/lib/observable.js +26 -15
- package/dist/esm/lib/observable.js.map +1 -1
- package/dist/esm/lib/pureComputed.js +15 -18
- package/dist/esm/lib/pureComputed.js.map +1 -1
- package/dist/esm/lib/styled.js +24 -77
- package/dist/esm/lib/styled.js.map +1 -1
- package/dist/esm/lib/subscribe.js +27 -36
- package/dist/esm/lib/subscribe.js.map +1 -1
- package/dist/esm/lib/util.js +1 -0
- package/dist/esm/lib/util.js.map +1 -1
- package/dist/esm/lib/widgets/input.js +3 -1
- package/dist/esm/lib/widgets/input.js.map +1 -1
- package/dist/esm/lib/widgets/select.js +3 -1
- package/dist/esm/lib/widgets/select.js.map +1 -1
- package/dist/grain-full.debug.js +2146 -3062
- package/dist/grain-full.debug.js.map +7 -0
- package/dist/grain-full.min.js +6 -2
- package/dist/grain-full.min.js.map +7 -1
- package/lib/binding.ts +9 -2
- package/lib/computed.ts +56 -56
- package/lib/dispose.ts +110 -85
- package/lib/dom.ts +39 -20
- package/lib/domComponent.ts +66 -57
- package/lib/domComputed.ts +29 -19
- package/lib/domDispose.ts +28 -11
- package/lib/domForEach.ts +7 -3
- package/lib/domImpl.ts +30 -7
- package/lib/domMethods.ts +101 -46
- package/lib/domevent.ts +85 -60
- package/lib/emit.ts +64 -50
- package/lib/kowrap.ts +5 -2
- package/lib/obsArray.ts +89 -54
- package/lib/observable.ts +26 -15
- package/lib/pureComputed.ts +16 -22
- package/lib/styled.ts +85 -71
- package/lib/subscribe.ts +41 -45
- package/lib/util.ts +1 -0
- package/lib/widgets/input.ts +3 -1
- package/lib/widgets/select.ts +3 -1
- package/package.json +38 -27
package/dist/cjs/lib/dom.js
CHANGED
|
@@ -1,25 +1,11 @@
|
|
|
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
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
21
3
|
if (k2 === undefined) k2 = k;
|
|
22
|
-
Object.
|
|
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);
|
|
23
9
|
}) : (function(o, m, k, k2) {
|
|
24
10
|
if (k2 === undefined) k2 = k;
|
|
25
11
|
o[k2] = m[k];
|
|
@@ -45,10 +31,48 @@ const _domImpl = require("./domImpl");
|
|
|
45
31
|
const _domMethods = require("./domMethods");
|
|
46
32
|
const domevent = require("./domevent");
|
|
47
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
|
+
*/
|
|
48
72
|
// We just want to re-export _domImpl.dom, but to allow adding methods to it in a typesafe way,
|
|
49
73
|
// TypeScript wants us to declare a real function in the same file.
|
|
50
74
|
function dom(tagString, ...args) {
|
|
51
|
-
return domImpl_1.dom(tagString, ...args);
|
|
75
|
+
return (0, domImpl_1.dom)(tagString, ...args);
|
|
52
76
|
}
|
|
53
77
|
exports.dom = dom;
|
|
54
78
|
// Additionally export all methods as properties of dom() function.
|
package/dist/cjs/lib/dom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dom.js","sourceRoot":"","sources":["../../../lib/dom.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dom.js","sourceRoot":"","sources":["../../../lib/dom.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iGAAiG;AACjG,4CAA0B;AAC1B,iDAA+B;AAC/B,gDAA8B;AAC9B,+CAA6B;AAC7B,+CAA6B;AAC7B,+CAA6B;AAC7B,6CAA2B;AAE3B,gDAAgD;AAChD,8CAA8C;AAC9C,4CAA4C;AAC5C,4CAA4C;AAC5C,sCAAsC;AACtC,4CAA4C;AAE5C,uCAAuC;AAEvC,uCAAkE;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,+FAA+F;AAC/F,mEAAmE;AACnE,SAAgB,GAAG,CAAsB,SAAc,EAAE,GAAG,IAA4B;IACt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C,IAAI,CAAC;IACnC,WAAO,GAAW,WAAW,CAAC,OAAO,CAAC;IACtC,OAAG,GAAe,WAAW,CAAC,GAAG,CAAC;IAClC,aAAS,GAAS,WAAW,CAAC,SAAS,CAAC;IACxC,YAAQ,GAAU,WAAW,CAAC,QAAQ,CAAC;IACvC,QAAI,GAAc,WAAW,CAAC,IAAI,CAAC;IACnC,WAAO,GAAW,WAAW,CAAC,OAAO,CAAC;IACtC,kBAAc,GAAI,YAAY,CAAC,cAAc,CAAC;IAC9C,eAAW,GAAO,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,71 +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()
|
|
15
|
+
* UI components that can be inserted into `dom()`.
|
|
3
16
|
*
|
|
4
|
-
* Components are created and inserted using dom.create()
|
|
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
|
+
* ```
|
|
5
24
|
*
|
|
6
|
-
*
|
|
7
|
-
* dom.create(MyWidget, ...myArgs), // Calls MyWidget.create(owner, ...myArgs)
|
|
8
|
-
* dom.create(createMyWidget, ...myArgs), // Calls createMyWidget(owner, ...myArgs)
|
|
9
|
-
* )
|
|
10
|
-
*
|
|
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()
|
|
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
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
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
|
-
*
|
|
45
|
+
* :::info Explanation
|
|
31
46
|
*
|
|
32
|
-
*
|
|
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
|
+
* ```
|
|
33
51
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
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.
|
|
56
|
+
*
|
|
57
|
+
* :::
|
|
38
58
|
*
|
|
39
59
|
* A function component may return DOM directly. A class component returns the class instance,
|
|
40
|
-
* which must have a
|
|
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
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
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
|
-
*
|
|
51
|
-
*
|
|
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 { DomContents } from './domComputed';
|
|
59
|
-
export interface IDomComponent {
|
|
60
|
-
buildDom(): DomContents;
|
|
61
|
-
}
|
|
62
|
-
export declare type DomComponentReturn = DomContents | IDomComponent;
|
|
63
|
-
export declare type IDomCreateFunc<Args extends any[]> = (owner: MultiHolder, ...args: Args) => DomComponentReturn;
|
|
64
|
-
export interface IDomCreateClass<Args extends any[]> {
|
|
65
|
-
create: IDomCreateFunc<Args>;
|
|
66
|
-
new (...args: Args): DomComponentReturn;
|
|
67
|
-
}
|
|
68
|
-
export declare type IDomCreator<Args extends any[]> = IDomCreateFunc<Args> | IDomCreateClass<Args>;
|
|
69
|
-
declare type DomCreatorArgs<T> = T extends (owner: MultiHolder, ...args: infer P) => any ? P : (T extends new (...args: infer P) => any ? P : never);
|
|
70
79
|
export declare function create<Fn extends IDomCreator<any[]>>(fn: Fn, ...args: DomCreatorArgs<Fn>): DomContents;
|
|
71
|
-
export {};
|
|
@@ -2,8 +2,73 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.create = void 0;
|
|
4
4
|
const domComputed_1 = require("./domComputed");
|
|
5
|
+
/**
|
|
6
|
+
* UI components that can be inserted into `dom()`.
|
|
7
|
+
*
|
|
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
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* The first argument may be a function, which is called directly, or a class with a `.create()`
|
|
17
|
+
* static method, in which case that's what gets called.
|
|
18
|
+
*
|
|
19
|
+
* In both cases, the call gets a first argument of `owner` followed by the rest of the arguments
|
|
20
|
+
* to `dom.create()`. The `owner` is a `MultiHolder` that will own this component. This works
|
|
21
|
+
* naturally with any class that derives from Disposable, since it then has a suitable static
|
|
22
|
+
* `create()` method.
|
|
23
|
+
*
|
|
24
|
+
* Function-based components may use owner to easily handle disposal. For example:
|
|
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
|
|
34
|
+
* the component is attached. When the parent DOM element is disposed, so is the component.
|
|
35
|
+
*
|
|
36
|
+
* :::info Explanation
|
|
37
|
+
*
|
|
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
|
+
* ```
|
|
42
|
+
*
|
|
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
|
+
* :::
|
|
49
|
+
*
|
|
50
|
+
* A function component may return DOM directly. A class component returns the class instance,
|
|
51
|
+
* which must have a `.buildDom()` method which will be called right after the constructor to get
|
|
52
|
+
* the DOM. Note that buildDom is only called once.
|
|
53
|
+
*
|
|
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
|
+
* ```
|
|
59
|
+
*
|
|
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
|
+
* ```
|
|
65
|
+
*
|
|
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
|
|
68
|
+
* disposed when the containing element is disposed, followed by the `owner` itself.
|
|
69
|
+
*/
|
|
5
70
|
function create(fn, ...args) {
|
|
6
|
-
return domComputed_1.domComputedOwned(null, (owner) => {
|
|
71
|
+
return (0, domComputed_1.domComputedOwned)(null, (owner) => {
|
|
7
72
|
const value = ('create' in fn) ?
|
|
8
73
|
fn.create(owner, ...args) :
|
|
9
74
|
fn(owner, ...args);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domComponent.js","sourceRoot":"","sources":["../../../lib/domComponent.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"domComponent.js","sourceRoot":"","sources":["../../../lib/domComponent.ts"],"names":[],"mappings":";;;AACA,+CAA4D;AAuB5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,SAAgB,MAAM,CAAgC,EAAM,EAAE,GAAG,IAAwB;IACvF,OAAO,IAAA,8BAAgB,EAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;QACtC,MAAM,KAAK,GAAuB,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,CAAC;YACjD,EAA6B,CAAC,MAAM,CAAC,KAAK,EAAE,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,8 +1,8 @@
|
|
|
1
1
|
import { BindableValue } from './binding';
|
|
2
2
|
import { MultiHolder } from './dispose';
|
|
3
3
|
import { DomArg, DomMethod } from './domImpl';
|
|
4
|
-
export
|
|
5
|
-
export
|
|
4
|
+
export type DomComputed = [Node, Node, DomMethod];
|
|
5
|
+
export type DomContents = Node | string | DomComputed | void | null | undefined | IDomContentsArray;
|
|
6
6
|
export interface IDomContentsArray extends Array<DomContents> {
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
@@ -21,29 +21,36 @@ export declare function replaceContent(nodeBefore: Node, nodeAfter: Node, conten
|
|
|
21
21
|
* changes, previous content is disposed and removed, and new content added in its place.
|
|
22
22
|
*
|
|
23
23
|
* The following are roughly equivalent:
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
+
* ```
|
|
27
32
|
*
|
|
28
33
|
* Here, (C) is best. Both (A) and (B) would rebuild DOM for any change in nlinesObs, but (C)
|
|
29
34
|
* encapsulates meaningful changes in the observable, and only recreates DOM when necessary.
|
|
30
35
|
*
|
|
31
36
|
* Syntax (B), without the second argument, may be useful in cases of DOM depending on several
|
|
32
37
|
* observables, e.g.
|
|
38
|
+
* ```ts
|
|
39
|
+
* domComputed(use => use(readonlyObs) ? dom('div') :
|
|
40
|
+
* (use(nlinesObs) > 1 ? dom('textarea') : dom('input')))
|
|
41
|
+
* ```
|
|
33
42
|
*
|
|
34
|
-
*
|
|
35
|
-
* (use(nlinesObs) > 1 ? dom('textarea') : dom('input')));
|
|
36
|
-
*
|
|
37
|
-
* 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
|
|
38
44
|
* are equivalent:
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* dom(..., domComputed(listValue, list => `Have ${list.length} items`), ...);
|
|
47
|
+
* dom(..., `Have ${listValue.length} items`, ...);
|
|
48
|
+
* ```
|
|
42
49
|
*
|
|
43
50
|
* In this case, the latter is preferred as the clearly simpler one.
|
|
44
51
|
*
|
|
45
|
-
* @param valueObs
|
|
46
|
-
* @param contentFunc
|
|
52
|
+
* @param valueObs - Observable or function for a computed.
|
|
53
|
+
* @param contentFunc - Function called with the result of valueObs as the input, and
|
|
47
54
|
* returning DOM as output. If omitted, defaults to the identity function.
|
|
48
55
|
*/
|
|
49
56
|
export declare function domComputed(valueObs: BindableValue<Exclude<DomArg, DomMethod>>): DomComputed;
|
|
@@ -53,7 +60,7 @@ export declare function domComputed<T>(valueObs: BindableValue<T>, contentFunc:
|
|
|
53
60
|
* used to take ownership of objects created by the callback. These will be disposed before each
|
|
54
61
|
* new call to the callback, and when the containing DOM is disposed.
|
|
55
62
|
*
|
|
56
|
-
*
|
|
63
|
+
* `domComputedOwned(valueObs, (owner, value) => Editor.create(owner, value).renderSomething())`
|
|
57
64
|
*/
|
|
58
65
|
export declare function domComputedOwned<T>(valueObs: BindableValue<T>, contentFunc: (owner: MultiHolder, val: T) => DomContents): DomComputed;
|
|
59
66
|
/**
|
|
@@ -64,26 +71,29 @@ export declare function domComputedOwned<T>(valueObs: BindableValue<T>, contentF
|
|
|
64
71
|
* Note that if the observable changes between different truthy values, contentFunc gets called
|
|
65
72
|
* for each value, and previous content gets destroyed. To consider all truthy values the same,
|
|
66
73
|
* use an observable that returns a proper boolean, e.g.
|
|
67
|
-
*
|
|
74
|
+
* ```ts
|
|
68
75
|
* dom.maybe(use => Boolean(use(fooObs)), () => dom(...));
|
|
76
|
+
* ```
|
|
69
77
|
*
|
|
70
78
|
* As with domComputed(), dom.maybe() may but should not be used when the argument is not an
|
|
71
79
|
* observable or function. The following are equivalent:
|
|
72
|
-
*
|
|
80
|
+
* ```ts
|
|
73
81
|
* dom(..., dom.maybe(myValue, () => dom(...)));
|
|
74
82
|
* dom(..., myValue ? dom(...) : null);
|
|
83
|
+
* ```
|
|
75
84
|
*
|
|
76
85
|
* The latter is preferred for being simpler.
|
|
77
86
|
*
|
|
78
|
-
* @param boolValueObs
|
|
79
|
-
* @param contentFunc
|
|
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.
|
|
80
89
|
*/
|
|
81
90
|
export declare function maybe<T>(boolValueObs: BindableValue<T>, contentFunc: (val: NonNullable<T>) => DomContents): DomComputed;
|
|
82
91
|
/**
|
|
83
92
|
* Like maybe(), but the callback gets an additional first argument, owner, which may be used to
|
|
84
93
|
* take ownership of objects created by the callback. These will be disposed before each new call
|
|
85
94
|
* to the callback, and when the condition becomes false or the containing DOM gets disposed.
|
|
86
|
-
*
|
|
87
|
-
* maybeOwned(showEditor, (owner) => Editor.create(owner).renderSomething())
|
|
95
|
+
* ```ts
|
|
96
|
+
* maybeOwned(showEditor, (owner) => Editor.create(owner).renderSomething())
|
|
97
|
+
* ```
|
|
88
98
|
*/
|
|
89
99
|
export declare function maybeOwned<T>(boolValueObs: BindableValue<T>, contentFunc: (owner: MultiHolder, val: NonNullable<T>) => DomContents): DomComputed;
|
|
@@ -18,11 +18,11 @@ function replaceContent(nodeBefore, nodeAfter, content) {
|
|
|
18
18
|
let next;
|
|
19
19
|
for (let n = nodeBefore.nextSibling; n && n !== nodeAfter; n = next) {
|
|
20
20
|
next = n.nextSibling;
|
|
21
|
-
domDispose_1.domDispose(n);
|
|
21
|
+
(0, domDispose_1.domDispose)(n);
|
|
22
22
|
elem.removeChild(n);
|
|
23
23
|
}
|
|
24
24
|
if (content) {
|
|
25
|
-
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);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -33,7 +33,7 @@ function domComputed(valueObs, contentFunc = identity) {
|
|
|
33
33
|
// Function is added after markerPre and markerPost, so that it runs once they have already been
|
|
34
34
|
// attached to elem (the parent element).
|
|
35
35
|
return [markerPre, markerPost, (elem) => {
|
|
36
|
-
binding_1.subscribeElem(markerPost, valueObs, (value) => replaceContent(markerPre, markerPost, contentFunc(value)));
|
|
36
|
+
(0, binding_1.subscribeElem)(markerPost, valueObs, (value) => replaceContent(markerPre, markerPost, contentFunc(value)));
|
|
37
37
|
}];
|
|
38
38
|
}
|
|
39
39
|
exports.domComputed = domComputed;
|
|
@@ -42,12 +42,12 @@ exports.domComputed = domComputed;
|
|
|
42
42
|
* used to take ownership of objects created by the callback. These will be disposed before each
|
|
43
43
|
* new call to the callback, and when the containing DOM is disposed.
|
|
44
44
|
*
|
|
45
|
-
*
|
|
45
|
+
* `domComputedOwned(valueObs, (owner, value) => Editor.create(owner, value).renderSomething())`
|
|
46
46
|
*/
|
|
47
47
|
function domComputedOwned(valueObs, contentFunc) {
|
|
48
48
|
const holder = dispose_1.Holder.create(null);
|
|
49
49
|
const [markerPre, markerPost, func] = domComputed(valueObs, (val) => contentFunc(dispose_1.MultiHolder.create(holder), val));
|
|
50
|
-
domDispose_1.autoDisposeElem(markerPost, holder);
|
|
50
|
+
(0, domDispose_1.autoDisposeElem)(markerPost, holder);
|
|
51
51
|
return [markerPre, markerPost, func];
|
|
52
52
|
}
|
|
53
53
|
exports.domComputedOwned = domComputedOwned;
|
|
@@ -60,19 +60,21 @@ function identity(arg) { return arg; }
|
|
|
60
60
|
* Note that if the observable changes between different truthy values, contentFunc gets called
|
|
61
61
|
* for each value, and previous content gets destroyed. To consider all truthy values the same,
|
|
62
62
|
* use an observable that returns a proper boolean, e.g.
|
|
63
|
-
*
|
|
63
|
+
* ```ts
|
|
64
64
|
* dom.maybe(use => Boolean(use(fooObs)), () => dom(...));
|
|
65
|
+
* ```
|
|
65
66
|
*
|
|
66
67
|
* As with domComputed(), dom.maybe() may but should not be used when the argument is not an
|
|
67
68
|
* observable or function. The following are equivalent:
|
|
68
|
-
*
|
|
69
|
+
* ```ts
|
|
69
70
|
* dom(..., dom.maybe(myValue, () => dom(...)));
|
|
70
71
|
* dom(..., myValue ? dom(...) : null);
|
|
72
|
+
* ```
|
|
71
73
|
*
|
|
72
74
|
* The latter is preferred for being simpler.
|
|
73
75
|
*
|
|
74
|
-
* @param boolValueObs
|
|
75
|
-
* @param contentFunc
|
|
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.
|
|
76
78
|
*/
|
|
77
79
|
function maybe(boolValueObs, contentFunc) {
|
|
78
80
|
return domComputed(boolValueObs, (value) => value ? contentFunc(value) : null);
|
|
@@ -82,8 +84,9 @@ exports.maybe = maybe;
|
|
|
82
84
|
* Like maybe(), but the callback gets an additional first argument, owner, which may be used to
|
|
83
85
|
* take ownership of objects created by the callback. These will be disposed before each new call
|
|
84
86
|
* to the callback, and when the condition becomes false or the containing DOM gets disposed.
|
|
85
|
-
*
|
|
86
|
-
* maybeOwned(showEditor, (owner) => Editor.create(owner).renderSomething())
|
|
87
|
+
* ```ts
|
|
88
|
+
* maybeOwned(showEditor, (owner) => Editor.create(owner).renderSomething())
|
|
89
|
+
* ```
|
|
87
90
|
*/
|
|
88
91
|
function maybeOwned(boolValueObs, contentFunc) {
|
|
89
92
|
return domComputedOwned(boolValueObs, (owner, value) => value ? contentFunc(owner, value) : null);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domComputed.js","sourceRoot":"","sources":["../../../lib/domComputed.ts"],"names":[],"mappings":";;;AAAA,uCAAuD;AACvD,uCAA8C;AAC9C,6CAAyD;AACzD,uCAAkD;AAElD,mFAAmF;AACnF,qDAAmC;AAUnC;;;;GAIG;AACH,SAAgB,cAAc,CAAC,UAAgB,EAAE,SAAe,EAAE,OAAoB;IACpF,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,
|
|
1
|
+
{"version":3,"file":"domComputed.js","sourceRoot":"","sources":["../../../lib/domComputed.ts"],"names":[],"mappings":";;;AAAA,uCAAuD;AACvD,uCAA8C;AAC9C,6CAAyD;AACzD,uCAAkD;AAElD,mFAAmF;AACnF,qDAAmC;AAUnC;;;;GAIG;AACH,SAAgB,cAAc,CAAC,UAAgB,EAAE,SAAe,EAAE,OAAoB;IAC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AS,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,WAAW,CAAC,QAAQ,EACxD,CAAC,GAAM,EAAE,EAAE,CAAC,WAAW,CAAC,qBAAW,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5D,IAAA,4BAAe,EAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACpC,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;AACvC,CAAC;AARD,4CAQC;AAED,SAAS,QAAQ,CAAI,GAAM,IAAO,OAAO,GAAG,CAAC,CAAC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,SAAgB,KAAK,CAAI,YAA8B,EACnD,WAAiD;IACnD,OAAO,WAAW,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,KAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAClF,CAAC;AAHD,sBAGC;AAED;;;;;;;GAOG;AACH,SAAgB,UAAU,CAAI,YAA8B,EACxD,WAAqE;IACvE,OAAO,gBAAgB,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,KAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AACrG,CAAC;AAHD,gCAGC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { IDisposable } from './dispose';
|
|
2
|
-
export
|
|
2
|
+
export type INodeFunc = (node: Node) => void;
|
|
3
|
+
/** @internal */
|
|
3
4
|
export declare function _disposeNode(node: Node): void;
|
|
4
5
|
export interface IDomDisposeHooks {
|
|
5
6
|
disposeRecursive: (node: Node) => void;
|
|
@@ -17,26 +18,40 @@ export declare const domDisposeHooks: IDomDisposeHooks;
|
|
|
17
18
|
* It is automatically called if one of the function arguments to dom() throws an exception during
|
|
18
19
|
* element creation. This way any onDispose() handlers set on the unfinished element get called.
|
|
19
20
|
*
|
|
20
|
-
* @param
|
|
21
|
+
* @param node - The element to run disposers on.
|
|
21
22
|
*/
|
|
22
23
|
export declare function domDispose(node: Node): void;
|
|
23
24
|
/**
|
|
24
|
-
* Associate a
|
|
25
|
-
* using domDispose() on it or any of its parents. If
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* @param {Function} disposerFunc(elem): Will be called when domDispose() is called on the
|
|
29
|
-
* element or its ancestor.
|
|
25
|
+
* Associate a disposer function with a DOM element. It will be called when the element is disposed
|
|
26
|
+
* using `domDispose()` on it or any of its parents. If called multiple times, all
|
|
27
|
+
* disposer functions will be called in reverse order.
|
|
28
|
+
*
|
|
30
29
|
* Note that it is not necessary usually to dispose event listeners attached to an element (e.g.
|
|
31
|
-
* with dom.on()) since their lifetime is naturally limited to the lifetime of the element.
|
|
30
|
+
* with `dom.on()`) since their lifetime is naturally limited to the lifetime of the element.
|
|
31
|
+
*
|
|
32
|
+
* @param elem - The element to associate the disposer with.
|
|
33
|
+
* @param disposerFunc - Will be called when `domDispose()` is called on the element or its ancestor.
|
|
32
34
|
*/
|
|
33
35
|
export declare function onDisposeElem(elem: Node, disposerFunc: INodeFunc): void;
|
|
36
|
+
/**
|
|
37
|
+
* Associate a disposer function with a DOM element. It will be called when the element is disposed
|
|
38
|
+
* using `domDispose()` on it or any of its parents. If called multiple times, all
|
|
39
|
+
* disposer functions will be called in reverse order.
|
|
40
|
+
*
|
|
41
|
+
* @param disposerFunc - Will be called when `domDispose()` is called on the element or its ancestor.
|
|
42
|
+
*/
|
|
34
43
|
export declare function onDispose(disposerFunc: INodeFunc): (elem: Node) => void;
|
|
35
44
|
/**
|
|
36
|
-
* Make the given element own the disposable, and call its dispose method when domDispose() is
|
|
45
|
+
* Make the given element own the disposable, and call its dispose method when `domDispose()` is
|
|
37
46
|
* called on the element or any of its parents.
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
47
|
+
* @param elem - The element to own the disposable.
|
|
48
|
+
* @param disposable - Anything with a `.dispose()` method.
|
|
40
49
|
*/
|
|
41
50
|
export declare function autoDisposeElem(elem: Node, disposable: IDisposable | null): void;
|
|
51
|
+
/**
|
|
52
|
+
* Make the given element own the disposable, and call its dispose method when `domDispose()` is
|
|
53
|
+
* called on the element or any of its parents.
|
|
54
|
+
*
|
|
55
|
+
* @param disposable - Anything with a `.dispose()` method.
|
|
56
|
+
*/
|
|
42
57
|
export declare function autoDispose(disposable: IDisposable | null): ((elem: Node) => void) | undefined;
|
|
@@ -26,6 +26,7 @@ function _walkDom(elem, visitFunc) {
|
|
|
26
26
|
visitFunc(elem);
|
|
27
27
|
}
|
|
28
28
|
// Internal helper to run all disposers for a single element.
|
|
29
|
+
/** @internal */
|
|
29
30
|
function _disposeNode(node) {
|
|
30
31
|
let disposer = _disposeMap.get(node);
|
|
31
32
|
if (disposer) {
|
|
@@ -58,21 +59,22 @@ exports.domDisposeHooks = {
|
|
|
58
59
|
* It is automatically called if one of the function arguments to dom() throws an exception during
|
|
59
60
|
* element creation. This way any onDispose() handlers set on the unfinished element get called.
|
|
60
61
|
*
|
|
61
|
-
* @param
|
|
62
|
+
* @param node - The element to run disposers on.
|
|
62
63
|
*/
|
|
63
64
|
function domDispose(node) {
|
|
64
65
|
exports.domDisposeHooks.disposeRecursive(node);
|
|
65
66
|
}
|
|
66
67
|
exports.domDispose = domDispose;
|
|
67
68
|
/**
|
|
68
|
-
* Associate a
|
|
69
|
-
* using domDispose() on it or any of its parents. If
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param {Function} disposerFunc(elem): Will be called when domDispose() is called on the
|
|
73
|
-
* element or its ancestor.
|
|
69
|
+
* Associate a disposer function with a DOM element. It will be called when the element is disposed
|
|
70
|
+
* using `domDispose()` on it or any of its parents. If called multiple times, all
|
|
71
|
+
* disposer functions will be called in reverse order.
|
|
72
|
+
*
|
|
74
73
|
* Note that it is not necessary usually to dispose event listeners attached to an element (e.g.
|
|
75
|
-
* with dom.on()) since their lifetime is naturally limited to the lifetime of the element.
|
|
74
|
+
* with `dom.on()`) since their lifetime is naturally limited to the lifetime of the element.
|
|
75
|
+
*
|
|
76
|
+
* @param elem - The element to associate the disposer with.
|
|
77
|
+
* @param disposerFunc - Will be called when `domDispose()` is called on the element or its ancestor.
|
|
76
78
|
*/
|
|
77
79
|
function onDisposeElem(elem, disposerFunc) {
|
|
78
80
|
const prevDisposer = _disposeMap.get(elem);
|
|
@@ -82,15 +84,22 @@ function onDisposeElem(elem, disposerFunc) {
|
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
exports.onDisposeElem = onDisposeElem;
|
|
87
|
+
/**
|
|
88
|
+
* Associate a disposer function with a DOM element. It will be called when the element is disposed
|
|
89
|
+
* using `domDispose()` on it or any of its parents. If called multiple times, all
|
|
90
|
+
* disposer functions will be called in reverse order.
|
|
91
|
+
*
|
|
92
|
+
* @param disposerFunc - Will be called when `domDispose()` is called on the element or its ancestor.
|
|
93
|
+
*/
|
|
85
94
|
function onDispose(disposerFunc) {
|
|
86
95
|
return (elem) => onDisposeElem(elem, disposerFunc);
|
|
87
96
|
}
|
|
88
97
|
exports.onDispose = onDispose;
|
|
89
98
|
/**
|
|
90
|
-
* Make the given element own the disposable, and call its dispose method when domDispose() is
|
|
99
|
+
* Make the given element own the disposable, and call its dispose method when `domDispose()` is
|
|
91
100
|
* called on the element or any of its parents.
|
|
92
|
-
* @param
|
|
93
|
-
* @param
|
|
101
|
+
* @param elem - The element to own the disposable.
|
|
102
|
+
* @param disposable - Anything with a `.dispose()` method.
|
|
94
103
|
*/
|
|
95
104
|
function autoDisposeElem(elem, disposable) {
|
|
96
105
|
if (disposable) {
|
|
@@ -98,6 +107,12 @@ function autoDisposeElem(elem, disposable) {
|
|
|
98
107
|
}
|
|
99
108
|
}
|
|
100
109
|
exports.autoDisposeElem = autoDisposeElem;
|
|
110
|
+
/**
|
|
111
|
+
* Make the given element own the disposable, and call its dispose method when `domDispose()` is
|
|
112
|
+
* called on the element or any of its parents.
|
|
113
|
+
*
|
|
114
|
+
* @param disposable - Anything with a `.dispose()` method.
|
|
115
|
+
*/
|
|
101
116
|
function autoDispose(disposable) {
|
|
102
117
|
if (disposable) {
|
|
103
118
|
return (elem) => autoDisposeElem(elem, disposable);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domDispose.js","sourceRoot":"","sources":["../../../lib/domDispose.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,GAAuC,IAAI,OAAO,EAAE,CAAC;AAItE,4FAA4F;AAC5F,mCAAmC;AACnC,SAAS,QAAQ,CAAC,IAAU,EAAE,SAAoB;IAChD,IAAI,CAAC,GAAc,IAAI,CAAC,UAAU,CAAC;IACnC,OAAO,CAAC,EAAE;QACR,4FAA4F;QAC5F,gCAAgC;QAChC,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;QACvB,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC;KACnB;IACD,SAAS,CAAC,IAAI,CAAC,CAAC;AAClB,CAAC;AAED,6DAA6D;AAC7D,SAAgB,YAAY,CAAC,IAAU;IACrC,IAAI,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACrC,IAAI,QAAQ,EAAE;QACZ,IAAI,GAAG,GAAmB,IAAI,CAAC;QAC/B,GAAG;YACD,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACxB,QAAQ,CAAC,IAAI,CAAC,CAAC;YACf,qEAAqE;YACrE,GAAG,GAAG,QAAQ,CAAC;YACf,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;SACjC,QAAQ,QAAQ,EAAE;KACpB;AACH,CAAC;AAZD,oCAYC;AAED,SAAS,qBAAqB,CAAC,IAAU;IACvC,QAAQ,CAAC,IAAI,EAAE,uBAAe,CAAC,WAAW,CAAC,CAAC;A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