extra-react-hooks 0.9.0 → 0.9.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/README.md CHANGED
@@ -266,3 +266,11 @@ function usePromise<T>(promise: PromiseLike<T>): T | undefined
266
266
  Similar to `use(promise)`,
267
267
  but it also works in environments without `<Suspense>` or server components.
268
268
  Errors thrown can be caught by error boundaries.
269
+
270
+ ### Strict Context
271
+ ```ts
272
+ type StrictContext<T> = Context<T | typeof dummy>
273
+
274
+ function createStrictContext<T>(): StrictContext<T>
275
+ function useStrictContext<T>(context: StrictContext<T>): T
276
+ ```
package/lib/index.d.ts CHANGED
@@ -28,3 +28,4 @@ export * from './use-number-input.js';
28
28
  export * from './use-media-query.js';
29
29
  export * from './use-derived-state.js';
30
30
  export * from './/use-promise.js';
31
+ export * from './strict-context.js';
package/lib/index.js CHANGED
@@ -28,4 +28,5 @@ export * from './use-number-input.js';
28
28
  export * from './use-media-query.js';
29
29
  export * from './use-derived-state.js';
30
30
  export * from './/use-promise.js';
31
+ export * from './strict-context.js';
31
32
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA;AACvC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,sBAAsB,CAAA;AACpC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AACrC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,wBAAwB,CAAA;AACtC,cAAc,+BAA+B,CAAA;AAC7C,cAAc,2BAA2B,CAAA;AACzC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,0BAA0B,CAAA;AACxC,c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jC,cAAc,qBAAqB,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { Context } from 'react';
2
+ export type StrictContext<T> = Context<T | typeof dummy>;
3
+ declare const dummy: unique symbol;
4
+ export declare function createStrictContext<T>(): StrictContext<T>;
5
+ export declare function useStrictContext<T>(context: StrictContext<T>): T;
6
+ export {};
@@ -0,0 +1,12 @@
1
+ import { useContext, createContext } from 'react';
2
+ import { assert } from '@blackglory/prelude';
3
+ const dummy = Symbol();
4
+ export function createStrictContext() {
5
+ return createContext(dummy);
6
+ }
7
+ export function useStrictContext(context) {
8
+ const value = useContext(context);
9
+ assert(value !== dummy, 'This context cannot be used outside of a provider');
10
+ return value;
11
+ }
12
+ //# sourceMappingURL=strict-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strict-context.js","sourceRoot":"","sources":["../src/strict-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAW,aAAa,EAAE,MAAM,OAAO,CAAA;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAI5C,MAAM,KAAK,GAAG,MAAM,EAAE,CAAA;AAEtB,MAAM,UAAU,mBAAmB;IACjC,OAAO,aAAa,CAAmB,KAAK,CAAC,CAAA;AAC/C,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAI,OAAyB;IAC3D,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IACjC,MAAM,CAAC,KAAK,KAAK,KAAK,EAAE,mDAAmD,CAAC,CAAA;IAE5E,OAAO,KAAK,CAAA;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-react-hooks",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
package/src/index.ts CHANGED
@@ -28,3 +28,4 @@ export * from './use-number-input.js'
28
28
  export * from './use-media-query.js'
29
29
  export * from './use-derived-state.js'
30
30
  export * from './/use-promise.js'
31
+ export * from './strict-context.js'
@@ -0,0 +1,17 @@
1
+ import { useContext, Context, createContext } from 'react'
2
+ import { assert } from '@blackglory/prelude'
3
+
4
+ export type StrictContext<T> = Context<T | typeof dummy>
5
+
6
+ const dummy = Symbol()
7
+
8
+ export function createStrictContext<T>(): StrictContext<T> {
9
+ return createContext<T | typeof dummy>(dummy)
10
+ }
11
+
12
+ export function useStrictContext<T>(context: StrictContext<T>): T {
13
+ const value = useContext(context)
14
+ assert(value !== dummy, 'This context cannot be used outside of a provider')
15
+
16
+ return value
17
+ }