Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions resources/ext.neowiki/src/neowiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export function registerSubjectCreatorClickHandler( pinia: Pinia, signal?: Abort
}, { signal } );
}

function fireRegistrationHook(): void {
const ext = NeoWikiExtension.getInstance();
mw.hook( 'neowiki.registration' ).fire(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regsiterExtensions? that seems not quite right. It fires a hook which extensions often register handlers for. So triggerRegistrationHooks, fireRegistrationEvent, or similar, seems better

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back to the original name that literally matches what happens: fireRegistrationHook.

new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);
}

function initializeNeoWikiApp(): void {
queueMicrotask( () => {
const neowikiApp = document.querySelector( '#mw-content-text > #ext-neowiki-app' );
Expand All @@ -46,9 +53,6 @@ function initializeNeoWikiApp(): void {
const pageHasMainSubject = ( neowikiApp as HTMLElement ).dataset.mwNeowikiPageHasMainSubject === 'true';

const ext = NeoWikiExtension.getInstance();
mw.hook( 'neowiki.registration' ).fire(
new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);

const app = createMwApp( NeoWikiApp, {
showSubjectCreator,
Expand All @@ -70,10 +74,6 @@ function initializeSchemaView(): void {
if ( viewSchema !== null ) {
const ext = NeoWikiExtension.getInstance();

mw.hook( 'neowiki.registration' ).fire(
new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);

const revisionId = mw.config.get( 'wgRevisionId' );
const schemaName = mw.config.get( 'wgTitle' ) as SchemaName;

Expand Down Expand Up @@ -107,9 +107,6 @@ function initializeSchemasPage(): void {

if ( schemasPage !== null ) {
const ext = NeoWikiExtension.getInstance();
mw.hook( 'neowiki.registration' ).fire(
new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);

const app = createMwApp( SchemasPage );
app.use( ext.getPinia() );
Expand All @@ -126,10 +123,6 @@ function initializeLayoutView(): void {
if ( viewLayout !== null ) {
const ext = NeoWikiExtension.getInstance();

mw.hook( 'neowiki.registration' ).fire(
new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);

const revisionId = mw.config.get( 'wgRevisionId' );
const layoutName = mw.config.get( 'wgTitle' ) as LayoutName;

Expand Down Expand Up @@ -159,9 +152,6 @@ function initializeLayoutsPage(): void {

if ( layoutsPage !== null ) {
const ext = NeoWikiExtension.getInstance();
mw.hook( 'neowiki.registration' ).fire(
new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);

const app = createMwApp( LayoutsPage );
app.use( ext.getPinia() );
Expand All @@ -178,10 +168,6 @@ function initializeSubjectsManagerPage(): void {
if ( subjectsManager !== null ) {
const ext = NeoWikiExtension.getInstance();

mw.hook( 'neowiki.registration' ).fire(
new FrontendRegistrar( ext.getTypeSpecificComponentRegistry(), ext.getPropertyTypeRegistry() ),
);

const app = createMwApp( SubjectsManagerPage ).directive( 'tooltip', CdxTooltip );
const pinia = ext.getPinia();
app.use( pinia );
Expand All @@ -196,6 +182,7 @@ const isTestEnvironment = typeof window !== 'undefined' &&
( window as unknown as { neoWikiTestMode?: boolean } ).neoWikiTestMode === true;

if ( !isTestEnvironment ) {
fireRegistrationHook();
initializeNeoWikiApp();
initializeSchemaView();
initializeLayoutView();
Expand Down
29 changes: 28 additions & 1 deletion resources/ext.neowiki/tests/integration/HookRegistration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { markRaw } from 'vue';
import { FrontendRegistrar } from '@/presentation/FrontendRegistrar';
import { TypeSpecificComponentRegistry } from '@/TypeSpecificComponentRegistry';
Expand Down Expand Up @@ -79,3 +79,30 @@ describe( 'neowiki.registration hook end-to-end', () => {
expect( typeRegistry.getTypeNames() ).toContain( 'late' );
} );
} );

describe( 'ext.neowiki module load', () => {
beforeEach( () => setupMwHook() );

afterEach( () => {
( window as unknown as { neoWikiTestMode?: boolean } ).neoWikiTestMode = true;
} );

it( 'fires neowiki.registration with a registrar wired to the live extension registries', async () => {
( window as unknown as { neoWikiTestMode?: boolean } ).neoWikiTestMode = false;

vi.resetModules();
await import( '@/neowiki' );
const { NeoWikiExtension } = await import( '@/NeoWikiExtension' );

let receivedRegistrar: FrontendRegistrar | null = null;
( globalThis as any ).mw.hook( 'neowiki.registration' ).add( ( r: FrontendRegistrar ) => {
receivedRegistrar = r;
} );

receivedRegistrar!.registerPropertyType( fakeRegistration( 'boot-fake' ) );

const ext = NeoWikiExtension.getInstance();
expect( ext.getPropertyTypeRegistry().getTypeNames() ).toContain( 'boot-fake' );
expect( ext.getTypeSpecificComponentRegistry().getPropertyTypes() ).toContain( 'boot-fake' );
} );
} );
10 changes: 1 addition & 9 deletions tests/RedHerb/resources/subjectFinder/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@
return;
}

var ext = nw.NeoWikiExtension.getInstance();
mw.hook( 'neowiki.registration' ).fire(
new nw.FrontendRegistrar(
ext.getTypeSpecificComponentRegistry(),
ext.getPropertyTypeRegistry()
)
);

var pinia = ext.getPinia();
var pinia = nw.NeoWikiExtension.getInstance().getPinia();
var app = Vue.createMwApp( SubjectFinderPanel )
.directive( 'tooltip', codex.CdxTooltip );
app.use( pinia );
Expand Down
Loading