create-packer 1.21.6 → 1.21.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-packer",
3
- "version": "1.21.6",
3
+ "version": "1.21.8",
4
4
  "main": "index.js",
5
5
  "repository": "https://github.com/kevily/create-packer",
6
6
  "author": "1k <bug_zero@163.com>",
@@ -10,7 +10,7 @@
10
10
  "preview": "vite preview",
11
11
  "up:vite": "pnpm up vite @vitejs/* -L",
12
12
  "format": "prettier --write \"src/**/*.{ts,js,tsx,jsx,css,scss,less}\" \"./package.json\"",
13
- "lint": "eslint ./src/**/*.{tsx,ts} && stylelint ./src/**/*.{css,scss,less}",
13
+ "lint": "tsc --noEmit && eslint ./src/**/*.{tsx,ts} && stylelint ./src/**/*.{css,scss,less}",
14
14
  "lint:fix": "eslint ./src/**/*.{tsx,ts} --fix && stylelint ./src/**/*.{css,scss,less} --fix",
15
15
  "test:watchs": "jest --watch",
16
16
  "test": "jest",
@@ -1,3 +1,2 @@
1
- export { default as useComponentInstance } from './useComponentInstance'
2
1
  export { default as useLoadingAction } from './useLoadingAction'
3
2
  export { default as useInterval } from './useInterval'
@@ -9,7 +9,7 @@ import {
9
9
  } from 'react'
10
10
  import { createRoot } from 'react-dom/client'
11
11
 
12
- const instanceMap: Record<string, any> = {}
12
+ const instanceMap: Record<string, { pending: Promise<any>; instance: any }> = {}
13
13
 
14
14
  export interface refsType<P> {
15
15
  $setProps: (newProps: P) => void
@@ -21,9 +21,8 @@ export async function create<P extends Record<string, any>, Refs extends Record<
21
21
  Component: ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<Refs>>,
22
22
  props?: P
23
23
  ) {
24
- let $instance: Refs & refsType<P> = instanceMap[key]
25
-
26
- if (!$instance) {
24
+ let current = instanceMap[key]
25
+ if (!current) {
27
26
  const div = document.createElement('div')
28
27
  document.body.appendChild(div)
29
28
  const ApiComponent = forwardRef<Refs & refsType<P>>((__, refs) => {
@@ -45,17 +44,21 @@ export async function create<P extends Record<string, any>, Refs extends Record<
45
44
  })
46
45
  return <Component ref={ref} {...(state as any)} />
47
46
  })
48
- await new Promise<void>(resolve => {
49
- createRoot(div).render(
50
- <ApiComponent
51
- ref={instance => {
52
- $instance = instance!
53
- resolve()
54
- }}
55
- />
56
- )
57
- })
47
+ current = instanceMap[key] = {
48
+ instance: void 0,
49
+ pending: new Promise<void>(resolve => {
50
+ createRoot(div).render(
51
+ <ApiComponent
52
+ ref={instance => {
53
+ current.instance = instanceMap[key].instance = instance!
54
+ resolve()
55
+ }}
56
+ />
57
+ )
58
+ })
59
+ }
58
60
  }
59
- props && $instance.$setProps(props)
60
- return $instance
61
+ await current.pending
62
+ props && current.instance.$setProps(props)
63
+ return current.instance as Refs & refsType<P>
61
64
  }
@@ -1,31 +0,0 @@
1
- import { ForwardRefExoticComponent, PropsWithoutRef, RefAttributes, useEffect, useRef } from 'react'
2
- import { componentInstance } from '@/providers'
3
-
4
- export default function useComponentInstance<
5
- P extends Record<string, any>,
6
- Refs extends Record<string, any>
7
- >(
8
- key: string,
9
- Component: ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<Refs>>,
10
- props?: P
11
- ) {
12
- const $instance = useRef<(Refs & componentInstance.refsType<P>) | null>(null)
13
- const pending = useRef(false)
14
-
15
- useEffect(() => {
16
- if ($instance.current === null) {
17
- if (pending.current === false) {
18
- pending.current = true
19
- componentInstance.create(key, Component, props).then(instance => {
20
- $instance.current = instance
21
- props && $instance.current.$updateProps(props)
22
- pending.current = false
23
- })
24
- }
25
- } else {
26
- props && $instance.current?.$updateProps(props)
27
- }
28
- }, [props])
29
-
30
- return $instance
31
- }