@norith/glimmer-component 1.0.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 +42 -0
- package/addon/-private/base-component-manager.ts +35 -0
- package/addon/-private/component.ts +287 -0
- package/addon/-private/ember-component-manager.ts +99 -0
- package/addon/index.ts +34 -0
- package/config/environment.js +5 -0
- package/dist/commonjs/addon/-private/base-component-manager.d.ts +13 -0
- package/dist/commonjs/addon/-private/base-component-manager.js +20 -0
- package/dist/commonjs/addon/-private/component.d.ts +244 -0
- package/dist/commonjs/addon/-private/component.js +170 -0
- package/dist/commonjs/index.d.ts +1 -0
- package/dist/commonjs/index.js +9 -0
- package/dist/commonjs/src/component-manager.d.ts +12 -0
- package/dist/commonjs/src/component-manager.js +30 -0
- package/dist/commonjs/src/component.d.ts +4 -0
- package/dist/commonjs/src/component.js +23 -0
- package/dist/commonjs/test/index.d.ts +1 -0
- package/dist/commonjs/test/index.js +4 -0
- package/dist/commonjs/test/interactive/args-test.d.ts +1 -0
- package/dist/commonjs/test/interactive/args-test.js +47 -0
- package/dist/commonjs/test/interactive/index.d.ts +2 -0
- package/dist/commonjs/test/interactive/index.js +5 -0
- package/dist/commonjs/test/interactive/lifecycle-hook-test.d.ts +1 -0
- package/dist/commonjs/test/interactive/lifecycle-hook-test.js +92 -0
- package/dist/modules/addon/-private/base-component-manager.d.ts +13 -0
- package/dist/modules/addon/-private/base-component-manager.js +17 -0
- package/dist/modules/addon/-private/component.d.ts +244 -0
- package/dist/modules/addon/-private/component.js +167 -0
- package/dist/modules/index.d.ts +1 -0
- package/dist/modules/index.js +2 -0
- package/dist/modules/src/component-manager.d.ts +12 -0
- package/dist/modules/src/component-manager.js +24 -0
- package/dist/modules/src/component.d.ts +4 -0
- package/dist/modules/src/component.js +17 -0
- package/dist/modules/test/index.d.ts +1 -0
- package/dist/modules/test/index.js +2 -0
- package/dist/modules/test/interactive/args-test.d.ts +1 -0
- package/dist/modules/test/interactive/args-test.js +42 -0
- package/dist/modules/test/interactive/index.d.ts +2 -0
- package/dist/modules/test/interactive/index.js +3 -0
- package/dist/modules/test/interactive/lifecycle-hook-test.d.ts +1 -0
- package/dist/modules/test/interactive/lifecycle-hook-test.js +87 -0
- package/ember-addon-main.js +5 -0
- package/index.ts +1 -0
- package/package.json +115 -0
- package/src/component-manager.ts +24 -0
- package/src/component.ts +22 -0
- package/test/ember/dummy/app/app.js +14 -0
- package/test/ember/dummy/app/components/conference-speakers.js +22 -0
- package/test/ember/dummy/app/config/environment.d.ts +16 -0
- package/test/ember/dummy/app/index.html +25 -0
- package/test/ember/dummy/app/resolver.js +3 -0
- package/test/ember/dummy/app/router.js +13 -0
- package/test/ember/dummy/app/styles/app.css +0 -0
- package/test/ember/dummy/app/templates/application.hbs +3 -0
- package/test/ember/dummy/app/templates/components/conference-speakers.hbs +14 -0
- package/test/ember/dummy/app/templates/conference-speakers.hbs +1 -0
- package/test/ember/dummy/config/environment.js +51 -0
- package/test/ember/dummy/config/optional-features.json +6 -0
- package/test/ember/dummy/config/targets.js +14 -0
- package/test/ember/dummy/public/robots.txt +3 -0
- package/test/ember/index.html +40 -0
- package/test/ember/integration/components/glimmer-component-guide-test.js +42 -0
- package/test/ember/integration/components/glimmer-component-test.js +288 -0
- package/test/ember/test-helper.js +12 -0
- package/test/index.ts +1 -0
- package/test/interactive/args-test.ts +49 -0
- package/test/interactive/index.ts +2 -0
- package/test/interactive/lifecycle-hook-test.ts +99 -0
- package/types/@ember/component.d.ts +11 -0
- package/types/ember/index.d.ts +9 -0
- package/types/ember-compatibility-helpers.d.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @glimmer/component
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40glimmer%2Fcomponent)
|
|
4
|
+
[](https://github.com/glimmerjs/glimmer.js/actions?query=workflow%3ACI)
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this package to your project with Yarn:
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
yarn add @glimmer/component
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or alternatively with npm:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install --save-dev @glimmer/component
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
To use this in a Glimmer application, import the package and export an extended class:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import Component from '@glimmer/component';
|
|
26
|
+
|
|
27
|
+
export default class MyComponent extends Component {
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Contributing
|
|
32
|
+
|
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/glimmerjs/glimmer.js.
|
|
34
|
+
|
|
35
|
+
## Acknowledgements
|
|
36
|
+
|
|
37
|
+
Thanks to [Monegraph](http://monegraph.com) for funding the initial development
|
|
38
|
+
of this library.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT License.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DEBUG } from '@glimmer/env';
|
|
2
|
+
import { ComponentManager, ComponentCapabilities } from '@norith/glimmer-core';
|
|
3
|
+
import { Arguments } from '@glimmer/interfaces';
|
|
4
|
+
import BaseComponent, { ARGS_SET } from './component';
|
|
5
|
+
|
|
6
|
+
export interface Constructor<T> {
|
|
7
|
+
new (owner: unknown, args: Record<string, unknown>): T;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default abstract class BaseComponentManager<GlimmerComponent extends BaseComponent>
|
|
11
|
+
implements ComponentManager<GlimmerComponent>
|
|
12
|
+
{
|
|
13
|
+
abstract capabilities: ComponentCapabilities;
|
|
14
|
+
|
|
15
|
+
private owner: unknown;
|
|
16
|
+
|
|
17
|
+
constructor(owner: unknown) {
|
|
18
|
+
this.owner = owner;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
createComponent(
|
|
22
|
+
ComponentClass: Constructor<GlimmerComponent>,
|
|
23
|
+
args: Arguments
|
|
24
|
+
): GlimmerComponent {
|
|
25
|
+
if (DEBUG) {
|
|
26
|
+
ARGS_SET.set(args.named, true);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return new ComponentClass(this.owner, args.named);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getContext(component: GlimmerComponent): GlimmerComponent {
|
|
33
|
+
return component;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { DEBUG } from '@glimmer/env';
|
|
2
|
+
|
|
3
|
+
const DESTROYING = new WeakMap<GlimmerComponent<object>, boolean>();
|
|
4
|
+
const DESTROYED = new WeakMap<GlimmerComponent<object>, boolean>();
|
|
5
|
+
|
|
6
|
+
export function setDestroying(component: GlimmerComponent<object>): void {
|
|
7
|
+
DESTROYING.set(component, true);
|
|
8
|
+
}
|
|
9
|
+
export function setDestroyed(component: GlimmerComponent<object>): void {
|
|
10
|
+
DESTROYED.set(component, true);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// This provides a type-safe `WeakMap`: the getter and setter link the key to a
|
|
14
|
+
// specific value. This is how `WeakMap`s actually behave, but the TS type
|
|
15
|
+
// system does not (yet!) have a good way to capture that for types like
|
|
16
|
+
// `WeakMap` where the type is generic over another generic type (here, `Args`).
|
|
17
|
+
interface ArgsSetMap extends WeakMap<Args<unknown>, boolean> {
|
|
18
|
+
get<S>(key: Args<S>): boolean | undefined;
|
|
19
|
+
set<S>(key: Args<S>, value: boolean): this;
|
|
20
|
+
has<S>(key: Args<S>): boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// SAFETY: this only holds because we *only* acces this when `DEBUG` is `true`.
|
|
24
|
+
// There is not a great way to connect that data in TS at present.
|
|
25
|
+
export let ARGS_SET: ArgsSetMap;
|
|
26
|
+
|
|
27
|
+
if (DEBUG) {
|
|
28
|
+
ARGS_SET = new WeakMap() as ArgsSetMap;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// --- Type utilities for component signatures --- //
|
|
32
|
+
// Type-only "symbol" to use with `EmptyObject` below, so that it is *not*
|
|
33
|
+
// equivalent to an empty interface.
|
|
34
|
+
declare const Empty: unique symbol;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* This provides us a way to have a "fallback" which represents an empty object,
|
|
38
|
+
* without the downsides of how TS treats `{}`. Specifically: this will
|
|
39
|
+
* correctly leverage "excess property checking" so that, given a component
|
|
40
|
+
* which has no named args, if someone invokes it with any named args, they will
|
|
41
|
+
* get a type error.
|
|
42
|
+
*
|
|
43
|
+
* @internal This is exported so declaration emit works (if it were not emitted,
|
|
44
|
+
* declarations which fall back to it would not work). It is *not* intended for
|
|
45
|
+
* public usage, and the specific mechanics it uses may change at any time.
|
|
46
|
+
* The location of this export *is* part of the public API, because moving it
|
|
47
|
+
* will break existing declarations, but is not legal for end users to import
|
|
48
|
+
* themselves, so ***DO NOT RELY ON IT***.
|
|
49
|
+
*/
|
|
50
|
+
export type EmptyObject = { [Empty]?: true };
|
|
51
|
+
|
|
52
|
+
type GetOrElse<Obj, K extends PropertyKey, Fallback> = Obj extends { [Key in K]: infer U }
|
|
53
|
+
? U
|
|
54
|
+
: Fallback;
|
|
55
|
+
|
|
56
|
+
/** Given a signature `S`, get back the `Args` type. */
|
|
57
|
+
type ArgsFor<S> = S extends { Args: infer Args }
|
|
58
|
+
? Args extends { Named?: object; Positional?: unknown[] } // Are they longhand already?
|
|
59
|
+
? {
|
|
60
|
+
Named: GetOrElse<S['Args'], 'Named', EmptyObject>;
|
|
61
|
+
Positional: GetOrElse<S['Args'], 'Positional', []>;
|
|
62
|
+
}
|
|
63
|
+
: { Named: S['Args']; Positional: [] }
|
|
64
|
+
: { Named: EmptyObject; Positional: [] };
|
|
65
|
+
|
|
66
|
+
type _ExpandSignature<T> = {
|
|
67
|
+
Element: GetOrElse<T, 'Element', null>;
|
|
68
|
+
Args: keyof T extends 'Args' | 'Element' | 'Blocks' // Is this a `Signature`?
|
|
69
|
+
? ArgsFor<T> // Then use `Signature` args
|
|
70
|
+
: { Named: T; Positional: [] }; // Otherwise fall back to classic `Args`.
|
|
71
|
+
Blocks: T extends { Blocks: infer Blocks }
|
|
72
|
+
? {
|
|
73
|
+
[Block in keyof Blocks]: Blocks[Block] extends unknown[]
|
|
74
|
+
? { Params: { Positional: Blocks[Block] } }
|
|
75
|
+
: Blocks[Block];
|
|
76
|
+
}
|
|
77
|
+
: EmptyObject;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Given any allowed shorthand form of a signature, desugars it to its full
|
|
81
|
+
* expanded type.
|
|
82
|
+
*
|
|
83
|
+
* @internal This is only exported so we can avoid duplicating it in
|
|
84
|
+
* [Glint](https://github.com/typed-ember/glint) or other such tooling. It is
|
|
85
|
+
* *not* intended for public usage, and the specific mechanics it uses may
|
|
86
|
+
* change at any time. Although the signature produced by is part of Glimmer's
|
|
87
|
+
* public API the existence and mechanics of this specific symbol are *not*,
|
|
88
|
+
* so ***DO NOT RELY ON IT***.
|
|
89
|
+
*/
|
|
90
|
+
// The conditional type here is because TS applies conditional types
|
|
91
|
+
// distributively. This means that for union types, checks like `keyof T` get
|
|
92
|
+
// all the keys from all elements of the union, instead of ending up as `never`
|
|
93
|
+
// and then always falling into the `Signature` path instead of falling back to
|
|
94
|
+
// the legacy args handling path.
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
export type ExpandSignature<T> = T extends any ? _ExpandSignature<T> : never;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @internal we use this type for convenience internally; inference means users
|
|
100
|
+
* should not normally need to name it
|
|
101
|
+
*/
|
|
102
|
+
export type Args<S> = ExpandSignature<S>['Args']['Named'];
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The `Component` class defines an encapsulated UI element that is rendered to
|
|
106
|
+
* the DOM. A component is made up of a template and, optionally, this component
|
|
107
|
+
* object.
|
|
108
|
+
*
|
|
109
|
+
* ## Defining a Component
|
|
110
|
+
*
|
|
111
|
+
* To define a component, subclass `Component` and add your own properties,
|
|
112
|
+
* methods and lifecycle hooks:
|
|
113
|
+
*
|
|
114
|
+
* ```ts
|
|
115
|
+
* import Component from '@norith/glimmer-component';
|
|
116
|
+
*
|
|
117
|
+
* export default class extends Component {
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* ## Lifecycle Hooks
|
|
122
|
+
*
|
|
123
|
+
* Lifecycle hooks allow you to respond to changes to a component, such as when
|
|
124
|
+
* it gets created, rendered, updated or destroyed. To add a lifecycle hook to a
|
|
125
|
+
* component, implement the hook as a method on your component subclass.
|
|
126
|
+
*
|
|
127
|
+
* For example, to be notified when Glimmer has rendered your component so you
|
|
128
|
+
* can attach a legacy jQuery plugin, implement the `didInsertElement()` method:
|
|
129
|
+
*
|
|
130
|
+
* ```ts
|
|
131
|
+
* import Component from '@norith/glimmer-component';
|
|
132
|
+
*
|
|
133
|
+
* export default class extends Component {
|
|
134
|
+
* didInsertElement() {
|
|
135
|
+
* $(this.element).pickadate();
|
|
136
|
+
* }
|
|
137
|
+
* }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* ## Data for Templates
|
|
141
|
+
*
|
|
142
|
+
* `Component`s have two different kinds of data, or state, that can be
|
|
143
|
+
* displayed in templates:
|
|
144
|
+
*
|
|
145
|
+
* 1. Arguments
|
|
146
|
+
* 2. Properties
|
|
147
|
+
*
|
|
148
|
+
* Arguments are data that is passed in to a component from its parent
|
|
149
|
+
* component. For example, if I have a `UserGreeting` component, I can pass it
|
|
150
|
+
* a name and greeting to use:
|
|
151
|
+
*
|
|
152
|
+
* ```hbs
|
|
153
|
+
* <UserGreeting @name="Ricardo" @greeting="Olá" />
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* Inside my `UserGreeting` template, I can access the `@name` and `@greeting`
|
|
157
|
+
* arguments that I've been given:
|
|
158
|
+
*
|
|
159
|
+
* ```hbs
|
|
160
|
+
* {{@greeting}}, {{@name}}!
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* Arguments are also available inside my component:
|
|
164
|
+
*
|
|
165
|
+
* ```ts
|
|
166
|
+
* console.log(this.args.greeting); // prints "Olá"
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* Properties, on the other hand, are internal to the component and declared in
|
|
170
|
+
* the class. You can use properties to store data that you want to show in the
|
|
171
|
+
* template, or pass to another component as an argument.
|
|
172
|
+
*
|
|
173
|
+
* ```ts
|
|
174
|
+
* import Component from '@norith/glimmer-component';
|
|
175
|
+
*
|
|
176
|
+
* export default class extends Component {
|
|
177
|
+
* user = {
|
|
178
|
+
* name: 'Robbie'
|
|
179
|
+
* }
|
|
180
|
+
* }
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* In the above example, we've defined a component with a `user` property that
|
|
184
|
+
* contains an object with its own `name` property.
|
|
185
|
+
*
|
|
186
|
+
* We can render that property in our template:
|
|
187
|
+
*
|
|
188
|
+
* ```hbs
|
|
189
|
+
* Hello, {{user.name}}!
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* We can also take that property and pass it as an argument to the
|
|
193
|
+
* `UserGreeting` component we defined above:
|
|
194
|
+
*
|
|
195
|
+
* ```hbs
|
|
196
|
+
* <UserGreeting @greeting="Hello" @name={{user.name}} />
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* ## Arguments vs. Properties
|
|
200
|
+
*
|
|
201
|
+
* Remember, arguments are data that was given to your component by its parent
|
|
202
|
+
* component, and properties are data your component has defined for itself.
|
|
203
|
+
*
|
|
204
|
+
* You can tell the difference between arguments and properties in templates
|
|
205
|
+
* because arguments always start with an `@` sign (think "A is for arguments"):
|
|
206
|
+
*
|
|
207
|
+
* ```hbs
|
|
208
|
+
* {{@firstName}}
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* We know that `@firstName` came from the parent component, not the current
|
|
212
|
+
* component, because it starts with `@` and is therefore an argument.
|
|
213
|
+
*
|
|
214
|
+
* On the other hand, if we see:
|
|
215
|
+
*
|
|
216
|
+
* ```hbs
|
|
217
|
+
* {{name}}
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* We know that `name` is a property on the component. If we want to know where
|
|
221
|
+
* the data is coming from, we can go look at our component class to find out.
|
|
222
|
+
*
|
|
223
|
+
* Inside the component itself, arguments always show up inside the component's
|
|
224
|
+
* `args` property. For example, if `{{@firstName}}` is `Tom` in the template,
|
|
225
|
+
* inside the component `this.args.firstName` would also be `Tom`.
|
|
226
|
+
*/
|
|
227
|
+
export default class GlimmerComponent<S = unknown> {
|
|
228
|
+
/**
|
|
229
|
+
* Constructs a new component and assigns itself the passed properties. You
|
|
230
|
+
* should not construct new components yourself. Instead, Glimmer will
|
|
231
|
+
* instantiate new components automatically as it renders.
|
|
232
|
+
*
|
|
233
|
+
* @param owner
|
|
234
|
+
* @param args
|
|
235
|
+
*/
|
|
236
|
+
constructor(owner: unknown, args: Args<S>) {
|
|
237
|
+
if (DEBUG && !(owner !== null && typeof owner === 'object' && ARGS_SET.has(args))) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
`You must pass both the owner and args to super() in your component: ${this.constructor.name}. You can pass them directly, or use ...arguments to pass all arguments through.`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.args = args;
|
|
244
|
+
|
|
245
|
+
DESTROYING.set(this, false);
|
|
246
|
+
DESTROYED.set(this, false);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Named arguments passed to the component from its parent component.
|
|
251
|
+
* They can be accessed in JavaScript via `this.args.argumentName` and in the template via `@argumentName`.
|
|
252
|
+
*
|
|
253
|
+
* Say you have the following component, which will have two `args`, `firstName` and `lastName`:
|
|
254
|
+
*
|
|
255
|
+
* ```hbs
|
|
256
|
+
* <my-component @firstName="Arthur" @lastName="Dent" />
|
|
257
|
+
* ```
|
|
258
|
+
*
|
|
259
|
+
* If you needed to calculate `fullName` by combining both of them, you would do:
|
|
260
|
+
*
|
|
261
|
+
* ```ts
|
|
262
|
+
* didInsertElement() {
|
|
263
|
+
* console.log(`Hi, my full name is ${this.args.firstName} ${this.args.lastName}`);
|
|
264
|
+
* }
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* While in the template you could do:
|
|
268
|
+
*
|
|
269
|
+
* ```hbs
|
|
270
|
+
* <p>Welcome, {{@firstName}} {{@lastName}}!</p>
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
readonly args: Readonly<Args<S>>;
|
|
274
|
+
|
|
275
|
+
get isDestroying(): boolean {
|
|
276
|
+
return DESTROYING.get(this) || false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
get isDestroyed(): boolean {
|
|
280
|
+
return DESTROYED.get(this) || false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Called before the component has been removed from the DOM.
|
|
285
|
+
*/
|
|
286
|
+
willDestroy(): void {} // eslint-disable-line @typescript-eslint/no-empty-function
|
|
287
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { DEBUG } from '@glimmer/env';
|
|
2
|
+
import { set } from '@ember/object';
|
|
3
|
+
import { destroy } from '@ember/destroyable';
|
|
4
|
+
import { capabilities } from '@ember/component';
|
|
5
|
+
import { schedule } from '@ember/runloop';
|
|
6
|
+
import { gte } from 'ember-compatibility-helpers';
|
|
7
|
+
import BaseComponentManager from './base-component-manager';
|
|
8
|
+
|
|
9
|
+
import GlimmerComponent, { setDestroyed, setDestroying } from './component';
|
|
10
|
+
import { ComponentCapabilities } from '@norith/glimmer-core';
|
|
11
|
+
import { Arguments } from '@glimmer/interfaces';
|
|
12
|
+
|
|
13
|
+
const CAPABILITIES = gte('3.13.0-beta.1')
|
|
14
|
+
? capabilities('3.13', {
|
|
15
|
+
destructor: true,
|
|
16
|
+
asyncLifeCycleCallbacks: false,
|
|
17
|
+
updateHook: false,
|
|
18
|
+
})
|
|
19
|
+
: capabilities('3.4', {
|
|
20
|
+
destructor: true,
|
|
21
|
+
asyncLifeCycleCallbacks: false,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function scheduledDestroyComponent(component: GlimmerComponent): void {
|
|
25
|
+
if (component.isDestroyed) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
destroy(component);
|
|
30
|
+
setDestroyed(component);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* This component manager runs in Ember.js environments and extends the base component manager to:
|
|
35
|
+
*
|
|
36
|
+
* 1. Properly destroy the component's associated `meta` data structure
|
|
37
|
+
* 2. Schedule destruction using Ember's runloop
|
|
38
|
+
*/
|
|
39
|
+
class EmberGlimmerComponentManager extends BaseComponentManager<GlimmerComponent> {
|
|
40
|
+
capabilities = CAPABILITIES;
|
|
41
|
+
|
|
42
|
+
destroyComponent(component: GlimmerComponent): void {
|
|
43
|
+
if (component.isDestroying) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setDestroying(component);
|
|
48
|
+
|
|
49
|
+
schedule('actions', component, component.willDestroy);
|
|
50
|
+
schedule('destroy', this, scheduledDestroyComponent, component);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface EmberGlimmerComponentManager {
|
|
55
|
+
updateComponent?: (component: GlimmerComponent, args: Arguments) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// In Ember 3.12 and earlier, the updateComponent hook was mandatory.
|
|
59
|
+
// As of Ember 3.13, the `args` object is stable and each property of the
|
|
60
|
+
// object participates in the autotrack stack on its own. This means we do not
|
|
61
|
+
// need to set the `args` property on the component instance to invalidate
|
|
62
|
+
// tracked getters that rely on `args`, and therefore don't require the `updateComponent`
|
|
63
|
+
// hook at all.
|
|
64
|
+
if (!gte('3.13.0-beta.1')) {
|
|
65
|
+
EmberGlimmerComponentManager.prototype.updateComponent = function updateComponent(
|
|
66
|
+
component: GlimmerComponent,
|
|
67
|
+
args: Arguments
|
|
68
|
+
): void {
|
|
69
|
+
let argSnapshot = args.named;
|
|
70
|
+
|
|
71
|
+
if (DEBUG) {
|
|
72
|
+
argSnapshot = Object.freeze(argSnapshot);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
set(component, 'args', argSnapshot);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default EmberGlimmerComponentManager;
|
|
80
|
+
|
|
81
|
+
interface EmberMeta {
|
|
82
|
+
setSourceDestroying(): void;
|
|
83
|
+
setSourceDestroyed(): void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
declare module 'ember' {
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
88
|
+
export namespace Ember {
|
|
89
|
+
function destroy(obj: {}): void;
|
|
90
|
+
function meta(obj: {}): EmberMeta;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
declare module '@ember/component' {
|
|
95
|
+
export function capabilities(
|
|
96
|
+
version: '3.13' | '3.4',
|
|
97
|
+
capabilities: Partial<ComponentCapabilities>
|
|
98
|
+
): ComponentCapabilities;
|
|
99
|
+
}
|
package/addon/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DEBUG } from '@glimmer/env';
|
|
2
|
+
import ApplicationInstance from '@ember/application/instance';
|
|
3
|
+
|
|
4
|
+
// Hax because Ember does not have types for `setComponentManager`
|
|
5
|
+
declare module '@ember/component' {
|
|
6
|
+
export function setComponentManager<T extends object>(
|
|
7
|
+
factory: (owner: ApplicationInstance) => GlimmerComponentManager,
|
|
8
|
+
componentClass: T
|
|
9
|
+
): T;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
import { setComponentManager } from '@ember/component';
|
|
13
|
+
|
|
14
|
+
import GlimmerComponentManager from './-private/ember-component-manager';
|
|
15
|
+
import _GlimmerComponent, { Args } from './-private/component';
|
|
16
|
+
import { setOwner } from '@ember/application';
|
|
17
|
+
|
|
18
|
+
export default class GlimmerComponent<S = unknown> extends _GlimmerComponent<S> {
|
|
19
|
+
constructor(owner: object, args: Args<S>) {
|
|
20
|
+
super(owner, args);
|
|
21
|
+
|
|
22
|
+
if (DEBUG && !(owner !== null && typeof owner === 'object')) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`You must pass both the owner and args to super() in your component: ${this.constructor.name}. You can pass them directly, or use ...arguments to pass all arguments through.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setOwner(this, owner);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setComponentManager((owner: ApplicationInstance) => {
|
|
33
|
+
return new GlimmerComponentManager(owner);
|
|
34
|
+
}, GlimmerComponent);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ComponentManager, ComponentCapabilities } from '@norith/glimmer-core';
|
|
2
|
+
import { Arguments } from '@glimmer/interfaces';
|
|
3
|
+
import BaseComponent from './component';
|
|
4
|
+
export interface Constructor<T> {
|
|
5
|
+
new (owner: unknown, args: Record<string, unknown>): T;
|
|
6
|
+
}
|
|
7
|
+
export default abstract class BaseComponentManager<GlimmerComponent extends BaseComponent> implements ComponentManager<GlimmerComponent> {
|
|
8
|
+
abstract capabilities: ComponentCapabilities;
|
|
9
|
+
private owner;
|
|
10
|
+
constructor(owner: unknown);
|
|
11
|
+
createComponent(ComponentClass: Constructor<GlimmerComponent>, args: Arguments): GlimmerComponent;
|
|
12
|
+
getContext(component: GlimmerComponent): GlimmerComponent;
|
|
13
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const env_1 = require("@glimmer/env");
|
|
4
|
+
const component_1 = require("./component");
|
|
5
|
+
class BaseComponentManager {
|
|
6
|
+
constructor(owner) {
|
|
7
|
+
this.owner = owner;
|
|
8
|
+
}
|
|
9
|
+
createComponent(ComponentClass, args) {
|
|
10
|
+
if (env_1.DEBUG) {
|
|
11
|
+
component_1.ARGS_SET.set(args.named, true);
|
|
12
|
+
}
|
|
13
|
+
return new ComponentClass(this.owner, args.named);
|
|
14
|
+
}
|
|
15
|
+
getContext(component) {
|
|
16
|
+
return component;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.default = BaseComponentManager;
|
|
20
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZS1jb21wb25lbnQtbWFuYWdlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL0BnbGltbWVyL2NvbXBvbmVudC9hZGRvbi8tcHJpdmF0ZS9iYXNlLWNvbXBvbmVudC1tYW5hZ2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsc0NBQXFDO0FBR3JDLDJDQUFzRDtBQU10RCxNQUE4QixvQkFBb0I7SUFPaEQsWUFBWSxLQUFjO1FBQ3hCLElBQUksQ0FBQyxLQUFLLEdBQUcsS0FBSyxDQUFDO0lBQ3JCLENBQUM7SUFFRCxlQUFlLENBQ2IsY0FBNkMsRUFDN0MsSUFBZTtRQUVmLElBQUksV0FBSyxFQUFFO1lBQ1Qsb0JBQVEsQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxJQUFJLENBQUMsQ0FBQztTQUNoQztRQUVELE9BQU8sSUFBSSxjQUFjLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxJQUFJLENBQUMsS0FBSyxDQUFDLENBQUM7SUFDcEQsQ0FBQztJQUVELFVBQVUsQ0FBQyxTQUEyQjtRQUNwQyxPQUFPLFNBQVMsQ0FBQztJQUNuQixDQUFDO0NBQ0Y7QUF6QkQsdUNBeUJDIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgREVCVUcgfSBmcm9tICdAZ2xpbW1lci9lbnYnO1xuaW1wb3J0IHsgQ29tcG9uZW50TWFuYWdlciwgQ29tcG9uZW50Q2FwYWJpbGl0aWVzIH0gZnJvbSAnQG5vcml0aC9nbGltbWVyLWNvcmUnO1xuaW1wb3J0IHsgQXJndW1lbnRzIH0gZnJvbSAnQGdsaW1tZXIvaW50ZXJmYWNlcyc7XG5pbXBvcnQgQmFzZUNvbXBvbmVudCwgeyBBUkdTX1NFVCB9IGZyb20gJy4vY29tcG9uZW50JztcblxuZXhwb3J0IGludGVyZmFjZSBDb25zdHJ1Y3RvcjxUPiB7XG4gIG5ldyAob3duZXI6IHVua25vd24sIGFyZ3M6IFJlY29yZDxzdHJpbmcsIHVua25vd24+KTogVDtcbn1cblxuZXhwb3J0IGRlZmF1bHQgYWJzdHJhY3QgY2xhc3MgQmFzZUNvbXBvbmVudE1hbmFnZXI8R2xpbW1lckNvbXBvbmVudCBleHRlbmRzIEJhc2VDb21wb25lbnQ+XG4gIGltcGxlbWVudHMgQ29tcG9uZW50TWFuYWdlcjxHbGltbWVyQ29tcG9uZW50Plxue1xuICBhYnN0cmFjdCBjYXBhYmlsaXRpZXM6IENvbXBvbmVudENhcGFiaWxpdGllcztcblxuICBwcml2YXRlIG93bmVyOiB1bmtub3duO1xuXG4gIGNvbnN0cnVjdG9yKG93bmVyOiB1bmtub3duKSB7XG4gICAgdGhpcy5vd25lciA9IG93bmVyO1xuICB9XG5cbiAgY3JlYXRlQ29tcG9uZW50KFxuICAgIENvbXBvbmVudENsYXNzOiBDb25zdHJ1Y3RvcjxHbGltbWVyQ29tcG9uZW50PixcbiAgICBhcmdzOiBBcmd1bWVudHNcbiAgKTogR2xpbW1lckNvbXBvbmVudCB7XG4gICAgaWYgKERFQlVHKSB7XG4gICAgICBBUkdTX1NFVC5zZXQoYXJncy5uYW1lZCwgdHJ1ZSk7XG4gICAgfVxuXG4gICAgcmV0dXJuIG5ldyBDb21wb25lbnRDbGFzcyh0aGlzLm93bmVyLCBhcmdzLm5hbWVkKTtcbiAgfVxuXG4gIGdldENvbnRleHQoY29tcG9uZW50OiBHbGltbWVyQ29tcG9uZW50KTogR2xpbW1lckNvbXBvbmVudCB7XG4gICAgcmV0dXJuIGNvbXBvbmVudDtcbiAgfVxufVxuIl19
|