mvc-kit 2.12.5 → 2.13.1
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/BEST_PRACTICES.md +1390 -0
- package/agent-config/bin/postinstall.mjs +4 -3
- package/agent-config/bin/setup.mjs +5 -1
- package/agent-config/claude-code/agents/mvc-kit-architect.md +16 -8
- package/agent-config/claude-code/skills/guide/SKILL.md +29 -7
- package/agent-config/claude-code/skills/guide/patterns.md +12 -0
- package/agent-config/claude-code/skills/guide/recipes.md +510 -0
- package/agent-config/claude-code/skills/guide/testing.md +297 -0
- package/agent-config/claude-code/skills/review/SKILL.md +3 -13
- package/agent-config/claude-code/skills/review/checklist.md +30 -5
- package/agent-config/claude-code/skills/scaffold/SKILL.md +4 -13
- package/agent-config/lib/install-claude.mjs +90 -25
- package/dist/Channel.cjs +276 -300
- package/dist/Channel.cjs.map +1 -1
- package/dist/Channel.js +275 -299
- package/dist/Channel.js.map +1 -1
- package/dist/Collection.cjs +424 -504
- package/dist/Collection.cjs.map +1 -1
- package/dist/Collection.js +423 -503
- package/dist/Collection.js.map +1 -1
- package/dist/Controller.cjs +70 -67
- package/dist/Controller.cjs.map +1 -1
- package/dist/Controller.js +69 -66
- package/dist/Controller.js.map +1 -1
- package/dist/EventBus.cjs +77 -88
- package/dist/EventBus.cjs.map +1 -1
- package/dist/EventBus.js +76 -87
- package/dist/EventBus.js.map +1 -1
- package/dist/Feed.cjs +81 -77
- package/dist/Feed.cjs.map +1 -1
- package/dist/Feed.js +80 -76
- package/dist/Feed.js.map +1 -1
- package/dist/Model.cjs +181 -207
- package/dist/Model.cjs.map +1 -1
- package/dist/Model.js +179 -205
- package/dist/Model.js.map +1 -1
- package/dist/Pagination.cjs +75 -73
- package/dist/Pagination.cjs.map +1 -1
- package/dist/Pagination.js +74 -72
- package/dist/Pagination.js.map +1 -1
- package/dist/Pending.cjs +255 -287
- package/dist/Pending.cjs.map +1 -1
- package/dist/Pending.js +253 -285
- package/dist/Pending.js.map +1 -1
- package/dist/PersistentCollection.cjs +242 -285
- package/dist/PersistentCollection.cjs.map +1 -1
- package/dist/PersistentCollection.js +241 -284
- package/dist/PersistentCollection.js.map +1 -1
- package/dist/Resource.cjs +166 -174
- package/dist/Resource.cjs.map +1 -1
- package/dist/Resource.js +164 -172
- package/dist/Resource.js.map +1 -1
- package/dist/Selection.cjs +84 -94
- package/dist/Selection.cjs.map +1 -1
- package/dist/Selection.js +83 -93
- package/dist/Selection.js.map +1 -1
- package/dist/Service.cjs +54 -55
- package/dist/Service.cjs.map +1 -1
- package/dist/Service.js +53 -54
- package/dist/Service.js.map +1 -1
- package/dist/Sorting.cjs +102 -101
- package/dist/Sorting.cjs.map +1 -1
- package/dist/Sorting.js +102 -101
- package/dist/Sorting.js.map +1 -1
- package/dist/Trackable.cjs +112 -80
- package/dist/Trackable.cjs.map +1 -1
- package/dist/Trackable.js +111 -79
- package/dist/Trackable.js.map +1 -1
- package/dist/ViewModel.cjs +528 -576
- package/dist/ViewModel.cjs.map +1 -1
- package/dist/ViewModel.js +525 -573
- package/dist/ViewModel.js.map +1 -1
- package/dist/bindPublicMethods.cjs +43 -24
- package/dist/bindPublicMethods.cjs.map +1 -1
- package/dist/bindPublicMethods.js +43 -24
- package/dist/bindPublicMethods.js.map +1 -1
- package/dist/errors.cjs +67 -68
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.js +68 -71
- package/dist/errors.js.map +1 -1
- package/dist/mvc-kit.cjs +44 -46
- package/dist/mvc-kit.js +5 -32
- package/dist/produceDraft.cjs +105 -95
- package/dist/produceDraft.cjs.map +1 -1
- package/dist/produceDraft.js +106 -97
- package/dist/produceDraft.js.map +1 -1
- package/dist/react/components/CardList.cjs +30 -40
- package/dist/react/components/CardList.cjs.map +1 -1
- package/dist/react/components/CardList.js +31 -41
- package/dist/react/components/CardList.js.map +1 -1
- package/dist/react/components/DataTable.cjs +146 -169
- package/dist/react/components/DataTable.cjs.map +1 -1
- package/dist/react/components/DataTable.js +147 -170
- package/dist/react/components/DataTable.js.map +1 -1
- package/dist/react/components/InfiniteScroll.cjs +51 -42
- package/dist/react/components/InfiniteScroll.cjs.map +1 -1
- package/dist/react/components/InfiniteScroll.js +52 -43
- package/dist/react/components/InfiniteScroll.js.map +1 -1
- package/dist/react/components/types.cjs +10 -6
- package/dist/react/components/types.cjs.map +1 -1
- package/dist/react/components/types.js +11 -9
- package/dist/react/components/types.js.map +1 -1
- package/dist/react/guards.cjs +10 -6
- package/dist/react/guards.cjs.map +1 -1
- package/dist/react/guards.js +11 -9
- package/dist/react/guards.js.map +1 -1
- package/dist/react/provider.cjs +23 -20
- package/dist/react/provider.cjs.map +1 -1
- package/dist/react/provider.js +23 -21
- package/dist/react/provider.js.map +1 -1
- package/dist/react/use-event-bus.cjs +24 -20
- package/dist/react/use-event-bus.cjs.map +1 -1
- package/dist/react/use-event-bus.js +24 -21
- package/dist/react/use-event-bus.js.map +1 -1
- package/dist/react/use-instance.cjs +43 -36
- package/dist/react/use-instance.cjs.map +1 -1
- package/dist/react/use-instance.js +43 -36
- package/dist/react/use-instance.js.map +1 -1
- package/dist/react/use-local.cjs +48 -64
- package/dist/react/use-local.cjs.map +1 -1
- package/dist/react/use-local.js +47 -63
- package/dist/react/use-local.js.map +1 -1
- package/dist/react/use-model.cjs +84 -98
- package/dist/react/use-model.cjs.map +1 -1
- package/dist/react/use-model.js +84 -100
- package/dist/react/use-model.js.map +1 -1
- package/dist/react/use-singleton.cjs +19 -23
- package/dist/react/use-singleton.cjs.map +1 -1
- package/dist/react/use-singleton.js +16 -20
- package/dist/react/use-singleton.js.map +1 -1
- package/dist/react/use-subscribe-only.cjs +28 -22
- package/dist/react/use-subscribe-only.cjs.map +1 -1
- package/dist/react/use-subscribe-only.js +28 -22
- package/dist/react/use-subscribe-only.js.map +1 -1
- package/dist/react/use-teardown.cjs +20 -19
- package/dist/react/use-teardown.cjs.map +1 -1
- package/dist/react/use-teardown.js +20 -19
- package/dist/react/use-teardown.js.map +1 -1
- package/dist/react-native/NativeCollection.cjs +98 -78
- package/dist/react-native/NativeCollection.cjs.map +1 -1
- package/dist/react-native/NativeCollection.js +97 -77
- package/dist/react-native/NativeCollection.js.map +1 -1
- package/dist/react-native.cjs +2 -4
- package/dist/react-native.js +1 -4
- package/dist/react.cjs +24 -26
- package/dist/react.js +1 -17
- package/dist/singleton.cjs +28 -22
- package/dist/singleton.cjs.map +1 -1
- package/dist/singleton.js +29 -26
- package/dist/singleton.js.map +1 -1
- package/dist/walkPrototypeChain.cjs +20 -12
- package/dist/walkPrototypeChain.cjs.map +1 -1
- package/dist/walkPrototypeChain.js +21 -13
- package/dist/walkPrototypeChain.js.map +1 -1
- package/dist/web/IndexedDBCollection.cjs +53 -36
- package/dist/web/IndexedDBCollection.cjs.map +1 -1
- package/dist/web/IndexedDBCollection.js +52 -35
- package/dist/web/IndexedDBCollection.js.map +1 -1
- package/dist/web/WebStorageCollection.cjs +82 -84
- package/dist/web/WebStorageCollection.cjs.map +1 -1
- package/dist/web/WebStorageCollection.js +81 -83
- package/dist/web/WebStorageCollection.js.map +1 -1
- package/dist/web/idb.cjs +107 -99
- package/dist/web/idb.cjs.map +1 -1
- package/dist/web/idb.js +108 -105
- package/dist/web/idb.js.map +1 -1
- package/dist/web.cjs +4 -6
- package/dist/web.js +1 -5
- package/dist/wrapAsyncMethods.cjs +141 -168
- package/dist/wrapAsyncMethods.cjs.map +1 -1
- package/dist/wrapAsyncMethods.js +141 -168
- package/dist/wrapAsyncMethods.js.map +1 -1
- package/examples/primitive/channel.ts +109 -0
- package/examples/primitive/collection.ts +118 -0
- package/examples/primitive/controller.ts +118 -0
- package/examples/primitive/counter.ts +108 -0
- package/examples/primitive/env.d.ts +1 -0
- package/examples/primitive/eventbus.ts +77 -0
- package/examples/primitive/feed.ts +162 -0
- package/examples/primitive/model.ts +82 -0
- package/examples/primitive/pagination.ts +91 -0
- package/examples/primitive/pending.ts +189 -0
- package/examples/primitive/persistent-collection.ts +116 -0
- package/examples/primitive/resource.ts +114 -0
- package/examples/primitive/selection.ts +96 -0
- package/examples/primitive/sorting.ts +112 -0
- package/examples/primitive/timer.ts +58 -0
- package/examples/primitive/trackable.ts +225 -0
- package/examples/primitive/tsconfig.json +20 -0
- package/examples/primitive/viewmodel-service.ts +161 -0
- package/examples/react/AuthExample/index.html +12 -0
- package/examples/react/AuthExample/src/App.tsx +29 -0
- package/examples/react/AuthExample/src/components/AdminPage.tsx +51 -0
- package/examples/react/AuthExample/src/components/AppHeader.tsx +32 -0
- package/examples/react/AuthExample/src/components/AuthGuard.tsx +50 -0
- package/examples/react/AuthExample/src/components/AuthScreen.tsx +181 -0
- package/examples/react/AuthExample/src/components/DashboardPage.tsx +41 -0
- package/examples/react/AuthExample/src/components/ProfilePage.tsx +44 -0
- package/examples/react/AuthExample/src/components/Toast.tsx +41 -0
- package/examples/react/AuthExample/src/env.d.ts +10 -0
- package/examples/react/AuthExample/src/events/AppEventBus.ts +7 -0
- package/examples/react/AuthExample/src/main.tsx +10 -0
- package/examples/react/AuthExample/src/mock/api.ts +78 -0
- package/examples/react/AuthExample/src/models/LoginFormModel.ts +19 -0
- package/examples/react/AuthExample/src/models/RegisterFormModel.ts +25 -0
- package/examples/react/AuthExample/src/services/AuthService.ts +21 -0
- package/examples/react/AuthExample/src/styles.css +445 -0
- package/examples/react/AuthExample/src/types/auth.ts +12 -0
- package/examples/react/AuthExample/src/viewmodels/AuthViewModel.ts +111 -0
- package/examples/react/AuthExample/tsconfig.json +22 -0
- package/examples/react/AuthExample/vite.config.ts +18 -0
- package/examples/react/ComplexApp/index.html +12 -0
- package/examples/react/ComplexApp/src/App.tsx +17 -0
- package/examples/react/ComplexApp/src/channels/ActivityChannel.ts +24 -0
- package/examples/react/ComplexApp/src/channels/DashboardChannel.ts +26 -0
- package/examples/react/ComplexApp/src/channels/ErrorsChannel.ts +5 -0
- package/examples/react/ComplexApp/src/channels/LatencyChannel.ts +5 -0
- package/examples/react/ComplexApp/src/channels/OrdersChannel.ts +5 -0
- package/examples/react/ComplexApp/src/channels/RevenueChannel.ts +5 -0
- package/examples/react/ComplexApp/src/channels/TrafficChannel.ts +5 -0
- package/examples/react/ComplexApp/src/channels/UsersMetricChannel.ts +5 -0
- package/examples/react/ComplexApp/src/collections/DashboardCollection.ts +6 -0
- package/examples/react/ComplexApp/src/collections/ErrorsCollection.ts +3 -0
- package/examples/react/ComplexApp/src/collections/LatencyCollection.ts +3 -0
- package/examples/react/ComplexApp/src/collections/OrdersCollection.ts +3 -0
- package/examples/react/ComplexApp/src/collections/RevenueCollection.ts +3 -0
- package/examples/react/ComplexApp/src/collections/TrafficCollection.ts +3 -0
- package/examples/react/ComplexApp/src/collections/UsersMetricCollection.ts +3 -0
- package/examples/react/ComplexApp/src/components/activity/ActivityFeed.tsx +31 -0
- package/examples/react/ComplexApp/src/components/activity/ActivityItemRow.tsx +35 -0
- package/examples/react/ComplexApp/src/components/dashboard/DashboardCard.tsx +37 -0
- package/examples/react/ComplexApp/src/components/dashboard/DashboardPage.tsx +34 -0
- package/examples/react/ComplexApp/src/components/layout/Navbar.tsx +32 -0
- package/examples/react/ComplexApp/src/components/layout/SocialFeedPanel.tsx +57 -0
- package/examples/react/ComplexApp/src/components/shared/Spinner.tsx +3 -0
- package/examples/react/ComplexApp/src/components/shared/StatusIndicator.tsx +13 -0
- package/examples/react/ComplexApp/src/components/shared/Toast.tsx +40 -0
- package/examples/react/ComplexApp/src/env.d.ts +10 -0
- package/examples/react/ComplexApp/src/events/AppEventBus.ts +7 -0
- package/examples/react/ComplexApp/src/main.tsx +10 -0
- package/examples/react/ComplexApp/src/mock-remote/MockWebSocket.ts +38 -0
- package/examples/react/ComplexApp/src/mock-remote/activity-api.ts +48 -0
- package/examples/react/ComplexApp/src/mock-remote/dashboard-generators.ts +45 -0
- package/examples/react/ComplexApp/src/mock-remote/delay.ts +18 -0
- package/examples/react/ComplexApp/src/mock-remote/social-api.ts +55 -0
- package/examples/react/ComplexApp/src/resources/ActivityResource.ts +12 -0
- package/examples/react/ComplexApp/src/resources/SocialFeedResource.ts +17 -0
- package/examples/react/ComplexApp/src/styles.css +463 -0
- package/examples/react/ComplexApp/src/types/activity.ts +8 -0
- package/examples/react/ComplexApp/src/types/dashboard.ts +5 -0
- package/examples/react/ComplexApp/src/types/social.ts +8 -0
- package/examples/react/ComplexApp/src/types/users.ts +6 -0
- package/examples/react/ComplexApp/src/viewmodels/ActivityFeedViewModel.ts +68 -0
- package/examples/react/ComplexApp/src/viewmodels/AppStateViewModel.ts +26 -0
- package/examples/react/ComplexApp/src/viewmodels/DashboardCardViewModel.ts +69 -0
- package/examples/react/ComplexApp/src/viewmodels/ErrorsCardViewModel.ts +9 -0
- package/examples/react/ComplexApp/src/viewmodels/LatencyCardViewModel.ts +9 -0
- package/examples/react/ComplexApp/src/viewmodels/OrdersCardViewModel.ts +9 -0
- package/examples/react/ComplexApp/src/viewmodels/RevenueCardViewModel.ts +9 -0
- package/examples/react/ComplexApp/src/viewmodels/SocialFeedViewModel.ts +39 -0
- package/examples/react/ComplexApp/src/viewmodels/TrafficCardViewModel.ts +9 -0
- package/examples/react/ComplexApp/src/viewmodels/UsersMetricCardViewModel.ts +9 -0
- package/examples/react/ComplexApp/tsconfig.json +22 -0
- package/examples/react/ComplexApp/vite.config.ts +18 -0
- package/examples/react/FullApp/index.html +12 -0
- package/examples/react/FullApp/src/App.tsx +28 -0
- package/examples/react/FullApp/src/collections/ConversationsCollection.ts +4 -0
- package/examples/react/FullApp/src/collections/LocationsCollection.ts +4 -0
- package/examples/react/FullApp/src/components/auth/LoginPage.tsx +80 -0
- package/examples/react/FullApp/src/components/dashboard/DashboardPage.tsx +29 -0
- package/examples/react/FullApp/src/components/dashboard/RecentActivityCard.tsx +35 -0
- package/examples/react/FullApp/src/components/dashboard/StatsCard.tsx +19 -0
- package/examples/react/FullApp/src/components/layout/AppShell.tsx +31 -0
- package/examples/react/FullApp/src/components/layout/Header.tsx +25 -0
- package/examples/react/FullApp/src/components/layout/Sidebar.tsx +29 -0
- package/examples/react/FullApp/src/components/locations/LocationFilters.tsx +60 -0
- package/examples/react/FullApp/src/components/locations/LocationForm.tsx +112 -0
- package/examples/react/FullApp/src/components/locations/LocationProfilePage.tsx +81 -0
- package/examples/react/FullApp/src/components/locations/LocationsPage.tsx +127 -0
- package/examples/react/FullApp/src/components/messaging/ConversationList.tsx +59 -0
- package/examples/react/FullApp/src/components/messaging/MessageBubble.tsx +22 -0
- package/examples/react/FullApp/src/components/messaging/MessageThread.tsx +100 -0
- package/examples/react/FullApp/src/components/messaging/MessagingPage.tsx +52 -0
- package/examples/react/FullApp/src/components/shared/ErrorBanner.tsx +3 -0
- package/examples/react/FullApp/src/components/shared/Spinner.tsx +7 -0
- package/examples/react/FullApp/src/components/shared/Toast.tsx +41 -0
- package/examples/react/FullApp/src/components/users/UserFilters.tsx +59 -0
- package/examples/react/FullApp/src/components/users/UsersPage.tsx +80 -0
- package/examples/react/FullApp/src/components/users/UsersTable.tsx +52 -0
- package/examples/react/FullApp/src/env.d.ts +10 -0
- package/examples/react/FullApp/src/events/AppEventBus.ts +7 -0
- package/examples/react/FullApp/src/main.tsx +10 -0
- package/examples/react/FullApp/src/mock/delay.ts +21 -0
- package/examples/react/FullApp/src/mock/locations.ts +76 -0
- package/examples/react/FullApp/src/mock/messages.ts +237 -0
- package/examples/react/FullApp/src/mock/users.ts +84 -0
- package/examples/react/FullApp/src/models/LocationFormModel.ts +31 -0
- package/examples/react/FullApp/src/models/LoginFormModel.ts +19 -0
- package/examples/react/FullApp/src/resources/UsersResource.ts +12 -0
- package/examples/react/FullApp/src/services/AuthService.ts +18 -0
- package/examples/react/FullApp/src/services/LocationService.ts +23 -0
- package/examples/react/FullApp/src/services/MessageService.ts +65 -0
- package/examples/react/FullApp/src/services/UserService.ts +23 -0
- package/examples/react/FullApp/src/styles.css +767 -0
- package/examples/react/FullApp/src/types/conversation.ts +7 -0
- package/examples/react/FullApp/src/types/location.ts +12 -0
- package/examples/react/FullApp/src/types/message.ts +7 -0
- package/examples/react/FullApp/src/types/user.ts +10 -0
- package/examples/react/FullApp/src/viewmodels/AuthViewModel.ts +51 -0
- package/examples/react/FullApp/src/viewmodels/ConversationsViewModel.ts +89 -0
- package/examples/react/FullApp/src/viewmodels/DashboardViewModel.ts +56 -0
- package/examples/react/FullApp/src/viewmodels/LocationProfileViewModel.ts +81 -0
- package/examples/react/FullApp/src/viewmodels/LocationsViewModel.ts +113 -0
- package/examples/react/FullApp/src/viewmodels/MessageThreadViewModel.ts +83 -0
- package/examples/react/FullApp/src/viewmodels/UsersViewModel.ts +88 -0
- package/examples/react/FullApp/tsconfig.json +22 -0
- package/examples/react/FullApp/vite.config.ts +18 -0
- package/examples/react/WorkerApp/index.html +12 -0
- package/examples/react/WorkerApp/src/App.tsx +24 -0
- package/examples/react/WorkerApp/src/channels/MessagingChannel.ts +46 -0
- package/examples/react/WorkerApp/src/channels/WorkerStatusChannel.ts +35 -0
- package/examples/react/WorkerApp/src/components/auth/LoginPage.tsx +60 -0
- package/examples/react/WorkerApp/src/components/layout/AppShell.tsx +31 -0
- package/examples/react/WorkerApp/src/components/layout/Header.tsx +23 -0
- package/examples/react/WorkerApp/src/components/layout/Sidebar.tsx +28 -0
- package/examples/react/WorkerApp/src/components/messaging/ComposeBar.tsx +33 -0
- package/examples/react/WorkerApp/src/components/messaging/ConversationList.tsx +59 -0
- package/examples/react/WorkerApp/src/components/messaging/MessageBubble.tsx +45 -0
- package/examples/react/WorkerApp/src/components/messaging/MessageThread.tsx +93 -0
- package/examples/react/WorkerApp/src/components/messaging/MessagingPage.tsx +53 -0
- package/examples/react/WorkerApp/src/components/shared/ErrorBanner.tsx +3 -0
- package/examples/react/WorkerApp/src/components/shared/PendingBanner.tsx +37 -0
- package/examples/react/WorkerApp/src/components/shared/Spinner.tsx +7 -0
- package/examples/react/WorkerApp/src/components/shared/Toast.tsx +41 -0
- package/examples/react/WorkerApp/src/components/shift/ShiftPage.tsx +98 -0
- package/examples/react/WorkerApp/src/components/shift/ShiftTimer.tsx +24 -0
- package/examples/react/WorkerApp/src/components/shift/SiteSelector.tsx +27 -0
- package/examples/react/WorkerApp/src/components/sites/SiteFilters.tsx +61 -0
- package/examples/react/WorkerApp/src/components/sites/SitesPage.tsx +102 -0
- package/examples/react/WorkerApp/src/env.d.ts +10 -0
- package/examples/react/WorkerApp/src/events/AppEventBus.ts +7 -0
- package/examples/react/WorkerApp/src/main.tsx +10 -0
- package/examples/react/WorkerApp/src/mock/MockWebSocket.ts +38 -0
- package/examples/react/WorkerApp/src/mock/delay.ts +31 -0
- package/examples/react/WorkerApp/src/mock/messages.ts +120 -0
- package/examples/react/WorkerApp/src/mock/shifts.ts +57 -0
- package/examples/react/WorkerApp/src/mock/sites.ts +14 -0
- package/examples/react/WorkerApp/src/mock/workers.ts +12 -0
- package/examples/react/WorkerApp/src/models/ComposeMessageModel.ts +17 -0
- package/examples/react/WorkerApp/src/resources/ConversationsResource.ts +10 -0
- package/examples/react/WorkerApp/src/resources/MessagesResource.ts +32 -0
- package/examples/react/WorkerApp/src/resources/ShiftResource.ts +73 -0
- package/examples/react/WorkerApp/src/resources/SitesResource.ts +11 -0
- package/examples/react/WorkerApp/src/resources/WorkersResource.ts +11 -0
- package/examples/react/WorkerApp/src/styles.css +756 -0
- package/examples/react/WorkerApp/src/types/conversation.ts +7 -0
- package/examples/react/WorkerApp/src/types/message.ts +7 -0
- package/examples/react/WorkerApp/src/types/shift.ts +13 -0
- package/examples/react/WorkerApp/src/types/site.ts +8 -0
- package/examples/react/WorkerApp/src/types/worker.ts +8 -0
- package/examples/react/WorkerApp/src/viewmodels/AuthViewModel.ts +41 -0
- package/examples/react/WorkerApp/src/viewmodels/ConversationsViewModel.ts +83 -0
- package/examples/react/WorkerApp/src/viewmodels/MessageThreadViewModel.ts +113 -0
- package/examples/react/WorkerApp/src/viewmodels/ShiftViewModel.ts +147 -0
- package/examples/react/WorkerApp/src/viewmodels/SitesViewModel.ts +82 -0
- package/examples/react/WorkerApp/tsconfig.json +22 -0
- package/examples/react/WorkerApp/vite.config.ts +18 -0
- package/package.json +11 -9
- package/src/Pending.test.ts +1 -2
- package/src/Sorting.test.ts +1 -1
- package/src/produceDraft.test.ts +3 -3
- package/src/react/components/CardList.test.tsx +1 -1
- package/src/react/components/DataTable.test.tsx +1 -1
- package/src/react/components/InfiniteScroll.test.tsx +5 -5
- package/dist/mvc-kit.cjs.map +0 -1
- package/dist/mvc-kit.js.map +0 -1
- package/dist/react-native.cjs.map +0 -1
- package/dist/react-native.js.map +0 -1
- package/dist/react.cjs.map +0 -1
- package/dist/react.js.map +0 -1
- package/dist/web.cjs.map +0 -1
- package/dist/web.js.map +0 -1
package/dist/wrapAsyncMethods.js
CHANGED
|
@@ -1,171 +1,144 @@
|
|
|
1
|
-
import { isAbortError, classifyError } from "./errors.js";
|
|
2
1
|
import { walkPrototypeChain } from "./walkPrototypeChain.js";
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { classifyError, isAbortError } from "./errors.js";
|
|
3
|
+
//#region src/wrapAsyncMethods.ts
|
|
4
|
+
var __DEV__ = typeof __MVC_KIT_DEV__ !== "undefined" && __MVC_KIT_DEV__;
|
|
5
|
+
var LOADING_TASK_STATE = Object.freeze({
|
|
6
|
+
loading: true,
|
|
7
|
+
error: null,
|
|
8
|
+
errorCode: null
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Shared async method wrapping logic used by both ViewModel and Resource.
|
|
12
|
+
* Walks the prototype chain, wraps methods with async tracking, and registers cleanup.
|
|
13
|
+
* Returns the list of wrapped method keys.
|
|
14
|
+
*/
|
|
5
15
|
function wrapAsyncMethods(ctx) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const classified = classifyError(error);
|
|
131
|
-
finalizeOp(classified.message, classified.code);
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
);
|
|
135
|
-
};
|
|
136
|
-
wrappedKeys.push(key);
|
|
137
|
-
instance[key] = wrapper;
|
|
138
|
-
}
|
|
139
|
-
if (wrappedKeys.length > 0) {
|
|
140
|
-
addCleanup(() => {
|
|
141
|
-
const opsSnapshot = __DEV__ && activeOps ? new Map(activeOps) : null;
|
|
142
|
-
for (const k of wrappedKeys) {
|
|
143
|
-
if (__DEV__) {
|
|
144
|
-
instance[k] = () => {
|
|
145
|
-
console.warn(`[mvc-kit] "${k}" called after dispose — ignored.`);
|
|
146
|
-
return void 0;
|
|
147
|
-
};
|
|
148
|
-
} else {
|
|
149
|
-
instance[k] = () => void 0;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
asyncListeners.clear();
|
|
153
|
-
asyncStates.clear();
|
|
154
|
-
asyncSnapshots.clear();
|
|
155
|
-
if (__DEV__ && opsSnapshot && opsSnapshot.size > 0) {
|
|
156
|
-
setTimeout(() => {
|
|
157
|
-
for (const [key, count] of opsSnapshot) {
|
|
158
|
-
console.warn(
|
|
159
|
-
`[mvc-kit] Ghost async operation detected: "${key}" had ${count} pending call(s) when the ${className} was disposed. Consider using disposeSignal to cancel in-flight work.`
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
}, ghostTimeout);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
return wrappedKeys;
|
|
16
|
+
const { instance, stopPrototype, reservedKeys, lifecycleHooks, isDisposed, isInitialized, asyncStates, asyncSnapshots, asyncListeners, notifyAsync, addCleanup, ghostTimeout, className, activeOps } = ctx;
|
|
17
|
+
if (__DEV__) {
|
|
18
|
+
for (const key of reservedKeys) if (Object.getOwnPropertyDescriptor(instance, key)?.value !== void 0) {
|
|
19
|
+
let fromPrototype = false;
|
|
20
|
+
let proto = Object.getPrototypeOf(instance);
|
|
21
|
+
while (proto && proto !== Object.prototype) {
|
|
22
|
+
const desc = Object.getOwnPropertyDescriptor(proto, key);
|
|
23
|
+
if (desc && typeof desc.value === "function") {
|
|
24
|
+
fromPrototype = true;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
proto = Object.getPrototypeOf(proto);
|
|
28
|
+
}
|
|
29
|
+
if (!fromPrototype) throw new Error(`[mvc-kit] "${key}" is a reserved property on ${className} and cannot be overridden.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const methodEntries = ctx.methods ?? (() => {
|
|
33
|
+
const result = [];
|
|
34
|
+
const processed = /* @__PURE__ */ new Set();
|
|
35
|
+
walkPrototypeChain(instance, stopPrototype, (key, desc) => {
|
|
36
|
+
if (desc.get || desc.set) return;
|
|
37
|
+
if (typeof desc.value !== "function") return;
|
|
38
|
+
if (key.startsWith("_")) return;
|
|
39
|
+
if (lifecycleHooks.has(key)) return;
|
|
40
|
+
if (processed.has(key)) return;
|
|
41
|
+
processed.add(key);
|
|
42
|
+
result.push({
|
|
43
|
+
key,
|
|
44
|
+
fn: desc.value
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
})();
|
|
49
|
+
const wrappedKeys = [];
|
|
50
|
+
for (const { key, fn: original } of methodEntries) {
|
|
51
|
+
let pruned = false;
|
|
52
|
+
const wrapper = function(...args) {
|
|
53
|
+
if (isDisposed()) {
|
|
54
|
+
if (__DEV__) console.warn(`[mvc-kit] "${key}" called after dispose — ignored.`);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (__DEV__ && !isInitialized()) console.warn(`[mvc-kit] "${key}" called before init(). Async tracking is active only after init().`);
|
|
58
|
+
let result;
|
|
59
|
+
try {
|
|
60
|
+
result = original.apply(instance, args);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
if (!result || typeof result.then !== "function") {
|
|
65
|
+
if (!pruned) {
|
|
66
|
+
pruned = true;
|
|
67
|
+
asyncStates.delete(key);
|
|
68
|
+
asyncSnapshots.delete(key);
|
|
69
|
+
instance[key] = original.bind(instance);
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
let internal = asyncStates.get(key);
|
|
74
|
+
if (!internal) {
|
|
75
|
+
internal = {
|
|
76
|
+
loading: false,
|
|
77
|
+
error: null,
|
|
78
|
+
errorCode: null,
|
|
79
|
+
count: 0
|
|
80
|
+
};
|
|
81
|
+
asyncStates.set(key, internal);
|
|
82
|
+
}
|
|
83
|
+
internal.count++;
|
|
84
|
+
internal.loading = true;
|
|
85
|
+
internal.error = null;
|
|
86
|
+
internal.errorCode = null;
|
|
87
|
+
asyncSnapshots.set(key, LOADING_TASK_STATE);
|
|
88
|
+
notifyAsync();
|
|
89
|
+
if (__DEV__ && activeOps) activeOps.set(key, (activeOps.get(key) ?? 0) + 1);
|
|
90
|
+
const finalizeOp = (errorMsg, errorCode) => {
|
|
91
|
+
internal.count--;
|
|
92
|
+
internal.loading = internal.count > 0;
|
|
93
|
+
if (errorMsg !== void 0) {
|
|
94
|
+
internal.error = errorMsg;
|
|
95
|
+
internal.errorCode = errorCode ?? null;
|
|
96
|
+
}
|
|
97
|
+
asyncSnapshots.set(key, Object.freeze({
|
|
98
|
+
loading: internal.loading,
|
|
99
|
+
error: internal.error,
|
|
100
|
+
errorCode: internal.errorCode
|
|
101
|
+
}));
|
|
102
|
+
notifyAsync();
|
|
103
|
+
if (__DEV__ && activeOps) {
|
|
104
|
+
const c = (activeOps.get(key) ?? 1) - 1;
|
|
105
|
+
if (c <= 0) activeOps.delete(key);
|
|
106
|
+
else activeOps.set(key, c);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
return result.then((value) => {
|
|
110
|
+
if (!isDisposed()) finalizeOp();
|
|
111
|
+
return value;
|
|
112
|
+
}, (error) => {
|
|
113
|
+
if (isAbortError(error)) {
|
|
114
|
+
if (!isDisposed()) finalizeOp();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (isDisposed()) return void 0;
|
|
118
|
+
const classified = classifyError(error);
|
|
119
|
+
finalizeOp(classified.message, classified.code);
|
|
120
|
+
throw error;
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
wrappedKeys.push(key);
|
|
124
|
+
instance[key] = wrapper;
|
|
125
|
+
}
|
|
126
|
+
if (wrappedKeys.length > 0) addCleanup(() => {
|
|
127
|
+
const opsSnapshot = __DEV__ && activeOps ? new Map(activeOps) : null;
|
|
128
|
+
for (const k of wrappedKeys) if (__DEV__) instance[k] = () => {
|
|
129
|
+
console.warn(`[mvc-kit] "${k}" called after dispose — ignored.`);
|
|
130
|
+
};
|
|
131
|
+
else instance[k] = () => void 0;
|
|
132
|
+
asyncListeners.clear();
|
|
133
|
+
asyncStates.clear();
|
|
134
|
+
asyncSnapshots.clear();
|
|
135
|
+
if (__DEV__ && opsSnapshot && opsSnapshot.size > 0) setTimeout(() => {
|
|
136
|
+
for (const [key, count] of opsSnapshot) console.warn(`[mvc-kit] Ghost async operation detected: "${key}" had ${count} pending call(s) when the ${className} was disposed. Consider using disposeSignal to cancel in-flight work.`);
|
|
137
|
+
}, ghostTimeout);
|
|
138
|
+
});
|
|
139
|
+
return wrappedKeys;
|
|
167
140
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
//# sourceMappingURL=wrapAsyncMethods.js.map
|
|
141
|
+
//#endregion
|
|
142
|
+
export { wrapAsyncMethods };
|
|
143
|
+
|
|
144
|
+
//# sourceMappingURL=wrapAsyncMethods.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrapAsyncMethods.js","sources":["../src/wrapAsyncMethods.ts"],"sourcesContent":["import { isAbortError, classifyError } from './errors';\nimport { walkPrototypeChain } from './walkPrototypeChain';\nimport type { TaskState } from './types';\n\nconst __DEV__ = typeof __MVC_KIT_DEV__ !== 'undefined' && __MVC_KIT_DEV__;\n\nconst LOADING_TASK_STATE: TaskState = Object.freeze({ loading: true, error: null, errorCode: null });\n\n/** @internal Mutable internal async tracking state per method. */\nexport interface InternalTaskState {\n loading: boolean;\n error: string | null;\n errorCode: TaskState['errorCode'];\n count: number;\n}\n\n/** @internal Configuration for the shared async method wrapping logic. */\nexport interface AsyncTrackingContext {\n instance: object;\n stopPrototype: object;\n reservedKeys: readonly string[];\n lifecycleHooks: Set<string>;\n isDisposed: () => boolean;\n isInitialized: () => boolean;\n asyncStates: Map<string, InternalTaskState>;\n asyncSnapshots: Map<string, TaskState>;\n asyncListeners: Set<() => void>;\n notifyAsync: () => void;\n addCleanup: (fn: () => void) => void;\n ghostTimeout: number;\n className: string;\n activeOps: Map<string, number> | null;\n /** Pre-scanned methods from class metadata cache. When provided, skips prototype walk. */\n methods?: Array<{ key: string; fn: Function }>;\n}\n\n/**\n * Shared async method wrapping logic used by both ViewModel and Resource.\n * Walks the prototype chain, wraps methods with async tracking, and registers cleanup.\n * Returns the list of wrapped method keys.\n */\nexport function wrapAsyncMethods(ctx: AsyncTrackingContext): string[] {\n const {\n instance,\n stopPrototype,\n reservedKeys,\n lifecycleHooks,\n isDisposed,\n isInitialized,\n asyncStates,\n asyncSnapshots,\n asyncListeners,\n notifyAsync,\n addCleanup,\n ghostTimeout,\n className,\n activeOps,\n } = ctx;\n\n // Instance property reserved key check (DEV-only — prototype check in constructor catches most cases)\n // Skip own properties that are bound methods from bindPublicMethods (the key\n // also exists as a method on the prototype chain). Only reject class-field overrides.\n if (__DEV__) {\n for (const key of reservedKeys) {\n if (Object.getOwnPropertyDescriptor(instance, key)?.value !== undefined) {\n // Check if the prototype chain has a method with this name —\n // if so, the own property is from bindPublicMethods, not a user override\n let fromPrototype = false;\n let proto = Object.getPrototypeOf(instance);\n while (proto && proto !== Object.prototype) {\n const desc = Object.getOwnPropertyDescriptor(proto, key);\n if (desc && typeof desc.value === 'function') { fromPrototype = true; break; }\n proto = Object.getPrototypeOf(proto);\n }\n if (!fromPrototype) {\n throw new Error(\n `[mvc-kit] \"${key}\" is a reserved property on ${className} and cannot be overridden.`\n );\n }\n }\n }\n }\n\n // Use pre-scanned methods from class metadata cache, or walk prototype chain\n const methodEntries: Array<{ key: string; fn: Function }> = ctx.methods ?? (() => {\n const result: Array<{ key: string; fn: Function }> = [];\n const processed = new Set<string>();\n walkPrototypeChain(instance, stopPrototype, (key, desc) => {\n if (desc.get || desc.set) return;\n if (typeof desc.value !== 'function') return;\n if (key.startsWith('_')) return;\n if (lifecycleHooks.has(key)) return;\n if (processed.has(key)) return;\n processed.add(key);\n result.push({ key, fn: desc.value });\n });\n return result;\n })();\n\n const wrappedKeys: string[] = [];\n\n for (const { key, fn: original } of methodEntries) {\n let pruned = false;\n\n const wrapper = function (this: any, ...args: unknown[]) {\n // Disposed guard\n if (isDisposed()) {\n if (__DEV__) {\n console.warn(`[mvc-kit] \"${key}\" called after dispose — ignored.`);\n }\n return undefined;\n }\n\n // Pre-init guard (DEV only — method still executes)\n if (__DEV__ && !isInitialized()) {\n console.warn(\n `[mvc-kit] \"${key}\" called before init(). ` +\n `Async tracking is active only after init().`\n );\n }\n\n let result: unknown;\n try {\n result = original.apply(instance, args);\n } catch (e) {\n // Sync throw — not tracked as async\n throw e;\n }\n\n // Sync detection: if not thenable, prune from async tracking\n if (!result || typeof (result as any).then !== 'function') {\n if (!pruned) {\n pruned = true;\n // Remove from async maps\n asyncStates.delete(key);\n asyncSnapshots.delete(key);\n // Replace wrapper with bound original for zero overhead\n (instance as any)[key] = original.bind(instance);\n }\n return result;\n }\n\n // ── Async tracking ──────────────────────────────────────\n let internal = asyncStates.get(key);\n if (!internal) {\n internal = { loading: false, error: null, errorCode: null, count: 0 };\n asyncStates.set(key, internal);\n }\n\n internal.count++;\n internal.loading = true;\n internal.error = null;\n internal.errorCode = null;\n asyncSnapshots.set(key, LOADING_TASK_STATE);\n notifyAsync();\n\n if (__DEV__ && activeOps) {\n activeOps.set(key, (activeOps.get(key) ?? 0) + 1);\n }\n\n // Shared bookkeeping: decrement count, snapshot state, update DEV active ops\n const finalizeOp = (errorMsg?: string | null, errorCode?: TaskState['errorCode']) => {\n internal!.count--;\n internal!.loading = internal!.count > 0;\n if (errorMsg !== undefined) {\n internal!.error = errorMsg;\n internal!.errorCode = errorCode ?? null;\n }\n asyncSnapshots.set(\n key,\n Object.freeze({ loading: internal!.loading, error: internal!.error, errorCode: internal!.errorCode }),\n );\n notifyAsync();\n\n if (__DEV__ && activeOps) {\n const c = (activeOps.get(key) ?? 1) - 1;\n if (c <= 0) activeOps.delete(key);\n else activeOps.set(key, c);\n }\n };\n\n return (result as Promise<unknown>).then(\n (value) => {\n if (!isDisposed()) finalizeOp();\n return value;\n },\n (error) => {\n // AbortError — silently swallow\n if (isAbortError(error)) {\n if (!isDisposed()) finalizeOp();\n return undefined;\n }\n\n // Disposed — fizzle silently\n if (isDisposed()) return undefined;\n\n const classified = classifyError(error);\n finalizeOp(classified.message, classified.code);\n\n // Re-throw to preserve standard Promise rejection\n throw error;\n },\n );\n };\n\n wrappedKeys.push(key);\n (instance as any)[key] = wrapper;\n }\n\n // Register cleanup for disposal\n if (wrappedKeys.length > 0) {\n addCleanup(() => {\n // Snapshot active ops for ghost check before clearing\n const opsSnapshot = __DEV__ && activeOps ? new Map(activeOps) : null;\n\n // Swap all wrapped methods to no-ops (with DEV warning)\n for (const k of wrappedKeys) {\n if (__DEV__) {\n (instance as any)[k] = () => {\n console.warn(`[mvc-kit] \"${k}\" called after dispose — ignored.`);\n return undefined;\n };\n } else {\n (instance as any)[k] = () => undefined;\n }\n }\n\n // Clear async state\n asyncListeners.clear();\n asyncStates.clear();\n asyncSnapshots.clear();\n\n // DEV: schedule ghost check\n if (__DEV__ && opsSnapshot && opsSnapshot.size > 0) {\n setTimeout(() => {\n for (const [key, count] of opsSnapshot) {\n console.warn(\n `[mvc-kit] Ghost async operation detected: \"${key}\" had ${count} ` +\n `pending call(s) when the ${className} was disposed. ` +\n `Consider using disposeSignal to cancel in-flight work.`\n );\n }\n }, ghostTimeout);\n }\n });\n }\n\n return wrappedKeys;\n}\n"],"names":[],"mappings":";;AAIA,MAAM,UAAU,OAAO,oBAAoB,eAAe;AAE1D,MAAM,qBAAgC,OAAO,OAAO,EAAE,SAAS,MAAM,OAAO,MAAM,WAAW,MAAM;AAmC5F,SAAS,iBAAiB,KAAqC;AACpE,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,IACE;AAKJ,MAAI,SAAS;AACX,eAAW,OAAO,cAAc;AAC9B,UAAI,OAAO,yBAAyB,UAAU,GAAG,GAAG,UAAU,QAAW;AAGvE,YAAI,gBAAgB;AACpB,YAAI,QAAQ,OAAO,eAAe,QAAQ;AAC1C,eAAO,SAAS,UAAU,OAAO,WAAW;AAC1C,gBAAM,OAAO,OAAO,yBAAyB,OAAO,GAAG;AACvD,cAAI,QAAQ,OAAO,KAAK,UAAU,YAAY;AAAE,4BAAgB;AAAM;AAAA,UAAO;AAC7E,kBAAQ,OAAO,eAAe,KAAK;AAAA,QACrC;AACA,YAAI,CAAC,eAAe;AAClB,gBAAM,IAAI;AAAA,YACR,cAAc,GAAG,+BAA+B,SAAS;AAAA,UAAA;AAAA,QAE7D;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAGA,QAAM,gBAAsD,IAAI,YAAY,MAAM;AAChF,UAAM,SAA+C,CAAA;AACrD,UAAM,gCAAgB,IAAA;AACtB,uBAAmB,UAAU,eAAe,CAAC,KAAK,SAAS;AACzD,UAAI,KAAK,OAAO,KAAK,IAAK;AAC1B,UAAI,OAAO,KAAK,UAAU,WAAY;AACtC,UAAI,IAAI,WAAW,GAAG,EAAG;AACzB,UAAI,eAAe,I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AACf,kBAAY,MAAA;AACZ,qBAAe,MAAA;AAGf,UAAI,WAAW,eAAe,YAAY,OAAO,GAAG;AAClD,mBAAW,MAAM;AACf,qBAAW,CAAC,KAAK,KAAK,KAAK,aAAa;AACtC,oBAAQ;AAAA,cACN,8CAA8C,GAAG,SAAS,KAAK,6BACnC,SAAS;AAAA,YAAA;AAAA,UAGzC;AAAA,QACF,GAAG,YAAY;AAAA,MACjB;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"wrapAsyncMethods.js","names":[],"sources":["../src/wrapAsyncMethods.ts"],"sourcesContent":["import { isAbortError, classifyError } from './errors';\nimport { walkPrototypeChain } from './walkPrototypeChain';\nimport type { TaskState } from './types';\n\nconst __DEV__ = typeof __MVC_KIT_DEV__ !== 'undefined' && __MVC_KIT_DEV__;\n\nconst LOADING_TASK_STATE: TaskState = Object.freeze({ loading: true, error: null, errorCode: null });\n\n/** @internal Mutable internal async tracking state per method. */\nexport interface InternalTaskState {\n loading: boolean;\n error: string | null;\n errorCode: TaskState['errorCode'];\n count: number;\n}\n\n/** @internal Configuration for the shared async method wrapping logic. */\nexport interface AsyncTrackingContext {\n instance: object;\n stopPrototype: object;\n reservedKeys: readonly string[];\n lifecycleHooks: Set<string>;\n isDisposed: () => boolean;\n isInitialized: () => boolean;\n asyncStates: Map<string, InternalTaskState>;\n asyncSnapshots: Map<string, TaskState>;\n asyncListeners: Set<() => void>;\n notifyAsync: () => void;\n addCleanup: (fn: () => void) => void;\n ghostTimeout: number;\n className: string;\n activeOps: Map<string, number> | null;\n /** Pre-scanned methods from class metadata cache. When provided, skips prototype walk. */\n methods?: Array<{ key: string; fn: Function }>;\n}\n\n/**\n * Shared async method wrapping logic used by both ViewModel and Resource.\n * Walks the prototype chain, wraps methods with async tracking, and registers cleanup.\n * Returns the list of wrapped method keys.\n */\nexport function wrapAsyncMethods(ctx: AsyncTrackingContext): string[] {\n const {\n instance,\n stopPrototype,\n reservedKeys,\n lifecycleHooks,\n isDisposed,\n isInitialized,\n asyncStates,\n asyncSnapshots,\n asyncListeners,\n notifyAsync,\n addCleanup,\n ghostTimeout,\n className,\n activeOps,\n } = ctx;\n\n // Instance property reserved key check (DEV-only — prototype check in constructor catches most cases)\n // Skip own properties that are bound methods from bindPublicMethods (the key\n // also exists as a method on the prototype chain). Only reject class-field overrides.\n if (__DEV__) {\n for (const key of reservedKeys) {\n if (Object.getOwnPropertyDescriptor(instance, key)?.value !== undefined) {\n // Check if the prototype chain has a method with this name —\n // if so, the own property is from bindPublicMethods, not a user override\n let fromPrototype = false;\n let proto = Object.getPrototypeOf(instance);\n while (proto && proto !== Object.prototype) {\n const desc = Object.getOwnPropertyDescriptor(proto, key);\n if (desc && typeof desc.value === 'function') { fromPrototype = true; break; }\n proto = Object.getPrototypeOf(proto);\n }\n if (!fromPrototype) {\n throw new Error(\n `[mvc-kit] \"${key}\" is a reserved property on ${className} and cannot be overridden.`\n );\n }\n }\n }\n }\n\n // Use pre-scanned methods from class metadata cache, or walk prototype chain\n const methodEntries: Array<{ key: string; fn: Function }> = ctx.methods ?? (() => {\n const result: Array<{ key: string; fn: Function }> = [];\n const processed = new Set<string>();\n walkPrototypeChain(instance, stopPrototype, (key, desc) => {\n if (desc.get || desc.set) return;\n if (typeof desc.value !== 'function') return;\n if (key.startsWith('_')) return;\n if (lifecycleHooks.has(key)) return;\n if (processed.has(key)) return;\n processed.add(key);\n result.push({ key, fn: desc.value });\n });\n return result;\n })();\n\n const wrappedKeys: string[] = [];\n\n for (const { key, fn: original } of methodEntries) {\n let pruned = false;\n\n const wrapper = function (this: any, ...args: unknown[]) {\n // Disposed guard\n if (isDisposed()) {\n if (__DEV__) {\n console.warn(`[mvc-kit] \"${key}\" called after dispose — ignored.`);\n }\n return undefined;\n }\n\n // Pre-init guard (DEV only — method still executes)\n if (__DEV__ && !isInitialized()) {\n console.warn(\n `[mvc-kit] \"${key}\" called before init(). ` +\n `Async tracking is active only after init().`\n );\n }\n\n let result: unknown;\n try {\n result = original.apply(instance, args);\n } catch (e) {\n // Sync throw — not tracked as async\n throw e;\n }\n\n // Sync detection: if not thenable, prune from async tracking\n if (!result || typeof (result as any).then !== 'function') {\n if (!pruned) {\n pruned = true;\n // Remove from async maps\n asyncStates.delete(key);\n asyncSnapshots.delete(key);\n // Replace wrapper with bound original for zero overhead\n (instance as any)[key] = original.bind(instance);\n }\n return result;\n }\n\n // ── Async tracking ──────────────────────────────────────\n let internal = asyncStates.get(key);\n if (!internal) {\n internal = { loading: false, error: null, errorCode: null, count: 0 };\n asyncStates.set(key, internal);\n }\n\n internal.count++;\n internal.loading = true;\n internal.error = null;\n internal.errorCode = null;\n asyncSnapshots.set(key, LOADING_TASK_STATE);\n notifyAsync();\n\n if (__DEV__ && activeOps) {\n activeOps.set(key, (activeOps.get(key) ?? 0) + 1);\n }\n\n // Shared bookkeeping: decrement count, snapshot state, update DEV active ops\n const finalizeOp = (errorMsg?: string | null, errorCode?: TaskState['errorCode']) => {\n internal!.count--;\n internal!.loading = internal!.count > 0;\n if (errorMsg !== undefined) {\n internal!.error = errorMsg;\n internal!.errorCode = errorCode ?? null;\n }\n asyncSnapshots.set(\n key,\n Object.freeze({ loading: internal!.loading, error: internal!.error, errorCode: internal!.errorCode }),\n );\n notifyAsync();\n\n if (__DEV__ && activeOps) {\n const c = (activeOps.get(key) ?? 1) - 1;\n if (c <= 0) activeOps.delete(key);\n else activeOps.set(key, c);\n }\n };\n\n return (result as Promise<unknown>).then(\n (value) => {\n if (!isDisposed()) finalizeOp();\n return value;\n },\n (error) => {\n // AbortError — silently swallow\n if (isAbortError(error)) {\n if (!isDisposed()) finalizeOp();\n return undefined;\n }\n\n // Disposed — fizzle silently\n if (isDisposed()) return undefined;\n\n const classified = classifyError(error);\n finalizeOp(classified.message, classified.code);\n\n // Re-throw to preserve standard Promise rejection\n throw error;\n },\n );\n };\n\n wrappedKeys.push(key);\n (instance as any)[key] = wrapper;\n }\n\n // Register cleanup for disposal\n if (wrappedKeys.length > 0) {\n addCleanup(() => {\n // Snapshot active ops for ghost check before clearing\n const opsSnapshot = __DEV__ && activeOps ? new Map(activeOps) : null;\n\n // Swap all wrapped methods to no-ops (with DEV warning)\n for (const k of wrappedKeys) {\n if (__DEV__) {\n (instance as any)[k] = () => {\n console.warn(`[mvc-kit] \"${k}\" called after dispose — ignored.`);\n return undefined;\n };\n } else {\n (instance as any)[k] = () => undefined;\n }\n }\n\n // Clear async state\n asyncListeners.clear();\n asyncStates.clear();\n asyncSnapshots.clear();\n\n // DEV: schedule ghost check\n if (__DEV__ && opsSnapshot && opsSnapshot.size > 0) {\n setTimeout(() => {\n for (const [key, count] of opsSnapshot) {\n console.warn(\n `[mvc-kit] Ghost async operation detected: \"${key}\" had ${count} ` +\n `pending call(s) when the ${className} was disposed. ` +\n `Consider using disposeSignal to cancel in-flight work.`\n );\n }\n }, ghostTimeout);\n }\n });\n }\n\n return wrappedKeys;\n}\n"],"mappings":";;;AAIA,IAAM,UAAU,OAAO,oBAAoB,eAAe;AAE1D,IAAM,qBAAgC,OAAO,OAAO;CAAE,SAAS;CAAM,OAAO;CAAM,WAAW;CAAM,CAAC;;;;;;AAmCpG,SAAgB,iBAAiB,KAAqC;CACpE,MAAM,EACJ,UACA,eACA,cACA,gBACA,YACA,eACA,aACA,gBACA,gBACA,aACA,YACA,cACA,WACA,cACE;AAKJ,KAAI;OACG,MAAM,OAAO,aAChB,KAAI,OAAO,yBAAyB,UAAU,IAAI,EAAE,UAAU,KAAA,GAAW;GAGvE,IAAI,gBAAgB;GACpB,IAAI,QAAQ,OAAO,eAAe,SAAS;AAC3C,UAAO,SAAS,UAAU,OAAO,WAAW;IAC1C,MAAM,OAAO,OAAO,yBAAyB,OAAO,IAAI;AACxD,QAAI,QAAQ,OAAO,KAAK,UAAU,YAAY;AAAE,qBAAgB;AAAM;;AACtE,YAAQ,OAAO,eAAe,MAAM;;AAEtC,OAAI,CAAC,cACH,OAAM,IAAI,MACR,cAAc,IA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,YACzB,SAAQ,KACN,8CAA8C,IAAI,QAAQ,MAAM,4BACpC,UAAU,uEAEvC;KAEF,aAAa;GAElB;AAGJ,QAAO"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Channel, singleton, teardownAll } from 'mvc-kit';
|
|
2
|
+
import type { ChannelStatus } from 'mvc-kit';
|
|
3
|
+
|
|
4
|
+
// Channel: Persistent connection with auto-reconnect and typed messages
|
|
5
|
+
//
|
|
6
|
+
// Extend Channel<MessageMap> and implement two abstract methods:
|
|
7
|
+
// open(signal) — establish the connection (WebSocket, SSE, etc.)
|
|
8
|
+
// close() — tear down the transport
|
|
9
|
+
//
|
|
10
|
+
// The framework handles connection status, reconnect with exponential
|
|
11
|
+
// backoff, message routing, and lifecycle management.
|
|
12
|
+
|
|
13
|
+
// --- Message type map ---
|
|
14
|
+
|
|
15
|
+
interface ChatMessages {
|
|
16
|
+
message: { userId: string; text: string };
|
|
17
|
+
typing: { userId: string };
|
|
18
|
+
presence: { online: string[] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// --- Channel subclass ---
|
|
22
|
+
|
|
23
|
+
class ChatChannel extends Channel<ChatMessages> {
|
|
24
|
+
// Tune reconnect behavior via static overrides
|
|
25
|
+
static override RECONNECT_BASE = 1000; // initial backoff (ms)
|
|
26
|
+
static override RECONNECT_MAX = 30000; // max backoff cap (ms)
|
|
27
|
+
static override MAX_ATTEMPTS = 5; // give up after 5 attempts
|
|
28
|
+
|
|
29
|
+
private ws: { close(): void } | null = null;
|
|
30
|
+
|
|
31
|
+
// open() is called by the framework when connect() is invoked.
|
|
32
|
+
// The signal aborts on disconnect() or dispose().
|
|
33
|
+
protected open(signal: AbortSignal): void {
|
|
34
|
+
// In production, this would be:
|
|
35
|
+
// this.ws = new WebSocket('wss://chat.example.com');
|
|
36
|
+
// this.ws.onmessage = (e) => {
|
|
37
|
+
// const { type, payload } = JSON.parse(e.data);
|
|
38
|
+
// this.receive(type, payload);
|
|
39
|
+
// };
|
|
40
|
+
|
|
41
|
+
// Simulated connection
|
|
42
|
+
console.log('Connecting...');
|
|
43
|
+
const connectTimer = setTimeout(() => {
|
|
44
|
+
if (!signal.aborted) {
|
|
45
|
+
console.log('Connected!');
|
|
46
|
+
// Simulate incoming messages
|
|
47
|
+
this.receive('presence', { online: ['alice', 'bob'] });
|
|
48
|
+
this.receive('message', { userId: 'alice', text: 'Hello!' });
|
|
49
|
+
}
|
|
50
|
+
}, 100);
|
|
51
|
+
|
|
52
|
+
this.ws = {
|
|
53
|
+
close: () => clearTimeout(connectTimer),
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Always clean up when the signal aborts (disconnect or dispose)
|
|
57
|
+
signal.addEventListener('abort', () => this.ws?.close());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// close() tears down the transport. Must not throw.
|
|
61
|
+
protected close(): void {
|
|
62
|
+
this.ws?.close();
|
|
63
|
+
this.ws = null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// --- Usage ---
|
|
68
|
+
|
|
69
|
+
const chat = singleton(ChatChannel);
|
|
70
|
+
chat.init();
|
|
71
|
+
|
|
72
|
+
// Subscribe to connection status changes
|
|
73
|
+
chat.subscribe((next: ChannelStatus, prev: ChannelStatus) => {
|
|
74
|
+
console.log(`Status: connected=${next.connected}, reconnecting=${next.reconnecting}`);
|
|
75
|
+
if (next.error) console.log(`Error: ${next.error}`);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Subscribe to typed messages
|
|
79
|
+
const unsubMessage = chat.on('message', ({ userId, text }) => {
|
|
80
|
+
console.log(`${userId}: ${text}`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// One-time subscription — auto-unsubscribes after first event
|
|
84
|
+
chat.once('presence', ({ online }) => {
|
|
85
|
+
console.log('Initial presence:', online);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Initiate connection
|
|
89
|
+
chat.connect();
|
|
90
|
+
|
|
91
|
+
// Read current status at any time
|
|
92
|
+
console.log('Connected:', chat.state.connected);
|
|
93
|
+
console.log('Reconnecting:', chat.state.reconnecting);
|
|
94
|
+
console.log('Attempt:', chat.state.attempt);
|
|
95
|
+
|
|
96
|
+
// Manually disconnect (cancels pending reconnect, resets status)
|
|
97
|
+
// chat.disconnect();
|
|
98
|
+
|
|
99
|
+
// Reconnect after disconnect
|
|
100
|
+
// chat.connect();
|
|
101
|
+
|
|
102
|
+
// Unsubscribe from a message type
|
|
103
|
+
unsubMessage();
|
|
104
|
+
|
|
105
|
+
// Cleanup — cancels timers, aborts signals, calls close()
|
|
106
|
+
setTimeout(() => {
|
|
107
|
+
chat.dispose();
|
|
108
|
+
teardownAll();
|
|
109
|
+
}, 500);
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Collection } from 'mvc-kit';
|
|
2
|
+
|
|
3
|
+
// Collection: Reactive typed array with CRUD and query methods
|
|
4
|
+
|
|
5
|
+
interface Todo {
|
|
6
|
+
id: string;
|
|
7
|
+
text: string;
|
|
8
|
+
done: boolean;
|
|
9
|
+
priority: 'low' | 'medium' | 'high';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// --- Basic usage ---
|
|
13
|
+
|
|
14
|
+
const todos = new Collection<Todo>();
|
|
15
|
+
|
|
16
|
+
// Subscribe to changes
|
|
17
|
+
todos.subscribe((items, prev) => {
|
|
18
|
+
console.log(`Collection changed: ${prev.length} → ${items.length} items`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// CRUD operations (trigger notifications)
|
|
22
|
+
|
|
23
|
+
// Add items
|
|
24
|
+
todos.add(
|
|
25
|
+
{ id: '1', text: 'Learn mvc-kit', done: false, priority: 'high' },
|
|
26
|
+
{ id: '2', text: 'Build an app', done: false, priority: 'medium' },
|
|
27
|
+
{ id: '3', text: 'Write tests', done: true, priority: 'low' }
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
console.log('Length:', todos.length); // 3
|
|
31
|
+
console.log('Items:', todos.items);
|
|
32
|
+
|
|
33
|
+
// Update an item
|
|
34
|
+
todos.update('1', { done: true });
|
|
35
|
+
console.log('After update:', todos.get('1')?.done); // true
|
|
36
|
+
|
|
37
|
+
// Remove an item
|
|
38
|
+
todos.remove('3');
|
|
39
|
+
console.log('After remove length:', todos.length); // 2
|
|
40
|
+
|
|
41
|
+
// Query operations (pure, no notifications)
|
|
42
|
+
|
|
43
|
+
// Get by id (O(1) lookup)
|
|
44
|
+
const todo = todos.get('1');
|
|
45
|
+
console.log('Get by id:', todo?.text);
|
|
46
|
+
|
|
47
|
+
// Check existence
|
|
48
|
+
console.log('Has id 1:', todos.has('1')); // true
|
|
49
|
+
console.log('Has id 99:', todos.has('99')); // false
|
|
50
|
+
|
|
51
|
+
// Find first match
|
|
52
|
+
const firstIncomplete = todos.find(t => !t.done);
|
|
53
|
+
console.log('First incomplete:', firstIncomplete?.text);
|
|
54
|
+
|
|
55
|
+
// Filter
|
|
56
|
+
const highPriority = todos.filter(t => t.priority === 'high');
|
|
57
|
+
console.log('High priority count:', highPriority.length);
|
|
58
|
+
|
|
59
|
+
// Sort (returns new array, doesn't mutate)
|
|
60
|
+
const sorted = todos.sorted((a, b) => a.text.localeCompare(b.text));
|
|
61
|
+
console.log('Sorted:', sorted.map(t => t.text));
|
|
62
|
+
|
|
63
|
+
// Map
|
|
64
|
+
const texts = todos.map(t => t.text);
|
|
65
|
+
console.log('Texts:', texts);
|
|
66
|
+
|
|
67
|
+
// Reset - replace all items
|
|
68
|
+
todos.reset([
|
|
69
|
+
{ id: 'a', text: 'New todo', done: false, priority: 'medium' }
|
|
70
|
+
]);
|
|
71
|
+
console.log('After reset length:', todos.length); // 1
|
|
72
|
+
|
|
73
|
+
// Clear all items
|
|
74
|
+
todos.clear();
|
|
75
|
+
console.log('After clear length:', todos.length); // 0
|
|
76
|
+
|
|
77
|
+
// --- Upsert: add-or-replace by ID ---
|
|
78
|
+
|
|
79
|
+
todos.reset([
|
|
80
|
+
{ id: '1', text: 'Learn mvc-kit', done: false, priority: 'high' },
|
|
81
|
+
{ id: '2', text: 'Build an app', done: false, priority: 'medium' },
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
// Upsert replaces existing items in-place and appends new ones
|
|
85
|
+
todos.upsert(
|
|
86
|
+
{ id: '2', text: 'Build an app', done: true, priority: 'medium' }, // replaces in position
|
|
87
|
+
{ id: '3', text: 'Ship it', done: false, priority: 'high' }, // appended
|
|
88
|
+
);
|
|
89
|
+
console.log('After upsert length:', todos.length); // 3
|
|
90
|
+
console.log('Item 2 done:', todos.get('2')?.done); // true (replaced)
|
|
91
|
+
console.log('Item 3 text:', todos.get('3')?.text); // 'Ship it' (new)
|
|
92
|
+
|
|
93
|
+
// --- Optimistic updates ---
|
|
94
|
+
|
|
95
|
+
// Reset with some data for the optimistic demo
|
|
96
|
+
todos.reset([
|
|
97
|
+
{ id: '1', text: 'Learn mvc-kit', done: false, priority: 'high' },
|
|
98
|
+
{ id: '2', text: 'Build an app', done: false, priority: 'medium' },
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
// Snapshot current state, apply mutations, get a rollback function
|
|
102
|
+
const rollback = todos.optimistic(() => {
|
|
103
|
+
todos.update('1', { done: true });
|
|
104
|
+
todos.remove('2');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
console.log('After optimistic:', todos.length); // 1
|
|
108
|
+
console.log('Item 1 done:', todos.get('1')?.done); // true
|
|
109
|
+
|
|
110
|
+
// If the server call fails, rollback restores pre-optimistic state
|
|
111
|
+
rollback();
|
|
112
|
+
|
|
113
|
+
console.log('After rollback:', todos.length); // 2
|
|
114
|
+
console.log('Item 1 done:', todos.get('1')?.done); // false
|
|
115
|
+
console.log('Item 2 exists:', todos.has('2')); // true
|
|
116
|
+
|
|
117
|
+
// Cleanup
|
|
118
|
+
todos.dispose();
|