UNPKG

3.25 kBPlain TextView Raw
1import { DEBUG } from '@glimmer/env';
2import type ApplicationInstance from '@ember/application/instance';
3import { setComponentManager } from '@ember/component';
4import { gte } from 'ember-compatibility-helpers';
5
6// Hax because the old version of `@types/ember__component` the `1.x` branch
7// uses does not provide any types for `setComponentManager` *and* because we
8// are using a very old version of `setComponentManager` for versions before
9// Ember 3.8.
10declare module '@ember/component' {
11 // The modern version.
12 export function setComponentManager<T extends object>(
13 factory: (owner: ApplicationInstance) => GlimmerComponentManager,
14 componentClass: T
15 ): T;
16
17 // The pre-3.8 version.
18 export function setComponentManager<T extends object>(name: string, componentClass: T): T;
19}
20
21import GlimmerComponentManager from './-private/ember-component-manager';
22import GlimmerComponentBase, { Args } from './-private/component';
23
24let GlimmerComponent = GlimmerComponentBase;
25
26if (DEBUG) {
27 // Add assertions against using Glimmer.js only APIs
28
29 // TODO: Add GlimmerComponent API docs link to these messages once API docs are live
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 // Methods should still throw whenever they are accessed
72 defineErrorProp(proto, 'bounds', throwPropertyUseError);
73 defineErrorProp(proto, 'element', throwPropertyUseError);
74 defineErrorProp(proto, 'debugName', throwPropertyUseError);
75}
76
77if (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
85export default GlimmerComponent;