react-obsidian 0.0.47 → 1.1.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.
Files changed (33) hide show
  1. package/.eslintrc.json +2 -1
  2. package/.vscode/settings.json +1 -0
  3. package/dist/src/Obsidian.d.ts +2 -2
  4. package/dist/src/Obsidian.d.ts.map +1 -1
  5. package/dist/src/Obsidian.js.map +1 -1
  6. package/dist/src/graph/registry/GraphRegistry.d.ts +1 -0
  7. package/dist/src/graph/registry/GraphRegistry.d.ts.map +1 -1
  8. package/dist/src/graph/registry/GraphRegistry.js +8 -1
  9. package/dist/src/graph/registry/GraphRegistry.js.map +1 -1
  10. package/dist/src/graph/registry/NullProps.d.ts +4 -0
  11. package/dist/src/graph/registry/NullProps.d.ts.map +1 -0
  12. package/dist/src/graph/registry/NullProps.js +25 -0
  13. package/dist/src/graph/registry/NullProps.js.map +1 -0
  14. package/dist/src/observable/useObserver.d.ts +4 -2
  15. package/dist/src/observable/useObserver.d.ts.map +1 -1
  16. package/dist/src/observable/useObserver.js +6 -1
  17. package/dist/src/observable/useObserver.js.map +1 -1
  18. package/dist/src/utils/isDev.d.ts +2 -0
  19. package/dist/src/utils/isDev.d.ts.map +1 -0
  20. package/dist/src/utils/isDev.js +17 -0
  21. package/dist/src/utils/isDev.js.map +1 -0
  22. package/dist/test/fixtures/LifecycleBoundGraph.d.ts +3 -2
  23. package/dist/test/fixtures/LifecycleBoundGraph.d.ts.map +1 -1
  24. package/dist/test/fixtures/LifecycleBoundGraph.js +9 -0
  25. package/dist/test/fixtures/LifecycleBoundGraph.js.map +1 -1
  26. package/documentation/docs/documentation/usage/Reactivity.mdx +20 -0
  27. package/documentation/yarn.lock +8167 -0
  28. package/package.json +1 -1
  29. package/src/Obsidian.ts +3 -3
  30. package/src/graph/registry/GraphRegistry.ts +9 -1
  31. package/src/graph/registry/NullProps.ts +23 -0
  32. package/src/observable/useObserver.ts +18 -3
  33. package/src/utils/isDev.ts +12 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-obsidian",
3
- "version": "0.0.47",
3
+ "version": "1.1.0",
4
4
  "description": "Dependency injection framework for React and React Native applications",
5
5
  "scripts": {
6
6
  "prepack": "npm run lint && tsc --project tsconfig.prod.json",
package/src/Obsidian.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import graphRegistry from './graph/registry/GraphRegistry';
2
2
  import { ObjectGraph } from './graph/ObjectGraph';
3
- import { Constructable, GraphInternals, ServiceLocator } from './types';
3
+ import { GraphInternals, ServiceLocator } from './types';
4
4
  import { GraphMiddleware } from './graph/registry/GraphMiddleware';
5
5
  import lateInjector from './injectors/class/LateInjector';
6
6
  import serviceLocatorFactory from './graph/ServiceLocatorFactory';
7
7
 
8
8
  export default class Obsidian {
9
- obtain<T extends ObjectGraph<P>, P = any>(
10
- Graph: Constructable<T>,
9
+ obtain<T extends ObjectGraph<P>, P>(
10
+ Graph: new(...args: P[]) => T,
11
11
  props?: P,
12
12
  ): ServiceLocator<Omit<T, GraphInternals>> {
13
13
  return serviceLocatorFactory.fromGraph(Graph, props);
@@ -2,6 +2,7 @@ import { Constructable } from '../../types';
2
2
  import { Graph } from '../Graph';
3
3
  import { Middleware } from './Middleware';
4
4
  import GraphMiddlewareChain from './GraphMiddlewareChain';
5
+ import { NullProps as createNullProps } from './NullProps';
5
6
 
6
7
  export class GraphRegistry {
7
8
  private readonly constructorToInstance = new Map<Constructable<Graph>, Set<Graph>>();
@@ -33,11 +34,18 @@ export class GraphRegistry {
33
34
  if ((this.isSingleton(Graph) || this.isBoundToReactLifecycle(Graph)) && this.has(Graph)) {
34
35
  return this.getFirst(Graph);
35
36
  }
36
- const graph = this.graphMiddlewares.resolve(Graph, props);
37
+ const graph = this.graphMiddlewares.resolve(Graph, this.ensurePropsIfLifecycleBoundGraph(Graph, props));
37
38
  this.set(Graph, graph);
38
39
  return graph as T;
39
40
  }
40
41
 
42
+ private ensurePropsIfLifecycleBoundGraph<T extends Graph>(Graph: Constructable<T>, props?: any): any {
43
+ if (this.isBoundToReactLifecycle(Graph)) {
44
+ return props ?? createNullProps(Graph);
45
+ }
46
+ return props;
47
+ }
48
+
41
49
  private has(Graph: Constructable<Graph>): boolean {
42
50
  return (this.constructorToInstance.get(Graph)?.size ?? 0) > 0;
43
51
  }
@@ -0,0 +1,23 @@
1
+ import type { Constructable } from '../../types';
2
+ import { isDev } from '../../utils/isDev';
3
+ import type { Graph } from '../Graph';
4
+
5
+ export function NullProps(Graph: Constructable<Graph>) {
6
+ return new Proxy({}, new NullPropsProxyHandler(Graph));
7
+ }
8
+
9
+ class NullPropsProxyHandler implements ProxyHandler<{}> {
10
+ constructor(private graph: Constructable<Graph>) {}
11
+
12
+ get(target: object, property: string, receiver: any) {
13
+ if (property in target) return Reflect.get(target, property, receiver);
14
+ throw new Error(this.createErrorMessage(this.graph, property));
15
+ }
16
+
17
+ private createErrorMessage(graph: Constructable<Graph>, property: string) {
18
+ const graphName = isDev() ? graph.name : '';
19
+ return `Tried to get prop ${property} in a @LifecycleBound graph ${graphName}, but props were undefined. `
20
+ + `If you're using Obsidian.obtain(${graphName}) - then you should pass props to it explicitly: `
21
+ + `Obsidian.obtain(${graphName}, { ${property}: 'value' });`;
22
+ }
23
+ }
@@ -1,8 +1,19 @@
1
1
  /* eslint-disable no-param-reassign */
2
- import { useCallback, useEffect, useState } from 'react';
3
- import { Observable } from './types';
2
+ import {
3
+ useCallback,
4
+ useEffect,
5
+ useMemo,
6
+ useState,
7
+ } from 'react';
8
+ import { Observable } from './Observable';
4
9
 
5
- export function useObserver<T>(observable: Observable<T>): [T, (next: T) => void] {
10
+ type ObservableOrGenerator<T> = Observable<T> | (() => Observable<T>);
11
+
12
+ export function useObserver<T>(observableOrGenerator: ObservableOrGenerator<T>): [T, (next: T) => void] {
13
+ const observable = useMemo(
14
+ () => getOrGenerateObservable(observableOrGenerator),
15
+ [],
16
+ );
6
17
  const [value, setValue] = useState(observable.value);
7
18
  const onNext = useCallback((next: T) => {
8
19
  observable.value = next;
@@ -14,3 +25,7 @@ export function useObserver<T>(observable: Observable<T>): [T, (next: T) => void
14
25
 
15
26
  return [value, onNext];
16
27
  }
28
+
29
+ function getOrGenerateObservable(observableOrGenerator: ObservableOrGenerator<any>) {
30
+ return observableOrGenerator instanceof Observable ? observableOrGenerator : observableOrGenerator();
31
+ }
@@ -0,0 +1,12 @@
1
+ export function isDev(): boolean {
2
+ return isNodeDev() || isReactNativeDev();
3
+ }
4
+
5
+ function isNodeDev(): boolean {
6
+ return ['test', 'development'].includes(process.env['NODE_ENV'] ?? '');
7
+ }
8
+
9
+ function isReactNativeDev(): boolean {
10
+ // @ts-ignore
11
+ return __DEV__ as boolean ?? false;
12
+ }