bunja 2.0.0-alpha.1 → 2.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/bunja.ts CHANGED
@@ -94,12 +94,7 @@ export class BunjaStore {
94
94
  this.#getScopeInstance(scope, readScope(scope)),
95
95
  ]),
96
96
  );
97
- const bunjaInstanceMap = new Map(
98
- bunja.relatedBunjas.map((relatedBunja) => [
99
- relatedBunja,
100
- this.#getBunjaInstance(relatedBunja, scopeInstanceMap),
101
- ]),
102
- );
97
+ const bunjaInstanceMap = new Map();
103
98
  bunjaFn.use = <T>(dep: Dep<T>) => {
104
99
  if (dep instanceof Bunja) {
105
100
  return bunjaInstanceMap.get(dep as Bunja<unknown>)!.value as T;
@@ -109,6 +104,12 @@ export class BunjaStore {
109
104
  }
110
105
  throw new Error("`bunja.use` can only be used with Bunja or Scope.");
111
106
  };
107
+ for (const relatedBunja of bunja.relatedBunjas) {
108
+ bunjaInstanceMap.set(
109
+ relatedBunja,
110
+ this.#getBunjaInstance(relatedBunja, scopeInstanceMap),
111
+ );
112
+ }
112
113
  const bunjaInstance = this.#getBunjaInstance(bunja, scopeInstanceMap);
113
114
  return { bunjaInstance, bunjaInstanceMap, scopeInstanceMap };
114
115
  }
@@ -0,0 +1,118 @@
1
+ module.exports = function (fileInfo, api) {
2
+ const j = api.jscodeshift;
3
+ const root = j(fileInfo.source);
4
+ let modified = false;
5
+
6
+ root.find(j.CallExpression, { callee: { name: "bunja" } }).forEach((path) => {
7
+ const { node } = path;
8
+ const args = node.arguments;
9
+ if (args.length !== 2) return;
10
+
11
+ const [depsArray, initFn] = args;
12
+ if (depsArray.type !== "ArrayExpression") return;
13
+ if (
14
+ initFn.type !== "ArrowFunctionExpression" &&
15
+ initFn.type !== "FunctionExpression"
16
+ ) return;
17
+ const params = initFn.params;
18
+
19
+ const bodyStatements = initFn.body.type === "BlockStatement"
20
+ ? [...initFn.body.body]
21
+ : [{ type: "ReturnStatement", argument: initFn.body }];
22
+
23
+ const useStatements = depsArray.elements.map((dep, index) => {
24
+ if (index < params.length) {
25
+ return {
26
+ type: "VariableDeclaration",
27
+ kind: "const",
28
+ declarations: [{
29
+ type: "VariableDeclarator",
30
+ id: params[index],
31
+ init: {
32
+ type: "CallExpression",
33
+ callee: {
34
+ type: "MemberExpression",
35
+ object: { type: "Identifier", name: "bunja" },
36
+ property: { type: "Identifier", name: "use" },
37
+ computed: false,
38
+ },
39
+ arguments: [dep],
40
+ },
41
+ }],
42
+ };
43
+ } else {
44
+ return {
45
+ type: "ExpressionStatement",
46
+ expression: {
47
+ type: "CallExpression",
48
+ callee: {
49
+ type: "MemberExpression",
50
+ object: { type: "Identifier", name: "bunja" },
51
+ property: { type: "Identifier", name: "use" },
52
+ computed: false,
53
+ },
54
+ arguments: [dep],
55
+ },
56
+ };
57
+ }
58
+ });
59
+
60
+ for (let i = 0; i < bodyStatements.length; ++i) {
61
+ const statement = bodyStatements[i];
62
+ if (!statement || statement.type !== "ReturnStatement") continue;
63
+ if (statement.argument?.type !== "ObjectExpression") continue;
64
+ const returnObj = statement.argument;
65
+ const props = returnObj.properties;
66
+
67
+ const effectPropIndex = props.findIndex((prop) =>
68
+ prop?.computed &&
69
+ prop.key?.type === "MemberExpression" &&
70
+ prop.key?.object?.name === "bunja" &&
71
+ prop.key?.property?.name === "effect"
72
+ );
73
+ if (effectPropIndex === -1) continue;
74
+
75
+ const prop = props[effectPropIndex];
76
+ let effectFn = prop.value;
77
+
78
+ if (effectFn.type === "FunctionExpression") {
79
+ effectFn = {
80
+ type: "ArrowFunctionExpression",
81
+ params: effectFn.params,
82
+ body: effectFn.body,
83
+ };
84
+ }
85
+
86
+ const effectStatement = {
87
+ type: "ExpressionStatement",
88
+ expression: {
89
+ type: "CallExpression",
90
+ callee: {
91
+ type: "MemberExpression",
92
+ object: { type: "Identifier", name: "bunja" },
93
+ property: { type: "Identifier", name: "effect" },
94
+ computed: false,
95
+ },
96
+ arguments: [effectFn],
97
+ },
98
+ };
99
+
100
+ props.splice(effectPropIndex, 1);
101
+ bodyStatements.splice(i, 0, effectStatement);
102
+ ++i;
103
+ }
104
+
105
+ node.arguments = [{
106
+ type: "ArrowFunctionExpression",
107
+ params: [],
108
+ body: {
109
+ type: "BlockStatement",
110
+ body: [...useStatements, ...bodyStatements],
111
+ },
112
+ }];
113
+
114
+ modified = true;
115
+ });
116
+
117
+ return modified ? root.toSource() : fileInfo.source;
118
+ };
@@ -1,4 +1,5 @@
1
1
  import { type Bunja, bunja as bunjaV2, type Dep } from "../bunja.ts";
2
+ export { Bunja, createScope, Scope } from "../bunja.ts";
2
3
 
3
4
  export const bunja: {
4
5
  <T>(deps: [], init: () => T & BunjaValue): Bunja<T>;
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@disjukr/bunja",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.3",
4
4
  "license": "Zlib",
5
5
  "exports": {
6
6
  ".": "./bunja.ts",
@@ -55,12 +55,13 @@ var BunjaStore = class {
55
55
  }
56
56
  #getBaked(bunja$1, readScope) {
57
57
  const scopeInstanceMap = new Map(bunja$1.relatedScopes.map((scope) => [scope, this.#getScopeInstance(scope, readScope(scope))]));
58
- const bunjaInstanceMap = new Map(bunja$1.relatedBunjas.map((relatedBunja) => [relatedBunja, this.#getBunjaInstance(relatedBunja, scopeInstanceMap)]));
58
+ const bunjaInstanceMap = new Map();
59
59
  bunjaFn.use = (dep) => {
60
60
  if (dep instanceof Bunja) return bunjaInstanceMap.get(dep).value;
61
61
  if (dep instanceof Scope) return scopeInstanceMap.get(dep).value;
62
62
  throw new Error("`bunja.use` can only be used with Bunja or Scope.");
63
63
  };
64
+ for (const relatedBunja of bunja$1.relatedBunjas) bunjaInstanceMap.set(relatedBunja, this.#getBunjaInstance(relatedBunja, scopeInstanceMap));
64
65
  const bunjaInstance = this.#getBunjaInstance(bunja$1, scopeInstanceMap);
65
66
  return {
66
67
  bunjaInstance,
@@ -56,12 +56,13 @@ var BunjaStore = class {
56
56
  }
57
57
  #getBaked(bunja$1, readScope) {
58
58
  const scopeInstanceMap = new Map(bunja$1.relatedScopes.map((scope) => [scope, this.#getScopeInstance(scope, readScope(scope))]));
59
- const bunjaInstanceMap = new Map(bunja$1.relatedBunjas.map((relatedBunja) => [relatedBunja, this.#getBunjaInstance(relatedBunja, scopeInstanceMap)]));
59
+ const bunjaInstanceMap = new Map();
60
60
  bunjaFn.use = (dep) => {
61
61
  if (dep instanceof Bunja) return bunjaInstanceMap.get(dep).value;
62
62
  if (dep instanceof Scope) return scopeInstanceMap.get(dep).value;
63
63
  throw new Error("`bunja.use` can only be used with Bunja or Scope.");
64
64
  };
65
+ for (const relatedBunja of bunja$1.relatedBunjas) bunjaInstanceMap.set(relatedBunja, this.#getBunjaInstance(relatedBunja, scopeInstanceMap));
65
66
  const bunjaInstance = this.#getBunjaInstance(bunja$1, scopeInstanceMap);
66
67
  return {
67
68
  bunjaInstance,
package/dist/bunja.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_bunja = require('./bunja-bUA1rGXy.cjs');
2
+ const require_bunja = require('./bunja-RF4EuS_C.cjs');
3
3
 
4
4
  exports.Bunja = require_bunja.Bunja
5
5
  exports.BunjaStore = require_bunja.BunjaStore
package/dist/bunja.js CHANGED
@@ -1,3 +1,3 @@
1
- import { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createScope } from "./bunja-wcx846sL.js";
1
+ import { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createScope } from "./bunja-DGNjfjPa.js";
2
2
 
3
3
  export { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createScope };
package/dist/react.cjs CHANGED
@@ -22,11 +22,20 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  }) : target, mod));
23
23
 
24
24
  //#endregion
25
- const require_bunja = require('./bunja-bUA1rGXy.cjs');
26
- const { createContext, use, useEffect } = __toESM(require("react"));
25
+ const require_bunja = require('./bunja-RF4EuS_C.cjs');
26
+ const { createContext, createElement, use, useEffect, useState } = __toESM(require("react"));
27
27
 
28
28
  //#region react.ts
29
+ "use client";
29
30
  const BunjaStoreContext = createContext(require_bunja.createBunjaStore());
31
+ function BunjaStoreProvider({ children }) {
32
+ const [value] = useState(require_bunja.createBunjaStore);
33
+ useEffect(() => () => value.dispose(), [value]);
34
+ return createElement(BunjaStoreContext, {
35
+ value,
36
+ children
37
+ });
38
+ }
30
39
  const scopeContextMap = new Map();
31
40
  function bindScope(scope, context) {
32
41
  scopeContextMap.set(scope, context);
@@ -58,6 +67,7 @@ function inject(overrideTable) {
58
67
 
59
68
  //#endregion
60
69
  exports.BunjaStoreContext = BunjaStoreContext
70
+ exports.BunjaStoreProvider = BunjaStoreProvider
61
71
  exports.bindScope = bindScope
62
72
  exports.createScopeFromContext = createScopeFromContext
63
73
  exports.inject = inject
package/dist/react.d.cts CHANGED
@@ -1,6 +1,7 @@
1
- import { type Context } from "react";
1
+ import { type Context, type PropsWithChildren } from "react";
2
2
  import { type Bunja, type BunjaStore, type HashFn, type ReadScope, type Scope } from "./bunja.ts";
3
3
  export declare const BunjaStoreContext: Context<BunjaStore>;
4
+ export declare function BunjaStoreProvider({ children }: PropsWithChildren): React.JSX.Element;
4
5
  export declare const scopeContextMap: Map<Scope<unknown>, Context<unknown>>;
5
6
  export declare function bindScope<T>(scope: Scope<T>, context: Context<T>): void;
6
7
  export declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<T>): Scope<T>;
package/dist/react.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { type Context } from "react";
1
+ import { type Context, type PropsWithChildren } from "react";
2
2
  import { type Bunja, type BunjaStore, type HashFn, type ReadScope, type Scope } from "./bunja.ts";
3
3
  export declare const BunjaStoreContext: Context<BunjaStore>;
4
+ export declare function BunjaStoreProvider({ children }: PropsWithChildren): React.JSX.Element;
4
5
  export declare const scopeContextMap: Map<Scope<unknown>, Context<unknown>>;
5
6
  export declare function bindScope<T>(scope: Scope<T>, context: Context<T>): void;
6
7
  export declare function createScopeFromContext<T>(context: Context<T>, hash?: HashFn<T>): Scope<T>;
package/dist/react.js CHANGED
@@ -1,8 +1,17 @@
1
- import { createBunjaStore, createScope } from "./bunja-wcx846sL.js";
2
- import { createContext, use, useEffect } from "react";
1
+ import { createBunjaStore, createScope } from "./bunja-DGNjfjPa.js";
2
+ import { createContext, createElement, use, useEffect, useState } from "react";
3
3
 
4
4
  //#region react.ts
5
+ "use client";
5
6
  const BunjaStoreContext = createContext(createBunjaStore());
7
+ function BunjaStoreProvider({ children }) {
8
+ const [value] = useState(createBunjaStore);
9
+ useEffect(() => () => value.dispose(), [value]);
10
+ return createElement(BunjaStoreContext, {
11
+ value,
12
+ children
13
+ });
14
+ }
6
15
  const scopeContextMap = new Map();
7
16
  function bindScope(scope, context) {
8
17
  scopeContextMap.set(scope, context);
@@ -33,4 +42,4 @@ function inject(overrideTable) {
33
42
  }
34
43
 
35
44
  //#endregion
36
- export { BunjaStoreContext, bindScope, createScopeFromContext, inject, scopeContextMap, useBunja };
45
+ export { BunjaStoreContext, BunjaStoreProvider, bindScope, createScopeFromContext, inject, scopeContextMap, useBunja };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bunja",
3
3
  "type": "module",
4
- "version": "2.0.0-alpha.1",
4
+ "version": "2.0.0-alpha.3",
5
5
  "description": "State Lifetime Manager",
6
6
  "main": "dist/bunja.cjs",
7
7
  "module": "dist/bunja.js",
package/react.ts CHANGED
@@ -1,4 +1,14 @@
1
- import { type Context, createContext, use, useEffect } from "react";
1
+ "use client";
2
+
3
+ import {
4
+ type Context,
5
+ createContext,
6
+ createElement,
7
+ type PropsWithChildren,
8
+ use,
9
+ useEffect,
10
+ useState,
11
+ } from "react";
2
12
  import {
3
13
  type Bunja,
4
14
  type BunjaStore,
@@ -13,6 +23,14 @@ export const BunjaStoreContext: Context<BunjaStore> = createContext(
13
23
  createBunjaStore(),
14
24
  );
15
25
 
26
+ export function BunjaStoreProvider(
27
+ { children }: PropsWithChildren,
28
+ ): React.JSX.Element {
29
+ const [value] = useState(createBunjaStore);
30
+ useEffect(() => () => value.dispose(), [value]);
31
+ return createElement(BunjaStoreContext, { value, children });
32
+ }
33
+
16
34
  export const scopeContextMap: Map<Scope<unknown>, Context<unknown>> = new Map();
17
35
  export function bindScope<T>(scope: Scope<T>, context: Context<T>): void {
18
36
  scopeContextMap.set(scope as Scope<unknown>, context as Context<unknown>);