|
| 1 | +// @ts-ignore |
| 2 | +import { createCache, getValue } from '@glimmer/tracking/primitives/cache'; |
| 3 | +import { assert } from '@ember/debug'; |
| 4 | +import { associateDestroyableChild, destroy, registerDestructor } from '@ember/destroyable'; |
| 5 | +// @ts-ignore |
| 6 | +import { invokeHelper, setHelperManager } from '@ember/helper'; |
| 7 | + |
| 8 | +import { ReadonlyCell } from './cell.ts'; |
| 9 | +import { ResourceManagerFactory } from './resource-manager.ts'; |
| 10 | +import { INTERNAL } from './types.ts'; |
| 11 | +import { registerUsable, TYPE_KEY } from './use.ts'; |
| 12 | +import { shallowFlat } from './utils.ts'; |
| 13 | + |
| 14 | +import type { Destructor, Reactive, ResourceFunction } from './types.ts'; |
| 15 | +import type Owner from '@ember/owner'; |
| 16 | + |
| 17 | +export const CREATE_KEY = Symbol.for('__configured-resource-key__'); |
| 18 | +export const DEBUG_NAME = Symbol.for('DEBUG_NAME'); |
| 19 | +export const RESOURCE_CACHE = Symbol.for('__resource_cache__'); |
| 20 | + |
| 21 | +import { compatOwner } from './ember-compat.ts'; |
| 22 | + |
| 23 | +const getOwner = compatOwner.getOwner; |
| 24 | +const setOwner = compatOwner.setOwner; |
| 25 | + |
| 26 | +/** |
| 27 | + * The return value from resource() |
| 28 | + * |
| 29 | + * This is semi-public API, and is meant to de-magic the intermediary |
| 30 | + * value returned from resource(), allowing us to both document how to |
| 31 | + * - manually create a resource (instance) |
| 32 | + * - explain how the helper manager interacts with the methods folks |
| 33 | + * can use to manually create a resource |
| 34 | + * |
| 35 | + * |
| 36 | + * With an owner, you can manually create a resource this way: |
| 37 | + * ```js |
| 38 | + * import { destroy } from '@ember/destroyable'; |
| 39 | + * import { resource } from 'ember-resources'; |
| 40 | + * |
| 41 | + * const builder = resource(() => {}); // builder can be invoked multiple times |
| 42 | + * const owner = {}; |
| 43 | + * const state = builder.create(owner); // state can be created any number of times |
| 44 | + * |
| 45 | + * state.current // the current value |
| 46 | + * destroy(state); // some time later, calls cleanup |
| 47 | + * ``` |
| 48 | + */ |
| 49 | +export class Builder<Value> { |
| 50 | + #fn: ResourceFunction<Value>; |
| 51 | + |
| 52 | + [TYPE_KEY] = TYPE; |
| 53 | + |
| 54 | + constructor(fn: ResourceFunction<Value>, key: Symbol) { |
| 55 | + assert( |
| 56 | + `Cannot instantiate ConfiguredResource without using the resource() function.`, |
| 57 | + key === CREATE_KEY, |
| 58 | + ); |
| 59 | + |
| 60 | + this.#fn = fn; |
| 61 | + } |
| 62 | + |
| 63 | + create() { |
| 64 | + return new Resource(this.#fn); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const TYPE = 'function-based'; |
| 69 | + |
| 70 | +registerUsable(TYPE, (context: object, config: Builder<unknown>) => { |
| 71 | + let instance = config.create(); |
| 72 | + |
| 73 | + instance.link(context); |
| 74 | + |
| 75 | + return instance[RESOURCE_CACHE]; |
| 76 | +}); |
| 77 | + |
| 78 | +/** |
| 79 | + * TODO: |
| 80 | + */ |
| 81 | +export class Resource<Value> { |
| 82 | + #originalFn: ResourceFunction<Value>; |
| 83 | + #owner: Owner | undefined; |
| 84 | + #previousFn: object | undefined; |
| 85 | + #usableCache = new WeakMap<object, ReturnType<typeof invokeHelper>>(); |
| 86 | + #cache: ReturnType<typeof invokeHelper>; |
| 87 | + |
| 88 | + constructor(fn: ResourceFunction<Value>) { |
| 89 | + /** |
| 90 | + * We have to copy the `fn` in case there are multiple |
| 91 | + * usages or invocations of the function. |
| 92 | + * |
| 93 | + * This copy is what we'll ultimately work with and eventually |
| 94 | + * destroy. |
| 95 | + */ |
| 96 | + this.#originalFn = fn.bind(null); |
| 97 | + |
| 98 | + this.#cache = createCache(() => { |
| 99 | + if (this.#previousFn) { |
| 100 | + destroy(this.#previousFn); |
| 101 | + } |
| 102 | + |
| 103 | + let currentFn = this.#originalFn.bind(null); |
| 104 | + |
| 105 | + associateDestroyableChild(this.#originalFn, currentFn); |
| 106 | + this.#previousFn = currentFn; |
| 107 | + |
| 108 | + assert( |
| 109 | + `Cannot create a resource without an owner. Must have previously called .link()`, |
| 110 | + this.#owner, |
| 111 | + ); |
| 112 | + |
| 113 | + let maybeValue = currentFn({ |
| 114 | + on: { |
| 115 | + cleanup: (destroyer: Destructor) => { |
| 116 | + registerDestructor(currentFn, destroyer); |
| 117 | + }, |
| 118 | + }, |
| 119 | + use: (usable) => { |
| 120 | + assert( |
| 121 | + `Expected the resource's \`use(...)\` utility to have been passed an object, but a \`${typeof usable}\` was passed.`, |
| 122 | + typeof usable === 'object', |
| 123 | + ); |
| 124 | + assert( |
| 125 | + `Expected the resource's \`use(...)\` utility to have been passed a truthy value, instead was passed: ${usable}.`, |
| 126 | + usable, |
| 127 | + ); |
| 128 | + assert( |
| 129 | + `Expected the resource's \`use(...)\` utility to have been passed another resource, but something else was passed.`, |
| 130 | + INTERNAL in usable || usable instanceof Builder, |
| 131 | + ); |
| 132 | + |
| 133 | + let previousCache = this.#usableCache.get(usable); |
| 134 | + |
| 135 | + if (previousCache) { |
| 136 | + destroy(previousCache); |
| 137 | + } |
| 138 | + |
| 139 | + let nestedCache = invokeHelper(this.#cache, usable); |
| 140 | + |
| 141 | + associateDestroyableChild(currentFn, nestedCache as object); |
| 142 | + |
| 143 | + this.#usableCache.set(usable, nestedCache); |
| 144 | + |
| 145 | + return new ReadonlyCell<any>(() => { |
| 146 | + let cache = this.#usableCache.get(usable); |
| 147 | + |
| 148 | + assert(`Cache went missing while evaluating the result of a resource.`, cache); |
| 149 | + |
| 150 | + return getValue(cache); |
| 151 | + }); |
| 152 | + }, |
| 153 | + owner: this.#owner, |
| 154 | + }); |
| 155 | + |
| 156 | + return maybeValue; |
| 157 | + }); |
| 158 | + } |
| 159 | + link(context: object) { |
| 160 | + let owner = getOwner(context); |
| 161 | + |
| 162 | + assert(`Cannot link without an owner`, owner); |
| 163 | + |
| 164 | + this.#owner = owner; |
| 165 | + |
| 166 | + associateDestroyableChild(context, this.#cache); |
| 167 | + associateDestroyableChild(context, this.#originalFn); |
| 168 | + |
| 169 | + setOwner(this.#cache, this.#owner); |
| 170 | + } |
| 171 | + |
| 172 | + get [RESOURCE_CACHE](): unknown { |
| 173 | + return this.#cache; |
| 174 | + } |
| 175 | + |
| 176 | + get fn() { |
| 177 | + return this.#originalFn; |
| 178 | + } |
| 179 | + |
| 180 | + get current() { |
| 181 | + return shallowFlat(this.#cache); |
| 182 | + } |
| 183 | + |
| 184 | + [DEBUG_NAME]() { |
| 185 | + return `Resource Function`; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +setHelperManager(ResourceManagerFactory, Builder.prototype); |
0 commit comments