@blac/react 0.0.2 → 0.0.4-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.
@@ -1,7 +1,10 @@
1
- import { Blac, BlocBase, ValueType, BlocClass } from 'blac';
2
- import React from 'react';
1
+ import { Blac, BlocBase, BlocClass, ValueType } from 'blac';
2
+ import React, { FC, ReactNode } from 'react';
3
3
 
4
- declare const BlacContext: React.Context<Blac<any, any> | null>;
4
+ declare const BlacApp: FC<{
5
+ children: JSX.Element;
6
+ blac: Blac<any, any>;
7
+ }>;
5
8
 
6
9
  /**
7
10
  * Keep track of the hierarchy of Blac BlocProvider components, so that
@@ -13,12 +16,55 @@ interface ProviderItem {
13
16
  parent?: string;
14
17
  bloc: BlocBase<any>;
15
18
  }
19
+ declare class BlacReact {
20
+ blac: Blac<any, any>;
21
+ blacContext: React.Context<Blac<any, any>>;
22
+ localContextProvider: React.Context<BlocBase<any> | null>;
23
+ static pluginKey: string;
24
+ constructor(blac: Blac<any, any>, blacContext: React.Context<Blac<any, any>>);
25
+ static getInstance(): BlacReact;
26
+ setup(): void;
27
+ /**
28
+ * React hook to get the bloc instance from the nearest BlocProvider
29
+ */
30
+ useLocalBlocContext(): BlocBase<any> | null;
31
+ private _contextLocalProviderKey;
32
+ get LocalProvider(): React.Provider<string>;
33
+ /**
34
+ * React hook to get the id from the nearest BlocProvider
35
+ */
36
+ useLocalProviderKey(): string;
37
+ addLocalBloc: (item: ProviderItem) => void;
38
+ removeLocalBloc: (id: string) => void;
39
+ createProviderId: () => string;
40
+ providerList: ProviderItem[];
41
+ getLocalBlocForProvider<B>(id: string, blocClass: BlocClass<B>): BlocBase<B> | undefined;
42
+ providerMounted(options: {
43
+ providerId: string;
44
+ localProviderKey?: string;
45
+ } & Parameters<BlacReact['useLocalProvider']>[0]): {
46
+ instance: BlocBase<any> | undefined;
47
+ destroyOnUnmount: boolean;
48
+ };
49
+ providerUnmounted(providerId: string | undefined): void;
50
+ readonly useLocalProvider: (options: {
51
+ bloc: BlocBase<any> | (() => BlocBase<any>);
52
+ debug?: boolean;
53
+ }) => string | undefined;
54
+ }
55
+
56
+ declare const BlocProvider: FC<{
57
+ children?: ReactNode;
58
+ bloc: (() => BlocBase<any>) | BlocBase<any>;
59
+ debug?: boolean;
60
+ }>;
16
61
 
17
62
  interface ExternalStore<B extends BlocBase<any>> {
18
63
  subscribe: (onStoreChange: () => void) => () => void;
19
64
  getSnapshot: () => ValueType<B>;
20
65
  getServerSnapshot?: () => ValueType<B>;
21
66
  }
67
+ declare const externalBlocStore: <B extends BlocBase<any>>(bloc: B) => ExternalStore<B>;
22
68
 
23
69
  type BlocHookData<B extends BlocBase<S>, S> = [
24
70
  value: ValueType<B>,
@@ -33,4 +79,4 @@ interface BlocHookOptions {
33
79
  }
34
80
  declare const useBloc: <B extends BlocBase<S>, S>(bloc: BlocClass<B> | (() => B), options?: BlocHookOptions) => BlocHookData<B, S>;
35
81
 
36
- export { BlacContext, BlocHookData, BlocHookOptions, ExternalStore, ProviderItem, useBloc };
82
+ export { BlacApp, BlacReact, BlocProvider, ExternalStore, externalBlocStore, useBloc };
@@ -133,7 +133,24 @@ class BlacReact {
133
133
  };
134
134
  }
135
135
 
136
- const BlacContext = createContext(null);
136
+ const BlacContext = createContext(null);
137
+ const BlacApp = (props) => {
138
+ const { children, blac } = props;
139
+ const blacApp = useMemo(() => new BlacReact(blac, BlacContext), [blac]);
140
+ if (!blacApp) {
141
+ throw new Error('BlacReact failed to initialize');
142
+ }
143
+ return React.createElement(BlacContext.Provider, { value: blac }, children);
144
+ };
145
+
146
+ const BlocProvider = ({ children, bloc, debug }) => {
147
+ const blacReact = BlacReact.getInstance();
148
+ const providerId = blacReact.useLocalProvider({ bloc, debug });
149
+ if (!providerId) {
150
+ return null;
151
+ }
152
+ return (React.createElement(blacReact.LocalProvider, { value: providerId }, children));
153
+ };
137
154
 
138
155
  const externalBlocStore = (bloc) => {
139
156
  return {
@@ -192,5 +209,5 @@ const useBloc = (bloc, options = {}) => {
192
209
  return [state, resolvedBloc];
193
210
  };
194
211
 
195
- export { BlacContext, useBloc };
212
+ export { BlacApp, BlacReact, BlocProvider, externalBlocStore, useBloc };
196
213
  //# sourceMappingURL=blac-react.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"blac-react.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"blac-react.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -135,7 +135,24 @@ class BlacReact {
135
135
  };
136
136
  }
137
137
 
138
- const BlacContext = React.createContext(null);
138
+ const BlacContext = React.createContext(null);
139
+ const BlacApp = (props) => {
140
+ const { children, blac } = props;
141
+ const blacApp = React.useMemo(() => new BlacReact(blac, BlacContext), [blac]);
142
+ if (!blacApp) {
143
+ throw new Error('BlacReact failed to initialize');
144
+ }
145
+ return React.createElement(BlacContext.Provider, { value: blac }, children);
146
+ };
147
+
148
+ const BlocProvider = ({ children, bloc, debug }) => {
149
+ const blacReact = BlacReact.getInstance();
150
+ const providerId = blacReact.useLocalProvider({ bloc, debug });
151
+ if (!providerId) {
152
+ return null;
153
+ }
154
+ return (React.createElement(blacReact.LocalProvider, { value: providerId }, children));
155
+ };
139
156
 
140
157
  const externalBlocStore = (bloc) => {
141
158
  return {
@@ -194,6 +211,9 @@ const useBloc = (bloc, options = {}) => {
194
211
  return [state, resolvedBloc];
195
212
  };
196
213
 
197
- exports.BlacContext = BlacContext;
214
+ exports.BlacApp = BlacApp;
215
+ exports.BlacReact = BlacReact;
216
+ exports.BlocProvider = BlocProvider;
217
+ exports.externalBlocStore = externalBlocStore;
198
218
  exports.useBloc = useBloc;
199
219
  //# sourceMappingURL=blac-react.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"blac-react.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"blac-react.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blac/react",
3
- "version": "0.0.2",
3
+ "version": "0.0.4-0",
4
4
  "license": "MIT",
5
5
  "main": "dist/blac-react.js",
6
6
  "module": "dist/blac-react.esm.js",