client.js 1.5 KB

12345678910111213141516171819202122232425262728293031
  1. /** Test package using the published registration protocol, locale and slots. */
  2. window.__ModuleLoader__.load({
  3. id: '@fixture/live-client',
  4. factory(require) {
  5. const React = require('react')
  6. const style = document.createElement('style')
  7. style.dataset.plugin = '@fixture/live-client'
  8. style.textContent = '[data-live-client] { color: rgb(12, 34, 56); position: absolute; bottom: 20px; right: 20px; }'
  9. document.head.append(style)
  10. return {
  11. inject: ['slots', 'locale'],
  12. apply(ctx) {
  13. const counters = document.documentElement.dataset
  14. counters.liveMounts = String(Number(counters.liveMounts ?? 0) + 1)
  15. ctx.effect(() => ctx.locale.register('fixtureLive', { zh: { active: '动态插件已启用' }, en: { active: 'Live plugin enabled' } }))
  16. ctx.slots.inject('shell.overlay', () => ctx.slots.register({
  17. name: 'shell.overlay', id: 'fixture-live-client', locale: 'fixtureLive',
  18. }, ({ t }) => React.createElement('div', { 'data-live-client': '' }, t('active'))))
  19. ctx.effect(() => {
  20. const ping = () => { counters.liveHits = String(Number(counters.liveHits ?? 0) + 1) }
  21. window.addEventListener('dsh-fixture-ping', ping)
  22. return async () => {
  23. window.removeEventListener('dsh-fixture-ping', ping)
  24. await Promise.resolve()
  25. counters.liveDisposals = String(Number(counters.liveDisposals ?? 0) + 1)
  26. }
  27. })
  28. },
  29. }
  30. },
  31. })