Skip to content

Commit

Permalink
Merge pull request #572 from NullVoxPopuli/resourceFactory-fixes
Browse files Browse the repository at this point in the history
fix resourceFactory
  • Loading branch information
NullVoxPopuli authored Jul 30, 2022
2 parents af73c2b + 7c647ff commit 44b6dd6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
52 changes: 40 additions & 12 deletions ember-resources/src/core/function-based/immediate-invocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import type { Cache } from './types';

type ResourceFactory = (...args: any[]) => ReturnType<typeof resource>;

interface State {
cache?: Cache;
fn: any;
args: any;
_?: any;
}

class ResourceInvokerManager {
capabilities = helperCapabilities('3.23', {
hasValue: true,
Expand All @@ -16,35 +23,56 @@ class ResourceInvokerManager {

constructor(protected owner: unknown) {}

createHelper(fn: ResourceFactory, args: any) {
let helper: object;

createHelper(fn: ResourceFactory, args: any): State {
/**
* This cache is for args passed to the ResourceInvoker/Factory
*
* We want to cache the helper result, and only re-inoke when the args
* change.
*/
let cache = createCache(() => {
if (helper === undefined) {
let resource = fn(...args.positional) as object;
let resource = fn(...args.positional) as object;

helper = invokeHelper(cache, resource);
}

return helper;
return invokeHelper(cache, resource);
});

return { fn, args, cache: getValue(cache) };
return { fn, args, cache, _: getValue(cache) };
}

getValue({ cache }: { cache: Cache }) {
return getValue(cache);
/**
* getValue is re-called when args change
*/
getValue({ cache }: State) {
let resource = getValue(cache);

return getValue(resource);
}

getDestroyable({ fn }: { fn: ResourceFactory }) {
return fn;
}

// createHelper(fn: AnyFunction, args: Arguments): State {
// return { fn, args };
// }

// getValue({ fn, args }: State): unknown {
// if (Object.keys(args.named).length > 0) {
// let argsForFn: FnArgs<Arguments> = [...args.positional, args.named];

// return fn(...argsForFn);
// }

// return fn(...args.positional);
// }

// getDebugName(fn: AnyFunction): string {
// if (fn.name) {
// return `(helper function ${fn.name})`;
// }

// return '(anonymous helper function)';
// }
}

/**
Expand Down
3 changes: 1 addition & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions testing/ember-app/tests/utils/function-resource/clock-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ module('Examples | resource | Clock', function (hooks) {
let wait = (ms = 1_100) => new Promise((resolve) => setTimeout(resolve, ms));

hooks.beforeEach(function (assert) {
// timeout is too new for the types to know about
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
assert.timeout(3000);
});

Expand Down Expand Up @@ -162,14 +159,14 @@ module('Examples | resource | Clock', function (hooks) {

let textD = find('time')?.innerText;

assert.strictEqual(textA, textD, 'Time is reset');
assert.strictEqual(textD, textA, 'Time is reset');

this.setProperties({ date: new Date() });
await settled();

let textE = find('time')?.innerText;

assert.notStrictEqual(textD, textE, 'Time has changed');
assert.notStrictEqual(textE, textD, 'Time has changed');
});
});
});

0 comments on commit 44b6dd6

Please sign in to comment.