Skip to content

Commit

Permalink
fix(RemoteData, function-resource): wrapped template usage
Browse files Browse the repository at this point in the history
In order to use RemoteData as template-resource, we need
_another helper manager_, and we need to register it as well.
This ends up resulting in _nested_ helper-manager invocation for a
single helper-invocation call.
  • Loading branch information
NullVoxPopuli committed May 19, 2022
1 parent e374ac0 commit 38bc686
Show file tree
Hide file tree
Showing 7 changed files with 1,464 additions and 3,724 deletions.
4 changes: 2 additions & 2 deletions ember-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"npm-run-all": "4.1.5",
"rollup": "2.73.0",
"rollup": "2.74.1",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-ts": "^2.0.5",
"rollup-plugin-ts": "^2.0.7",
"semantic-release": "^19.0.2",
"tslib": "^2.3.1",
"typescript": "~4.2.0"
Expand Down
36 changes: 29 additions & 7 deletions ember-resources/src/util/function-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ export function resource<Value>(
(context as any)[INTERNAL] = true;

return context as ResourceFunction<Value>;

// return {
// [INTERNAL]: true,
// [INTERMEDIATE_VALUE]: context as ResourceFunction<Value>,
// };
}

setHelperManager(() => MANAGER, setup);
Expand Down Expand Up @@ -270,8 +265,6 @@ class FunctionResourceManager {
});

createHelper(fn: ResourceFunction) {
// let destroyer: Destructor | undefined;

/**
* The helper is only created once.
* It's the cache's callback that is invoked multiple times,
Expand Down Expand Up @@ -320,8 +313,37 @@ class FunctionResourceManager {
}
}

type AnyFunction = (...args: any[]) => ResourceFunction;

class ResourceInvokerManager {
capabilities = helperCapabilities('3.23', {
hasValue: true,
});

createHelper(fn: AnyFunction, args: any): ResourceFunction {
// this calls `resource`, which registers
// with the other helper manager
return fn(...args.positional);
}

getValue(helper: ResourceFunction) {
let result = invokeHelper(this, helper, () => ({}));

return getValue(result);
}

getDestroyable(cache: Cache) {
return cache;
}
}

// Provide a singleton manager.
const MANAGER = new FunctionResourceManager();
const ResourceInvoker = new ResourceInvokerManager();

export function registerResourceWrapper(wrapperFn: any) {
setHelperManager(() => ResourceInvoker, wrapperFn);
}

interface Descriptor {
initializer: () => unknown;
Expand Down
7 changes: 6 additions & 1 deletion ember-resources/src/util/remote-data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { tracked } from '@glimmer/tracking';
import { waitForPromise } from '@ember/test-waiters';

import { resource } from './function-resource';
import { resource, registerResourceWrapper } from './function-resource';

// @ts-ignore
import { capabilities as helperCapabilities, invokeHelper, setHelperManager } from '@ember/helper';

import type { Hooks } from './function-resource';

Expand Down Expand Up @@ -188,3 +191,5 @@ export function RemoteData(
return remoteData(hooks, targetUrl, options);
});
}

registerResourceWrapper(RemoteData);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"ci:update": "cd build/github-workflows && pnpm build",
"build": "pnpm run --filter ember-resources build",
"build:docs": "pnpm run --filter docs docs:collect",
"start:docs": "pnpm run --filter docs docs:watch"
"start:docs": "pnpm run --filter docs docs:watch",
"dev:ember": "pnpm run --filter ember-app start"
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
Expand Down
Loading

0 comments on commit 38bc686

Please sign in to comment.