1 | import { DEBUG } from '@glimmer/env';
|
2 | import type ApplicationInstance from '@ember/application/instance';
|
3 | import { setComponentManager } from '@ember/component';
|
4 | import { gte } from 'ember-compatibility-helpers';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | declare module '@ember/component' {
|
11 |
|
12 | export function setComponentManager<T extends object>(
|
13 | factory: (owner: ApplicationInstance) => GlimmerComponentManager,
|
14 | componentClass: T
|
15 | ): T;
|
16 |
|
17 |
|
18 | export function setComponentManager<T extends object>(name: string, componentClass: T): T;
|
19 | }
|
20 |
|
21 | import GlimmerComponentManager from './-private/ember-component-manager';
|
22 | import GlimmerComponentBase, { Args } from './-private/component';
|
23 |
|
24 | let GlimmerComponent = GlimmerComponentBase;
|
25 |
|
26 | if (DEBUG) {
|
27 |
|
28 |
|
29 |
|
30 | function throwMethodUseError(methodName: string) {
|
31 | throw new Error(
|
32 | `You attempted to define the '${methodName}' method on a Glimmer Component, but that lifecycle hook does not exist in Ember.js applications, it only exists in Glimmer.js apps. You can rename this method, and you can trigger it using a modifier such as {{did-insert}} from '@ember/render-modifiers': https://github.com/emberjs/ember-render-modifiers.`
|
33 | );
|
34 | }
|
35 |
|
36 | function throwPropertyUseError(propertyName: string) {
|
37 | throw new Error(
|
38 | `You attempted to access the '${propertyName}' property on a Glimmer Component, but that property does not exist in Ember.js applications, it only exists in Glimmer.js apps. You define a class field with the same name on your component class and it will overwrite this error message, but it will not be used by the framework.`
|
39 | );
|
40 | }
|
41 |
|
42 | GlimmerComponent = class GlimmerDebugComponent<S = unknown> extends GlimmerComponent<S> {
|
43 | constructor(owner: unknown, args: Args<S>) {
|
44 | super(owner, args);
|
45 |
|
46 | if (typeof this['didInsertElement'] === 'function') {
|
47 | throwMethodUseError('didInsertElement');
|
48 | }
|
49 |
|
50 | if (typeof this['didUpdate'] === 'function') {
|
51 | throwMethodUseError('didUpdate');
|
52 | }
|
53 | }
|
54 | };
|
55 |
|
56 | let proto = GlimmerComponent.prototype;
|
57 |
|
58 | function defineErrorProp(
|
59 | proto: GlimmerComponentBase,
|
60 | key: string,
|
61 | getterMethod: (key: string) => unknown
|
62 | ) {
|
63 | Object.defineProperty(proto, key, {
|
64 | get: () => getterMethod(key),
|
65 | set(value) {
|
66 | Object.defineProperty(this, key, { value });
|
67 | },
|
68 | });
|
69 | }
|
70 |
|
71 |
|
72 | defineErrorProp(proto, 'bounds', throwPropertyUseError);
|
73 | defineErrorProp(proto, 'element', throwPropertyUseError);
|
74 | defineErrorProp(proto, 'debugName', throwPropertyUseError);
|
75 | }
|
76 |
|
77 | if (gte('3.8.0-beta.1')) {
|
78 | setComponentManager((owner: ApplicationInstance) => {
|
79 | return new GlimmerComponentManager(owner);
|
80 | }, GlimmerComponent);
|
81 | } else {
|
82 | setComponentManager('glimmer', GlimmerComponent);
|
83 | }
|
84 |
|
85 | export default GlimmerComponent;
|