phecda-react 1.0.0 → 1.0.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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react from 'react';
2
- import { WebPhecda, Events, Construct } from 'phecda-web';
2
+ import { WebPhecda, Construct, Events } from 'phecda-web';
3
3
  export * from 'phecda-web';
4
4
 
5
5
  declare const PhecdaContext: react.Context<WebPhecda | undefined>;
@@ -7,6 +7,8 @@ declare const phecdaSymbol: unique symbol;
7
7
  declare function createPhecda(): WebPhecda;
8
8
 
9
9
  declare function usePhecda(): WebPhecda;
10
+ declare function getPhecda(phecda?: WebPhecda): WebPhecda;
11
+ declare function getR<Model extends Construct>(model: Model, phecda?: WebPhecda): InstanceType<Model>;
10
12
  declare function useEvent<Key extends keyof Events>(eventName: Key, cb: (event: Events[Key]) => void): ((arg: Events[Key]) => void)[];
11
13
  declare function useR<Model extends Construct>(model: Model): (InstanceType<Model> | (InstanceType<Model> extends infer T ? T extends InstanceType<Model> ? T extends {
12
14
  $$valtioSnapshot: infer S;
@@ -32,4 +34,4 @@ declare function useR<Model extends Construct>(model: Model): (InstanceType<Mode
32
34
  $$valtioSnapshot: infer S;
33
35
  } ? S : T_20 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_20 : T_20 extends Promise<unknown> ? Awaited<T_20> : T_20 extends object ? T_20 extends infer T_21 extends object ? { readonly [K_10 in keyof T_21]: any; } : never : T_20 : never : never; } : never : T_18 : never : never; } : never : T_16 : never : never; } : never : T_14 : never : never; } : never : T_12 : never : never; } : never : T_10 : never : never; } : never : T_8 : never : never; } : never : T_6 : never : never; } : never : T_4 : never : never; } : never : T_2 : never : never; } : never : T : never : never))[];
34
36
 
35
- export { PhecdaContext, createPhecda, phecdaSymbol, useEvent, usePhecda, useR };
37
+ export { PhecdaContext, createPhecda, getPhecda, getR, phecdaSymbol, useEvent, usePhecda, useR };
package/dist/index.js CHANGED
@@ -24,6 +24,8 @@ var src_exports = {};
24
24
  __export(src_exports, {
25
25
  PhecdaContext: () => PhecdaContext,
26
26
  createPhecda: () => createPhecda,
27
+ getPhecda: () => getPhecda,
28
+ getR: () => getR,
27
29
  phecdaSymbol: () => phecdaSymbol,
28
30
  useEvent: () => useEvent,
29
31
  usePhecda: () => usePhecda,
@@ -55,12 +57,25 @@ var cacheMap = /* @__PURE__ */ new WeakMap();
55
57
  function usePhecda() {
56
58
  const activePhecda = (0, import_react2.useContext)(PhecdaContext);
57
59
  if (!activePhecda)
58
- throw new Error("[phecda-react]: must under <PhecdaContext.Provider></PhecdaContext.Provider> or manually inject the phecda instance ");
60
+ throw new Error("[phecda-react]: must under <PhecdaContext.Provider></PhecdaContext.Provider> ");
59
61
  if (!cacheMap.has(activePhecda))
60
62
  cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
61
63
  return cacheMap.get(activePhecda);
62
64
  }
63
65
  __name(usePhecda, "usePhecda");
66
+ function getPhecda(phecda) {
67
+ const activePhecda = phecda || (0, import_phecda_web2.getDefaultPhecda)();
68
+ if (!activePhecda)
69
+ throw new Error("[phecda-react] manually inject the phecda instance if there is no default phecda");
70
+ if (!cacheMap.has(activePhecda))
71
+ cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
72
+ return cacheMap.get(activePhecda);
73
+ }
74
+ __name(getPhecda, "getPhecda");
75
+ function getR(model, phecda) {
76
+ return getPhecda(phecda).init(model);
77
+ }
78
+ __name(getR, "getR");
64
79
  function useEvent(eventName, cb) {
65
80
  (0, import_react2.useEffect)(() => {
66
81
  return () => import_phecda_web2.emitter.off(eventName, cb);
@@ -84,6 +99,8 @@ __name(useR, "useR");
84
99
  0 && (module.exports = {
85
100
  PhecdaContext,
86
101
  createPhecda,
102
+ getPhecda,
103
+ getR,
87
104
  phecdaSymbol,
88
105
  useEvent,
89
106
  usePhecda,
package/dist/index.mjs CHANGED
@@ -18,19 +18,32 @@ __name(createPhecda, "createPhecda");
18
18
  export * from "phecda-web";
19
19
 
20
20
  // src/hook.ts
21
- import { bindMethod, emitter } from "phecda-web";
21
+ import { bindMethod, emitter, getDefaultPhecda } from "phecda-web";
22
22
  import { useContext, useEffect } from "react";
23
23
  import { useSnapshot } from "valtio";
24
24
  var cacheMap = /* @__PURE__ */ new WeakMap();
25
25
  function usePhecda() {
26
26
  const activePhecda = useContext(PhecdaContext);
27
27
  if (!activePhecda)
28
- throw new Error("[phecda-react]: must under <PhecdaContext.Provider></PhecdaContext.Provider> or manually inject the phecda instance ");
28
+ throw new Error("[phecda-react]: must under <PhecdaContext.Provider></PhecdaContext.Provider> ");
29
29
  if (!cacheMap.has(activePhecda))
30
30
  cacheMap.set(activePhecda, bindMethod(activePhecda));
31
31
  return cacheMap.get(activePhecda);
32
32
  }
33
33
  __name(usePhecda, "usePhecda");
34
+ function getPhecda(phecda) {
35
+ const activePhecda = phecda || getDefaultPhecda();
36
+ if (!activePhecda)
37
+ throw new Error("[phecda-react] manually inject the phecda instance if there is no default phecda");
38
+ if (!cacheMap.has(activePhecda))
39
+ cacheMap.set(activePhecda, bindMethod(activePhecda));
40
+ return cacheMap.get(activePhecda);
41
+ }
42
+ __name(getPhecda, "getPhecda");
43
+ function getR(model, phecda) {
44
+ return getPhecda(phecda).init(model);
45
+ }
46
+ __name(getR, "getR");
34
47
  function useEvent(eventName, cb) {
35
48
  useEffect(() => {
36
49
  return () => emitter.off(eventName, cb);
@@ -53,6 +66,8 @@ __name(useR, "useR");
53
66
  export {
54
67
  PhecdaContext,
55
68
  createPhecda,
69
+ getPhecda,
70
+ getR,
56
71
  phecdaSymbol,
57
72
  useEvent,
58
73
  usePhecda,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-react",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "provide phecda function to react by valtio",
5
5
  "author": "fgsreally",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  "dependencies": {
19
19
  "react": "^18.2.0",
20
20
  "valtio": "^1.13.0",
21
- "phecda-web": "2.0.1"
21
+ "phecda-web": "2.0.2"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/react": "^18.2.48",