grainjs 0.1.0 → 1.0.2

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 (164) hide show
  1. package/README.md +54 -9
  2. package/dist/cjs/index.d.ts +6 -2
  3. package/dist/cjs/index.js +24 -17
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/lib/PriorityQueue.d.ts +1 -1
  6. package/dist/cjs/lib/PriorityQueue.js +1 -0
  7. package/dist/cjs/lib/PriorityQueue.js.map +1 -1
  8. package/dist/cjs/lib/_computed_queue.d.ts +18 -0
  9. package/dist/cjs/lib/_computed_queue.js +6 -1
  10. package/dist/cjs/lib/_computed_queue.js.map +1 -1
  11. package/dist/cjs/lib/binding.d.ts +16 -10
  12. package/dist/cjs/lib/binding.js +22 -27
  13. package/dist/cjs/lib/binding.js.map +1 -1
  14. package/dist/cjs/lib/browserGlobals.d.ts +4 -1
  15. package/dist/cjs/lib/browserGlobals.js +2 -0
  16. package/dist/cjs/lib/browserGlobals.js.map +1 -1
  17. package/dist/cjs/lib/computed.d.ts +11 -7
  18. package/dist/cjs/lib/computed.js +16 -0
  19. package/dist/cjs/lib/computed.js.map +1 -1
  20. package/dist/cjs/lib/dispose.d.ts +106 -14
  21. package/dist/cjs/lib/dispose.js +76 -11
  22. package/dist/cjs/lib/dispose.js.map +1 -1
  23. package/dist/cjs/lib/dom.d.ts +21 -17
  24. package/dist/cjs/lib/dom.js +33 -26
  25. package/dist/cjs/lib/dom.js.map +1 -1
  26. package/dist/cjs/lib/domComponent.d.ts +71 -0
  27. package/dist/cjs/lib/domComponent.js +15 -0
  28. package/dist/cjs/lib/domComponent.js.map +1 -0
  29. package/dist/cjs/lib/domComputed.d.ts +89 -0
  30. package/dist/cjs/lib/domComputed.js +92 -0
  31. package/dist/cjs/lib/domComputed.js.map +1 -0
  32. package/dist/cjs/lib/{_domDispose.d.ts → domDispose.d.ts} +12 -2
  33. package/dist/cjs/lib/{_domDispose.js → domDispose.js} +21 -8
  34. package/dist/cjs/lib/domDispose.js.map +1 -0
  35. package/dist/cjs/lib/{_domForEach.d.ts → domForEach.d.ts} +2 -2
  36. package/dist/cjs/lib/domForEach.js +72 -0
  37. package/dist/cjs/lib/domForEach.js.map +1 -0
  38. package/dist/cjs/lib/{_domImpl.d.ts → domImpl.d.ts} +15 -12
  39. package/dist/cjs/lib/{_domImpl.js → domImpl.js} +23 -6
  40. package/dist/cjs/lib/domImpl.js.map +1 -0
  41. package/dist/cjs/lib/{_domMethods.d.ts → domMethods.d.ts} +27 -62
  42. package/dist/cjs/lib/{_domMethods.js → domMethods.js} +21 -76
  43. package/dist/cjs/lib/domMethods.js.map +1 -0
  44. package/dist/cjs/lib/domevent.d.ts +32 -21
  45. package/dist/cjs/lib/domevent.js +33 -12
  46. package/dist/cjs/lib/domevent.js.map +1 -1
  47. package/dist/cjs/lib/emit.d.ts +25 -2
  48. package/dist/cjs/lib/emit.js +3 -1
  49. package/dist/cjs/lib/emit.js.map +1 -1
  50. package/dist/cjs/lib/kowrap.d.ts +45 -3
  51. package/dist/cjs/lib/kowrap.js +93 -10
  52. package/dist/cjs/lib/kowrap.js.map +1 -1
  53. package/dist/cjs/lib/obsArray.d.ts +8 -8
  54. package/dist/cjs/lib/obsArray.js +1 -0
  55. package/dist/cjs/lib/obsArray.js.map +1 -1
  56. package/dist/cjs/lib/observable.d.ts +6 -1
  57. package/dist/cjs/lib/observable.js +11 -2
  58. package/dist/cjs/lib/observable.js.map +1 -1
  59. package/dist/cjs/lib/pureComputed.d.ts +3 -3
  60. package/dist/cjs/lib/pureComputed.js +2 -1
  61. package/dist/cjs/lib/pureComputed.js.map +1 -1
  62. package/dist/cjs/lib/styled.d.ts +76 -11
  63. package/dist/cjs/lib/styled.js +55 -23
  64. package/dist/cjs/lib/styled.js.map +1 -1
  65. package/dist/cjs/lib/subscribe.d.ts +15 -6
  66. package/dist/cjs/lib/subscribe.js +6 -2
  67. package/dist/cjs/lib/subscribe.js.map +1 -1
  68. package/dist/cjs/lib/util.js +1 -0
  69. package/dist/cjs/lib/util.js.map +1 -1
  70. package/dist/cjs/lib/widgets/input.d.ts +2 -2
  71. package/dist/cjs/lib/widgets/input.js +2 -2
  72. package/dist/cjs/lib/widgets/input.js.map +1 -1
  73. package/dist/cjs/lib/widgets/select.d.ts +1 -1
  74. package/dist/cjs/lib/widgets/select.js +1 -0
  75. package/dist/cjs/lib/widgets/select.js.map +1 -1
  76. package/dist/esm/index.js +6 -2
  77. package/dist/esm/index.js.map +1 -1
  78. package/dist/esm/lib/PriorityQueue.js.map +1 -1
  79. package/dist/esm/lib/_computed_queue.js +5 -1
  80. package/dist/esm/lib/_computed_queue.js.map +1 -1
  81. package/dist/esm/lib/binding.js +20 -27
  82. package/dist/esm/lib/binding.js.map +1 -1
  83. package/dist/esm/lib/browserGlobals.js +1 -0
  84. package/dist/esm/lib/browserGlobals.js.map +1 -1
  85. package/dist/esm/lib/computed.js +15 -0
  86. package/dist/esm/lib/computed.js.map +1 -1
  87. package/dist/esm/lib/dispose.js +74 -11
  88. package/dist/esm/lib/dispose.js.map +1 -1
  89. package/dist/esm/lib/dom.js +21 -17
  90. package/dist/esm/lib/dom.js.map +1 -1
  91. package/dist/esm/lib/domComponent.js +11 -0
  92. package/dist/esm/lib/domComponent.js.map +1 -0
  93. package/dist/esm/lib/domComputed.js +84 -0
  94. package/dist/esm/lib/domComputed.js.map +1 -0
  95. package/dist/esm/lib/{_domDispose.js → domDispose.js} +19 -8
  96. package/dist/esm/lib/domDispose.js.map +1 -0
  97. package/dist/esm/lib/domForEach.js +68 -0
  98. package/dist/esm/lib/domForEach.js.map +1 -0
  99. package/dist/esm/lib/{_domImpl.js → domImpl.js} +20 -4
  100. package/dist/esm/lib/domImpl.js.map +1 -0
  101. package/dist/esm/lib/{_domMethods.js → domMethods.js} +8 -63
  102. package/dist/esm/lib/domMethods.js.map +1 -0
  103. package/dist/esm/lib/domevent.js +30 -11
  104. package/dist/esm/lib/domevent.js.map +1 -1
  105. package/dist/esm/lib/emit.js +2 -1
  106. package/dist/esm/lib/emit.js.map +1 -1
  107. package/dist/esm/lib/kowrap.js +90 -10
  108. package/dist/esm/lib/kowrap.js.map +1 -1
  109. package/dist/esm/lib/obsArray.js.map +1 -1
  110. package/dist/esm/lib/observable.js +9 -1
  111. package/dist/esm/lib/observable.js.map +1 -1
  112. package/dist/esm/lib/pureComputed.js +1 -1
  113. package/dist/esm/lib/pureComputed.js.map +1 -1
  114. package/dist/esm/lib/styled.js +52 -22
  115. package/dist/esm/lib/styled.js.map +1 -1
  116. package/dist/esm/lib/subscribe.js +5 -2
  117. package/dist/esm/lib/subscribe.js.map +1 -1
  118. package/dist/esm/lib/util.js.map +1 -1
  119. package/dist/esm/lib/widgets/input.js +1 -2
  120. package/dist/esm/lib/widgets/input.js.map +1 -1
  121. package/dist/esm/lib/widgets/select.js.map +1 -1
  122. package/dist/grain-full.debug.js +1627 -1222
  123. package/dist/grain-full.min.js +1 -1
  124. package/dist/grain-full.min.js.map +1 -1
  125. package/index.ts +6 -2
  126. package/lib/_computed_queue.ts +7 -1
  127. package/lib/binding.ts +33 -28
  128. package/lib/browserGlobals.ts +3 -1
  129. package/lib/computed.ts +37 -7
  130. package/lib/dispose.ts +81 -33
  131. package/lib/dom.ts +24 -18
  132. package/lib/domComponent.ts +89 -0
  133. package/lib/domComputed.ts +146 -0
  134. package/lib/{_domDispose.ts → domDispose.ts} +26 -8
  135. package/lib/{_domForEach.ts → domForEach.ts} +12 -11
  136. package/lib/{_domImpl.ts → domImpl.ts} +36 -30
  137. package/lib/{_domMethods.ts → domMethods.ts} +33 -103
  138. package/lib/domevent.ts +59 -22
  139. package/lib/emit.ts +2 -1
  140. package/lib/kowrap.ts +109 -11
  141. package/lib/obsArray.ts +2 -2
  142. package/lib/observable.ts +10 -2
  143. package/lib/pureComputed.ts +7 -6
  144. package/lib/styled.ts +65 -39
  145. package/lib/subscribe.ts +24 -8
  146. package/lib/widgets/input.ts +9 -7
  147. package/lib/widgets/select.ts +3 -3
  148. package/package.json +41 -42
  149. package/dist/cjs/lib/_domComponent.d.ts +0 -84
  150. package/dist/cjs/lib/_domComponent.js +0 -160
  151. package/dist/cjs/lib/_domComponent.js.map +0 -1
  152. package/dist/cjs/lib/_domDispose.js.map +0 -1
  153. package/dist/cjs/lib/_domForEach.js +0 -71
  154. package/dist/cjs/lib/_domForEach.js.map +0 -1
  155. package/dist/cjs/lib/_domImpl.js.map +0 -1
  156. package/dist/cjs/lib/_domMethods.js.map +0 -1
  157. package/dist/esm/lib/_domComponent.js +0 -155
  158. package/dist/esm/lib/_domComponent.js.map +0 -1
  159. package/dist/esm/lib/_domDispose.js.map +0 -1
  160. package/dist/esm/lib/_domForEach.js +0 -68
  161. package/dist/esm/lib/_domForEach.js.map +0 -1
  162. package/dist/esm/lib/_domImpl.js.map +0 -1
  163. package/dist/esm/lib/_domMethods.js.map +0 -1
  164. package/lib/_domComponent.ts +0 -167
@@ -1,3 +1,75 @@
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';
1
73
  /**
2
74
  * Anything with a .dispose() method is a disposable object, and implements the IDisposable interface.
3
75
  */
@@ -17,6 +89,13 @@ export interface IDisposableOwnerT<T extends IDisposable> {
17
89
  export interface IDisposableOwner {
18
90
  autoDispose(obj: IDisposable): void;
19
91
  }
92
+ /**
93
+ * The static portion of class Disposable.
94
+ */
95
+ export interface IDisposableCtor<Derived, CtorArgs extends any[]> {
96
+ new (...args: CtorArgs): Derived;
97
+ create<T extends new (...args: any[]) => any>(this: T, owner: IDisposableOwnerT<InstanceType<T>> | null, ...args: ConstructorParameters<T>): InstanceType<T>;
98
+ }
20
99
  /**
21
100
  * Base class for disposable objects that can own other objects. See the module documentation.
22
101
  */
@@ -28,22 +107,14 @@ export declare abstract class Disposable implements IDisposable, IDisposableOwne
28
107
  * exception, dispose() gets called to clean up the partially-constructed object.
29
108
  *
30
109
  * Owner may be null if intend to ensure disposal some other way.
31
- *
32
- * TODO: create() needs more unittests, including to ensure that TypeScript types are done
33
- * correctly.
34
110
  */
35
- static create<T extends IDisposable>(this: new () => T, owner: IDisposableOwnerT<T> | null): T;
36
- static create<T extends IDisposable, A>(this: new (a: A) => T, owner: IDisposableOwnerT<T> | null, a: A): T;
37
- static create<T extends IDisposable, A, B>(this: new (a: A, b: B) => T, owner: IDisposableOwnerT<T> | null, a: A, b: B): T;
38
- static create<T extends IDisposable, A, B, C>(this: new (a: A, b: B, c: C) => T, owner: IDisposableOwnerT<T> | null, a: A, b: B, c: C): T;
39
- static create<T extends IDisposable, A, B, C, D>(this: new (a: A, b: B, c: C, d: D) => T, owner: IDisposableOwnerT<T> | null, a: A, b: B, c: C, d: D): T;
40
- static create<T extends IDisposable, A, B, C, D, E>(this: new (a: A, b: B, c: C, d: D, e: E) => T, owner: IDisposableOwnerT<T> | null, a: A, b: B, c: C, d: D, e: E): T;
111
+ static create<T extends new (...args: any[]) => any>(this: T, owner: IDisposableOwnerT<InstanceType<T>> | null, ...args: ConstructorParameters<T>): InstanceType<T>;
41
112
  private _disposalList;
42
113
  constructor();
43
114
  /** Take ownership of obj, and dispose it when this.dispose() is called. */
44
115
  autoDispose<T extends IDisposable>(obj: T): T;
45
116
  /** Call the given callback when this.dispose() is called. */
46
- onDispose<T>(callback: (this: T) => void, context?: T): void;
117
+ onDispose<T>(callback: (this: T) => void, context?: T): DisposeListener;
47
118
  /**
48
119
  * Wipe out this object when it is disposed, i.e. set all its properties to null. It is
49
120
  * recommended to call this early in the constructor.
@@ -73,7 +144,7 @@ export declare abstract class Disposable implements IDisposable, IDisposableOwne
73
144
  * Wipe out this object by setting each property to null. This is helpful for objects that are
74
145
  * disposed and should be ready to be garbage-collected.
75
146
  */
76
- private _wipeOutObject();
147
+ private _wipeOutObject;
77
148
  }
78
149
  /**
79
150
  * Holder keeps a single disposable object. If given responsibility for another object using
@@ -82,12 +153,11 @@ export declare abstract class Disposable implements IDisposable, IDisposableOwne
82
153
  *
83
154
  * If the object is an instance of Disposable, the holder will also notice when the object gets
84
155
  * disposed from outside of it, in which case the holder will become empty again.
85
- *
86
- * TODO Holder needs unittests.
87
156
  */
88
157
  export declare class Holder<T extends IDisposable> implements IDisposable, IDisposableOwner {
89
- static create<T extends IDisposable>(owner: IDisposableOwner | null): Holder<T>;
158
+ static create<T extends IDisposable>(owner: IDisposableOwnerT<Holder<T>> | null): Holder<T>;
90
159
  protected _owned: T | null;
160
+ private _disposalListener;
91
161
  /** Take ownership of a new object, disposing the previously held one. */
92
162
  autoDispose(obj: T): T;
93
163
  /** Releases the held object without disposing it, emptying the holder. */
@@ -100,8 +170,30 @@ export declare class Holder<T extends IDisposable> implements IDisposable, IDisp
100
170
  isEmpty(): boolean;
101
171
  /** When the holder is disposed, it disposes the held object if any. */
102
172
  dispose(): void;
173
+ /** Stop listening for the disposal of this._owned. */
174
+ private _unlisten;
175
+ private _onOutsideDispose;
176
+ }
177
+ /**
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
180
+ * exposed with a clearer name that describes its purpose.
181
+ */
182
+ export declare class MultiHolder extends Disposable {
103
183
  }
104
184
  /**
105
185
  * Sets owner of obj (i.e. calls owner.autoDispose(obj)) unless owner is null. Returns obj.
106
186
  */
107
187
  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 {};
@@ -46,11 +46,32 @@
46
46
  * this._holder.clear(); // disposes contained object
47
47
  * this._holder.release(); // releases contained object
48
48
  *
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
54
+ *
49
55
  * If creating your own class with a dispose() method, do NOT throw exceptions from dispose().
50
56
  * These cannot be handled properly in all cases. Read here about the same issue in C++:
51
57
  * http://bin-login.name/ftp/pub/docs/programming_languages/cpp/cffective_cpp/MAGAZINE/SU_FRAME.HTM#destruct
58
+ *
59
+ * 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
64
+ *
65
+ * 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!
52
72
  */
53
73
  Object.defineProperty(exports, "__esModule", { value: true });
74
+ exports.setDisposeOwner = exports.MultiHolder = exports.Holder = exports.Disposable = void 0;
54
75
  const emit_1 = require("./emit");
55
76
  // Internal "owner" of disposable objects which doesn't actually dispose or keep track of them. It
56
77
  // is the effective owner when creating a Disposable with `new Foo()` rather than `Foo.create()`.
@@ -68,7 +89,18 @@ class Disposable {
68
89
  this._disposalList = new DisposalList();
69
90
  // This registers with a temp Holder when using create(), and is a no-op when using `new Foo`.
70
91
  _defaultDisposableOwner.autoDispose(this);
92
+ // Be sure to reset to no-op, so that a (non-recommended) direct call like 'new Bar()', from
93
+ // inside Foo's constructor doesn't use the same Holder that's temporarily holding Foo.
94
+ _defaultDisposableOwner = _noopOwner;
71
95
  }
96
+ /**
97
+ * Create Disposable instances using `Class.create(owner, ...)` rather than `new Class(...)`.
98
+ *
99
+ * This reminds you to provide an owner, and ensures that if the constructor throws an
100
+ * exception, dispose() gets called to clean up the partially-constructed object.
101
+ *
102
+ * Owner may be null if intend to ensure disposal some other way.
103
+ */
72
104
  static create(owner, ...args) {
73
105
  const origDefaultOwner = _defaultDisposableOwner;
74
106
  const holder = new Holder();
@@ -102,7 +134,7 @@ class Disposable {
102
134
  }
103
135
  /** Call the given callback when this.dispose() is called. */
104
136
  onDispose(callback, context) {
105
- this._disposalList.addListener(callback, context);
137
+ return this._disposalList.addListener(callback, context);
106
138
  }
107
139
  /**
108
140
  * Wipe out this object when it is disposed, i.e. set all its properties to null. It is
@@ -134,8 +166,14 @@ class Disposable {
134
166
  */
135
167
  dispose() {
136
168
  const disposalList = this._disposalList;
137
- this._disposalList = null;
138
- disposalList.callAndDispose(this);
169
+ if (!disposalList) {
170
+ // tslint:disable-next-line:no-console
171
+ console.error("Error disposing %s which is already disposed", _describe(this));
172
+ }
173
+ else {
174
+ this._disposalList = null;
175
+ disposalList.callAndDispose(this);
176
+ }
139
177
  }
140
178
  /**
141
179
  * Wipe out this object by setting each property to null. This is helpful for objects that are
@@ -158,38 +196,38 @@ exports.Disposable = Disposable;
158
196
  *
159
197
  * If the object is an instance of Disposable, the holder will also notice when the object gets
160
198
  * disposed from outside of it, in which case the holder will become empty again.
161
- *
162
- * TODO Holder needs unittests.
163
199
  */
164
200
  class Holder {
165
201
  constructor() {
166
202
  this._owned = null;
203
+ this._disposalListener = undefined;
167
204
  }
168
205
  static create(owner) {
169
206
  return setDisposeOwner(owner, new Holder());
170
207
  }
171
208
  /** Take ownership of a new object, disposing the previously held one. */
172
209
  autoDispose(obj) {
173
- if (this._owned) {
174
- this._owned.dispose();
175
- }
210
+ this.clear();
176
211
  this._owned = obj;
177
212
  if (obj instanceof Disposable) {
178
- obj.onDispose(this.release, this);
213
+ this._disposalListener = obj.onDispose(this._onOutsideDispose, this);
179
214
  }
180
215
  return obj;
181
216
  }
182
217
  /** Releases the held object without disposing it, emptying the holder. */
183
218
  release() {
219
+ this._unlisten();
184
220
  const ret = this._owned;
185
221
  this._owned = null;
186
222
  return ret;
187
223
  }
188
224
  /** Disposes the held object and empties the holder. */
189
225
  clear() {
190
- if (this._owned) {
191
- this._owned.dispose();
226
+ this._unlisten();
227
+ const owned = this._owned;
228
+ if (owned) {
192
229
  this._owned = null;
230
+ owned.dispose();
193
231
  }
194
232
  }
195
233
  /** Returns the held object, or null if the Holder is empty. */
@@ -198,8 +236,28 @@ class Holder {
198
236
  isEmpty() { return !this._owned; }
199
237
  /** When the holder is disposed, it disposes the held object if any. */
200
238
  dispose() { this.clear(); }
239
+ /** Stop listening for the disposal of this._owned. */
240
+ _unlisten() {
241
+ const disposalListener = this._disposalListener;
242
+ if (disposalListener) {
243
+ this._disposalListener = undefined;
244
+ disposalListener.dispose();
245
+ }
246
+ }
247
+ _onOutsideDispose() {
248
+ this._disposalListener = undefined;
249
+ this._owned = null;
250
+ }
201
251
  }
202
252
  exports.Holder = Holder;
253
+ /**
254
+ * MultiHolder keeps multiple disposable object. It disposes all held object when the holder
255
+ * itself is disposed. It's actually nothing more than the Disposable base class itself, just
256
+ * exposed with a clearer name that describes its purpose.
257
+ */
258
+ class MultiHolder extends Disposable {
259
+ }
260
+ exports.MultiHolder = MultiHolder;
203
261
  /**
204
262
  * Sets owner of obj (i.e. calls owner.autoDispose(obj)) unless owner is null. Returns obj.
205
263
  */
@@ -225,6 +283,7 @@ class DisposalList extends emit_1.LLink {
225
283
  addListener(callback, optContext) {
226
284
  const lis = new DisposeListener(callback, optContext);
227
285
  this._insertBefore(this._next, lis);
286
+ return lis;
228
287
  }
229
288
  /**
230
289
  * Call all callbacks and dispose this object. The owner is required for better reporting of
@@ -262,5 +321,11 @@ class DisposeListener extends emit_1.LLink {
262
321
  begin = lis._next;
263
322
  }
264
323
  }
324
+ dispose() {
325
+ if (this.isDisposed()) {
326
+ return;
327
+ }
328
+ this._removeNode(this);
329
+ }
265
330
  }
266
331
  //# sourceMappingURL=dispose.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dispose.js","sourceRoot":"","sources":["../../../lib/dispose.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;;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;AAEzC;;GAEG;AACH;IAuDE;QAFQ,kBAAa,GAAiB,IAAI,YAAY,EAAE,CAAC;QAGvD,8FAA8F;QAC9F,uBAAuB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IA/BM,MAAM,CAAC,MAAM,CACe,KAAgC,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AAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QACtD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,KAAM,EAAE,GAAG,CAAC,CAAC;QACrC,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,cAAc,CAAC,KAAiB;QACrC,IAAI;YACF,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,KAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;SACnD;gBAAS;YACR,IAAI,CAAC,YAAY,EAAE,CAAC;SACrB;IACH,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,eAAgB,SAAQ,YAAK;IAcjC,YAAoB,QAAoB,EAAU,OAAa;QAAI,KAAK,EAAE,CAAC;QAAvD,aAAQ,GAAR,QAAQ,CAAY;QAAU,YAAO,GAAP,OAAO,CAAM;IAAa,CAAC;IAbtE,MAAM,CAAC,OAAO,CAAC,KAAY,EAAE,GAAU,EAAE,KAAiB;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;IAIM,OAAO;QACZ,IAAI,IAAI,CAAC,UAAU,EAAE,EAAE;YAAE,OAAO;SAAE;QAClC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CACF"}
@@ -16,19 +16,22 @@
16
16
  * creates Node <a id="link" class="c1 c2" href={{url}}Hello <span>world</span></a>,
17
17
  * identical to the first example above.
18
18
  */
19
- export { DomMethod, DomElementMethod, DomArg, DomElementArg, svg, update, frag, find, findAll } from './_domImpl';
20
- export * from './_domComponent';
21
- export * from './_domDispose';
22
- export * from './_domForEach';
23
- export * from './_domMethods';
19
+ export * from './domImpl';
20
+ export * from './domComponent';
21
+ export * from './domComputed';
22
+ export * from './domDispose';
23
+ export * from './domForEach';
24
+ export * from './domMethods';
24
25
  export * from './domevent';
25
- import * as _domComponent from './_domComponent';
26
- import * as _domDispose from './_domDispose';
27
- import * as _domForEach from './_domForEach';
28
- import * as _domImpl from './_domImpl';
29
- import * as _domMethods from './_domMethods';
26
+ import * as _domComponent from './domComponent';
27
+ import * as _domComputed from './domComputed';
28
+ import * as _domDispose from './domDispose';
29
+ import * as _domForEach from './domForEach';
30
+ import * as _domImpl from './domImpl';
31
+ import * as _domMethods from './domMethods';
30
32
  import * as domevent from './domevent';
31
- export declare function dom(tagString: string, ...args: _domImpl.DomElementArg[]): HTMLElement;
33
+ import { IDomArgs, TagElem, TagName } from './domImpl';
34
+ export declare function dom<Tag extends TagName>(tagString: Tag, ...args: IDomArgs<TagElem<Tag>>): TagElem<Tag>;
32
35
  export declare namespace dom {
33
36
  const svg: typeof _domImpl.svg;
34
37
  const frag: typeof _domImpl.frag;
@@ -62,17 +65,18 @@ export declare namespace dom {
62
65
  const dataElem: typeof _domMethods.dataElem;
63
66
  const data: typeof _domMethods.data;
64
67
  const getData: typeof _domMethods.getData;
65
- const replaceContent: typeof _domMethods.replaceContent;
66
- const domComputed: typeof _domMethods.domComputed;
67
- const maybe: typeof _domMethods.maybe;
68
+ const replaceContent: typeof _domComputed.replaceContent;
69
+ const domComputed: typeof _domComputed.domComputed;
70
+ const domComputedOwned: typeof _domComputed.domComputedOwned;
71
+ const maybe: typeof _domComputed.maybe;
72
+ const maybeOwned: typeof _domComputed.maybeOwned;
68
73
  const forEach: typeof _domForEach.forEach;
69
- const Component: typeof _domComponent.Component;
70
74
  const create: typeof _domComponent.create;
71
- const createInit: typeof _domComponent.createInit;
72
75
  const onElem: typeof domevent.onElem;
73
76
  const on: typeof domevent.on;
74
77
  const onMatchElem: typeof domevent.onMatchElem;
75
78
  const onMatch: typeof domevent.onMatch;
76
- const onKeyPressElem: typeof domevent.onKeyPressElem;
79
+ const onKeyElem: typeof domevent.onKeyElem;
77
80
  const onKeyPress: typeof domevent.onKeyPress;
81
+ const onKeyDown: typeof domevent.onKeyDown;
78
82
  }
@@ -17,32 +17,38 @@
17
17
  * creates Node <a id="link" class="c1 c2" href={{url}}Hello <span>world</span></a>,
18
18
  * identical to the first example above.
19
19
  */
20
- function __export(m) {
21
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
22
- }
20
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
23
+ }) : (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ o[k2] = m[k];
26
+ }));
27
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
28
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
29
+ };
23
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.dom = void 0;
24
32
  // We keep various dom-related functions organized in private modules, but they are exposed here.
25
- var _domImpl_1 = require("./_domImpl");
26
- exports.svg = _domImpl_1.svg;
27
- exports.update = _domImpl_1.update;
28
- exports.frag = _domImpl_1.frag;
29
- exports.find = _domImpl_1.find;
30
- exports.findAll = _domImpl_1.findAll;
31
- __export(require("./_domComponent"));
32
- __export(require("./_domDispose"));
33
- __export(require("./_domForEach"));
34
- __export(require("./_domMethods"));
35
- __export(require("./domevent"));
36
- const _domComponent = require("./_domComponent");
37
- const _domDispose = require("./_domDispose");
38
- const _domForEach = require("./_domForEach");
39
- const _domImpl = require("./_domImpl");
40
- const _domMethods = require("./_domMethods");
33
+ __exportStar(require("./domImpl"), exports);
34
+ __exportStar(require("./domComponent"), exports);
35
+ __exportStar(require("./domComputed"), exports);
36
+ __exportStar(require("./domDispose"), exports);
37
+ __exportStar(require("./domForEach"), exports);
38
+ __exportStar(require("./domMethods"), exports);
39
+ __exportStar(require("./domevent"), exports);
40
+ const _domComponent = require("./domComponent");
41
+ const _domComputed = require("./domComputed");
42
+ const _domDispose = require("./domDispose");
43
+ const _domForEach = require("./domForEach");
44
+ const _domImpl = require("./domImpl");
45
+ const _domMethods = require("./domMethods");
41
46
  const domevent = require("./domevent");
47
+ const domImpl_1 = require("./domImpl");
42
48
  // We just want to re-export _domImpl.dom, but to allow adding methods to it in a typesafe way,
43
49
  // TypeScript wants us to declare a real function in the same file.
44
50
  function dom(tagString, ...args) {
45
- return _domImpl.dom(tagString, ...args);
51
+ return domImpl_1.dom(tagString, ...args);
46
52
  }
47
53
  exports.dom = dom;
48
54
  // Additionally export all methods as properties of dom() function.
@@ -79,18 +85,19 @@ exports.dom = dom;
79
85
  dom.dataElem = _domMethods.dataElem;
80
86
  dom.data = _domMethods.data;
81
87
  dom.getData = _domMethods.getData;
82
- dom.replaceContent = _domMethods.replaceContent;
83
- dom.domComputed = _domMethods.domComputed;
84
- dom.maybe = _domMethods.maybe;
88
+ dom.replaceContent = _domComputed.replaceContent;
89
+ dom.domComputed = _domComputed.domComputed;
90
+ dom.domComputedOwned = _domComputed.domComputedOwned;
91
+ dom.maybe = _domComputed.maybe;
92
+ dom.maybeOwned = _domComputed.maybeOwned;
85
93
  dom.forEach = _domForEach.forEach;
86
- dom.Component = _domComponent.Component;
87
94
  dom.create = _domComponent.create;
88
- dom.createInit = _domComponent.createInit;
89
95
  dom.onElem = domevent.onElem;
90
96
  dom.on = domevent.on;
91
97
  dom.onMatchElem = domevent.onMatchElem;
92
98
  dom.onMatch = domevent.onMatch;
93
- dom.onKeyPressElem = domevent.onKeyPressElem;
99
+ dom.onKeyElem = domevent.onKeyElem;
94
100
  dom.onKeyPress = domevent.onKeyPress;
101
+ dom.onKeyDown = domevent.onKeyDown;
95
102
  })(dom = exports.dom || (exports.dom = {}));
96
103
  //# sourceMappingURL=dom.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dom.js","sourceRoot":"","sources":["../../../lib/dom.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;;;AAEH,iGAAiG;AACjG,uCAAgH;AAApD,yBAAA,GAAG,CAAA;AAAE,4BAAA,MAAM,CAAA;AAAE,0BAAA,IAAI,CAAA;AAAE,0BAAA,IAAI,CAAA;AAAE,6BAAA,OAAO,CAAA;AAC5F,qCAAgC;AAChC,mCAA8B;AAC9B,mCAA8B;AAC9B,mCAA8B;AAC9B,gCAA2B;AAE3B,iDAAiD;AACjD,6CAA6C;AAC7C,6CAA6C;AAC7C,uCAAuC;AACvC,6CAA6C;AAC7C,uCAAuC;AAEvC,+FAA+F;AAC/F,mEAAmE;AACnE,aAAoB,SAAiB,EAAE,GAAG,IAA8B;IACtE,OAAO,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;AAC1C,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;IAC5C,aAAS,GAAS,WAAW,CAAC,SAAS,CAAC;IACxC,mBAAe,GAAG,WAAW,CAAC,eAAe,CAAC;IAC9C,eAAW,GAAO,WAAW,CAAC,WAAW,CAAC;IAE1C,aAAS,GAAS,WAAW,CAAC,SAAS,CAAC;IACxC,SAAK,GAAa,WAAW,CAAC,KAAK,CAAC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C;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"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * UI components that can be inserted into dom().
3
+ *
4
+ * Components are created and inserted using dom.create():
5
+ *
6
+ * dom('div',
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()
12
+ * static method, in which case that's what gets called.
13
+ *
14
+ * 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
16
+ * naturally with any class that derives from Disposable, since it then has a suitable static
17
+ * create() method.
18
+ *
19
+ * 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
28
+ * the component is attached. When the parent DOM element is disposed, so is the component.
29
+ *
30
+ * [Explanation] To understand why the syntax is such, consider a potential alternative such as:
31
+ *
32
+ * dom('div', _insert_(new Comp1()), _insert_(new Comp2(...args)))
33
+ *
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.
38
+ *
39
+ * 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
41
+ * the DOM. Note that buildDom is only called once.
42
+ *
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))
47
+ *
48
+ * Note that ownership should be handled using the `owner` argument. Don't do this:
49
+ *
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
55
+ * disposed when the containing element is disposed, followed by the `owner` itself.
56
+ */
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
+ export declare function create<Fn extends IDomCreator<any[]>>(fn: Fn, ...args: DomCreatorArgs<Fn>): DomContents;
71
+ export {};
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = void 0;
4
+ const domComputed_1 = require("./domComputed");
5
+ function create(fn, ...args) {
6
+ return domComputed_1.domComputedOwned(null, (owner) => {
7
+ const value = ('create' in fn) ?
8
+ fn.create(owner, ...args) :
9
+ fn(owner, ...args);
10
+ return (value && typeof value === 'object' && 'buildDom' in value) ?
11
+ value.buildDom() : value;
12
+ });
13
+ }
14
+ exports.create = create;
15
+ //# sourceMappingURL=domComponent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domComponent.js","sourceRoot":"","sources":["../../../lib/domComponent.ts"],"names":[],"mappings":";;;AAyDA,+CAA4D;AAuB5D,SAAgB,MAAM,CAAgC,EAAM,EAAE,GAAG,IAAwB;IACvF,OAAO,8BAAgB,CAAC,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"}