bunja 2.1.0 → 3.0.0-alpha.3
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 +135 -4
- package/bunja.ts +847 -222
- package/deno.json +1 -1
- package/dist/bunja-BJSmIdkQ.cjs +682 -0
- package/dist/{bunja-BKpQTG04.d.cts → bunja-BxbzuHdH.d.cts} +73 -19
- package/dist/bunja-CtHOS4_Q.js +634 -0
- package/dist/{bunja-B_HNgDan.d.ts → bunja-DEFeIlpt.d.ts} +73 -19
- package/dist/bunja.cjs +1 -1
- package/dist/bunja.d.cts +2 -2
- package/dist/bunja.d.ts +2 -2
- package/dist/bunja.js +1 -1
- package/dist/react.cjs +16 -4
- package/dist/react.d.cts +3 -2
- package/dist/react.d.ts +3 -2
- package/dist/react.js +17 -4
- package/dist/solid.cjs +1 -1
- package/dist/solid.d.cts +2 -2
- package/dist/solid.d.ts +2 -2
- package/dist/solid.js +1 -1
- package/package.json +2 -2
- package/react.ts +34 -8
- package/solid.ts +4 -3
- package/test.ts +434 -4
- package/dist/bunja-BOUkMIz6.js +0 -396
- package/dist/bunja-DFFVW7Gi.cjs +0 -444
|
@@ -1,13 +1,40 @@
|
|
|
1
1
|
//#region bunja.d.ts
|
|
2
2
|
interface BunjaFn {
|
|
3
|
-
<T>(init: () => T): Bunja<T>;
|
|
3
|
+
<T>(init: () => T): Bunja<T, NoSeed>;
|
|
4
|
+
withSeed: BunjaWithSeedFn;
|
|
4
5
|
use: BunjaUseFn;
|
|
5
|
-
|
|
6
|
+
will: BunjaWillFn;
|
|
6
7
|
effect: BunjaEffectFn;
|
|
7
8
|
}
|
|
8
9
|
declare const bunja: BunjaFn;
|
|
9
|
-
|
|
10
|
-
type
|
|
10
|
+
declare const NO_SEED: unique symbol;
|
|
11
|
+
type NoSeed = typeof NO_SEED;
|
|
12
|
+
type BunjaWithSeedFn = <Seed, T>(defaultSeed: Seed, init: (seed: Seed) => T) => Bunja<T, Seed>;
|
|
13
|
+
type ScopeValuePairs = ScopeValuePair<any>[];
|
|
14
|
+
type BunjaRefBase<T, Seed> = {
|
|
15
|
+
bunja: Bunja<T, Seed>;
|
|
16
|
+
with?: ScopeValuePairs;
|
|
17
|
+
};
|
|
18
|
+
type BunjaGetRef<T, Seed = NoSeed> = BunjaRefBase<T, Seed> & ([Seed] extends [NoSeed] ? {
|
|
19
|
+
seed?: never;
|
|
20
|
+
} : {
|
|
21
|
+
seed?: Seed;
|
|
22
|
+
});
|
|
23
|
+
type BunjaRef<T, Seed = NoSeed> = BunjaGetRef<T, Seed>;
|
|
24
|
+
type BunjaPrebakeRef<T, Seed = NoSeed> = BunjaRefBase<T, Seed> & {
|
|
25
|
+
seed?: never;
|
|
26
|
+
};
|
|
27
|
+
interface BunjaUseFn {
|
|
28
|
+
<T>(dep: Scope<T>): T;
|
|
29
|
+
<T, Seed>(dep: Bunja<T, Seed>): T;
|
|
30
|
+
<T, Seed>(bunja: Bunja<T, Seed>, scopeValuePairs: ScopeValuePairs): T;
|
|
31
|
+
<T, Seed>(ref: BunjaRef<T, Seed>): T;
|
|
32
|
+
}
|
|
33
|
+
interface BunjaWillFn {
|
|
34
|
+
<T, Seed>(dep: Bunja<T, Seed>): () => T;
|
|
35
|
+
<T, Seed>(bunja: Bunja<T, Seed>, scopeValuePairs: ScopeValuePairs): () => T;
|
|
36
|
+
<T, Seed>(ref: BunjaRef<T, Seed>): () => T;
|
|
37
|
+
}
|
|
11
38
|
type BunjaEffectFn = (callback: BunjaEffectCallback) => void;
|
|
12
39
|
type BunjaEffectCallback = () => (() => void) | void;
|
|
13
40
|
declare function createScope<T>(hash?: HashFn<T>): Scope<T>;
|
|
@@ -15,12 +42,25 @@ interface CreateBunjaStoreConfig {
|
|
|
15
42
|
wrapInstance?: WrapInstanceFn;
|
|
16
43
|
}
|
|
17
44
|
declare function createBunjaStore(config?: CreateBunjaStoreConfig): BunjaStore;
|
|
18
|
-
type Dep<T> = Bunja<T> | Scope<T>;
|
|
45
|
+
type Dep<T> = Bunja<T, any> | Scope<T>;
|
|
46
|
+
type AnyBunja = Bunja<any, any>;
|
|
19
47
|
interface InternalState {
|
|
20
48
|
bunjas: Record<string, BunjaInstance>;
|
|
21
49
|
scopes: Map<Scope<unknown>, Map<unknown, ScopeInstance>>;
|
|
22
50
|
instantiating: boolean;
|
|
23
51
|
}
|
|
52
|
+
type AnyNormalizedBunjaRef = NormalizedBunjaRef<any, any>;
|
|
53
|
+
interface NormalizedBunjaRef<T, Seed> {
|
|
54
|
+
bunja: Bunja<T, Seed>;
|
|
55
|
+
scopeValuePairs: ScopeValuePairs;
|
|
56
|
+
}
|
|
57
|
+
interface ActiveDependencyRecipe {
|
|
58
|
+
ref: AnyNormalizedBunjaRef;
|
|
59
|
+
seed: unknown;
|
|
60
|
+
}
|
|
61
|
+
interface BunjaInstanceRecipe {
|
|
62
|
+
activeDependencies: ActiveDependencyRecipe[];
|
|
63
|
+
}
|
|
24
64
|
type WrapInstanceFn = <T>(fn: (dispose: () => void) => T) => T;
|
|
25
65
|
declare class BunjaStore {
|
|
26
66
|
#private;
|
|
@@ -30,7 +70,8 @@ declare class BunjaStore {
|
|
|
30
70
|
constructor();
|
|
31
71
|
get _internalState(): InternalState | undefined;
|
|
32
72
|
dispose(): void;
|
|
33
|
-
get<T>(
|
|
73
|
+
get<T, Seed>(bunjaOrRef: Bunja<T, Seed> | BunjaGetRef<T, Seed>, readScope: ReadScope): BunjaStoreGetResult<T>;
|
|
74
|
+
prebake<T, Seed>(bunjaOrRef: Bunja<T, Seed> | BunjaPrebakeRef<T, Seed>, readScope: ReadScope): BunjaStorePrebakeResult;
|
|
34
75
|
}
|
|
35
76
|
type ReadScope = <T>(scope: Scope<T>) => T;
|
|
36
77
|
declare function createReadScopeFn(scopeValuePairs: ScopeValuePair<any>[], readScope: ReadScope): ReadScope;
|
|
@@ -40,22 +81,33 @@ interface BunjaStoreGetResult<T> {
|
|
|
40
81
|
deps: unknown[];
|
|
41
82
|
bunjaInstance?: BunjaInstance;
|
|
42
83
|
}
|
|
84
|
+
interface BunjaStorePrebakeResult {
|
|
85
|
+
relatedBunjas: Bunja<any, any>[];
|
|
86
|
+
requiredScopes: Scope<unknown>[];
|
|
87
|
+
}
|
|
43
88
|
declare function delayUnmount(mount: () => () => void, ms?: number): () => () => void;
|
|
44
|
-
declare class Bunja<T> {
|
|
89
|
+
declare class Bunja<T, Seed = NoSeed> {
|
|
45
90
|
#private;
|
|
46
|
-
init: () => T;
|
|
91
|
+
init: (seed: Seed) => T;
|
|
92
|
+
defaultSeed: Seed;
|
|
47
93
|
private static counter;
|
|
48
94
|
readonly id: string;
|
|
49
95
|
debugLabel: string;
|
|
50
|
-
constructor(init: () => T);
|
|
96
|
+
constructor(init: (seed: Seed) => T, defaultSeed: Seed);
|
|
51
97
|
get baked(): boolean;
|
|
52
|
-
get
|
|
53
|
-
get
|
|
54
|
-
get
|
|
55
|
-
|
|
98
|
+
get requiredBunjas(): AnyBunja[];
|
|
99
|
+
get optionalBunjas(): AnyBunja[];
|
|
100
|
+
get requiredBunjaRefs(): AnyNormalizedBunjaRef[];
|
|
101
|
+
get optionalBunjaRefs(): AnyNormalizedBunjaRef[];
|
|
102
|
+
get expandedRequiredBunjas(): AnyBunja[];
|
|
103
|
+
get relatedBunjas(): AnyBunja[];
|
|
104
|
+
get requiredScopes(): Scope<unknown>[];
|
|
105
|
+
addRequiredBunjaRef(ref: AnyNormalizedBunjaRef): void;
|
|
106
|
+
addOptionalBunjaRef(ref: AnyNormalizedBunjaRef): void;
|
|
56
107
|
addScope(scope: Scope<unknown>): void;
|
|
57
108
|
bake(): void;
|
|
58
|
-
|
|
109
|
+
calcBaseInstanceId(scopeInstanceMap: Map<Scope<unknown>, ScopeInstance>): string;
|
|
110
|
+
calcInstanceId(scopeInstanceMap: Map<Scope<unknown>, ScopeInstance>, activeDependencyIds?: Iterable<string>): string;
|
|
59
111
|
toString(): string;
|
|
60
112
|
}
|
|
61
113
|
declare class Scope<T> {
|
|
@@ -79,10 +131,13 @@ declare abstract class RefCounter {
|
|
|
79
131
|
declare class BunjaInstance extends RefCounter {
|
|
80
132
|
#private;
|
|
81
133
|
readonly id: string;
|
|
134
|
+
readonly baseId: string;
|
|
82
135
|
readonly value: unknown;
|
|
83
|
-
readonly
|
|
136
|
+
private readonly dependencyMounts;
|
|
137
|
+
private readonly effects;
|
|
138
|
+
readonly recipe: BunjaInstanceRecipe;
|
|
84
139
|
private readonly _dispose;
|
|
85
|
-
constructor(id: string, value: unknown,
|
|
140
|
+
constructor(id: string, baseId: string, value: unknown, dependencyMounts: (() => () => void)[], effects: BunjaEffectCallback[], recipe: BunjaInstanceRecipe, _dispose: () => void);
|
|
86
141
|
dispose(): void;
|
|
87
142
|
add(): void;
|
|
88
143
|
}
|
|
@@ -94,9 +149,8 @@ declare class ScopeInstance extends RefCounter {
|
|
|
94
149
|
constructor(value: unknown, dispose: () => void);
|
|
95
150
|
}
|
|
96
151
|
interface BunjaDevtoolsGlobalHook {
|
|
97
|
-
bunjas: Record<string, Bunja<any>>;
|
|
152
|
+
bunjas: Record<string, Bunja<any, any>>;
|
|
98
153
|
scopes: Record<string, Scope<any>>;
|
|
99
|
-
stores: Record<string, BunjaStore>;
|
|
100
154
|
listeners: Record<BunjaDevtoolsEventType, Set<(event: any) => void>>;
|
|
101
155
|
emit<T extends BunjaDevtoolsEventType>(type: T, event: BunjaDevtoolsEvent[T]): void;
|
|
102
156
|
on<T extends BunjaDevtoolsEventType>(type: T, listener: (event: BunjaDevtoolsEvent[T]) => void): () => void;
|
|
@@ -139,4 +193,4 @@ interface BunjaDevtoolsEvent {
|
|
|
139
193
|
}
|
|
140
194
|
type BunjaDevtoolsEventType = keyof BunjaDevtoolsEvent;
|
|
141
195
|
//#endregion
|
|
142
|
-
export { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn,
|
|
196
|
+
export { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn, BunjaGetRef, BunjaRef, BunjaStore, BunjaStoreGetResult, BunjaStorePrebakeResult, BunjaUseFn, BunjaWillFn, BunjaWithSeedFn, CreateBunjaStoreConfig, Dep, HashFn, NoSeed, ReadScope, Scope, ScopeValuePair, ScopeValuePairs, WrapInstanceFn, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount };
|
package/dist/bunja.cjs
CHANGED
package/dist/bunja.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn,
|
|
2
|
-
export { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn,
|
|
1
|
+
import { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn, BunjaGetRef, BunjaRef, BunjaStore, BunjaStoreGetResult, BunjaStorePrebakeResult, BunjaUseFn, BunjaWillFn, BunjaWithSeedFn, CreateBunjaStoreConfig, Dep, HashFn, NoSeed, ReadScope, Scope, ScopeValuePair, ScopeValuePairs, WrapInstanceFn, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount } from "./bunja-BxbzuHdH.cjs";
|
|
2
|
+
export { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn, BunjaGetRef, BunjaRef, BunjaStore, BunjaStoreGetResult, BunjaStorePrebakeResult, BunjaUseFn, BunjaWillFn, BunjaWithSeedFn, CreateBunjaStoreConfig, Dep, HashFn, NoSeed, ReadScope, Scope, ScopeValuePair, ScopeValuePairs, WrapInstanceFn, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount };
|
package/dist/bunja.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn,
|
|
2
|
-
export { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn,
|
|
1
|
+
import { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn, BunjaGetRef, BunjaRef, BunjaStore, BunjaStoreGetResult, BunjaStorePrebakeResult, BunjaUseFn, BunjaWillFn, BunjaWithSeedFn, CreateBunjaStoreConfig, Dep, HashFn, NoSeed, ReadScope, Scope, ScopeValuePair, ScopeValuePairs, WrapInstanceFn, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount } from "./bunja-DEFeIlpt.js";
|
|
2
|
+
export { Bunja, BunjaDevtoolsEvent, BunjaDevtoolsEventType, BunjaDevtoolsGlobalHook, BunjaEffectCallback, BunjaEffectFn, BunjaFn, BunjaGetRef, BunjaRef, BunjaStore, BunjaStoreGetResult, BunjaStorePrebakeResult, BunjaUseFn, BunjaWillFn, BunjaWithSeedFn, CreateBunjaStoreConfig, Dep, HashFn, NoSeed, ReadScope, Scope, ScopeValuePair, ScopeValuePairs, WrapInstanceFn, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount };
|
package/dist/bunja.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount } from "./bunja-
|
|
1
|
+
import { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount } from "./bunja-CtHOS4_Q.js";
|
|
2
2
|
|
|
3
3
|
export { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount };
|
package/dist/react.cjs
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
5
|
-
const require_bunja = require('./bunja-
|
|
5
|
+
const require_bunja = require('./bunja-BJSmIdkQ.cjs');
|
|
6
6
|
let react = require("react");
|
|
7
7
|
react = require_chunk.__toESM(react);
|
|
8
8
|
|
|
9
9
|
//#region react.ts
|
|
10
10
|
const __DEV__ = process.env.NODE_ENV !== "production";
|
|
11
|
+
const reactUse = react.use;
|
|
11
12
|
const BunjaStoreContext = (0, react.createContext)(require_bunja.createBunjaStore());
|
|
12
13
|
function BunjaStoreProvider({ children }) {
|
|
13
14
|
const [value] = (0, react.useState)(require_bunja.createBunjaStore);
|
|
@@ -18,7 +19,9 @@ function BunjaStoreProvider({ children }) {
|
|
|
18
19
|
});
|
|
19
20
|
}
|
|
20
21
|
const scopeContextMap = /* @__PURE__ */ new Map();
|
|
22
|
+
let scopeContextMapLocked = false;
|
|
21
23
|
function bindScope(scope, context) {
|
|
24
|
+
if (__DEV__ && !reactUse && scopeContextMapLocked) throw new Error("`bindScope` must be called before rendering when using React 18.");
|
|
22
25
|
scopeContextMap.set(scope, context);
|
|
23
26
|
}
|
|
24
27
|
function createScopeFromContext(context, hash) {
|
|
@@ -26,11 +29,20 @@ function createScopeFromContext(context, hash) {
|
|
|
26
29
|
bindScope(scope, context);
|
|
27
30
|
return scope;
|
|
28
31
|
}
|
|
29
|
-
|
|
30
|
-
return
|
|
31
|
-
|
|
32
|
+
function useScopeContextValues() {
|
|
33
|
+
if (reactUse) return void 0;
|
|
34
|
+
scopeContextMapLocked = true;
|
|
35
|
+
return new Map(Array.from(scopeContextMap, ([scope, context]) => [scope, (0, react.useContext)(context)]));
|
|
36
|
+
}
|
|
32
37
|
function useBunja(bunja, scopeValuePairs) {
|
|
33
38
|
const store = (0, react.useContext)(BunjaStoreContext);
|
|
39
|
+
const scopeContextValues = useScopeContextValues();
|
|
40
|
+
const defaultReadScope = (scope) => {
|
|
41
|
+
const context = scopeContextMap.get(scope);
|
|
42
|
+
if (!context) throw new Error("Scope is not bound to a React context.");
|
|
43
|
+
if (reactUse) return reactUse(context);
|
|
44
|
+
return scopeContextValues.get(scope);
|
|
45
|
+
};
|
|
34
46
|
const readScope = scopeValuePairs ? require_bunja.createReadScopeFn(scopeValuePairs, defaultReadScope) : defaultReadScope;
|
|
35
47
|
if (__DEV__) {
|
|
36
48
|
if (store._internalState?.instantiating) throw new Error("`useBunja` cannot be called inside a bunja init function.");
|
package/dist/react.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Bunja, BunjaStore, HashFn, Scope,
|
|
1
|
+
import { Bunja, BunjaGetRef, BunjaStore, HashFn, Scope, ScopeValuePairs } from "./bunja-BxbzuHdH.cjs";
|
|
2
|
+
import * as React from "react";
|
|
2
3
|
import { Context, PropsWithChildren } from "react";
|
|
3
4
|
|
|
4
5
|
//#region react.d.ts
|
|
@@ -9,6 +10,6 @@ declare function BunjaStoreProvider({
|
|
|
9
10
|
declare const scopeContextMap: Map<Scope<unknown>, Context<unknown>>;
|
|
10
11
|
declare function bindScope<T>(scope: Scope<T>, context: Context<T>): void;
|
|
11
12
|
declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<T>): Scope<T>;
|
|
12
|
-
declare function useBunja<T>(bunja: Bunja<T>, scopeValuePairs?:
|
|
13
|
+
declare function useBunja<T, Seed>(bunja: Bunja<T, Seed> | BunjaGetRef<T, Seed>, scopeValuePairs?: ScopeValuePairs): T;
|
|
13
14
|
//#endregion
|
|
14
15
|
export { BunjaStoreContext, BunjaStoreProvider, bindScope, createScopeFromContext, scopeContextMap, useBunja };
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Bunja, BunjaStore, HashFn, Scope,
|
|
1
|
+
import { Bunja, BunjaGetRef, BunjaStore, HashFn, Scope, ScopeValuePairs } from "./bunja-DEFeIlpt.js";
|
|
2
|
+
import * as React from "react";
|
|
2
3
|
import { Context, PropsWithChildren } from "react";
|
|
3
4
|
|
|
4
5
|
//#region react.d.ts
|
|
@@ -9,6 +10,6 @@ declare function BunjaStoreProvider({
|
|
|
9
10
|
declare const scopeContextMap: Map<Scope<unknown>, Context<unknown>>;
|
|
10
11
|
declare function bindScope<T>(scope: Scope<T>, context: Context<T>): void;
|
|
11
12
|
declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<T>): Scope<T>;
|
|
12
|
-
declare function useBunja<T>(bunja: Bunja<T>, scopeValuePairs?:
|
|
13
|
+
declare function useBunja<T, Seed>(bunja: Bunja<T, Seed> | BunjaGetRef<T, Seed>, scopeValuePairs?: ScopeValuePairs): T;
|
|
13
14
|
//#endregion
|
|
14
15
|
export { BunjaStoreContext, BunjaStoreProvider, bindScope, createScopeFromContext, scopeContextMap, useBunja };
|
package/dist/react.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
import { createBunjaStore, createReadScopeFn, createScope, delayUnmount } from "./bunja-
|
|
4
|
+
import { createBunjaStore, createReadScopeFn, createScope, delayUnmount } from "./bunja-CtHOS4_Q.js";
|
|
5
|
+
import * as React from "react";
|
|
5
6
|
import { createContext, createElement, useContext, useEffect, useMemo, useState } from "react";
|
|
6
7
|
|
|
7
8
|
//#region react.ts
|
|
8
9
|
const __DEV__ = process.env.NODE_ENV !== "production";
|
|
10
|
+
const reactUse = React.use;
|
|
9
11
|
const BunjaStoreContext = createContext(createBunjaStore());
|
|
10
12
|
function BunjaStoreProvider({ children }) {
|
|
11
13
|
const [value] = useState(createBunjaStore);
|
|
@@ -16,7 +18,9 @@ function BunjaStoreProvider({ children }) {
|
|
|
16
18
|
});
|
|
17
19
|
}
|
|
18
20
|
const scopeContextMap = /* @__PURE__ */ new Map();
|
|
21
|
+
let scopeContextMapLocked = false;
|
|
19
22
|
function bindScope(scope, context) {
|
|
23
|
+
if (__DEV__ && !reactUse && scopeContextMapLocked) throw new Error("`bindScope` must be called before rendering when using React 18.");
|
|
20
24
|
scopeContextMap.set(scope, context);
|
|
21
25
|
}
|
|
22
26
|
function createScopeFromContext(context, hash) {
|
|
@@ -24,11 +28,20 @@ function createScopeFromContext(context, hash) {
|
|
|
24
28
|
bindScope(scope, context);
|
|
25
29
|
return scope;
|
|
26
30
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
function useScopeContextValues() {
|
|
32
|
+
if (reactUse) return void 0;
|
|
33
|
+
scopeContextMapLocked = true;
|
|
34
|
+
return new Map(Array.from(scopeContextMap, ([scope, context]) => [scope, useContext(context)]));
|
|
35
|
+
}
|
|
30
36
|
function useBunja(bunja, scopeValuePairs) {
|
|
31
37
|
const store = useContext(BunjaStoreContext);
|
|
38
|
+
const scopeContextValues = useScopeContextValues();
|
|
39
|
+
const defaultReadScope = (scope) => {
|
|
40
|
+
const context = scopeContextMap.get(scope);
|
|
41
|
+
if (!context) throw new Error("Scope is not bound to a React context.");
|
|
42
|
+
if (reactUse) return reactUse(context);
|
|
43
|
+
return scopeContextValues.get(scope);
|
|
44
|
+
};
|
|
32
45
|
const readScope = scopeValuePairs ? createReadScopeFn(scopeValuePairs, defaultReadScope) : defaultReadScope;
|
|
33
46
|
if (__DEV__) {
|
|
34
47
|
if (store._internalState?.instantiating) throw new Error("`useBunja` cannot be called inside a bunja init function.");
|
package/dist/solid.cjs
CHANGED
package/dist/solid.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bunja, BunjaStore, HashFn, Scope,
|
|
1
|
+
import { Bunja, BunjaGetRef, BunjaStore, HashFn, Scope, ScopeValuePairs } from "./bunja-BxbzuHdH.cjs";
|
|
2
2
|
import { Accessor, Context, JSX, ParentProps } from "solid-js";
|
|
3
3
|
|
|
4
4
|
//#region solid.d.ts
|
|
@@ -9,6 +9,6 @@ declare function BunjaStoreProvider(props: ParentProps): JSX.Element;
|
|
|
9
9
|
declare const scopeContextMap: Map<Scope<unknown>, Context<MaybeAccessor<unknown>>>;
|
|
10
10
|
declare function bindScope<T>(scope: Scope<T>, context: Context<MaybeAccessor<T>>): void;
|
|
11
11
|
declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<AccessedValue<T>>): Scope<AccessedValue<T>>;
|
|
12
|
-
declare function useBunja<T>(bunja: MaybeAccessor<Bunja<T>>, scopeValuePairs?: MaybeAccessor<
|
|
12
|
+
declare function useBunja<T>(bunja: MaybeAccessor<Bunja<T, any> | BunjaGetRef<T, any>>, scopeValuePairs?: MaybeAccessor<ScopeValuePairs>): Accessor<T>;
|
|
13
13
|
//#endregion
|
|
14
14
|
export { BunjaStoreContext, BunjaStoreProvider, bindScope, createScopeFromContext, scopeContextMap, useBunja };
|
package/dist/solid.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bunja, BunjaStore, HashFn, Scope,
|
|
1
|
+
import { Bunja, BunjaGetRef, BunjaStore, HashFn, Scope, ScopeValuePairs } from "./bunja-DEFeIlpt.js";
|
|
2
2
|
import { Accessor, Context, JSX, ParentProps } from "solid-js";
|
|
3
3
|
|
|
4
4
|
//#region solid.d.ts
|
|
@@ -9,6 +9,6 @@ declare function BunjaStoreProvider(props: ParentProps): JSX.Element;
|
|
|
9
9
|
declare const scopeContextMap: Map<Scope<unknown>, Context<MaybeAccessor<unknown>>>;
|
|
10
10
|
declare function bindScope<T>(scope: Scope<T>, context: Context<MaybeAccessor<T>>): void;
|
|
11
11
|
declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<AccessedValue<T>>): Scope<AccessedValue<T>>;
|
|
12
|
-
declare function useBunja<T>(bunja: MaybeAccessor<Bunja<T>>, scopeValuePairs?: MaybeAccessor<
|
|
12
|
+
declare function useBunja<T>(bunja: MaybeAccessor<Bunja<T, any> | BunjaGetRef<T, any>>, scopeValuePairs?: MaybeAccessor<ScopeValuePairs>): Accessor<T>;
|
|
13
13
|
//#endregion
|
|
14
14
|
export { BunjaStoreContext, BunjaStoreProvider, bindScope, createScopeFromContext, scopeContextMap, useBunja };
|
package/dist/solid.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createBunjaStore, createReadScopeFn, createScope } from "./bunja-
|
|
1
|
+
import { createBunjaStore, createReadScopeFn, createScope } from "./bunja-CtHOS4_Q.js";
|
|
2
2
|
import { createComponent, createContext, createEffect, createMemo, createRoot, getOwner, onCleanup, useContext } from "solid-js";
|
|
3
3
|
|
|
4
4
|
//#region solid.ts
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bunja",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0-alpha.3",
|
|
5
5
|
"description": "State Lifetime Manager",
|
|
6
6
|
"main": "dist/bunja.cjs",
|
|
7
7
|
"module": "dist/bunja.js",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"clean": "rm -rf ./dist",
|
|
56
|
-
"build": "tsdown"
|
|
56
|
+
"build": "tsdown --format cjs && tsdown --format esm --no-clean"
|
|
57
57
|
},
|
|
58
58
|
"keywords": [
|
|
59
59
|
"bunja",
|
package/react.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
} from "react";
|
|
13
13
|
import {
|
|
14
14
|
type Bunja,
|
|
15
|
+
type BunjaGetRef,
|
|
15
16
|
type BunjaStore,
|
|
16
17
|
createBunjaStore,
|
|
17
18
|
createReadScopeFn,
|
|
@@ -20,12 +21,16 @@ import {
|
|
|
20
21
|
type HashFn,
|
|
21
22
|
type ReadScope,
|
|
22
23
|
type Scope,
|
|
23
|
-
type
|
|
24
|
+
type ScopeValuePairs,
|
|
24
25
|
} from "./bunja.ts";
|
|
26
|
+
import * as React from "react";
|
|
25
27
|
|
|
26
28
|
// @ts-ignore dev
|
|
27
29
|
// deno-lint-ignore no-process-global
|
|
28
30
|
const __DEV__ = process.env.NODE_ENV !== "production";
|
|
31
|
+
const reactUse = (React as unknown as {
|
|
32
|
+
use?: <T>(usable: Context<T>) => T;
|
|
33
|
+
}).use;
|
|
29
34
|
|
|
30
35
|
export const BunjaStoreContext: Context<BunjaStore> = createContext(
|
|
31
36
|
createBunjaStore(),
|
|
@@ -40,7 +45,13 @@ export function BunjaStoreProvider(
|
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
export const scopeContextMap: Map<Scope<unknown>, Context<unknown>> = new Map();
|
|
48
|
+
let scopeContextMapLocked = false;
|
|
43
49
|
export function bindScope<T>(scope: Scope<T>, context: Context<T>): void {
|
|
50
|
+
if (__DEV__ && !reactUse && scopeContextMapLocked) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
"`bindScope` must be called before rendering when using React 18.",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
44
55
|
scopeContextMap.set(scope as Scope<unknown>, context as Context<unknown>);
|
|
45
56
|
}
|
|
46
57
|
|
|
@@ -53,16 +64,31 @@ export function createScopeFromContext<T>(
|
|
|
53
64
|
return scope;
|
|
54
65
|
}
|
|
55
66
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
67
|
+
function useScopeContextValues(): Map<Scope<unknown>, unknown> | undefined {
|
|
68
|
+
if (reactUse) return undefined;
|
|
69
|
+
scopeContextMapLocked = true;
|
|
70
|
+
return new Map(
|
|
71
|
+
Array.from(scopeContextMap, ([scope, context]) => [
|
|
72
|
+
scope,
|
|
73
|
+
useContext(context),
|
|
74
|
+
]),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
60
77
|
|
|
61
|
-
export function useBunja<T>(
|
|
62
|
-
bunja: Bunja<T>,
|
|
63
|
-
scopeValuePairs?:
|
|
78
|
+
export function useBunja<T, Seed>(
|
|
79
|
+
bunja: Bunja<T, Seed> | BunjaGetRef<T, Seed>,
|
|
80
|
+
scopeValuePairs?: ScopeValuePairs,
|
|
64
81
|
): T {
|
|
65
82
|
const store = useContext(BunjaStoreContext);
|
|
83
|
+
const scopeContextValues = useScopeContextValues();
|
|
84
|
+
const defaultReadScope: ReadScope = <T>(scope: Scope<T>) => {
|
|
85
|
+
const context = scopeContextMap.get(scope as Scope<unknown>) as
|
|
86
|
+
| Context<T>
|
|
87
|
+
| undefined;
|
|
88
|
+
if (!context) throw new Error("Scope is not bound to a React context.");
|
|
89
|
+
if (reactUse) return reactUse(context);
|
|
90
|
+
return scopeContextValues!.get(scope as Scope<unknown>) as T;
|
|
91
|
+
};
|
|
66
92
|
const readScope = scopeValuePairs
|
|
67
93
|
? createReadScopeFn(scopeValuePairs, defaultReadScope)
|
|
68
94
|
: defaultReadScope;
|
package/solid.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from "solid-js";
|
|
15
15
|
import {
|
|
16
16
|
type Bunja,
|
|
17
|
+
type BunjaGetRef,
|
|
17
18
|
type BunjaStore,
|
|
18
19
|
createBunjaStore,
|
|
19
20
|
createReadScopeFn,
|
|
@@ -21,7 +22,7 @@ import {
|
|
|
21
22
|
type HashFn,
|
|
22
23
|
type ReadScope,
|
|
23
24
|
type Scope,
|
|
24
|
-
type
|
|
25
|
+
type ScopeValuePairs,
|
|
25
26
|
} from "./bunja.ts";
|
|
26
27
|
|
|
27
28
|
type MaybeAccessor<T> = T | Accessor<T>;
|
|
@@ -82,8 +83,8 @@ const defaultReadScope: ReadScope = <T>(scope: Scope<T>) => {
|
|
|
82
83
|
};
|
|
83
84
|
|
|
84
85
|
export function useBunja<T>(
|
|
85
|
-
bunja: MaybeAccessor<Bunja<T>>,
|
|
86
|
-
scopeValuePairs?: MaybeAccessor<
|
|
86
|
+
bunja: MaybeAccessor<Bunja<T, any> | BunjaGetRef<T, any>>,
|
|
87
|
+
scopeValuePairs?: MaybeAccessor<ScopeValuePairs>,
|
|
87
88
|
): Accessor<T> {
|
|
88
89
|
const store = useContext(BunjaStoreContext);
|
|
89
90
|
const readScope = createMemo(() => {
|