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