Skip to content

Commit

Permalink
fix helper registration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Jul 2, 2024
1 parent e5d57e1 commit 90bf7d3
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type { InternalOwner } from '@ember/-internals/owner';
import type { Helper, HelperDefinitionState } from '@glimmer/interfaces';
// import { setInternalHelperManager } from '@glimmer/manager';
import { setInternalHelperManager } from '@glimmer/manager';

export function internalHelper(helper: Helper<InternalOwner>): HelperDefinitionState {
return function () {
console.log('internal helper', this, [...arguments]);
return helper(...arguments);
}
return setInternalHelperManager(helper, {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ interface State {

export default class OutletHelper extends Component {
get state() {
return this.args.state().outlets.main || this.args.state();
let state = this.args.state();
if (typeof state === 'function') {
state = state();
}
return state.outlets.main || state;
}
get nextState() {
return () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/@ember/-internals/glimmer/lib/templates/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export default function(owner) {
// }
// debugger;
const state = rootState.root.ref;
const owner = rootState.render.owner;
console.log('rootState', state);
return hbs`
{{log 'root-template-create' this rootState}}
{{#let (component rootState.root.template state=state root=true) as |Layout|}}
{{#let (component rootState.root.template state=state owner=owner root=true) as |Layout|}}
<Layout />
{{/let}}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ moduleFor(
this.application.register('helper:x-borf', myHelper);

return this.visit('/').then(() => {
debugger;
assert.equal(
this.$('#wrapper').text(),
'BORF YES',
Expand All @@ -27,10 +26,9 @@ moduleFor(
}

['@test Bound helpers registered on the container can be late-invoked'](assert) {
this.addTemplate(
'application',
`<div id='wrapper'>{{x-reverse}} {{x-reverse this.foo}}</div>`
);
this.addTemplate('application', function () {
return hbs`<div id='wrapper'>{{x-reverse}} {{x-reverse this.foo}}</div>`;
});

this.add(
'controller:application',
Expand All @@ -56,10 +54,9 @@ moduleFor(
}

['@test Undashed helpers registered on the container can be invoked'](assert) {
this.addTemplate(
'application',
`<div id='wrapper'>{{omg}}|{{yorp 'boo'}}|{{yorp 'ya'}}</div>`
);
this.addTemplate('application', function () {
return hbs`<div id='wrapper'>{{omg}}|{{yorp 'boo'}}|{{yorp 'ya'}}</div>`;
});

this.application.register(
'helper:omg',
Expand All @@ -81,7 +78,9 @@ moduleFor(
}

['@test Helpers can receive injections'](assert) {
this.addTemplate('application', `<div id='wrapper'>{{full-name}}</div>`);
this.addTemplate('application', function () {
return hbs`<div id='wrapper'>{{full-name}}</div>`;
});

let serviceCalled = false;

Expand Down
63 changes: 63 additions & 0 deletions packages/demo/compat/helper-manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function argsProxyFor(capturedArgs: any, type: string) {
return new Proxy(capturedArgs, {
get(target, prop) {
if (prop === 'named') {
return {};
} else if (prop === 'positional') {
return target;
} else {
throw new Error(`Cannot get ${prop} from ${type} args`);
}
},
});
}

export class CustomHelperManager {
factory: (owner: unknown) => any;
constructor(factory: (owner: unknown) => any) {
this.factory = factory;
}
private helperManagerDelegates = new WeakMap<O, any>();
private undefinedDelegate: any | null = null;

private getDelegateForOwner(owner: any) {
let delegate = this.helperManagerDelegates.get(owner);

if (delegate === undefined) {
let { factory } = this;
delegate = factory(owner);

this.helperManagerDelegates.set(owner, delegate);
}

return delegate;
}

getDelegateFor(owner: any | undefined) {
if (owner === undefined) {
let { undefinedDelegate } = this;

if (undefinedDelegate === null) {
let { factory } = this;
this.undefinedDelegate = undefinedDelegate = factory(undefined);
}

return undefinedDelegate;
} else {
return this.getDelegateForOwner(owner);
}
}

getHelper(definition: any): any {
return (capturedArgs, owner) => {
let manager = this.getDelegateFor(owner as any | undefined);

const args = argsProxyFor(capturedArgs, 'helper');
const bucket = manager.createHelper(definition, args);

return () => {
return manager.getValue(bucket);
};
};
}
}
41 changes: 35 additions & 6 deletions packages/demo/compat/manager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { $_MANAGERS, $PROPS_SYMBOL, formula } from '@lifeart/gxt';

import { CustomHelperManager } from './helper-manager';

globalThis.EmberFunctionalHelpers = new Set();
globalThis.COMPONENT_TEMPLATES = globalThis.COMPONENT_TEMPLATES || new WeakMap();
globalThis.COMPONENT_MANAGERS = globalThis.COMPONENT_MANAGERS || new WeakMap();
globalThis.INTERNAL_MANAGERS = globalThis.INTERNAL_MANAGERS || new WeakMap();
globalThis.INTERNAL_HELPER_MANAGERS = globalThis.INTERNAL_HELPER_MANAGERS || new WeakMap();
globalThis.INTERNAL_MODIFIER_MANAGERS = globalThis.INTERNAL_MODIFIER_MANAGERS || new WeakMap();
globalThis.HELPER_MANAGERS = globalThis.HELPER_MANAGERS || new WeakMap();

$_MANAGERS.component.canHandle = function (komp) {
if (globalThis.INTERNAL_MANAGERS.has(komp)) {
Expand All @@ -20,6 +22,27 @@ $_MANAGERS.component.canHandle = function (komp) {
// console.log('canHandle', komp);
// debugger;
};
$_MANAGERS.helper.canHandle = function (helper: unknown) {
if (typeof helper === 'string') {
return true;
}
return false;
};
$_MANAGERS.helper.handle = function (helper: any, params: any, hash: any) {
if (typeof helper === 'string') {
const argScope = hash['$_scope']?.() || null;
if (!argScope) {
const owner = globalThis.owner;
const maybeHelper = owner.lookup(`helper:${helper}`);
const manager = getInternalHelperManager(maybeHelper);
if (manager) {
return manager.getHelper(maybeHelper)(params, owner);
} else {
debugger;
}
}
}
};

function argsForInternalManager(args, fw) {
const named = {};
Expand Down Expand Up @@ -73,7 +96,10 @@ export function setInternalComponentManager(manager: any, handle: any) {
}

export function getInternalHelperManager(helper: any) {
return globalThis.INTERNAL_HELPER_MANAGERS.get(helper);
return (
globalThis.INTERNAL_HELPER_MANAGERS.get(helper) ||
globalThis.INTERNAL_HELPER_MANAGERS.get(Object.getPrototypeOf(helper))
);
}
export function helperCapabilities(v: string, value: any) {
return value;
Expand All @@ -84,12 +110,15 @@ export function modifierCapabilities() {
export function componentCapabilities() {
console.log('componentCapabilities', ...arguments);
}
export function setHelperManager(manager: any, helper: any) {
globalThis.HELPER_MANAGERS.set(helper, manager);
return helper;
export function setHelperManager(factory: any, helper: any) {
return setInternalHelperManager(new CustomHelperManager(factory), helper);
// console.log('setHelperManager', ...arguments);
// debugger;
// globalThis.HELPER_MANAGERS.set(helper, manager);
// return helper;
}
export function getHelperManager(helper: any) {
return globalThis.HELPER_MANAGERS.get(helper);
return getInternalHelperManager(helper);
}
export function getInternalComponentManager(handle: any) {
return globalThis.INTERNAL_MANAGERS.get(handle);
Expand Down
10 changes: 9 additions & 1 deletion packages/demo/compat/validator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { formula } from '@lifeart/gxt';
import { validator, caching } from '@lifeart/gxt/glimmer-compatibility';

export const { consumeTag, dirtyTagFor, tagFor, isTracking, tagMetaFor, track, trackedData } = validator;
export const { dirtyTagFor, tagFor, isTracking, tagMetaFor, track, trackedData } = validator;
export const { getValue, createCache } = caching; // createCache,

export function consumeTag(tag) {
if (!tag) {
console.log('consumeEmptyTag');
return;
}
return validator.consumeTag(tag);
}

export const CURRENT_TAG = formula(() => {
return Date.now() + Math.random();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@ export default abstract class TestResolverApplicationTestCase extends AbstractAp
this.resolver!.add(specifier, factory);
}

addTemplate(templateName: string, templateString: string) {
this.resolver!.add(
`template:${templateName}`,
this.compile(templateString, {
moduleName: `my-app/templates/${templateName.replace(/\./g, '/')}.hbs`,
})
);
asTemplate(ComponentKlass: any) {
return (_owner: any) => {
// template lookup
return () => {
// template init
return function () {
return {
template: ComponentKlass,
};
};
};
};
}

addTemplate(templateName: string, templateString: any) {
if (typeof templateString === 'function') {
this.resolver!.add(`template:${templateName}`, this.asTemplate(templateString));
} else {
this.resolver!.add(
`template:${templateName}`,
this.compile(templateString, {
moduleName: `my-app/templates/${templateName.replace(/\./g, '/')}.hbs`,
})
);
}
}

addComponent(
Expand Down

0 comments on commit 90bf7d3

Please sign in to comment.