@blac/react 0.0.14 → 0.0.16

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,67 +1,20 @@
1
- import { Blac, BlocBase, BlocClass, ValueType } from 'blac';
1
+ import { Blac, BlocBase, ValueType, BlocConstructor } from 'blac';
2
2
  import React, { FC, ReactNode } from 'react';
3
3
 
4
4
  declare const BlacApp: FC<{
5
5
  children: ReactNode;
6
- blac: Blac<any, any>;
6
+ blac?: Blac<any>;
7
7
  }>;
8
8
 
9
- /**
10
- * Keep track of the hierarchy of Blac BlocProvider components, so that
11
- * useBloc can find the correct bloc instance.
12
- */
13
-
14
- interface ProviderItem {
15
- id: string;
16
- parent?: string;
17
- bloc: BlocBase<any>;
18
- }
19
- interface ProviderOptions<B> {
20
- bloc: BlocClass<B> | (() => B);
21
- debug?: boolean;
22
- }
23
9
  declare class BlacReact {
24
- blac: Blac<any, any>;
25
- blacContext: React.Context<Blac<any, any>>;
26
- localContextProvider: React.Context<BlocBase<any> | null>;
27
10
  static pluginKey: string;
28
- constructor(blac: Blac<any, any>, blacContext: React.Context<Blac<any, any>>);
29
- static safeGetInstance(): BlacReact | undefined;
11
+ blac: Blac<any>;
12
+ blacContext: React.Context<Blac<any>>;
13
+ constructor(blac: Blac<any>, blacContext: React.Context<Blac<any>>);
30
14
  static getInstance(throwError?: boolean): BlacReact;
31
15
  setup(): void;
32
- /**
33
- * React hook to get the bloc instance from the nearest BlocProvider
34
- */
35
- useLocalBlocContext(): BlocBase<any> | null;
36
- private _contextLocalProviderKey;
37
- get LocalProvider(): React.Provider<string>;
38
- /**
39
- * React hook to get the id from the nearest BlocProvider
40
- */
41
- useLocalProviderKey(): string;
42
- addLocalBloc: (item: ProviderItem) => void;
43
- removeLocalBloc: (id: string) => void;
44
- createProviderId: () => string;
45
- providerList: ProviderItem[];
46
- getLocalBlocForProvider<B>(id: string, blocClass: BlocClass<B>): BlocBase<B> | undefined;
47
- providerMounted(options: {
48
- providerId: string;
49
- localProviderKey?: string;
50
- } & Parameters<BlacReact['useLocalProvider']>[0]): {
51
- instance: BlocBase<any> | undefined;
52
- destroyOnUnmount: boolean;
53
- };
54
- providerUnmounted(providerId: string | undefined): void;
55
- readonly useLocalProvider: (options: ProviderOptions<any>) => string | undefined;
56
16
  }
57
17
 
58
- interface BlocProviderProps<B> {
59
- children?: ReactNode;
60
- bloc: BlocClass<B> | (() => B) | B;
61
- debug?: boolean;
62
- }
63
- declare const BlocProvider: FC<BlocProviderProps<any>>;
64
-
65
18
  interface ExternalStore<B extends BlocBase<any>> {
66
19
  subscribe: (onStoreChange: () => void) => () => void;
67
20
  getSnapshot: () => ValueType<B>;
@@ -69,21 +22,10 @@ interface ExternalStore<B extends BlocBase<any>> {
69
22
  }
70
23
  declare const externalBlocStore: <B extends BlocBase<any>>(bloc: B) => ExternalStore<B>;
71
24
 
72
- interface BlocResolveOptions {
73
- /**
74
- * Set to true if you want blac to automatically create a new instance of the bloc,
75
- * for this to work, pass the unconstructed class to the hook. The constructor should not expect any arguments.
76
- */
77
- create?: boolean;
78
- }
79
-
80
25
  type BlocHookData<B extends BlocBase<S>, S> = [
81
26
  value: ValueType<B>,
82
- instance: B,
83
- Provider: FC<{
84
- children: ReactNode;
85
- }>
27
+ instance: B
86
28
  ];
87
- declare const useBloc: <B extends BlocBase<S>, S>(bloc: BlocClass<B> | (() => B), options?: BlocResolveOptions) => BlocHookData<B, S>;
29
+ declare const useBloc: <B extends BlocBase<S>, S>(bloc: BlocConstructor<B>) => BlocHookData<B, S>;
88
30
 
89
- export { BlacApp, BlacReact, BlocProvider, ExternalStore, externalBlocStore, useBloc };
31
+ export { BlacApp, BlacReact, ExternalStore, externalBlocStore, useBloc };
@@ -1,255 +1,112 @@
1
- import React, { useContext, useMemo, useEffect, createContext, useSyncExternalStore } from 'react';
1
+ import { Blac } from 'blac';
2
+ import React, { createContext, useMemo, useContext, useSyncExternalStore } from 'react';
2
3
 
3
- /**
4
- * Keep track of the hierarchy of Blac BlocProvider components, so that
5
- * useBloc can find the correct bloc instance.
6
- */
7
- class BlacReact {
8
- blac;
9
- blacContext;
10
- localContextProvider = React.createContext(null);
11
- static pluginKey = 'blacReact';
12
- constructor(blac, blacContext) {
13
- // treat this as singleton
14
- console.log('create', blac);
15
- const blacReact = blac.getPluginKey(BlacReact.pluginKey);
16
- // new setup
17
- this.blac = blac;
18
- this.blacContext = blacContext;
19
- this.setup();
20
- if (blacReact) {
21
- return blacReact;
22
- }
23
- }
24
- static safeGetInstance() {
25
- const blac = globalThis.blac;
26
- const blacReact = blac?.getPluginKey(BlacReact.pluginKey);
27
- return blacReact;
28
- }
29
- static getInstance(throwError = true) {
30
- const blac = globalThis.blac;
31
- if (!blac) {
32
- throw new Error('BlacReact: blac instance not found');
33
- }
34
- const blacReact = blac.getPluginKey(BlacReact.pluginKey);
35
- if (!blacReact) {
36
- throw new Error('BlacReact: blacReact instance not found');
37
- }
38
- return blacReact;
39
- }
40
- setup() {
41
- // register blac instance on global object
42
- globalThis.blac = this.blac;
43
- // add the BlacReact instance to blac
44
- this.blac.addPluginKey(BlacReact.pluginKey, this);
45
- }
46
- /**
47
- * React hook to get the bloc instance from the nearest BlocProvider
48
- */
49
- useLocalBlocContext() {
50
- return useContext(this.localContextProvider);
51
- }
52
- _contextLocalProviderKey = React.createContext('none');
53
- get LocalProvider() {
54
- return this._contextLocalProviderKey.Provider;
55
- }
56
- /**
57
- * React hook to get the id from the nearest BlocProvider
58
- */
59
- useLocalProviderKey() {
60
- return useContext(this._contextLocalProviderKey);
61
- }
62
- addLocalBloc = (item) => {
63
- this.providerList.push(item);
64
- };
65
- removeLocalBloc = (id) => {
66
- const index = this.providerList.findIndex((i) => i.id === id);
67
- if (!index)
68
- return;
69
- const item = this.providerList[index];
70
- item.bloc.dispose();
71
- if (index !== -1) {
72
- this.providerList.splice(index, 1);
73
- }
74
- };
75
- createProviderId = () => {
76
- return Math.random().toString(36).split('.')[1];
77
- };
78
- providerList = new Array();
79
- getLocalBlocForProvider(id, blocClass) {
80
- for (const providerItem of this.providerList) {
81
- if (providerItem.id === id) {
82
- if (providerItem.bloc instanceof blocClass) {
83
- return providerItem.bloc;
84
- }
85
- let parent = providerItem.parent;
86
- while (parent) {
87
- const parentItem = this.providerList.find((i) => i.id === parent);
88
- if (parentItem?.bloc instanceof blocClass) {
89
- return parentItem.bloc;
90
- }
91
- parent = parentItem?.parent;
92
- }
93
- }
94
- }
95
- return undefined;
96
- }
97
- providerMounted(options) {
98
- const { bloc, providerId, localProviderKey } = options;
99
- let instance = undefined;
100
- let destroyOnUnmount = true;
101
- const isFunction = bloc instanceof Function;
102
- const isLiveBloc = !isFunction &&
103
- typeof bloc === 'object' &&
104
- bloc?.isBlacLive;
105
- if (isFunction) {
106
- instance = bloc();
107
- }
108
- if (isLiveBloc) {
109
- instance = bloc;
110
- }
111
- if (instance) {
112
- this.addLocalBloc({
113
- bloc: instance,
114
- id: providerId,
115
- parent: localProviderKey,
116
- });
117
- }
118
- return { instance, destroyOnUnmount };
119
- }
120
- providerUnmounted(providerId) {
121
- this.removeLocalBloc(providerId);
122
- }
123
- useLocalProvider = (options) => {
124
- const [blocInstance, setBlocInstance] = React.useState(undefined);
125
- const localProviderKey = this.useLocalProviderKey();
126
- const providerId = useMemo(() => this.createProviderId(), []);
127
- useEffect(() => {
128
- const { instance, destroyOnUnmount } = this.providerMounted({
129
- ...options,
130
- localProviderKey,
131
- providerId,
132
- });
133
- setBlocInstance(instance);
134
- if (destroyOnUnmount)
135
- return this.providerUnmounted(providerId);
136
- }, []);
137
- if (blocInstance)
138
- return providerId;
139
- };
4
+ class BlacReact {
5
+ static pluginKey = "blacReact";
6
+ blac;
7
+ blacContext;
8
+ constructor(blac, blacContext) {
9
+ const blacReact = blac.getPluginKey(BlacReact.pluginKey);
10
+ // new setup
11
+ this.blac = blac;
12
+ this.blacContext = blacContext;
13
+ this.setup();
14
+ if (blacReact) {
15
+ return blacReact;
16
+ }
17
+ }
18
+ static getInstance(throwError = true) {
19
+ // const blac = (globalThis as any).blac;
20
+ const blac = BlacContext._currentValue;
21
+ if (!blac) {
22
+ throw new Error("BlacReact: blac instance not found, the <BlacApp> provider component might be missing");
23
+ }
24
+ const blacReact = blac.getPluginKey(BlacReact.pluginKey);
25
+ if (!blacReact) {
26
+ throw new Error("BlacReact: blacReact instance not found");
27
+ }
28
+ return blacReact;
29
+ }
30
+ setup() {
31
+ // (globalThis as any).blac = this.blac;
32
+ this.blac.addPluginKey(BlacReact.pluginKey, this);
33
+ }
140
34
  }
141
35
 
142
- const BlacContext = createContext(null);
143
- const BlacApp = (props) => {
144
- const { children, blac } = props;
145
- const blacApp = useMemo(() => new BlacReact(blac, BlacContext), [blac]);
146
- if (!blacApp) {
147
- throw new Error('BlacReact failed to initialize');
148
- }
149
- return React.createElement(BlacContext.Provider, { value: blac }, children);
36
+ const BlacContext = createContext(null);
37
+ const BlacApp = (props) => {
38
+ const { children, blac } = props;
39
+ const blacInstance = useMemo(() => blac ?? new Blac(), [blac]);
40
+ const blacApp = useMemo(() => new BlacReact(blacInstance, BlacContext), [blacInstance]);
41
+ if (!blacApp) {
42
+ throw new Error("BlacReact failed to initialize");
43
+ }
44
+ return React.createElement(BlacContext.Provider, { value: blacInstance }, children);
150
45
  };
151
46
 
152
- const BlocProvider = ({ children, bloc, debug }) => {
153
- const blacReact = BlacReact.getInstance();
154
- const providerId = blacReact.useLocalProvider({ bloc, debug });
155
- if (!providerId) {
156
- return null;
157
- }
158
- return (React.createElement(blacReact.LocalProvider, { value: providerId }, children));
47
+ const externalBlocStore = (bloc) => {
48
+ return {
49
+ subscribe: (listener) => {
50
+ const unSub = bloc.onStateChange(listener);
51
+ return () => {
52
+ unSub();
53
+ };
54
+ },
55
+ getSnapshot: () => bloc.state
56
+ };
159
57
  };
160
58
 
161
- const externalBlocStore = (bloc) => {
162
- return {
163
- subscribe: (listener) => {
164
- const unSub = bloc.onStateChange(listener);
165
- return () => {
166
- unSub();
167
- };
168
- },
169
- getSnapshot: () => bloc.state,
170
- };
59
+ const resolveBloc = ({ bloc, blacInstance, blacReact
60
+ // create
61
+ }) => {
62
+ // check if its a create function or a class
63
+ const isFunction = bloc instanceof Function;
64
+ const isBloc = isFunction && bloc?.isBlacClass;
65
+ const isLiveBloc = !isFunction &&
66
+ typeof bloc === "object" &&
67
+ bloc?.isBlacLive;
68
+ // if it is a create function, call it
69
+ if (!isBloc && isFunction) {
70
+ return bloc();
71
+ }
72
+ if (bloc?.isBlacLive) {
73
+ return bloc;
74
+ }
75
+ // if it is a class
76
+ const blocClass = bloc;
77
+ if (isLiveBloc) {
78
+ return bloc;
79
+ }
80
+ const registered = blacReact.blac.getBloc(blocClass);
81
+ if (registered) {
82
+ return registered;
83
+ }
171
84
  };
172
-
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 = {}) => {
220
- const blacReact = BlacReact.getInstance();
221
- const localProviderKey = blacReact.useLocalProviderKey();
222
- const blacInstance = useContext(BlacContext);
223
- const resolvedBloc = useMemo(() => {
224
- if (!blacReact || !localProviderKey) {
225
- return undefined;
226
- }
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;
85
+ const useResolvedBloc = (bloc) => {
86
+ const blacReact = BlacReact.getInstance();
87
+ const blacInstance = useContext(BlacContext);
88
+ const resolvedBloc = useMemo(() => {
89
+ if (!blacReact) {
90
+ return undefined;
91
+ }
92
+ return resolveBloc({
93
+ bloc,
94
+ blacInstance,
95
+ blacReact
96
+ });
97
+ }, []);
98
+ return resolvedBloc;
237
99
  };
238
100
 
239
- const useBloc = (bloc, options = {}) => {
240
- const resolvedBloc = useResolvedBloc(bloc, options);
241
- if (!resolvedBloc) {
242
- throw new Error('useBloc: bloc is undefined');
243
- }
244
- const { subscribe, getSnapshot } = useMemo(() => externalBlocStore(resolvedBloc), [resolvedBloc]);
245
- const state = useSyncExternalStore(subscribe, getSnapshot);
246
- const Provider = useMemo(() => {
247
- return ({ children }) => {
248
- return React.createElement(BlocProvider, { bloc: resolvedBloc }, children);
249
- };
250
- }, [resolvedBloc,]);
251
- return [state, resolvedBloc, Provider];
101
+ const useBloc = (bloc) => {
102
+ const resolvedBloc = useResolvedBloc(bloc);
103
+ if (!resolvedBloc) {
104
+ throw new Error(`useBloc: could not resolve: ${bloc.name || bloc}`);
105
+ }
106
+ const { subscribe, getSnapshot } = useMemo(() => externalBlocStore(resolvedBloc), [resolvedBloc]);
107
+ const state = useSyncExternalStore(subscribe, getSnapshot);
108
+ return [state, resolvedBloc];
252
109
  };
253
110
 
254
- export { BlacApp, BlacReact, BlocProvider, externalBlocStore, useBloc };
111
+ export { BlacApp, BlacReact, externalBlocStore, useBloc };
255
112
  //# 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,261 +1,117 @@
1
1
  'use strict';
2
2
 
3
+ var blac = require('blac');
3
4
  var React = require('react');
4
5
 
5
- /**
6
- * Keep track of the hierarchy of Blac BlocProvider components, so that
7
- * useBloc can find the correct bloc instance.
8
- */
9
- class BlacReact {
10
- blac;
11
- blacContext;
12
- localContextProvider = React.createContext(null);
13
- static pluginKey = 'blacReact';
14
- constructor(blac, blacContext) {
15
- // treat this as singleton
16
- console.log('create', blac);
17
- const blacReact = blac.getPluginKey(BlacReact.pluginKey);
18
- // new setup
19
- this.blac = blac;
20
- this.blacContext = blacContext;
21
- this.setup();
22
- if (blacReact) {
23
- return blacReact;
24
- }
25
- }
26
- static safeGetInstance() {
27
- const blac = globalThis.blac;
28
- const blacReact = blac?.getPluginKey(BlacReact.pluginKey);
29
- return blacReact;
30
- }
31
- static getInstance(throwError = true) {
32
- const blac = globalThis.blac;
33
- if (!blac) {
34
- throw new Error('BlacReact: blac instance not found');
35
- }
36
- const blacReact = blac.getPluginKey(BlacReact.pluginKey);
37
- if (!blacReact) {
38
- throw new Error('BlacReact: blacReact instance not found');
39
- }
40
- return blacReact;
41
- }
42
- setup() {
43
- // register blac instance on global object
44
- globalThis.blac = this.blac;
45
- // add the BlacReact instance to blac
46
- this.blac.addPluginKey(BlacReact.pluginKey, this);
47
- }
48
- /**
49
- * React hook to get the bloc instance from the nearest BlocProvider
50
- */
51
- useLocalBlocContext() {
52
- return React.useContext(this.localContextProvider);
53
- }
54
- _contextLocalProviderKey = React.createContext('none');
55
- get LocalProvider() {
56
- return this._contextLocalProviderKey.Provider;
57
- }
58
- /**
59
- * React hook to get the id from the nearest BlocProvider
60
- */
61
- useLocalProviderKey() {
62
- return React.useContext(this._contextLocalProviderKey);
63
- }
64
- addLocalBloc = (item) => {
65
- this.providerList.push(item);
66
- };
67
- removeLocalBloc = (id) => {
68
- const index = this.providerList.findIndex((i) => i.id === id);
69
- if (!index)
70
- return;
71
- const item = this.providerList[index];
72
- item.bloc.dispose();
73
- if (index !== -1) {
74
- this.providerList.splice(index, 1);
75
- }
76
- };
77
- createProviderId = () => {
78
- return Math.random().toString(36).split('.')[1];
79
- };
80
- providerList = new Array();
81
- getLocalBlocForProvider(id, blocClass) {
82
- for (const providerItem of this.providerList) {
83
- if (providerItem.id === id) {
84
- if (providerItem.bloc instanceof blocClass) {
85
- return providerItem.bloc;
86
- }
87
- let parent = providerItem.parent;
88
- while (parent) {
89
- const parentItem = this.providerList.find((i) => i.id === parent);
90
- if (parentItem?.bloc instanceof blocClass) {
91
- return parentItem.bloc;
92
- }
93
- parent = parentItem?.parent;
94
- }
95
- }
96
- }
97
- return undefined;
98
- }
99
- providerMounted(options) {
100
- const { bloc, providerId, localProviderKey } = options;
101
- let instance = undefined;
102
- let destroyOnUnmount = true;
103
- const isFunction = bloc instanceof Function;
104
- const isLiveBloc = !isFunction &&
105
- typeof bloc === 'object' &&
106
- bloc?.isBlacLive;
107
- if (isFunction) {
108
- instance = bloc();
109
- }
110
- if (isLiveBloc) {
111
- instance = bloc;
112
- }
113
- if (instance) {
114
- this.addLocalBloc({
115
- bloc: instance,
116
- id: providerId,
117
- parent: localProviderKey,
118
- });
119
- }
120
- return { instance, destroyOnUnmount };
121
- }
122
- providerUnmounted(providerId) {
123
- this.removeLocalBloc(providerId);
124
- }
125
- useLocalProvider = (options) => {
126
- const [blocInstance, setBlocInstance] = React.useState(undefined);
127
- const localProviderKey = this.useLocalProviderKey();
128
- const providerId = React.useMemo(() => this.createProviderId(), []);
129
- React.useEffect(() => {
130
- const { instance, destroyOnUnmount } = this.providerMounted({
131
- ...options,
132
- localProviderKey,
133
- providerId,
134
- });
135
- setBlocInstance(instance);
136
- if (destroyOnUnmount)
137
- return this.providerUnmounted(providerId);
138
- }, []);
139
- if (blocInstance)
140
- return providerId;
141
- };
6
+ class BlacReact {
7
+ static pluginKey = "blacReact";
8
+ blac;
9
+ blacContext;
10
+ constructor(blac, blacContext) {
11
+ const blacReact = blac.getPluginKey(BlacReact.pluginKey);
12
+ // new setup
13
+ this.blac = blac;
14
+ this.blacContext = blacContext;
15
+ this.setup();
16
+ if (blacReact) {
17
+ return blacReact;
18
+ }
19
+ }
20
+ static getInstance(throwError = true) {
21
+ // const blac = (globalThis as any).blac;
22
+ const blac = BlacContext._currentValue;
23
+ if (!blac) {
24
+ throw new Error("BlacReact: blac instance not found, the <BlacApp> provider component might be missing");
25
+ }
26
+ const blacReact = blac.getPluginKey(BlacReact.pluginKey);
27
+ if (!blacReact) {
28
+ throw new Error("BlacReact: blacReact instance not found");
29
+ }
30
+ return blacReact;
31
+ }
32
+ setup() {
33
+ // (globalThis as any).blac = this.blac;
34
+ this.blac.addPluginKey(BlacReact.pluginKey, this);
35
+ }
142
36
  }
143
37
 
144
- const BlacContext = React.createContext(null);
145
- const BlacApp = (props) => {
146
- const { children, blac } = props;
147
- const blacApp = React.useMemo(() => new BlacReact(blac, BlacContext), [blac]);
148
- if (!blacApp) {
149
- throw new Error('BlacReact failed to initialize');
150
- }
151
- return React.createElement(BlacContext.Provider, { value: blac }, children);
38
+ const BlacContext = React.createContext(null);
39
+ const BlacApp = (props) => {
40
+ const { children, blac: blac$1 } = props;
41
+ const blacInstance = React.useMemo(() => blac$1 ?? new blac.Blac(), [blac$1]);
42
+ const blacApp = React.useMemo(() => new BlacReact(blacInstance, BlacContext), [blacInstance]);
43
+ if (!blacApp) {
44
+ throw new Error("BlacReact failed to initialize");
45
+ }
46
+ return React.createElement(BlacContext.Provider, { value: blacInstance }, children);
152
47
  };
153
48
 
154
- const BlocProvider = ({ children, bloc, debug }) => {
155
- const blacReact = BlacReact.getInstance();
156
- const providerId = blacReact.useLocalProvider({ bloc, debug });
157
- if (!providerId) {
158
- return null;
159
- }
160
- return (React.createElement(blacReact.LocalProvider, { value: providerId }, children));
49
+ const externalBlocStore = (bloc) => {
50
+ return {
51
+ subscribe: (listener) => {
52
+ const unSub = bloc.onStateChange(listener);
53
+ return () => {
54
+ unSub();
55
+ };
56
+ },
57
+ getSnapshot: () => bloc.state
58
+ };
161
59
  };
162
60
 
163
- const externalBlocStore = (bloc) => {
164
- return {
165
- subscribe: (listener) => {
166
- const unSub = bloc.onStateChange(listener);
167
- return () => {
168
- unSub();
169
- };
170
- },
171
- getSnapshot: () => bloc.state,
172
- };
61
+ const resolveBloc = ({ bloc, blacInstance, blacReact
62
+ // create
63
+ }) => {
64
+ // check if its a create function or a class
65
+ const isFunction = bloc instanceof Function;
66
+ const isBloc = isFunction && bloc?.isBlacClass;
67
+ const isLiveBloc = !isFunction &&
68
+ typeof bloc === "object" &&
69
+ bloc?.isBlacLive;
70
+ // if it is a create function, call it
71
+ if (!isBloc && isFunction) {
72
+ return bloc();
73
+ }
74
+ if (bloc?.isBlacLive) {
75
+ return bloc;
76
+ }
77
+ // if it is a class
78
+ const blocClass = bloc;
79
+ if (isLiveBloc) {
80
+ return bloc;
81
+ }
82
+ const registered = blacReact.blac.getBloc(blocClass);
83
+ if (registered) {
84
+ return registered;
85
+ }
173
86
  };
174
-
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 = {}) => {
222
- const blacReact = BlacReact.getInstance();
223
- const localProviderKey = blacReact.useLocalProviderKey();
224
- const blacInstance = React.useContext(BlacContext);
225
- const resolvedBloc = React.useMemo(() => {
226
- if (!blacReact || !localProviderKey) {
227
- return undefined;
228
- }
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;
87
+ const useResolvedBloc = (bloc) => {
88
+ const blacReact = BlacReact.getInstance();
89
+ const blacInstance = React.useContext(BlacContext);
90
+ const resolvedBloc = React.useMemo(() => {
91
+ if (!blacReact) {
92
+ return undefined;
93
+ }
94
+ return resolveBloc({
95
+ bloc,
96
+ blacInstance,
97
+ blacReact
98
+ });
99
+ }, []);
100
+ return resolvedBloc;
239
101
  };
240
102
 
241
- const useBloc = (bloc, options = {}) => {
242
- const resolvedBloc = useResolvedBloc(bloc, options);
243
- if (!resolvedBloc) {
244
- throw new Error('useBloc: bloc is undefined');
245
- }
246
- const { subscribe, getSnapshot } = React.useMemo(() => externalBlocStore(resolvedBloc), [resolvedBloc]);
247
- const state = React.useSyncExternalStore(subscribe, getSnapshot);
248
- const Provider = React.useMemo(() => {
249
- return ({ children }) => {
250
- return React.createElement(BlocProvider, { bloc: resolvedBloc }, children);
251
- };
252
- }, [resolvedBloc,]);
253
- return [state, resolvedBloc, Provider];
103
+ const useBloc = (bloc) => {
104
+ const resolvedBloc = useResolvedBloc(bloc);
105
+ if (!resolvedBloc) {
106
+ throw new Error(`useBloc: could not resolve: ${bloc.name || bloc}`);
107
+ }
108
+ const { subscribe, getSnapshot } = React.useMemo(() => externalBlocStore(resolvedBloc), [resolvedBloc]);
109
+ const state = React.useSyncExternalStore(subscribe, getSnapshot);
110
+ return [state, resolvedBloc];
254
111
  };
255
112
 
256
113
  exports.BlacApp = BlacApp;
257
114
  exports.BlacReact = BlacReact;
258
- exports.BlocProvider = BlocProvider;
259
115
  exports.externalBlocStore = externalBlocStore;
260
116
  exports.useBloc = useBloc;
261
117
  //# 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.14",
3
+ "version": "0.0.16",
4
4
  "license": "MIT",
5
5
  "main": "dist/blac-react.js",
6
6
  "module": "dist/blac-react.esm.js",
@@ -29,12 +29,15 @@
29
29
  "blac": "*"
30
30
  },
31
31
  "devDependencies": {
32
- "@types/react": "^18.0.28",
33
- "@types/react-dom": "^18.0.11",
34
- "@vitejs/plugin-react": "^3.1.0",
32
+ "@types/react": "^18.2.17",
33
+ "@types/react-dom": "^18.2.7",
34
+ "@types/react-highlight": "^0.12.5",
35
+ "@vitejs/plugin-react": "^4.0.3",
36
+ "highlight.js": "^11.8.0",
35
37
  "react": "^18.2.0",
36
38
  "react-dom": "^18.2.0",
37
- "vite": "^4.1.4",
38
- "vitest": "^0.29.1"
39
+ "react-highlight": "^0.15.0",
40
+ "vite": "^4.4.7",
41
+ "vitest": "^0.33.0"
39
42
  }
40
43
  }
package/readme.md CHANGED
@@ -13,4 +13,4 @@ npm install blac-react
13
13
  - [Counter with Bloc](ui/examples/CounterWithBloc.tsx)
14
14
  - [Counter with Cubit](ui/examples/CounterWithCubit.tsx)
15
15
  - [Counter with Global State](ui/examples/CounterWithCubitGlobal.tsx)
16
- - [Counters with Local State](ui/examples/CounterScoped.tsx)
16
+ - [Counters with Local State](ui/examples/CounterLocalDemo.tsx)