@blac/react 0.0.4-0 → 0.0.13-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.
@@ -2,7 +2,7 @@ import { Blac, BlocBase, BlocClass, ValueType } from 'blac';
2
2
  import React, { FC, ReactNode } from 'react';
3
3
 
4
4
  declare const BlacApp: FC<{
5
- children: JSX.Element;
5
+ children: ReactNode;
6
6
  blac: Blac<any, any>;
7
7
  }>;
8
8
 
@@ -16,13 +16,18 @@ interface ProviderItem {
16
16
  parent?: string;
17
17
  bloc: BlocBase<any>;
18
18
  }
19
+ interface ProviderOptions<B> {
20
+ bloc: BlocClass<B> | (() => B);
21
+ debug?: boolean;
22
+ }
19
23
  declare class BlacReact {
20
24
  blac: Blac<any, any>;
21
25
  blacContext: React.Context<Blac<any, any>>;
22
26
  localContextProvider: React.Context<BlocBase<any> | null>;
23
27
  static pluginKey: string;
24
28
  constructor(blac: Blac<any, any>, blacContext: React.Context<Blac<any, any>>);
25
- static getInstance(): BlacReact;
29
+ static safeGetInstance(): BlacReact | undefined;
30
+ static getInstance(throwError?: boolean): BlacReact;
26
31
  setup(): void;
27
32
  /**
28
33
  * React hook to get the bloc instance from the nearest BlocProvider
@@ -47,17 +52,15 @@ declare class BlacReact {
47
52
  destroyOnUnmount: boolean;
48
53
  };
49
54
  providerUnmounted(providerId: string | undefined): void;
50
- readonly useLocalProvider: (options: {
51
- bloc: BlocBase<any> | (() => BlocBase<any>);
52
- debug?: boolean;
53
- }) => string | undefined;
55
+ readonly useLocalProvider: (options: ProviderOptions<any>) => string | undefined;
54
56
  }
55
57
 
56
- declare const BlocProvider: FC<{
58
+ interface BlocProviderProps<B> {
57
59
  children?: ReactNode;
58
- bloc: (() => BlocBase<any>) | BlocBase<any>;
60
+ bloc: BlocClass<B> | (() => B) | B;
59
61
  debug?: boolean;
60
- }>;
62
+ }
63
+ declare const BlocProvider: FC<BlocProviderProps<any>>;
61
64
 
62
65
  interface ExternalStore<B extends BlocBase<any>> {
63
66
  subscribe: (onStoreChange: () => void) => () => void;
@@ -66,17 +69,21 @@ interface ExternalStore<B extends BlocBase<any>> {
66
69
  }
67
70
  declare const externalBlocStore: <B extends BlocBase<any>>(bloc: B) => ExternalStore<B>;
68
71
 
69
- type BlocHookData<B extends BlocBase<S>, S> = [
70
- value: ValueType<B>,
71
- instance: B
72
- ];
73
- interface BlocHookOptions {
72
+ interface BlocResolveOptions {
74
73
  /**
75
74
  * Set to true if you want blac to automatically create a new instance of the bloc,
76
75
  * for this to work, pass the unconstructed class to the hook. The constructor should not expect any arguments.
77
76
  */
78
77
  create?: boolean;
79
78
  }
80
- declare const useBloc: <B extends BlocBase<S>, S>(bloc: BlocClass<B> | (() => B), options?: BlocHookOptions) => BlocHookData<B, S>;
79
+
80
+ type BlocHookData<B extends BlocBase<S>, S> = [
81
+ value: ValueType<B>,
82
+ instance: B,
83
+ Provider: FC<{
84
+ children: ReactNode;
85
+ }>
86
+ ];
87
+ declare const useBloc: <B extends BlocBase<S>, S>(bloc: BlocClass<B> | (() => B), options?: BlocResolveOptions) => BlocHookData<B, S>;
81
88
 
82
89
  export { BlacApp, BlacReact, BlocProvider, ExternalStore, externalBlocStore, useBloc };
@@ -11,6 +11,7 @@ class BlacReact {
11
11
  static pluginKey = 'blacReact';
12
12
  constructor(blac, blacContext) {
13
13
  // treat this as singleton
14
+ console.log('create', blac);
14
15
  const blacReact = blac.getPluginKey(BlacReact.pluginKey);
15
16
  // new setup
16
17
  this.blac = blac;
@@ -20,7 +21,12 @@ class BlacReact {
20
21
  return blacReact;
21
22
  }
22
23
  }
23
- static getInstance() {
24
+ static safeGetInstance() {
25
+ const blac = globalThis.blac;
26
+ const blacReact = blac?.getPluginKey(BlacReact.pluginKey);
27
+ return blacReact;
28
+ }
29
+ static getInstance(throwError = true) {
24
30
  const blac = globalThis.blac;
25
31
  if (!blac) {
26
32
  throw new Error('BlacReact: blac instance not found');
@@ -112,7 +118,7 @@ class BlacReact {
112
118
  return { instance, destroyOnUnmount };
113
119
  }
114
120
  providerUnmounted(providerId) {
115
- this.removeLocalBloc;
121
+ this.removeLocalBloc(providerId);
116
122
  }
117
123
  useLocalProvider = (options) => {
118
124
  const [blocInstance, setBlocInstance] = React.useState(undefined);
@@ -164,49 +170,85 @@ const externalBlocStore = (bloc) => {
164
170
  };
165
171
  };
166
172
 
167
- const useBloc = (bloc, options = {}) => {
173
+ const resolveBloc = ({ bloc, blacInstance, localProviderKey, blacReact, create }) => {
174
+ // check if its a create function or a class
175
+ const isFunction = bloc instanceof Function;
176
+ const isBloc = isFunction && bloc?.isBlacClass;
177
+ const isLiveBloc = !isFunction &&
178
+ typeof bloc === 'object' &&
179
+ bloc?.isBlacLive;
180
+ // if its a create function, call it
181
+ if (!isBloc && isFunction) {
182
+ return bloc();
183
+ }
184
+ // if its a class
185
+ if (isFunction && isBloc) {
186
+ const blocClass = bloc;
187
+ // check if the bloc is registered in the blac instance
188
+ if (blacInstance) {
189
+ const e = blacReact.getLocalBlocForProvider(localProviderKey, blocClass);
190
+ if (e) {
191
+ return e;
192
+ }
193
+ }
194
+ // search in global blocs
195
+ const globalBloc = blacReact.blac.getBloc(blocClass);
196
+ if (globalBloc) {
197
+ return globalBloc;
198
+ }
199
+ if (isLiveBloc) {
200
+ return bloc;
201
+ }
202
+ console.log(typeof bloc, {
203
+ isFunction,
204
+ isBloc,
205
+ isLiveBloc,
206
+ bloc,
207
+ create,
208
+ });
209
+ // if it is not, check if we can create a new instance
210
+ if (!create) {
211
+ // creating it automatically should be opt-in
212
+ throw new Error('useBloc: set create to true to create a new bloc when a class constructor is passed');
213
+ }
214
+ // create a new instance -- this can cause issues if the constructor expects arguments
215
+ const constructed = new blocClass();
216
+ return constructed;
217
+ }
218
+ };
219
+ const useResolvedBloc = (bloc, options = {}) => {
168
220
  const blacReact = BlacReact.getInstance();
169
221
  const localProviderKey = blacReact.useLocalProviderKey();
170
222
  const blacInstance = useContext(BlacContext);
171
223
  const resolvedBloc = useMemo(() => {
172
- // check if its a create function or a class
173
- const isFunction = bloc instanceof Function;
174
- const isBloc = isFunction && bloc?.isBlacClass;
175
- // if its a create function, call it
176
- if (!isBloc && isFunction) {
177
- return bloc();
178
- }
179
- // if its a class
180
- if (isFunction && isBloc) {
181
- const blocClass = bloc;
182
- // check if the bloc is registered in the blac instance
183
- if (blacInstance) {
184
- const e = blacReact.getLocalBlocForProvider(localProviderKey, blocClass);
185
- if (e) {
186
- return e;
187
- }
188
- }
189
- // search in global blocs
190
- const globalBloc = blacReact.blac.getBloc(blocClass);
191
- if (globalBloc) {
192
- return globalBloc;
193
- }
194
- // if it is not, check if we can create a new instance
195
- if (!options.create) {
196
- // creating it automatically should be opt-in
197
- throw new Error('useBloc: set create to true to create a new bloc when a class constructor is passed');
198
- }
199
- // create a new instance -- this can cause issues if the constructor expects arguments
200
- const constructed = new blocClass();
201
- return constructed;
224
+ if (!blacReact || !localProviderKey) {
225
+ return undefined;
202
226
  }
203
- }, []);
227
+ return resolveBloc({
228
+ bloc,
229
+ blacInstance,
230
+ blacReact,
231
+ localProviderKey,
232
+ create: options.create ?? false
233
+ });
234
+ }, [localProviderKey]);
235
+ console.trace(resolvedBloc, options);
236
+ return resolvedBloc;
237
+ };
238
+
239
+ const useBloc = (bloc, options = {}) => {
240
+ const resolvedBloc = useResolvedBloc(bloc, options);
204
241
  if (!resolvedBloc) {
205
242
  throw new Error('useBloc: bloc is undefined');
206
243
  }
207
244
  const { subscribe, getSnapshot } = useMemo(() => externalBlocStore(resolvedBloc), [resolvedBloc]);
208
245
  const state = useSyncExternalStore(subscribe, getSnapshot);
209
- return [state, resolvedBloc];
246
+ const Provider = useMemo(() => {
247
+ return ({ children }) => {
248
+ return React.createElement(BlocProvider, { bloc: resolvedBloc }, children);
249
+ };
250
+ }, [resolvedBloc,]);
251
+ return [state, resolvedBloc, Provider];
210
252
  };
211
253
 
212
254
  export { BlacApp, BlacReact, BlocProvider, externalBlocStore, useBloc };
@@ -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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -13,6 +13,7 @@ class BlacReact {
13
13
  static pluginKey = 'blacReact';
14
14
  constructor(blac, blacContext) {
15
15
  // treat this as singleton
16
+ console.log('create', blac);
16
17
  const blacReact = blac.getPluginKey(BlacReact.pluginKey);
17
18
  // new setup
18
19
  this.blac = blac;
@@ -22,7 +23,12 @@ class BlacReact {
22
23
  return blacReact;
23
24
  }
24
25
  }
25
- static getInstance() {
26
+ static safeGetInstance() {
27
+ const blac = globalThis.blac;
28
+ const blacReact = blac?.getPluginKey(BlacReact.pluginKey);
29
+ return blacReact;
30
+ }
31
+ static getInstance(throwError = true) {
26
32
  const blac = globalThis.blac;
27
33
  if (!blac) {
28
34
  throw new Error('BlacReact: blac instance not found');
@@ -114,7 +120,7 @@ class BlacReact {
114
120
  return { instance, destroyOnUnmount };
115
121
  }
116
122
  providerUnmounted(providerId) {
117
- this.removeLocalBloc;
123
+ this.removeLocalBloc(providerId);
118
124
  }
119
125
  useLocalProvider = (options) => {
120
126
  const [blocInstance, setBlocInstance] = React.useState(undefined);
@@ -166,49 +172,85 @@ const externalBlocStore = (bloc) => {
166
172
  };
167
173
  };
168
174
 
169
- const useBloc = (bloc, options = {}) => {
175
+ const resolveBloc = ({ bloc, blacInstance, localProviderKey, blacReact, create }) => {
176
+ // check if its a create function or a class
177
+ const isFunction = bloc instanceof Function;
178
+ const isBloc = isFunction && bloc?.isBlacClass;
179
+ const isLiveBloc = !isFunction &&
180
+ typeof bloc === 'object' &&
181
+ bloc?.isBlacLive;
182
+ // if its a create function, call it
183
+ if (!isBloc && isFunction) {
184
+ return bloc();
185
+ }
186
+ // if its a class
187
+ if (isFunction && isBloc) {
188
+ const blocClass = bloc;
189
+ // check if the bloc is registered in the blac instance
190
+ if (blacInstance) {
191
+ const e = blacReact.getLocalBlocForProvider(localProviderKey, blocClass);
192
+ if (e) {
193
+ return e;
194
+ }
195
+ }
196
+ // search in global blocs
197
+ const globalBloc = blacReact.blac.getBloc(blocClass);
198
+ if (globalBloc) {
199
+ return globalBloc;
200
+ }
201
+ if (isLiveBloc) {
202
+ return bloc;
203
+ }
204
+ console.log(typeof bloc, {
205
+ isFunction,
206
+ isBloc,
207
+ isLiveBloc,
208
+ bloc,
209
+ create,
210
+ });
211
+ // if it is not, check if we can create a new instance
212
+ if (!create) {
213
+ // creating it automatically should be opt-in
214
+ throw new Error('useBloc: set create to true to create a new bloc when a class constructor is passed');
215
+ }
216
+ // create a new instance -- this can cause issues if the constructor expects arguments
217
+ const constructed = new blocClass();
218
+ return constructed;
219
+ }
220
+ };
221
+ const useResolvedBloc = (bloc, options = {}) => {
170
222
  const blacReact = BlacReact.getInstance();
171
223
  const localProviderKey = blacReact.useLocalProviderKey();
172
224
  const blacInstance = React.useContext(BlacContext);
173
225
  const resolvedBloc = React.useMemo(() => {
174
- // check if its a create function or a class
175
- const isFunction = bloc instanceof Function;
176
- const isBloc = isFunction && bloc?.isBlacClass;
177
- // if its a create function, call it
178
- if (!isBloc && isFunction) {
179
- return bloc();
180
- }
181
- // if its a class
182
- if (isFunction && isBloc) {
183
- const blocClass = bloc;
184
- // check if the bloc is registered in the blac instance
185
- if (blacInstance) {
186
- const e = blacReact.getLocalBlocForProvider(localProviderKey, blocClass);
187
- if (e) {
188
- return e;
189
- }
190
- }
191
- // search in global blocs
192
- const globalBloc = blacReact.blac.getBloc(blocClass);
193
- if (globalBloc) {
194
- return globalBloc;
195
- }
196
- // if it is not, check if we can create a new instance
197
- if (!options.create) {
198
- // creating it automatically should be opt-in
199
- throw new Error('useBloc: set create to true to create a new bloc when a class constructor is passed');
200
- }
201
- // create a new instance -- this can cause issues if the constructor expects arguments
202
- const constructed = new blocClass();
203
- return constructed;
226
+ if (!blacReact || !localProviderKey) {
227
+ return undefined;
204
228
  }
205
- }, []);
229
+ return resolveBloc({
230
+ bloc,
231
+ blacInstance,
232
+ blacReact,
233
+ localProviderKey,
234
+ create: options.create ?? false
235
+ });
236
+ }, [localProviderKey]);
237
+ console.trace(resolvedBloc, options);
238
+ return resolvedBloc;
239
+ };
240
+
241
+ const useBloc = (bloc, options = {}) => {
242
+ const resolvedBloc = useResolvedBloc(bloc, options);
206
243
  if (!resolvedBloc) {
207
244
  throw new Error('useBloc: bloc is undefined');
208
245
  }
209
246
  const { subscribe, getSnapshot } = React.useMemo(() => externalBlocStore(resolvedBloc), [resolvedBloc]);
210
247
  const state = React.useSyncExternalStore(subscribe, getSnapshot);
211
- return [state, resolvedBloc];
248
+ const Provider = React.useMemo(() => {
249
+ return ({ children }) => {
250
+ return React.createElement(BlocProvider, { bloc: resolvedBloc }, children);
251
+ };
252
+ }, [resolvedBloc,]);
253
+ return [state, resolvedBloc, Provider];
212
254
  };
213
255
 
214
256
  exports.BlacApp = BlacApp;
@@ -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.4-0",
3
+ "version": "0.0.13-0",
4
4
  "license": "MIT",
5
5
  "main": "dist/blac-react.js",
6
6
  "module": "dist/blac-react.esm.js",