bunja 4.0.0-alpha.1 → 4.0.0-alpha.2

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.
@@ -0,0 +1,11 @@
1
+ import { Accessor, Context, JSX, ParentProps } from "solid-js";
2
+ import { Bunja, BunjaGetRef, BunjaStore, HashFn, Scope, ScopeValuePairs } from "../bunja.js";
3
+ type MaybeAccessor<T> = T | Accessor<T>;
4
+ type AccessedValue<T> = T extends Accessor<infer U> ? U : T;
5
+ export declare const BunjaStoreContext: Context<BunjaStore>;
6
+ export declare function BunjaStoreProvider(props: ParentProps): JSX.Element;
7
+ export declare const scopeContextMap: Map<Scope<unknown>, Context<MaybeAccessor<unknown>>>;
8
+ export declare function bindScope<T>(scope: Scope<T>, context: Context<MaybeAccessor<T>>): void;
9
+ export declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<AccessedValue<T>>): Scope<AccessedValue<T>>;
10
+ export declare function useBunja<T, Seed = undefined>(bunja: MaybeAccessor<Bunja<T, Seed> | BunjaGetRef<T, Seed>>, scopeValuePairs?: MaybeAccessor<ScopeValuePairs>): Accessor<T>;
11
+ export {};
@@ -0,0 +1,69 @@
1
+ import { createComponent, createContext, createEffect, createMemo, createRoot, getOwner, onCleanup, runWithOwner, useContext } from "solid-js";
2
+ import { createBunjaStore, createReadScopeFn, createScope } from "../bunja.js";
3
+ // solid/v1-compat.ts
4
+ const __DEV__ = process.env.NODE_ENV !== "production";
5
+ function access(maybeAccessor) {
6
+ if (typeof maybeAccessor !== "function") return maybeAccessor;
7
+ return maybeAccessor();
8
+ }
9
+ const BunjaStoreContext = createContext(createBunjaStore({ wrapInstance: createRoot }));
10
+ function BunjaStoreProvider(props) {
11
+ const owner = getOwner();
12
+ const bunjaStore = createBunjaStore({ wrapInstance: (impl) => createRoot(impl, owner) });
13
+ onCleanup(() => bunjaStore.dispose());
14
+ return createComponent(BunjaStoreContext.Provider, {
15
+ get value() {
16
+ return bunjaStore;
17
+ },
18
+ get children() {
19
+ return props.children;
20
+ }
21
+ });
22
+ }
23
+ const scopeContextMap = /* @__PURE__ */ new Map();
24
+ function bindScope(scope, context) {
25
+ scopeContextMap.set(scope, context);
26
+ }
27
+ function createScopeFromContext(context, hash) {
28
+ const scope = createScope(hash);
29
+ bindScope(scope, context);
30
+ return scope;
31
+ }
32
+ function useBunja(bunja, scopeValuePairs) {
33
+ const owner = getOwner();
34
+ const store = useContext(BunjaStoreContext);
35
+ const defaultReadScope = (scope) => {
36
+ const context = scopeContextMap.get(scope);
37
+ return access(runWithOwner(owner, () => useContext(context)));
38
+ };
39
+ const readScope = createMemo(() => {
40
+ const pairs = access(scopeValuePairs);
41
+ return {
42
+ scopeValuePairs: pairs,
43
+ read: pairs ? createReadScopeFn(pairs, defaultReadScope) : defaultReadScope
44
+ };
45
+ });
46
+ const entry = createMemo(() => {
47
+ const { read, scopeValuePairs } = readScope();
48
+ const resolvedBunja = access(bunja);
49
+ const scopes = /* @__PURE__ */ new Set();
50
+ const result = store.get(resolvedBunja, (scope) => {
51
+ scopes.add(scope);
52
+ return read(scope);
53
+ });
54
+ for (const scope of scopes) read(scope);
55
+ if (__DEV__) Object.assign(getOwner(), {
56
+ storeId: store.id,
57
+ bunja: resolvedBunja,
58
+ scopeValuePairs,
59
+ bunjaInstance: result.bunjaInstance
60
+ });
61
+ return result;
62
+ });
63
+ createEffect(() => {
64
+ const cleanup = entry().mount();
65
+ onCleanup(() => setTimeout(cleanup));
66
+ });
67
+ return () => entry().value;
68
+ }
69
+ export { BunjaStoreContext, BunjaStoreProvider, bindScope, createScopeFromContext, scopeContextMap, useBunja };
package/dist/solid.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { Bunja, BunjaGetRef, BunjaStore, HashFn, Scope, ScopeValuePairs } from "./bunja.js";
2
- import { Accessor, Context, JSX, ParentProps } from "solid-js";
2
+ import { Accessor, Context, Element, ParentProps } from "solid-js";
3
3
  type MaybeAccessor<T> = T | Accessor<T>;
4
4
  type AccessedValue<T> = T extends Accessor<infer U> ? U : T;
5
5
  export declare const BunjaStoreContext: Context<BunjaStore>;
6
- export declare function BunjaStoreProvider(props: ParentProps): JSX.Element;
6
+ export declare function BunjaStoreProvider(props: ParentProps): Element;
7
7
  export declare const scopeContextMap: Map<Scope<unknown>, Context<MaybeAccessor<unknown>>>;
8
8
  export declare function bindScope<T>(scope: Scope<T>, context: Context<MaybeAccessor<T>>): void;
9
9
  export declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<AccessedValue<T>>): Scope<AccessedValue<T>>;
10
- export declare function useBunja<T>(bunja: MaybeAccessor<Bunja<T, any> | BunjaGetRef<T, any>>, scopeValuePairs?: MaybeAccessor<ScopeValuePairs>): Accessor<T>;
10
+ export declare function useBunja<T, Seed = undefined>(bunja: MaybeAccessor<Bunja<T, Seed> | BunjaGetRef<T, Seed>>, scopeValuePairs?: MaybeAccessor<ScopeValuePairs>): Accessor<T>;
11
11
  export {};
package/dist/solid.js CHANGED
@@ -6,12 +6,12 @@ function access(maybeAccessor) {
6
6
  if (typeof maybeAccessor !== "function") return maybeAccessor;
7
7
  return maybeAccessor();
8
8
  }
9
- const BunjaStoreContext = createContext(createBunjaStore({ wrapInstance: createRoot }));
9
+ const BunjaStoreContext = createContext(createBunjaStore({ wrapInstance: (impl) => runWithOwner(null, () => createRoot(impl)) }));
10
10
  function BunjaStoreProvider(props) {
11
11
  const owner = getOwner();
12
- const bunjaStore = createBunjaStore({ wrapInstance: (impl) => createRoot(impl, owner) });
12
+ const bunjaStore = createBunjaStore({ wrapInstance: (impl) => runWithOwner(owner, () => createRoot(impl)) });
13
13
  onCleanup(() => bunjaStore.dispose());
14
- return createComponent(BunjaStoreContext.Provider, {
14
+ return createComponent(BunjaStoreContext, {
15
15
  get value() {
16
16
  return bunjaStore;
17
17
  },
@@ -36,33 +36,31 @@ function useBunja(bunja, scopeValuePairs) {
36
36
  const context = scopeContextMap.get(scope);
37
37
  return access(runWithOwner(owner, () => useContext(context)));
38
38
  };
39
- const readScope = createMemo(() => {
40
- const pairs = access(scopeValuePairs);
41
- return {
42
- scopeValuePairs: pairs,
43
- read: pairs ? createReadScopeFn(pairs, defaultReadScope) : defaultReadScope
44
- };
45
- });
46
39
  const entry = createMemo(() => {
47
- const { read, scopeValuePairs } = readScope();
40
+ const pairs = access(scopeValuePairs);
41
+ const read = pairs ? createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
48
42
  const resolvedBunja = access(bunja);
49
- const scopes = /* @__PURE__ */ new Set();
43
+ const resolutionOwner = getOwner();
44
+ const scopes = /* @__PURE__ */ new Map();
50
45
  const result = store.get(resolvedBunja, (scope) => {
51
- scopes.add(scope);
52
- return read(scope);
46
+ const key = scope;
47
+ if (!scopes.has(key)) scopes.set(key, runWithOwner(resolutionOwner, () => read(scope)));
48
+ return scopes.get(key);
53
49
  });
54
- for (const scope of scopes) read(scope);
50
+ for (const scope of scopes.keys()) read(scope);
55
51
  if (__DEV__) Object.assign(getOwner(), {
56
52
  storeId: store.id,
57
53
  bunja: resolvedBunja,
58
- scopeValuePairs,
54
+ scopeValuePairs: pairs,
59
55
  bunjaInstance: result.bunjaInstance
60
56
  });
61
57
  return result;
62
58
  });
63
- createEffect(() => {
64
- const cleanup = entry().mount();
65
- onCleanup(() => setTimeout(cleanup));
59
+ createEffect(entry, (resolved) => {
60
+ const cleanup = resolved.mount();
61
+ return () => {
62
+ setTimeout(cleanup);
63
+ };
66
64
  });
67
65
  return () => entry().value;
68
66
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bunja",
3
3
  "type": "module",
4
- "version": "4.0.0-alpha.1",
4
+ "version": "4.0.0-alpha.2",
5
5
  "description": "State Lifetime Manager",
6
6
  "main": "./dist/bunja.js",
7
7
  "types": "dist/bunja.d.ts",
@@ -17,13 +17,17 @@
17
17
  "./solid": {
18
18
  "types": "./dist/solid.d.ts",
19
19
  "default": "./dist/solid.js"
20
+ },
21
+ "./solid/v1-compat": {
22
+ "types": "./dist/solid/v1-compat.d.ts",
23
+ "default": "./dist/solid/v1-compat.js"
20
24
  }
21
25
  },
22
26
  "scripts": {
23
27
  "clean": "rm -rf ./dist",
24
28
  "build": "tsdown",
25
- "typecheck": "tsc --noEmit",
26
- "test": "node --import tsx --conditions=browser --test test.ts solid_test.ts react_test.ts override_test.ts"
29
+ "typecheck": "tsc --noEmit && tsc -p solid/tsconfig.json",
30
+ "test": "node --import tsx --conditions=browser --test test.ts solid_test.ts react_test.ts override_test.ts && node --import tsx --test solid_server_test.ts"
27
31
  },
28
32
  "keywords": [
29
33
  "bunja",
@@ -41,15 +45,16 @@
41
45
  "@types/react-test-renderer": "^19.3.0",
42
46
  "react": "^19",
43
47
  "react-test-renderer": "^19.3.1",
44
- "solid-js": "^1.9.7",
48
+ "solid-js": "2.0.0-rc.20",
45
49
  "tsdown": "0.23.3",
46
50
  "typescript": "^5.6.3",
47
- "tsx": "^4.20.0"
51
+ "tsx": "^4.20.0",
52
+ "solid-js-v1": "npm:solid-js@^1.9.7"
48
53
  },
49
54
  "peerDependencies": {
50
55
  "@types/react": "*",
51
56
  "react": ">=18",
52
- "solid-js": "^1"
57
+ "solid-js": "^1 || ^2.0.0-rc.20"
53
58
  },
54
59
  "peerDependenciesMeta": {
55
60
  "@types/react": {