bunja 2.0.0-alpha.1 → 2.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,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.2",
4
4
  "license": "Zlib",
5
5
  "exports": {
6
6
  ".": "./bunja.ts",
package/dist/react.cjs CHANGED
@@ -23,10 +23,18 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
 
24
24
  //#endregion
25
25
  const require_bunja = require('./bunja-bUA1rGXy.cjs');
26
- const { createContext, use, useEffect } = __toESM(require("react"));
26
+ const { createContext, createElement, use, useEffect, useState } = __toESM(require("react"));
27
27
 
28
28
  //#region react.ts
29
29
  const BunjaStoreContext = createContext(require_bunja.createBunjaStore());
30
+ function BunjaStoreProvider({ children }) {
31
+ const [value] = useState(require_bunja.createBunjaStore);
32
+ useEffect(() => () => value.dispose(), [value]);
33
+ return createElement(BunjaStoreContext, {
34
+ value,
35
+ children
36
+ });
37
+ }
30
38
  const scopeContextMap = new Map();
31
39
  function bindScope(scope, context) {
32
40
  scopeContextMap.set(scope, context);
@@ -58,6 +66,7 @@ function inject(overrideTable) {
58
66
 
59
67
  //#endregion
60
68
  exports.BunjaStoreContext = BunjaStoreContext
69
+ exports.BunjaStoreProvider = BunjaStoreProvider
61
70
  exports.bindScope = bindScope
62
71
  exports.createScopeFromContext = createScopeFromContext
63
72
  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,16 @@
1
1
  import { createBunjaStore, createScope } from "./bunja-wcx846sL.js";
2
- import { createContext, use, useEffect } from "react";
2
+ import { createContext, createElement, use, useEffect, useState } from "react";
3
3
 
4
4
  //#region react.ts
5
5
  const BunjaStoreContext = createContext(createBunjaStore());
6
+ function BunjaStoreProvider({ children }) {
7
+ const [value] = useState(createBunjaStore);
8
+ useEffect(() => () => value.dispose(), [value]);
9
+ return createElement(BunjaStoreContext, {
10
+ value,
11
+ children
12
+ });
13
+ }
6
14
  const scopeContextMap = new Map();
7
15
  function bindScope(scope, context) {
8
16
  scopeContextMap.set(scope, context);
@@ -33,4 +41,4 @@ function inject(overrideTable) {
33
41
  }
34
42
 
35
43
  //#endregion
36
- export { BunjaStoreContext, bindScope, createScopeFromContext, inject, scopeContextMap, useBunja };
44
+ 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.2",
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,12 @@
1
- import { type Context, createContext, use, useEffect } from "react";
1
+ import {
2
+ type Context,
3
+ createContext,
4
+ createElement,
5
+ type PropsWithChildren,
6
+ use,
7
+ useEffect,
8
+ useState,
9
+ } from "react";
2
10
  import {
3
11
  type Bunja,
4
12
  type BunjaStore,
@@ -13,6 +21,14 @@ export const BunjaStoreContext: Context<BunjaStore> = createContext(
13
21
  createBunjaStore(),
14
22
  );
15
23
 
24
+ export function BunjaStoreProvider(
25
+ { children }: PropsWithChildren,
26
+ ): React.JSX.Element {
27
+ const [value] = useState(createBunjaStore);
28
+ useEffect(() => () => value.dispose(), [value]);
29
+ return createElement(BunjaStoreContext, { value, children });
30
+ }
31
+
16
32
  export const scopeContextMap: Map<Scope<unknown>, Context<unknown>> = new Map();
17
33
  export function bindScope<T>(scope: Scope<T>, context: Context<T>): void {
18
34
  scopeContextMap.set(scope as Scope<unknown>, context as Context<unknown>);