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,75 +1,3 @@
1
- /**
2
- * dispose.js provides tools to objects that needs to dispose resources, such as destroy DOM, and
3
- * unsubscribe from events. The motivation with examples is presented here:
4
- *
5
- * https://phab.getgrist.com/w/disposal/
6
- *
7
- * Disposable is a class for components that need cleanup (e.g. maintain DOM, listen to events,
8
- * subscribe to anything). It provides a .dispose() method that should be called to destroy the
9
- * component, and .onDispose()/.autoDispose() methods that the component should use to take
10
- * responsibility for other pieces that require cleanup.
11
- *
12
- * To define a disposable class:
13
- * class Foo extends Disposable { ... }
14
- *
15
- * To create Foo:
16
- * const foo = Foo.create(owner, ...args);
17
- * This is better than `new Foo` for two reasons:
18
- * 1. If Foo's constructor throws an exception, any disposals registered in that constructor
19
- * before the exception are honored.
20
- * 2. It ensures you specify the owner of the new instance (but you can use null to skip it).
21
- *
22
- * In Foo's constructor (or rarely methods), take ownership of other Disposable objects:
23
- * this.bar = Bar.create(this, ...);
24
- *
25
- * For objects that are not instances of Disposable but have a .dispose() methods, use:
26
- * this.bar = this.autoDispose(createSomethingDisposable());
27
- *
28
- * To call a function on disposal (e.g. to add custom disposal logic):
29
- * this.onDispose(() => this.myUnsubscribeAllMethod());
30
- * this.onDispose(this.myUnsubscribeAllMethod, this); // slightly more efficient
31
- *
32
- * To mark this object to be wiped out on disposal (i.e. set all properties to null):
33
- * this.wipeOnDispose();
34
- * See the documentation of that method for more info.
35
- *
36
- * To dispose Foo directly:
37
- * foo.dispose();
38
- * To determine if an object has already been disposed:
39
- * foo.isDisposed()
40
- *
41
- * If you need to replace an owned object, or release, or dispose it early, use a Holder:
42
- * this._holder = Holder.create(this);
43
- * Bar.create(this._holder, 1); // creates new Bar(1)
44
- * Bar.create(this._holder, 2); // creates new Bar(2) and disposes previous object
45
- * this._holder.clear(); // disposes contained object
46
- * this._holder.release(); // releases contained object
47
- *
48
- * If you need a container for multiple objects and dispose them all together, use a MultiHolder:
49
- * this._mholder = MultiHolder.create(null);
50
- * Bar.create(this._mholder, 1); // create new Bar(1)
51
- * Bar.create(this._mholder, 2); // create new Bar(2)
52
- * this._mholder.dispose(); // disposes both objects
53
- *
54
- * If creating your own class with a dispose() method, do NOT throw exceptions from dispose().
55
- * These cannot be handled properly in all cases. Read here about the same issue in C++:
56
- * http://bin-login.name/ftp/pub/docs/programming_languages/cpp/cffective_cpp/MAGAZINE/SU_FRAME.HTM#destruct
57
- *
58
- * Using a parametrized (generic) class as a Disposable is tricky. E.g.
59
- * class Bar<T> extends Disposable { ... }
60
- * // Bar<T>.create(...) <-- doesn't work
61
- * // Bar.create<T>(...) <-- doesn't work
62
- * // Bar.create(...) <-- works, but with {} for Bar's type parameters
63
- *
64
- * The solution is to expose the constructor type using a helper method:
65
- * class Bar<T> extends Disposable {
66
- * // Note the tuple below which must match the constructor parameters of Bar<U>.
67
- * public static ctor<U>(): IDisposableCtor<Bar<U>, [U, boolean]> { return this; }
68
- * constructor(a: T, b: boolean) { ... }
69
- * }
70
- * Bar.ctor<T>().create(...) // <-- works, creates Bar<T>, and does type-checking!
71
- */
72
- import { LLink } from './emit';
73
1
  /**
74
2
  * Anything with a .dispose() method is a disposable object, and implements the IDisposable interface.
75
3
  */
@@ -77,7 +5,7 @@ export interface IDisposable {
77
5
  dispose(): void;
78
6
  }
79
7
  /**
80
- * Anything with .autoDispose() can be the owner of a disposable object. This is a type-specific
8
+ * Anything with `.autoDispose()` can be the owner of a disposable object. This is a type-specific
81
9
  * class that can only own a disposable object of type T.
82
10
  */
83
11
  export interface IDisposableOwnerT<T extends IDisposable> {
@@ -97,33 +25,107 @@ export interface IDisposableCtor<Derived, CtorArgs extends any[]> {
97
25
  create<T extends new (...args: any[]) => any>(this: T, owner: IDisposableOwnerT<InstanceType<T>> | null, ...args: ConstructorParameters<T>): InstanceType<T>;
98
26
  }
99
27
  /**
100
- * Base class for disposable objects that can own other objects. See the module documentation.
28
+ * Base class for disposable objects that can own other objects.
29
+ *
30
+ * For background and motivation, see [Disposables](/dispose).
31
+ *
32
+ * `Disposable` is a class for components that need cleanup (e.g. maintain DOM, listen to events,
33
+ * subscribe to anything). It provides a `.dispose()` method that should be called to destroy the
34
+ * component, and `.onDispose()` / `.autoDispose()` methods that the component should use to take
35
+ * responsibility for other pieces that require cleanup.
36
+ *
37
+ * To define a disposable class:
38
+ * ```ts
39
+ * class Foo extends Disposable { ... }
40
+ * ```
41
+ *
42
+ * To create `Foo`:
43
+ * ```ts
44
+ * const foo = Foo.create(owner, ...args);
45
+ * ```
46
+ * This is better than `new Foo` for two reasons:
47
+ * 1. If `Foo`'s constructor throws an exception, any disposals registered in that constructor
48
+ * before the exception are honored.
49
+ * 2. It ensures you specify the owner of the new instance (but you can use null to skip it).
50
+ *
51
+ * In `Foo`'s constructor (or rarely methods), take ownership of other Disposable objects:
52
+ * ```ts
53
+ * this.bar = Bar.create(this, ...);
54
+ * ```
55
+ *
56
+ * For objects that are not instances of Disposable but have a .dispose() methods, use:
57
+ * ```ts
58
+ * this.bar = this.autoDispose(createSomethingDisposable());
59
+ * ```
60
+ *
61
+ * To call a function on disposal (e.g. to add custom disposal logic):
62
+ * ```ts
63
+ * this.onDispose(() => this.myUnsubscribeAllMethod());
64
+ * this.onDispose(this.myUnsubscribeAllMethod, this);
65
+ * ```
66
+ *
67
+ * To mark this object to be wiped out on disposal (i.e. set all properties to null):
68
+ * ```ts
69
+ * this.wipeOnDispose();
70
+ * ```
71
+ * See the documentation of that method for more info.
72
+ *
73
+ * To dispose Foo directly: `foo.dispose()`.
74
+ *
75
+ * To determine if an object has already been disposed: `foo.isDisposed()`.
76
+ *
77
+ * If you need to replace an owned object, or release, or dispose it early, use a
78
+ * [`Holder`](#Holder) or [`MultiHolder`](#MultiHolder).
79
+ *
80
+ * If creating your own class with a `dispose()` method, do NOT throw exceptions from `dispose()`.
81
+ * These cannot be handled properly in all cases.
82
+ *
83
+ * Using a parametrized (generic) class as a Disposable is tricky. E.g.
84
+ * ```ts
85
+ * class Bar<T> extends Disposable { ... }
86
+ * // Bar<T>.create(...) <-- doesn't work
87
+ * // Bar.create<T>(...) <-- doesn't work
88
+ * // Bar.create(...) <-- works, but with {} for Bar's type parameters
89
+ * ```
90
+ *
91
+ * The solution is to expose the constructor type using a helper method:
92
+ * ```ts
93
+ * class Bar<T> extends Disposable {
94
+ * // Note the tuple below which must match the constructor parameters of Bar<U>.
95
+ * public static ctor<U>(): IDisposableCtor<Bar<U>, [U, boolean]> { return this; }
96
+ * constructor(a: T, b: boolean) { ... }
97
+ * }
98
+ * Bar.ctor<T>().create(...) // <-- works, creates Bar<T>, and does type-checking!
99
+ * ```
101
100
  */
102
101
  export declare abstract class Disposable implements IDisposable, IDisposableOwner {
103
102
  /**
104
103
  * Create Disposable instances using `Class.create(owner, ...)` rather than `new Class(...)`.
105
104
  *
106
105
  * This reminds you to provide an owner, and ensures that if the constructor throws an
107
- * exception, dispose() gets called to clean up the partially-constructed object.
106
+ * exception, `dispose()` gets called to clean up the partially-constructed object.
108
107
  *
109
- * Owner may be null if intend to ensure disposal some other way.
108
+ * Owner may be `null` if you intend to ensure disposal some other way.
110
109
  */
111
110
  static create<T extends new (...args: any[]) => any>(this: T, owner: IDisposableOwnerT<InstanceType<T>> | null, ...args: ConstructorParameters<T>): InstanceType<T>;
112
111
  private _disposalList;
113
112
  constructor();
114
- /** Take ownership of obj, and dispose it when this.dispose() is called. */
113
+ /** Take ownership of `obj`, and dispose it when `this.dispose()` is called. */
115
114
  autoDispose<T extends IDisposable>(obj: T): T;
116
- /** Call the given callback when this.dispose() is called. */
117
- onDispose<T>(callback: (this: T) => void, context?: T): DisposeListener;
115
+ /** Call the given callback when `this.dispose()` is called. */
116
+ onDispose<T>(callback: (this: T) => void, context?: T): IDisposable;
118
117
  /**
119
118
  * Wipe out this object when it is disposed, i.e. set all its properties to null. It is
120
119
  * recommended to call this early in the constructor.
121
120
  *
122
121
  * This makes disposal more costly, but has certain benefits:
122
+ *
123
123
  * - If anything still refers to the object and uses it, we'll get an early error, rather than
124
124
  * silently keep going, potentially doing useless work (or worse) and wasting resources.
125
+ *
125
126
  * - If anything still refers to the object (even without using it), the fields of the object
126
127
  * can still be garbage-collected.
128
+ *
127
129
  * - If there are circular references involving this object, they get broken, making the job
128
130
  * easier for the garbage collector.
129
131
  *
@@ -136,7 +138,7 @@ export declare abstract class Disposable implements IDisposable, IDisposableOwne
136
138
  */
137
139
  isDisposed(): boolean;
138
140
  /**
139
- * Clean up `this` by disposing all owned objects, and calling onDispose() callbacks, in reverse
141
+ * Clean up `this` by disposing all owned objects, and calling `onDispose()` callbacks, in reverse
140
142
  * order to that in which they were added.
141
143
  */
142
144
  dispose(): void;
@@ -148,14 +150,28 @@ export declare abstract class Disposable implements IDisposable, IDisposableOwne
148
150
  }
149
151
  /**
150
152
  * Holder keeps a single disposable object. If given responsibility for another object using
151
- * holder.autoDispose() or Foo.create(holder, ...), it automatically disposes the currently held
153
+ * `holder.autoDispose()` or `Foo.create(holder, ...)`, it automatically disposes the currently held
152
154
  * object. It also disposes it when the holder itself is disposed.
153
155
  *
154
- * If the object is an instance of Disposable, the holder will also notice when the object gets
156
+ * If the object is an instance of `Disposable`, the holder will also notice when the object gets
155
157
  * disposed from outside of it, in which case the holder will become empty again.
158
+ *
159
+ * If you need a container for multiple objects and dispose them all together, use a `MultiHolder`:
160
+ *
161
+ * :::info Example
162
+ * ```ts
163
+ * this._holder = Holder.create(this);
164
+ * Bar.create(this._holder, 1); // creates new Bar(1), assuming it's a Disposable
165
+ * Bar.create(this._holder, 2); // creates new Bar(2) and disposes previous object
166
+ * this._holder.clear(); // disposes contained object
167
+ * this._holder.release(); // releases contained object
168
+ * ```
169
+ * :::
156
170
  */
157
171
  export declare class Holder<T extends IDisposable> implements IDisposable, IDisposableOwner {
172
+ /** `Holder.create(owner)` creates a new `Holder`. */
158
173
  static create<T extends IDisposable>(owner: IDisposableOwnerT<Holder<T>> | null): Holder<T>;
174
+ /** @internal */
159
175
  protected _owned: T | null;
160
176
  private _disposalListener;
161
177
  /** Take ownership of a new object, disposing the previously held one. */
@@ -175,9 +191,18 @@ export declare class Holder<T extends IDisposable> implements IDisposable, IDisp
175
191
  private _onOutsideDispose;
176
192
  }
177
193
  /**
178
- * MultiHolder keeps multiple disposable object. It disposes all held object when the holder
179
- * itself is disposed. It's actually nothing more than the Disposable base class itself, just
194
+ * `MultiHolder` keeps multiple disposable objects. It disposes all held object when the holder
195
+ * itself is disposed. It's actually nothing more than the `Disposable` base class itself, just
180
196
  * exposed with a clearer name that describes its purpose.
197
+ *
198
+ * :::info Example
199
+ * ```ts
200
+ * this._mholder = MultiHolder.create(null);
201
+ * Bar.create(this._mholder, 1); // create new Bar(1)
202
+ * Bar.create(this._mholder, 2); // create new Bar(2)
203
+ * this._mholder.dispose(); // disposes both objects
204
+ * ```
205
+ * :::
181
206
  */
182
207
  export declare class MultiHolder extends Disposable {
183
208
  }
@@ -185,15 +210,3 @@ export declare class MultiHolder extends Disposable {
185
210
  * Sets owner of obj (i.e. calls owner.autoDispose(obj)) unless owner is null. Returns obj.
186
211
  */
187
212
  export declare function setDisposeOwner<T extends IDisposable>(owner: IDisposableOwnerT<T> | null, obj: T): T;
188
- /**
189
- * Internal class that keeps track of one item of the DisposalList. It mimicks emit.Listener, but
190
- * reports and swallows erros when it calls the callbacks in the list.
191
- */
192
- declare class DisposeListener extends LLink implements IDisposable {
193
- private callback;
194
- private context?;
195
- static callAll(begin: LLink, end: LLink, owner: Disposable): void;
196
- constructor(callback: () => void, context?: any);
197
- dispose(): void;
198
- }
199
- export {};
@@ -1,104 +1,97 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setDisposeOwner = exports.MultiHolder = exports.Holder = exports.Disposable = void 0;
4
+ const emit_1 = require("./emit");
5
+ // Internal "owner" of disposable objects which doesn't actually dispose or keep track of them. It
6
+ // is the effective owner when creating a Disposable with `new Foo()` rather than `Foo.create()`.
7
+ const _noopOwner = {
8
+ autoDispose(obj) { },
9
+ };
10
+ // Newly-created Disposable instances will have this as their owner. This is not a constant, it
11
+ // is used by create() for the safe creation of Disposables.
12
+ let _defaultDisposableOwner = _noopOwner;
2
13
  /**
3
- * dispose.js provides tools to objects that needs to dispose resources, such as destroy DOM, and
4
- * unsubscribe from events. The motivation with examples is presented here:
14
+ * Base class for disposable objects that can own other objects.
5
15
  *
6
- * https://phab.getgrist.com/w/disposal/
16
+ * For background and motivation, see [Disposables](/dispose).
7
17
  *
8
- * Disposable is a class for components that need cleanup (e.g. maintain DOM, listen to events,
9
- * subscribe to anything). It provides a .dispose() method that should be called to destroy the
10
- * component, and .onDispose()/.autoDispose() methods that the component should use to take
18
+ * `Disposable` is a class for components that need cleanup (e.g. maintain DOM, listen to events,
19
+ * subscribe to anything). It provides a `.dispose()` method that should be called to destroy the
20
+ * component, and `.onDispose()` / `.autoDispose()` methods that the component should use to take
11
21
  * responsibility for other pieces that require cleanup.
12
22
  *
13
23
  * To define a disposable class:
14
- * class Foo extends Disposable { ... }
24
+ * ```ts
25
+ * class Foo extends Disposable { ... }
26
+ * ```
15
27
  *
16
- * To create Foo:
17
- * const foo = Foo.create(owner, ...args);
28
+ * To create `Foo`:
29
+ * ```ts
30
+ * const foo = Foo.create(owner, ...args);
31
+ * ```
18
32
  * This is better than `new Foo` for two reasons:
19
- * 1. If Foo's constructor throws an exception, any disposals registered in that constructor
33
+ * 1. If `Foo`'s constructor throws an exception, any disposals registered in that constructor
20
34
  * before the exception are honored.
21
35
  * 2. It ensures you specify the owner of the new instance (but you can use null to skip it).
22
36
  *
23
- * In Foo's constructor (or rarely methods), take ownership of other Disposable objects:
24
- * this.bar = Bar.create(this, ...);
37
+ * In `Foo`'s constructor (or rarely methods), take ownership of other Disposable objects:
38
+ * ```ts
39
+ * this.bar = Bar.create(this, ...);
40
+ * ```
25
41
  *
26
42
  * For objects that are not instances of Disposable but have a .dispose() methods, use:
27
- * this.bar = this.autoDispose(createSomethingDisposable());
43
+ * ```ts
44
+ * this.bar = this.autoDispose(createSomethingDisposable());
45
+ * ```
28
46
  *
29
47
  * To call a function on disposal (e.g. to add custom disposal logic):
30
- * this.onDispose(() => this.myUnsubscribeAllMethod());
31
- * this.onDispose(this.myUnsubscribeAllMethod, this); // slightly more efficient
48
+ * ```ts
49
+ * this.onDispose(() => this.myUnsubscribeAllMethod());
50
+ * this.onDispose(this.myUnsubscribeAllMethod, this);
51
+ * ```
32
52
  *
33
53
  * To mark this object to be wiped out on disposal (i.e. set all properties to null):
34
- * this.wipeOnDispose();
54
+ * ```ts
55
+ * this.wipeOnDispose();
56
+ * ```
35
57
  * See the documentation of that method for more info.
36
58
  *
37
- * To dispose Foo directly:
38
- * foo.dispose();
39
- * To determine if an object has already been disposed:
40
- * foo.isDisposed()
59
+ * To dispose Foo directly: `foo.dispose()`.
41
60
  *
42
- * If you need to replace an owned object, or release, or dispose it early, use a Holder:
43
- * this._holder = Holder.create(this);
44
- * Bar.create(this._holder, 1); // creates new Bar(1)
45
- * Bar.create(this._holder, 2); // creates new Bar(2) and disposes previous object
46
- * this._holder.clear(); // disposes contained object
47
- * this._holder.release(); // releases contained object
61
+ * To determine if an object has already been disposed: `foo.isDisposed()`.
48
62
  *
49
- * If you need a container for multiple objects and dispose them all together, use a MultiHolder:
50
- * this._mholder = MultiHolder.create(null);
51
- * Bar.create(this._mholder, 1); // create new Bar(1)
52
- * Bar.create(this._mholder, 2); // create new Bar(2)
53
- * this._mholder.dispose(); // disposes both objects
63
+ * If you need to replace an owned object, or release, or dispose it early, use a
64
+ * [`Holder`](#Holder) or [`MultiHolder`](#MultiHolder).
54
65
  *
55
- * If creating your own class with a dispose() method, do NOT throw exceptions from dispose().
56
- * These cannot be handled properly in all cases. Read here about the same issue in C++:
57
- * http://bin-login.name/ftp/pub/docs/programming_languages/cpp/cffective_cpp/MAGAZINE/SU_FRAME.HTM#destruct
66
+ * If creating your own class with a `dispose()` method, do NOT throw exceptions from `dispose()`.
67
+ * These cannot be handled properly in all cases.
58
68
  *
59
69
  * Using a parametrized (generic) class as a Disposable is tricky. E.g.
60
- * class Bar<T> extends Disposable { ... }
61
- * // Bar<T>.create(...) <-- doesn't work
62
- * // Bar.create<T>(...) <-- doesn't work
63
- * // Bar.create(...) <-- works, but with {} for Bar's type parameters
70
+ * ```ts
71
+ * class Bar<T> extends Disposable { ... }
72
+ * // Bar<T>.create(...) <-- doesn't work
73
+ * // Bar.create<T>(...) <-- doesn't work
74
+ * // Bar.create(...) <-- works, but with {} for Bar's type parameters
75
+ * ```
64
76
  *
65
77
  * The solution is to expose the constructor type using a helper method:
66
- * class Bar<T> extends Disposable {
67
- * // Note the tuple below which must match the constructor parameters of Bar<U>.
68
- * public static ctor<U>(): IDisposableCtor<Bar<U>, [U, boolean]> { return this; }
69
- * constructor(a: T, b: boolean) { ... }
70
- * }
71
- * Bar.ctor<T>().create(...) // <-- works, creates Bar<T>, and does type-checking!
72
- */
73
- Object.defineProperty(exports, "__esModule", { value: true });
74
- const emit_1 = require("./emit");
75
- // Internal "owner" of disposable objects which doesn't actually dispose or keep track of them. It
76
- // is the effective owner when creating a Disposable with `new Foo()` rather than `Foo.create()`.
77
- const _noopOwner = {
78
- autoDispose(obj) { },
79
- };
80
- // Newly-created Disposable instances will have this as their owner. This is not a constant, it
81
- // is used by create() for the safe creation of Disposables.
82
- let _defaultDisposableOwner = _noopOwner;
83
- /**
84
- * Base class for disposable objects that can own other objects. See the module documentation.
78
+ * ```ts
79
+ * class Bar<T> extends Disposable {
80
+ * // Note the tuple below which must match the constructor parameters of Bar<U>.
81
+ * public static ctor<U>(): IDisposableCtor<Bar<U>, [U, boolean]> { return this; }
82
+ * constructor(a: T, b: boolean) { ... }
83
+ * }
84
+ * Bar.ctor<T>().create(...) // <-- works, creates Bar<T>, and does type-checking!
85
+ * ```
85
86
  */
86
87
  class Disposable {
87
- constructor() {
88
- this._disposalList = new DisposalList();
89
- // This registers with a temp Holder when using create(), and is a no-op when using `new Foo`.
90
- _defaultDisposableOwner.autoDispose(this);
91
- // Be sure to reset to no-op, so that a (non-recommended) direct call like 'new Bar()', from
92
- // inside Foo's constructor doesn't use the same Holder that's temporarily holding Foo.
93
- _defaultDisposableOwner = _noopOwner;
94
- }
95
88
  /**
96
89
  * Create Disposable instances using `Class.create(owner, ...)` rather than `new Class(...)`.
97
90
  *
98
91
  * This reminds you to provide an owner, and ensures that if the constructor throws an
99
- * exception, dispose() gets called to clean up the partially-constructed object.
92
+ * exception, `dispose()` gets called to clean up the partially-constructed object.
100
93
  *
101
- * Owner may be null if intend to ensure disposal some other way.
94
+ * Owner may be `null` if you intend to ensure disposal some other way.
102
95
  */
103
96
  static create(owner, ...args) {
104
97
  const origDefaultOwner = _defaultDisposableOwner;
@@ -126,12 +119,20 @@ class Disposable {
126
119
  _defaultDisposableOwner = origDefaultOwner;
127
120
  }
128
121
  }
129
- /** Take ownership of obj, and dispose it when this.dispose() is called. */
122
+ constructor() {
123
+ this._disposalList = new DisposalList();
124
+ // This registers with a temp Holder when using create(), and is a no-op when using `new Foo`.
125
+ _defaultDisposableOwner.autoDispose(this);
126
+ // Be sure to reset to no-op, so that a (non-recommended) direct call like 'new Bar()', from
127
+ // inside Foo's constructor doesn't use the same Holder that's temporarily holding Foo.
128
+ _defaultDisposableOwner = _noopOwner;
129
+ }
130
+ /** Take ownership of `obj`, and dispose it when `this.dispose()` is called. */
130
131
  autoDispose(obj) {
131
132
  this.onDispose(obj.dispose, obj);
132
133
  return obj;
133
134
  }
134
- /** Call the given callback when this.dispose() is called. */
135
+ /** Call the given callback when `this.dispose()` is called. */
135
136
  onDispose(callback, context) {
136
137
  return this._disposalList.addListener(callback, context);
137
138
  }
@@ -140,10 +141,13 @@ class Disposable {
140
141
  * recommended to call this early in the constructor.
141
142
  *
142
143
  * This makes disposal more costly, but has certain benefits:
144
+ *
143
145
  * - If anything still refers to the object and uses it, we'll get an early error, rather than
144
146
  * silently keep going, potentially doing useless work (or worse) and wasting resources.
147
+ *
145
148
  * - If anything still refers to the object (even without using it), the fields of the object
146
149
  * can still be garbage-collected.
150
+ *
147
151
  * - If there are circular references involving this object, they get broken, making the job
148
152
  * easier for the garbage collector.
149
153
  *
@@ -160,7 +164,7 @@ class Disposable {
160
164
  return this._disposalList === null;
161
165
  }
162
166
  /**
163
- * Clean up `this` by disposing all owned objects, and calling onDispose() callbacks, in reverse
167
+ * Clean up `this` by disposing all owned objects, and calling `onDispose()` callbacks, in reverse
164
168
  * order to that in which they were added.
165
169
  */
166
170
  dispose() {
@@ -190,17 +194,31 @@ class Disposable {
190
194
  exports.Disposable = Disposable;
191
195
  /**
192
196
  * Holder keeps a single disposable object. If given responsibility for another object using
193
- * holder.autoDispose() or Foo.create(holder, ...), it automatically disposes the currently held
197
+ * `holder.autoDispose()` or `Foo.create(holder, ...)`, it automatically disposes the currently held
194
198
  * object. It also disposes it when the holder itself is disposed.
195
199
  *
196
- * If the object is an instance of Disposable, the holder will also notice when the object gets
200
+ * If the object is an instance of `Disposable`, the holder will also notice when the object gets
197
201
  * disposed from outside of it, in which case the holder will become empty again.
202
+ *
203
+ * If you need a container for multiple objects and dispose them all together, use a `MultiHolder`:
204
+ *
205
+ * :::info Example
206
+ * ```ts
207
+ * this._holder = Holder.create(this);
208
+ * Bar.create(this._holder, 1); // creates new Bar(1), assuming it's a Disposable
209
+ * Bar.create(this._holder, 2); // creates new Bar(2) and disposes previous object
210
+ * this._holder.clear(); // disposes contained object
211
+ * this._holder.release(); // releases contained object
212
+ * ```
213
+ * :::
198
214
  */
199
215
  class Holder {
200
216
  constructor() {
217
+ /** @internal */
201
218
  this._owned = null;
202
219
  this._disposalListener = undefined;
203
220
  }
221
+ /** `Holder.create(owner)` creates a new `Holder`. */
204
222
  static create(owner) {
205
223
  return setDisposeOwner(owner, new Holder());
206
224
  }
@@ -250,9 +268,18 @@ class Holder {
250
268
  }
251
269
  exports.Holder = Holder;
252
270
  /**
253
- * MultiHolder keeps multiple disposable object. It disposes all held object when the holder
254
- * itself is disposed. It's actually nothing more than the Disposable base class itself, just
271
+ * `MultiHolder` keeps multiple disposable objects. It disposes all held object when the holder
272
+ * itself is disposed. It's actually nothing more than the `Disposable` base class itself, just
255
273
  * exposed with a clearer name that describes its purpose.
274
+ *
275
+ * :::info Example
276
+ * ```ts
277
+ * this._mholder = MultiHolder.create(null);
278
+ * Bar.create(this._mholder, 1); // create new Bar(1)
279
+ * Bar.create(this._mholder, 2); // create new Bar(2)
280
+ * this._mholder.dispose(); // disposes both objects
281
+ * ```
282
+ * :::
256
283
  */
257
284
  class MultiHolder extends Disposable {
258
285
  }
@@ -302,11 +329,6 @@ class DisposalList extends emit_1.LLink {
302
329
  * reports and swallows erros when it calls the callbacks in the list.
303
330
  */
304
331
  class DisposeListener extends emit_1.LLink {
305
- constructor(callback, context) {
306
- super();
307
- this.callback = callback;
308
- this.context = context;
309
- }
310
332
  static callAll(begin, end, owner) {
311
333
  while (begin !== end) {
312
334
  const lis = begin;
@@ -320,6 +342,11 @@ class DisposeListener extends emit_1.LLink {
320
342
  begin = lis._next;
321
343
  }
322
344
  }
345
+ constructor(callback, context) {
346
+ super();
347
+ this.callback = callback;
348
+ this.context = context;
349
+ }
323
350
  dispose() {
324
351
  if (this.isDisposed()) {
325
352
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"dispose.js","sourceRoot":"","sources":["../../../lib/dispose.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;;AAEH,iCAA6B;AAwB7B,kGAAkG;AAClG,iGAAiG;AACjG,MAAM,UAAU,GAAqB;IACnC,WAAW,CAAC,GAAgB,IAAqB,CAAC;CACnD,CAAC;AAEF,+FAA+F;AAC/F,4DAA4D;AAC5D,IAAI,uBAAuB,GAAG,UAAU,CAAC;AAWzC;;GAEG;AACH,MAAsB,UAAU;IAqC9B;QAFQ,kBAAa,GAAiB,IAAI,YAAY,EAAE,CAAC;QAGvD,8FAA8F;QAC9F,uBAAuB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QAC1C,4FAA4F;QAC5F,uFAAuF;QACvF,uBAAuB,GAAG,UAAU,CAAC;IACvC,CAAC;IA1CD;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACT,KAA8C,EAAE,GAAG,IAA8B;QAE1F,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;QACjD,MAAM,MAAM,GAAG,IAAI,MAAM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AAiB;QAC/D,OAAO,KAAK,KAAK,GAAG,EAAE;YACpB,MAAM,GAAG,GAAG,KAAwB,CAAC;YACrC,IAAI;gBACF,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;aAChC;YAAC,OAAO,CAAC,EAAE;gBACV,sCAAsC;gBACtC,OAAO,CAAC,KAAK,CAAC,4CAA4C,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;aACnG;YACD,KAAK,GAAG,GAAG,CAAC,KAAM,CAAC;SACpB;IACH,CAAC;IAED,YAAoB,QAAoB,EAAU,OAAa;QAAI,KAAK,EAAE,CAAC;QAAvD,aAAQ,GAAR,QAAQ,CAAY;QAAU,YAAO,GAAP,OAAO,CAAM;IAAa,CAAC;IAEtE,OAAO;QACZ,IAAI,IAAI,CAAC,UAAU,EAAE,EAAE;YAAE,OAAO;SAAE;QAClC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CACF"}
@@ -1,21 +1,3 @@
1
- /**
2
- * dom.js provides a way to build a DOM tree easily.
3
- *
4
- * E.g.
5
- * import {dom} from 'grainjs';
6
- * dom('a#link.c1.c2', {'href': url}, 'Hello ', dom('span', 'world'));
7
- * creates Node <a id="link" class="c1 c2" href={{url}}Hello <span>world</span></a>.
8
- *
9
- * dom.frag(dom('span', 'Hello'), ['blah', dom('div', 'world')])
10
- * creates document fragment with <span>Hello</span>blah<div>world</div>.
11
- *
12
- * DOM can also be created and modified inline during creation:
13
- * dom('a#id.c1',
14
- * dom.cls('c2'), dom.attr('href', url),
15
- * dom.text('Hello '), dom('span', dom.text('world')))
16
- * creates Node <a id="link" class="c1 c2" href={{url}}Hello <span>world</span></a>,
17
- * identical to the first example above.
18
- */
19
1
  export * from './domImpl';
20
2
  export * from './domComponent';
21
3
  export * from './domComputed';
@@ -31,6 +13,44 @@ import * as _domImpl from './domImpl';
31
13
  import * as _domMethods from './domMethods';
32
14
  import * as domevent from './domevent';
33
15
  import { IDomArgs, TagElem, TagName } from './domImpl';
16
+ /**
17
+ * `dom()` provides a way to build a DOM tree easily.
18
+ *
19
+ * The first argument is a string consisting of a lowercase tag name (e.g. `"div"`), with optional
20
+ * `"#foo"` suffix to add the ID `'foo'`, and zero or more `".bar"` suffixes to add a CSS class
21
+ * `'bar'`.
22
+ *
23
+ * The rest of the arguments are optional and may be any number, of these types:
24
+ *
25
+ * @param Nodes - become children of the created element
26
+ * @param strings - become text node children
27
+ * @param objects - Literal objects to set string attributes on the element.
28
+ * E.g. `{title: "foo"}`.
29
+ * @param null - The values `null` and `undefined` values are ignored completely.
30
+ * @param Arrays - flattened with each item processed recursively
31
+ * @param functions - called with the element being built as the argument, for a chance to modify
32
+ * the element as it's being created. Return values are processed recursively.
33
+ * @param functions - "dom methods" are a expressions such as `dom.attr('href', url)` or
34
+ * `dom.hide(obs)`, which are special cases of the "functions" category.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * import {dom} from 'grainjs';
39
+ * dom('a', {href: url, className: 'myclass'}, 'Hello ', dom('strong', 'world'));
40
+ * ```
41
+ * creates HTML element `<a href={{url}} class="myclass">Hello <strong>world</strong></a>`.
42
+ *
43
+ * @example
44
+ * Here's an example equivalent to the one above, using dom methods `dom.cls`, `dom.attr`,
45
+ * `dom.text`. In reality, these methods are useful with observable values rather than constant
46
+ * strings.
47
+ * ```ts
48
+ * dom('a', dom.attr('href', url), dom.cls('myclass'),
49
+ * dom.text('Hello '), dom('strong', dom.text('world')));
50
+ * ```
51
+ *
52
+ * @see [DOM & Observables](/basics).
53
+ */
34
54
  export declare function dom<Tag extends TagName>(tagString: Tag, ...args: IDomArgs<TagElem<Tag>>): TagElem<Tag>;
35
55
  export declare namespace dom {
36
56
  const svg: typeof _domImpl.svg;
@@ -67,7 +87,9 @@ export declare namespace dom {
67
87
  const getData: typeof _domMethods.getData;
68
88
  const replaceContent: typeof _domComputed.replaceContent;
69
89
  const domComputed: typeof _domComputed.domComputed;
90
+ const domComputedOwned: typeof _domComputed.domComputedOwned;
70
91
  const maybe: typeof _domComputed.maybe;
92
+ const maybeOwned: typeof _domComputed.maybeOwned;
71
93
  const forEach: typeof _domForEach.forEach;
72
94
  const create: typeof _domComponent.create;
73
95
  const onElem: typeof domevent.onElem;