|
| 1 | +import { getOwner, setOwner } from '@ember/application'; |
| 2 | +import { assert } from '@ember/debug'; |
| 3 | +import { associateDestroyableChild } from '@ember/destroyable'; |
| 4 | + |
| 5 | +import type { Class, Stage1Decorator, Stage1DecoratorDescriptor } from '[core-types]'; |
| 6 | + |
| 7 | +type NonKey<K> = K extends string ? never : K extends symbol ? never : K; |
| 8 | + |
| 9 | +/** |
| 10 | + * A util to abstract away the boilerplate of linking of "things" with an owner |
| 11 | + * and making them destroyable. |
| 12 | + * |
| 13 | + * ```js |
| 14 | + * import Component from '@glimmer/component'; |
| 15 | + * import { link } from 'ember-resources/link'; |
| 16 | + * |
| 17 | + * class MyClass { ... } |
| 18 | + * |
| 19 | + * export default class Demo extends Component { |
| 20 | + * @link(MyClass) myInstance; |
| 21 | + * } |
| 22 | + * ``` |
| 23 | + */ |
| 24 | +export function link<Instance>(child: Class<Instance>): Stage1Decorator; |
| 25 | +/** |
| 26 | + * A util to abstract away the boilerplate of linking of "things" with an owner |
| 27 | + * and making them destroyable. |
| 28 | + * |
| 29 | + * ```js |
| 30 | + * import Component from '@glimmer/component'; |
| 31 | + * import { cached } from '@glimmer/tracking'; |
| 32 | + * import { link } from 'ember-resources/link'; |
| 33 | + * |
| 34 | + * export default class Demo extends Component { |
| 35 | + * @cached |
| 36 | + * get myFunction() { |
| 37 | + * let instance = new MyClass(this.args.foo); |
| 38 | + * |
| 39 | + * return link(instance, this); |
| 40 | + * } |
| 41 | + * } |
| 42 | + * ``` |
| 43 | + * |
| 44 | + * NOTE: If args change, as in this example, memory pressure will increase, |
| 45 | + * as the linked instance will be held on to until the host object is destroyed. |
| 46 | + */ |
| 47 | +export function link<Child, Other>(child: Child, parent: NonKey<Other>): Child; |
| 48 | + |
| 49 | +/** |
| 50 | + * A util to abstract away the boilerplate of linking of "things" with an owner |
| 51 | + * and making them destroyable. |
| 52 | + * |
| 53 | + * ```js |
| 54 | + * import Component from '@glimmer/component'; |
| 55 | + * import { link } from 'ember-resources/link'; |
| 56 | + * |
| 57 | + * class MyClass { ... } |
| 58 | + * |
| 59 | + * export default class Demo extends Component { |
| 60 | + * @link myInstance = new MyClass(); |
| 61 | + * } |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * NOTE: reactive args may not be passed to `MyClass` directly if you wish updates to be observed. |
| 65 | + * A way to use reactive args is this: |
| 66 | + * |
| 67 | + * ```js |
| 68 | + * import Component from '@glimmer/component'; |
| 69 | + * import { tracked } from '@glimmer/tracking'; |
| 70 | + * import { link } from 'ember-resources/link'; |
| 71 | + * |
| 72 | + * class MyClass { ... } |
| 73 | + * |
| 74 | + * export default class Demo extends Component { |
| 75 | + * @tracked foo = 'bar'; |
| 76 | + * |
| 77 | + * @link myInstance = new MyClass({ |
| 78 | + * foo: () => this.args.foo, |
| 79 | + * bar: () => this.bar, |
| 80 | + * }); |
| 81 | + * } |
| 82 | + * ``` |
| 83 | + * |
| 84 | + * This way, whenever foo() or bar() is invoked within `MyClass`, |
| 85 | + * only the thing that does that invocation will become entangled with the tracked data |
| 86 | + * referenced within those functions. |
| 87 | + */ |
| 88 | +export function link(...args: Parameters<Stage1Decorator>): void; |
| 89 | + |
| 90 | +export function link(...args: any[]) { |
| 91 | + if (args.length === 3) { |
| 92 | + /** |
| 93 | + * Uses initializer to get the child |
| 94 | + */ |
| 95 | + return linkDecorator(...(args as Parameters<Stage1Decorator>)); |
| 96 | + } |
| 97 | + |
| 98 | + if (args.length === 1) { |
| 99 | + return linkDecoratorFactory(...(args as unknown as [any])); |
| 100 | + } |
| 101 | + |
| 102 | + // Because TS types assume property decorators might not have a descriptor, |
| 103 | + // we have to cast.... |
| 104 | + return directLink(...(args as unknown as [object, object])); |
| 105 | +} |
| 106 | + |
| 107 | +function directLink(child: object, parent: object) { |
| 108 | + associateDestroyableChild(parent, child); |
| 109 | + |
| 110 | + let owner = getOwner(parent); |
| 111 | + |
| 112 | + if (owner) { |
| 113 | + setOwner(child, owner); |
| 114 | + } |
| 115 | + |
| 116 | + return child; |
| 117 | +} |
| 118 | + |
| 119 | +function linkDecoratorFactory(child: Class<unknown>) { |
| 120 | + return function decoratorPrep(...args: Parameters<Stage1Decorator>) { |
| 121 | + return linkDecorator(...args, child); |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +function linkDecorator( |
| 126 | + _prototype: object, |
| 127 | + key: string | Symbol, |
| 128 | + descriptor: Stage1DecoratorDescriptor | undefined, |
| 129 | + explicitChild?: Class<unknown> |
| 130 | +): void { |
| 131 | + assert(`@link is a stage 1 decorator, and requires a descriptor`, descriptor); |
| 132 | + assert(`@link can only be used with string-keys`, typeof key === 'string'); |
| 133 | + |
| 134 | + let { initializer } = descriptor; |
| 135 | + |
| 136 | + assert( |
| 137 | + `@link requires an initializer or be used as a decorator factory (\`@link(...))\`). For example, ` + |
| 138 | + `\`@link foo = new MyClass();\` or \`@link(MyClass) foo;\``, |
| 139 | + initializer || explicitChild |
| 140 | + ); |
| 141 | + |
| 142 | + let caches = new WeakMap<object, any>(); |
| 143 | + |
| 144 | + return { |
| 145 | + get(this: object) { |
| 146 | + let child = caches.get(this); |
| 147 | + |
| 148 | + if (!child) { |
| 149 | + if (initializer) { |
| 150 | + child = initializer.call(this); |
| 151 | + } |
| 152 | + |
| 153 | + if (explicitChild) { |
| 154 | + // How do you narrow this to a constructor? |
| 155 | + child = new explicitChild(); |
| 156 | + } |
| 157 | + |
| 158 | + assert(`Failed to create child instance.`, child); |
| 159 | + |
| 160 | + associateDestroyableChild(this, child); |
| 161 | + |
| 162 | + let owner = getOwner(this); |
| 163 | + |
| 164 | + assert(`Owner was not present on parent. Is instance of ${this.constructor.name}`, owner); |
| 165 | + |
| 166 | + setOwner(child, owner); |
| 167 | + |
| 168 | + caches.set(this, child); |
| 169 | + assert(`Failed to create cache for internal resource configuration object`, child); |
| 170 | + } |
| 171 | + |
| 172 | + return child; |
| 173 | + }, |
| 174 | + } as unknown as void /* Thanks TS. */; |
| 175 | +} |
0 commit comments