@tilia/react 1.0.0

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 ADDED
@@ -0,0 +1,92 @@
1
+ # Tilia React
2
+
3
+ Tilia is a simple state management library for "FRP" style programming with React.
4
+
5
+ This package contains the hook for React.
6
+
7
+ This package is compatible with _TypeScript_ and _ReScript_, you can find the examples for the latter at the end.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ npm install @tilia/react
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { tilia, observe, useTilia } from "@tilia/react";
19
+
20
+ // Create a tracked object or array:
21
+ const state: MyState = tilia({
22
+ flowers: "are beautiful",
23
+ clouds: { morning: "can be pink", evening: "can be orange" },
24
+ });
25
+
26
+ // Observe 'evening' and update 'morning' with a calculation.
27
+ observe(state, () => {
28
+ state.clouds.morning = state.clouds.evening + ", maybe";
29
+ });
30
+
31
+ function Clouds(props: { clouds: MyState["clouds"] }) {
32
+ // Start observing the clouds props (must be a tilia object).
33
+ const clouds = useTilia(props.clouds);
34
+ function onChange(e) {
35
+ // Write to a tilia object and trigger re-draw in
36
+ // observers.
37
+ clouds.evening = e.target.value;
38
+ }
39
+
40
+ // Will register this component as watching the "evening"
41
+ // property inside clouds.
42
+ return (
43
+ <div>
44
+ <input value={clouds.evening} onChange={onChange} />
45
+ </div>
46
+ );
47
+ }
48
+ ```
49
+
50
+ # ReScript example
51
+
52
+ ```res
53
+ open TiliaCore
54
+ open JsxEvent.Form
55
+
56
+ type clouds = {
57
+ mutable morning: string,
58
+ mutable evening: string,
59
+ }
60
+ type state = {
61
+ mutable flowers: string,
62
+ mutable clouds: clouds,
63
+ }
64
+
65
+ // Create a tracked object or array:
66
+ let tree = make({
67
+ flowers: "are beautiful",
68
+ clouds: { morning: "can be pink", evening: "can be orange" },
69
+ })
70
+
71
+ // Observe 'evening' and update 'morning' with a calculation.
72
+ Tilia.observe(state, () => {
73
+ state.clouds.morning = state.clouds.evening ++ ", maybe"
74
+ })
75
+
76
+ @react.component
77
+ let make(~clouds: clouds) {
78
+ // Start observing the clouds props (must be a tilia object).
79
+ let clouds = Tilia.use(clouds)
80
+ let onChange = (e) => {
81
+ // Write to a tilia object and trigger re-draw in
82
+ // observers.
83
+ clouds.evening = target(e)["value"]
84
+ }
85
+
86
+ // Will register this component as watching the "evening"
87
+ // property inside clouds.
88
+ <div>
89
+ <input value={clouds.evening} onChange />
90
+ </div>
91
+ }
92
+ ```
@@ -0,0 +1,2 @@
1
+ var i=Object.defineProperty;var s=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var a=Object.prototype.hasOwnProperty;var l=(e,t)=>{for(var r in t)i(e,r,{get:t[r],enumerable:!0})},m=(e,t,r,u)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of f(t))!a.call(e,n)&&n!==r&&i(e,n,{get:()=>t[n],enumerable:!(u=s(t,n))||u.enumerable});return e};var p=e=>m(i({},"__esModule",{value:!0}),e);var x={};l(x,{observe:()=>o.observe,tilia:()=>o.tilia,useTilia:()=>_});module.exports=p(x);var c=require("react"),o=require("@tilia/core");function _(e){let[,t]=(0,c.useState)(0),r=(0,o._connect)(e,()=>t(u=>u+1));return(0,c.useEffect)(function(){return(0,o._flush)(r),()=>(0,o._clear)(r)}),e}
2
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.js"],
4
+ "sourcesContent": ["import { useState, useEffect } from \"react\";\nimport { _connect, _flush, _clear, tilia, observe } from \"@tilia/core\";\n\nexport function useTilia(p) {\n const [, setCount] = useState(0);\n const o = _connect(p, () => setCount((i) => i + 1));\n useEffect(function () {\n _flush(o);\n return () => _clear(o);\n });\n return p;\n}\n\nexport { tilia, observe };\n"],
5
+ "mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,sDAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAoC,iBACpCC,EAAyD,uBAElD,SAASH,EAASI,EAAG,CAC1B,GAAM,CAAC,CAAEC,CAAQ,KAAI,YAAS,CAAC,EACzBC,KAAI,YAASF,EAAG,IAAMC,EAAUE,GAAMA,EAAI,CAAC,CAAC,EAClD,sBAAU,UAAY,CACpB,mBAAOD,CAAC,EACD,OAAM,UAAOA,CAAC,CACvB,CAAC,EACMF,CACT",
6
+ "names": ["src_exports", "__export", "useTilia", "__toCommonJS", "import_react", "import_core", "p", "setCount", "o", "i"]
7
+ }
@@ -0,0 +1,3 @@
1
+ export const tilia: <a>(tree: a) => a;
2
+ export const observe: <a>(tree: a, callback: (tree: a) => void) => void;
3
+ export const useTilia: <a>(tree: a) => a;
@@ -0,0 +1,2 @@
1
+ import{useState as n,useEffect as u}from"react";import{_connect as c,_flush as i,_clear as s,tilia as l,observe as m}from"@tilia/core";function p(t){let[,o]=n(0),e=c(t,()=>o(r=>r+1));return u(function(){return i(e),()=>s(e)}),t}export{m as observe,l as tilia,p as useTilia};
2
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.js"],
4
+ "sourcesContent": ["import { useState, useEffect } from \"react\";\nimport { _connect, _flush, _clear, tilia, observe } from \"@tilia/core\";\n\nexport function useTilia(p) {\n const [, setCount] = useState(0);\n const o = _connect(p, () => setCount((i) => i + 1));\n useEffect(function () {\n _flush(o);\n return () => _clear(o);\n });\n return p;\n}\n\nexport { tilia, observe };\n"],
5
+ "mappings": "AAAA,OAAS,YAAAA,EAAU,aAAAC,MAAiB,QACpC,OAAS,YAAAC,EAAU,UAAAC,EAAQ,UAAAC,EAAQ,SAAAC,EAAO,WAAAC,MAAe,cAElD,SAASC,EAASC,EAAG,CAC1B,GAAM,CAAC,CAAEC,CAAQ,EAAIT,EAAS,CAAC,EACzBU,EAAIR,EAASM,EAAG,IAAMC,EAAUE,GAAMA,EAAI,CAAC,CAAC,EAClD,OAAAV,EAAU,UAAY,CACpB,OAAAE,EAAOO,CAAC,EACD,IAAMN,EAAOM,CAAC,CACvB,CAAC,EACMF,CACT",
6
+ "names": ["useState", "useEffect", "_connect", "_flush", "_clear", "tilia", "observe", "useTilia", "p", "setCount", "o", "i"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@tilia/react",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.cjs.js",
5
+ "module": "./dist/index.esm.js",
6
+ "types": "./dist/index.d.ts",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/tiliajs/tilia.git"
11
+ },
12
+ "description": "🍃 React State Management with Tilia.",
13
+ "keywords": [
14
+ "rescript",
15
+ "react",
16
+ "state",
17
+ "state management",
18
+ "tilia",
19
+ "frp"
20
+ ],
21
+ "author": "Gaspard Anna Bucher <g.a.bucher@midasum.com>",
22
+ "license": "MIT",
23
+ "peerDependencies": {
24
+ "react": "*"
25
+ },
26
+ "dependencies": {
27
+ "@tilia/core": "^1.0.0"
28
+ }
29
+ }
package/rescript.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@tilia/react",
3
+ "sources": [
4
+ {
5
+ "dir": "src",
6
+ "public": [
7
+ "Tilia"
8
+ ],
9
+ "subdirs": true
10
+ },
11
+ {
12
+ "dir": "test",
13
+ "type": "dev",
14
+ }
15
+ ],
16
+ "package-specs": {
17
+ "module": "esmodule",
18
+ "in-source": true
19
+ },
20
+ "suffix": ".mjs",
21
+ "bs-dependencies": [
22
+ "@tilia/core"
23
+ ],
24
+ "bs-dev-dependencies": [
25
+ "@rescript/core",
26
+ "@rescript/react",
27
+ "@dzakh/rescript-ava"
28
+ ],
29
+ "bsc-flags": [],
30
+ "jsx": {
31
+ "version": 4
32
+ }
33
+ }
package/src/Tilia.res ADDED
@@ -0,0 +1,15 @@
1
+ @module("react") external useState: int => (int, (int => int) => unit) = "useState"
2
+ @module("react") external useEffect: (unit => option<unit => unit>) => unit = "useEffect"
3
+ open TiliaCore
4
+
5
+ let make = make
6
+ let observe = observe
7
+ let use = p => {
8
+ let (_, setCount) = useState(0)
9
+ let o = _connect(p, () => setCount(i => i + 1))
10
+ useEffect(() => {
11
+ _flush(o)
12
+ Some(() => _clear(o))
13
+ })
14
+ p
15
+ }
package/src/Tilia.resi ADDED
@@ -0,0 +1,3 @@
1
+ let make: 'a => 'a
2
+ let observe: ('a, 'a => unit) => unit
3
+ let use: 'a => 'a