@alwatr/page-ready 9.24.0 → 9.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/package.json +5 -5
package/dist/main.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/* 📦 @alwatr/page-ready v9.
|
|
1
|
+
/* 📦 @alwatr/page-ready v9.25.0 */
|
|
2
2
|
import{createLogger as o}from"@alwatr/logger";import{createChannelSignal as c}from"@alwatr/signal";var a=o("page-ready"),i=c({name:"page-ready"});function d(e,t){return a.logMethodArgs?.("onPageReady",{pageId:e}),i.on(e,t)}function b(e){return a.logMethod?.("subscribePageReady"),i.subscribe((t)=>{e(t.name)})}function n(){a.logMethod?.("dispatchPageReady");let e=document.querySelector("[page-id]")?.getAttribute("page-id")?.trim();if(e==null){a.accident("dispatchPageReady","page_id_not_found");return}i.dispatch(e)}export{b as subscribePageReady,d as onPageReady,n as dispatchPageReady};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=0ED817DA7B59A0B664756E2164756E21
|
|
5
5
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
"/**\n * @file page-ready.ts\n *\n * Lightweight page identity signal for MPA routing.\n *\n * ## Design\n *\n * Uses a dedicated `ChannelSignal` keyed by page identifier. This gives O(1)\n * dispatch — only the handler(s) registered for the current page ID are invoked,\n * regardless of how many pages are declared in the application.\n *\n * The signal is intentionally separate from `@alwatr/action`'s action bus:\n * page identity is a routing/lifecycle concern, not a user-interaction action.\n *\n * ## Attribute convention\n *\n * Place `page-id` anywhere in the document — `dispatchPageReady` finds it\n * automatically via `querySelector('[page-id]')`:\n *\n * ```html\n * <body page-id=\"home\">…</body>\n * ```\n *\n * In SSG/SSR setups each generated page has a different `page-id` value baked\n * into the HTML, so `dispatchPageReady()` always reads the correct page without\n * any runtime routing logic.\n */\n\nimport type {Awaitable} from '@alwatr/type-helper';\nimport {createLogger} from '@alwatr/logger';\nimport {createChannelSignal} from '@alwatr/signal';\nimport type {SubscribeResult} from '@alwatr/signal';\n\nconst logger = createLogger('page-ready');\n\n/**\n * Internal channel keyed by page identifier.\n *\n * O(1) dispatch: routes directly to the handler set for the dispatched key,\n * never invoking handlers for other page IDs.\n *\n * @internal\n */\nconst pageReadyChannel_ = createChannelSignal<Record<string, void>>({name: 'page-ready'});\n\n/**\n * Subscribes to a specific page becoming ready.\n *\n * The handler is invoked when `dispatchPageReady()` is called and the\n * `page-id` attribute in the document matches `pageId`.\n *\n * Pass a string literal union as the generic parameter to constrain which\n * page IDs are valid across your application:\n *\n * ```ts\n * type PageId = 'home' | 'about' | 'product-detail';\n *\n * onPageReady<PageId>('home', () => initHomePage());\n * ```\n *\n * @param pageId - The page identifier to listen for (must match the `page-id` attribute value).\n * @param handler - Called with no arguments when the page is ready.\n * @returns A `SubscribeResult` with an `unsubscribe()` method for cleanup.\n *\n * @example\n * ```ts\n * import {onPageReady} from '@alwatr/page-ready';\n *\n * const sub = onPageReady('home', () => initHomePage());\n * sub.unsubscribe(); // stop listening when no longer needed\n * ```\n */\nexport function onPageReady<T extends string>(pageId: T, handler: () => Awaitable<void>): SubscribeResult {\n logger.logMethodArgs?.('onPageReady', {pageId});\n return pageReadyChannel_.on(pageId, handler);\n}\n\n/**\n * Subscribes to **all** page-ready events, regardless of which page ID is dispatched.\n *\n * Unlike `onPageReady` — which targets a single page ID — this handler is invoked\n * every time `dispatchPageReady()` fires, and receives the dispatched page ID as\n * its first argument.\n *\n * Useful for cross-cutting concerns that must react to every page change:\n * analytics tracking, active nav-link updates, layout transitions, etc.\n *\n * Pass a string literal union as the generic parameter to get type-safe page IDs:\n *\n * ```ts\n * type PageId = 'home' | 'about' | 'product-detail';\n *\n * subscribePageReady<PageId>((pageId) => {\n * analytics.trackPageView(pageId);\n * updateActiveNavLink(pageId);\n * });\n * ```\n *\n * @param handler - Called with the dispatched page ID on every `dispatchPageReady()` call.\n * @returns A `SubscribeResult` with an `unsubscribe()` method for cleanup.\n *\n * @example\n * ```ts\n * import {subscribePageReady} from '@alwatr/page-ready';\n *\n * const sub = subscribePageReady((pageId) => console.log('Page ready:', pageId));\n * sub.unsubscribe(); // stop listening when no longer needed\n * ```\n */\nexport function subscribePageReady<T extends string>(handler: (pageId: T) => Awaitable<void>): SubscribeResult {\n logger.logMethod?.('subscribePageReady');\n return pageReadyChannel_.subscribe((message) => {\n handler(message.name as T);\n });\n}\n\n/**\n * Reads the `page-id` attribute from the first matching element in the document\n * and notifies all `onPageReady` subscribers registered for that page identifier.\n *\n * Finds the element via `document.querySelector('[page-id]')` — no argument\n * needed. Call once at application bootstrap after the DOM is ready.\n *\n * If no element with `page-id` is found in the document, an accident is logged\n * and nothing is dispatched. An empty attribute value (`page-id=\"\"`) is treated\n * as a valid identifier and will be dispatched normally.\n *\n * @example\n * ```html\n * <body page-id=\"home\">…</body>\n * ```\n * ```ts\n * import {onPageReady, dispatchPageReady} from '@alwatr/page-ready';\n *\n * onPageReady('home', () => initHomePage());\n * dispatchPageReady();\n * ```\n */\nexport function dispatchPageReady(): void {\n logger.logMethod?.('dispatchPageReady');\n\n const pageId = document.querySelector('[page-id]')?.getAttribute('page-id')?.trim();\n\n if (pageId == null) {\n logger.accident('dispatchPageReady', 'page_id_not_found');\n return;\n }\n\n // An empty string is a valid page identifier — dispatch as-is.\n pageReadyChannel_.dispatch(pageId);\n}\n"
|
|
6
6
|
],
|
|
7
7
|
"mappings": ";AA6BA,uBAAQ,uBACR,8BAAQ,uBAGR,IAAM,EAAS,EAAa,YAAY,EAUlC,EAAoB,EAA0C,CAAC,KAAM,YAAY,CAAC,EA6BjF,SAAS,CAA6B,CAAC,EAAW,EAAiD,CAExG,OADA,EAAO,gBAAgB,cAAe,CAAC,QAAM,CAAC,EACvC,EAAkB,GAAG,EAAQ,CAAO,EAmCtC,SAAS,CAAoC,CAAC,EAA0D,CAE7G,OADA,EAAO,YAAY,oBAAoB,EAChC,EAAkB,UAAU,CAAC,IAAY,CAC9C,EAAQ,EAAQ,IAAS,EAC1B,EAyBI,SAAS,CAAiB,EAAS,CACxC,EAAO,YAAY,mBAAmB,EAEtC,IAAM,EAAS,SAAS,cAAc,WAAW,GAAG,aAAa,SAAS,GAAG,KAAK,EAElF,GAAI,GAAU,KAAM,CAClB,EAAO,SAAS,oBAAqB,mBAAmB,EACxD,OAIF,EAAkB,SAAS,CAAM",
|
|
8
|
-
"debugId": "
|
|
8
|
+
"debugId": "0ED817DA7B59A0B664756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/page-ready",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.25.0",
|
|
4
4
|
"description": "Lightweight page identity signal for MPA routing — reads page-id attribute and notifies subscribers.",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
},
|
|
22
22
|
"sideEffects": false,
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@alwatr/logger": "9.
|
|
25
|
-
"@alwatr/signal": "9.
|
|
24
|
+
"@alwatr/logger": "9.25.0",
|
|
25
|
+
"@alwatr/signal": "9.25.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@alwatr/nano-build": "9.
|
|
28
|
+
"@alwatr/nano-build": "9.25.0",
|
|
29
29
|
"@alwatr/standard": "9.16.0",
|
|
30
30
|
"@alwatr/type-helper": "9.14.0",
|
|
31
31
|
"@happy-dom/global-registrator": "^20.9.0",
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
"mpa",
|
|
73
73
|
"typescript"
|
|
74
74
|
],
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "a49ae304180faab79539b5ddfe62d18ac91a232e"
|
|
76
76
|
}
|