react-rock 0.1.0 → 3.0.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.
package/README.md CHANGED
@@ -1,160 +1,94 @@
1
- # TSDX React User Guide
2
-
3
- Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.
4
-
5
- > This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
6
-
7
- > If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
8
-
9
- ## Commands
10
-
11
- TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`.
12
-
13
- The recommended workflow is to run TSDX in one terminal:
14
-
15
- ```bash
16
- npm start # or yarn start
17
- ```
18
-
19
- This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.
20
-
21
- Then run the example inside another:
22
-
23
- ```bash
24
- cd example
25
- npm i # or yarn to install dependencies
26
- npm start # or yarn start
27
- ```
28
-
29
- The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**, we use [Parcel's aliasing](https://parceljs.org/module_resolution.html#aliases).
30
-
31
- To do a one-off build, use `npm run build` or `yarn build`.
32
-
33
- To run tests, use `npm test` or `yarn test`.
34
-
35
- ## Configuration
36
-
37
- Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
38
-
39
- ### Jest
40
-
41
- Jest tests are set up to run with `npm test` or `yarn test`.
42
-
43
- ### Bundle analysis
44
-
45
- Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`.
46
-
47
- #### Setup Files
48
-
49
- This is the folder structure we set up for you:
50
-
51
- ```txt
52
- /example
53
- index.html
54
- index.tsx # test your component here in a demo app
55
- package.json
56
- tsconfig.json
57
- /src
58
- index.tsx # EDIT THIS
59
- /test
60
- blah.test.tsx # EDIT THIS
61
- .gitignore
62
- package.json
63
- README.md # EDIT THIS
64
- tsconfig.json
65
- ```
66
-
67
- #### React Testing Library
68
-
69
- We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
70
-
71
- ### Rollup
72
-
73
- TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
74
-
75
- ### TypeScript
76
-
77
- `tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
78
-
79
- ## Continuous Integration
80
-
81
- ### GitHub Actions
82
-
83
- Two actions are added by default:
84
-
85
- - `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
86
- - `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)
87
-
88
- ## Optimizations
89
-
90
- Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:
91
-
92
- ```js
93
- // ./types/index.d.ts
94
- declare var __DEV__: boolean;
95
-
96
- // inside your code...
97
- if (__DEV__) {
98
- console.log('foo');
99
- }
100
- ```
101
-
102
- You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
103
-
104
- ## Module Formats
105
-
106
- CJS, ESModules, and UMD module formats are supported.
107
-
108
- The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
109
-
110
- ## Deploying the Example Playground
111
-
112
- The Playground is just a simple [Parcel](https://parceljs.org) app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for **manually** deploying with the Netlify CLI (`npm i -g netlify-cli`):
113
-
114
- ```bash
115
- cd example # if not already in the example folder
116
- npm run build # builds to dist
117
- netlify deploy # deploy the dist folder
118
- ```
119
-
120
- Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:
121
-
122
- ```bash
123
- netlify init
124
- # build command: yarn build && cd example && yarn && yarn build
125
- # directory to deploy: example/dist
126
- # pick yes for netlify.toml
127
- ```
128
-
129
- ## Named Exports
130
-
131
- Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
132
-
133
- ## Including Styles
134
-
135
- There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
136
-
137
- For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
138
-
139
- ## Publishing to NPM
140
-
141
- We recommend using [np](https://github.com/sindresorhus/np).
142
-
143
- ## Usage with Lerna
144
-
145
- When creating a new package with TSDX within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_.
146
-
147
- The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.
148
-
149
- Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.
150
-
151
- ```diff
152
- "alias": {
153
- - "react": "../node_modules/react",
154
- - "react-dom": "../node_modules/react-dom"
155
- + "react": "../../../node_modules/react",
156
- + "react-dom": "../../../node_modules/react-dom"
157
- },
158
- ```
159
-
160
- An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/palmerhq/tsdx/issues/64)
1
+ ### React Rock
2
+ `react-rock` is a very powerfull react state management system library. you can use it with your small and big application.
3
+
4
+
5
+ ### How to use
6
+ `UserHandler.js`
7
+ ```js
8
+ import {createState} from 'react-rock'
9
+
10
+ const User = createState()
11
+
12
+ export default User
13
+ ```
14
+
15
+ ```jsx
16
+ import User from 'UserHandler'
17
+
18
+ const App = () => {
19
+ const items = User.getAll()
20
+
21
+ return(
22
+ <div>
23
+ <ul>
24
+ {
25
+ items.map(item => ...)
26
+ }
27
+ </ul>
28
+ <button onClick={() => {
29
+ User.insert({
30
+ name: "",
31
+ email: ""
32
+ })
33
+ }}>Add Item</button>
34
+ </div>
35
+ )
36
+ }
37
+
38
+ ```
39
+
40
+ ## How to use in class component
41
+ ```js
42
+ import {StateComponent} from 'react-rock'
43
+ import User from 'UserHandler'
44
+
45
+ class View extends StateComponent{
46
+
47
+ render(){
48
+ const items = User.getAll()
49
+
50
+ return (
51
+ <div>
52
+ <ul>
53
+ {
54
+ items.map(item => ...)
55
+ }
56
+ </ul>
57
+ <button onClick={() => {
58
+ User.insert({
59
+ name: "",
60
+ email: ""
61
+ })
62
+ }}>Add Item</button>
63
+ </div>
64
+ )
65
+ }
66
+ }
67
+
68
+ ```
69
+
70
+ ## Methods
71
+
72
+ ```js
73
+ import User from 'UserHandler'
74
+ const users = User.find({name: "nax"})
75
+
76
+ const users = User.find({
77
+ name: {
78
+ contain: "abc",
79
+ startWith: "a",
80
+ endWith: "@",
81
+ }
82
+ })
83
+
84
+ const users = User.find({
85
+ age: {
86
+ lt: 20,
87
+ gt: 10,
88
+
89
+ lte: 20,
90
+ gte: 10
91
+ }
92
+ })
93
+
94
+ ```
@@ -0,0 +1,22 @@
1
+ export type QueryValueType = {
2
+ contain?: string | number;
3
+ startWith?: string | number;
4
+ endWith?: string | number;
5
+ gt?: number;
6
+ lt?: number;
7
+ gte?: number;
8
+ lte?: number;
9
+ };
10
+ export type QueryType<Row = {}> = {
11
+ [key in keyof Row]?: string | number | null | undefined | QueryValueType;
12
+ };
13
+ export type FinderArgsType<Row> = {
14
+ getRow?: (row: Row, index: number) => Row | void;
15
+ skip?: number;
16
+ take?: number;
17
+ };
18
+ declare const Finder: <Row extends object>(rows: Row[], query: null | QueryType<Row>, args?: FinderArgsType<Row>) => {
19
+ rows: Row[];
20
+ indexes: number[];
21
+ };
22
+ export default Finder;
package/dist/Finder.js ADDED
@@ -0,0 +1,113 @@
1
+ const isOb = (ob) => typeof ob === "object" && !Array.isArray(ob) && ob !== null;
2
+ const isInQuery = (rowVlaue, queryObject) => {
3
+ let match = true;
4
+ for (let queryKey in queryObject) {
5
+ let queryVal = queryObject[queryKey];
6
+ switch (queryKey) {
7
+ case "contain":
8
+ if (!(typeof rowVlaue === 'string' && rowVlaue.search(queryVal) !== -1)) {
9
+ match = false;
10
+ break;
11
+ }
12
+ break;
13
+ case "startWith":
14
+ if (!(typeof rowVlaue === 'string' && rowVlaue.startsWith(queryVal))) {
15
+ match = false;
16
+ break;
17
+ }
18
+ break;
19
+ case "endWith":
20
+ if (!(typeof rowVlaue === 'string' && rowVlaue.endsWith(queryVal))) {
21
+ match = false;
22
+ break;
23
+ }
24
+ break;
25
+ case "lt":
26
+ if (!(typeof rowVlaue === 'number' && rowVlaue < queryVal)) {
27
+ match = false;
28
+ break;
29
+ }
30
+ break;
31
+ case "gt":
32
+ if (!(typeof rowVlaue === 'number' && rowVlaue > queryVal)) {
33
+ match = false;
34
+ break;
35
+ }
36
+ break;
37
+ case "lte":
38
+ if (!(typeof rowVlaue === 'number' && rowVlaue <= queryVal)) {
39
+ match = false;
40
+ break;
41
+ }
42
+ break;
43
+ case "gte":
44
+ if (!(typeof rowVlaue === 'number' && rowVlaue >= queryVal)) {
45
+ match = false;
46
+ break;
47
+ }
48
+ break;
49
+ }
50
+ }
51
+ return match;
52
+ };
53
+ const Finder = (rows, query, args) => {
54
+ let result = [];
55
+ let indexes = [];
56
+ for (let i = 0; i < rows.length; i++) {
57
+ let row = rows[i];
58
+ let found = false;
59
+ if (isOb(query)) {
60
+ for (let rowKey in query) {
61
+ let queryVal = query[rowKey];
62
+ if (!(rowKey in row))
63
+ break;
64
+ if (isOb(queryVal)) {
65
+ if (isInQuery(row[rowKey], queryVal)) {
66
+ found = true;
67
+ }
68
+ else {
69
+ found = false;
70
+ break;
71
+ }
72
+ }
73
+ else if (row[rowKey] === queryVal) {
74
+ found = true;
75
+ }
76
+ else {
77
+ found = false;
78
+ break;
79
+ }
80
+ }
81
+ }
82
+ else {
83
+ found = true;
84
+ }
85
+ if (found) {
86
+ if (args?.getRow) {
87
+ let r = args.getRow({ ...row }, i);
88
+ if (r) {
89
+ row = r;
90
+ }
91
+ else {
92
+ continue;
93
+ }
94
+ }
95
+ result.push({ ...row, _index: i });
96
+ indexes.push(i);
97
+ }
98
+ }
99
+ const take = args?.take || result.length;
100
+ const skip = args?.skip || 0;
101
+ if (args?.take && args?.skip) {
102
+ result = result.splice(skip, take);
103
+ }
104
+ else if (!args?.skip && args?.take) {
105
+ result.splice(args.take);
106
+ }
107
+ else if (!args?.take && args?.skip) {
108
+ result = result.splice(args.skip);
109
+ }
110
+ return { rows: result, indexes };
111
+ };
112
+ export default Finder;
113
+ //# sourceMappingURL=Finder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Finder.js","sourceRoot":"","sources":["../src/Finder.ts"],"names":[],"mappings":"AAsBA,MAAM,IAAI,GAAG,CAAC,EAAO,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,IAAI,CAAA;AAErF,MAAM,SAAS,GAAG,CAAM,QAAa,EAAE,WAA2B,EAAE,EAAE;IAClE,IAAI,KAAK,GAAG,IAAI,CAAA;IAChB,KAAK,IAAI,QAAQ,IAAI,WAAW,EAAE,CAAC;QAC/B,IAAI,QAAQ,GAAI,WAAmB,CAAC,QAAQ,CAAC,CAAA;QAE7C,QAAQ,QAAQ,EAAE,CAAC;YACf,KAAK,SAAS;gBACV,IAAI,CAAC,CAAC,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;oBACtE,KAAK,GAAG,KAAK,CAAC;oBACd,MAAM;gBACV,CAAC;gBACD,MAAM;YACV,KAAK,WAAW;gBACZ,IAAI,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AC;oBACJ,SAAS;gBACb,CAAC;YACL,CAAC;YACD,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAA;YAClC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACnB,CAAC;IACL,CAAC;IAED,MAAM,IAAI,GAAG,IAAI,EAAE,IAAI,IAAI,MAAM,CAAC,MAAM,CAAA;IACxC,MAAM,IAAI,GAAG,IAAI,EAAE,IAAI,IAAI,CAAC,CAAA;IAE5B,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,IAAI,EAAE,CAAC;QAC3B,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IACtC,CAAC;SAAM,IAAI,CAAC,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,IAAI,EAAE,CAAC;QACnC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC5B,CAAC;SAAM,IAAI,CAAC,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,IAAI,EAAE,CAAC;QACnC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACrC,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAA;AACpC,CAAC,CAAA;AAED,eAAe,MAAM,CAAA"}
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ export declare class StateComponent<P = {}, S = {}, SS = any> extends React.Component<P, S, SS> {
3
+ constructor(props: P);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use client";
2
+ import React from 'react';
3
+ export class StateComponent extends React.Component {
4
+ constructor(props) {
5
+ super(props);
6
+ const R = this.render.bind(this);
7
+ this.render = () => React.createElement(React.Fragment, null,
8
+ React.createElement(R, null));
9
+ }
10
+ }
11
+ //# sourceMappingURL=StateComponent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StateComponent.js","sourceRoot":"","sources":["../src/StateComponent.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,MAAM,OAAO,cAAyC,SAAQ,KAAK,CAAC,SAAmB;IACnF,YAAY,KAAQ;QAChB,KAAK,CAAC,KAAK,CAAC,CAAA;QACZ,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAQ,CAAA;QACvC,IAAI,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;YAAE,oBAAC,CAAC,OAAG,CAAG,CAAA;IAClC,CAAC;CACJ"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- import * as React from 'react';
2
- export declare const Thing: () => React.JSX.Element;
1
+ import { IStateHandler } from './types';
2
+ export * from './types';
3
+ export * from './StateComponent';
4
+ export declare const noDispatch: (cb: Function) => void;
5
+ export declare const createState: <Row extends object, MetaProps extends object = {}>() => IStateHandler<Row, MetaProps>;
package/dist/index.js CHANGED
@@ -1,8 +1,148 @@
1
-
2
- 'use strict'
3
-
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./react-rock.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./react-rock.cjs.development.js')
8
- }
1
+ import { useEffect, useId, useMemo, useState } from 'react';
2
+ import Finder from './Finder';
3
+ export * from './types';
4
+ export * from './StateComponent';
5
+ const _row = (row) => {
6
+ return {
7
+ ...row,
8
+ _id: row._id || _uid(),
9
+ _observe: row._observe || _random()
10
+ };
11
+ };
12
+ const _random = () => Date.now() + Math.floor(1000 + Math.random() * 9000);
13
+ const _uid = () => _random().toString(32).replace("-", "").substring(0, 15);
14
+ let activeDispatch = true;
15
+ export const noDispatch = (cb) => {
16
+ activeDispatch = false;
17
+ cb();
18
+ activeDispatch = true;
19
+ };
20
+ export const createState = () => {
21
+ let DATA = [];
22
+ let META = new Map();
23
+ const STATE_INFO = {
24
+ dispatches: new Map(),
25
+ observe: 0,
26
+ meta_observe: 0
27
+ };
28
+ const _dispatch = (type) => {
29
+ STATE_INFO[type == "meta" ? "meta_observe" : "observe"] = Math.random();
30
+ activeDispatch && STATE_INFO.dispatches.forEach(d => d.type === type && d.cb());
31
+ };
32
+ const useHook = (type) => {
33
+ const id = useId();
34
+ const [, dispatch] = useState(0);
35
+ useEffect(() => {
36
+ STATE_INFO.dispatches.set(id, { type, cb: () => dispatch(Math.random()) });
37
+ return () => {
38
+ STATE_INFO.dispatches.delete(id);
39
+ };
40
+ }, []);
41
+ };
42
+ class StateHandler {
43
+ static create(row) {
44
+ const r = _row(row);
45
+ DATA.push(r);
46
+ _dispatch("state");
47
+ return r;
48
+ }
49
+ static createMany(rows) {
50
+ for (let row of rows) {
51
+ const r = _row(row);
52
+ DATA.push(r);
53
+ }
54
+ _dispatch("state");
55
+ }
56
+ static update(row, where, args) {
57
+ Finder(DATA, where, {
58
+ ...args,
59
+ getRow: (r, index) => {
60
+ args?.getRow && args.getRow(r, index);
61
+ DATA[index] = _row({ ...r, ...row });
62
+ }
63
+ });
64
+ _dispatch("state");
65
+ }
66
+ static updateAll(row) {
67
+ for (let i = 0; i < DATA.length; i++) {
68
+ DATA[i] = _row({ ...DATA[i], ...row });
69
+ }
70
+ _dispatch("state");
71
+ }
72
+ static delete(where, args) {
73
+ const found = Finder(DATA, where, args);
74
+ for (let index of found.indexes) {
75
+ DATA.splice(index, 1);
76
+ }
77
+ _dispatch("state");
78
+ }
79
+ static clearState() {
80
+ DATA = [];
81
+ _dispatch("state");
82
+ }
83
+ static getAll(args) {
84
+ try {
85
+ useHook("state");
86
+ return useMemo(() => Finder(DATA, null, args).rows, [STATE_INFO.observe]);
87
+ }
88
+ catch (error) {
89
+ return Finder(DATA, null, args);
90
+ }
91
+ }
92
+ static find(where, args) {
93
+ try {
94
+ useHook("state");
95
+ return useMemo(() => Finder(DATA, where, args).rows, [STATE_INFO.observe]);
96
+ }
97
+ catch (error) {
98
+ return Finder(DATA, where, args).rows;
99
+ }
100
+ }
101
+ static findFirst(where) {
102
+ return StateHandler.find(where)[0];
103
+ }
104
+ static findById(_id) {
105
+ return StateHandler.findFirst({ _id });
106
+ }
107
+ static move(oldIdx, newIdx) {
108
+ const row = DATA[oldIdx];
109
+ if (row) {
110
+ DATA.splice(oldIdx, 1);
111
+ DATA.splice(newIdx, 0, _row(row));
112
+ _dispatch("state");
113
+ }
114
+ }
115
+ static setMeta(key, value) {
116
+ META.set(key, value);
117
+ _dispatch("meta");
118
+ }
119
+ static getMeta(key, def) {
120
+ try {
121
+ useHook("meta");
122
+ return META.get(key) || def;
123
+ }
124
+ catch (error) {
125
+ return META.get(key) || def;
126
+ }
127
+ }
128
+ static getAllMeta() {
129
+ try {
130
+ useHook("meta");
131
+ return useMemo(() => Object.fromEntries(META), [STATE_INFO.meta_observe]);
132
+ }
133
+ catch (error) {
134
+ return Object.fromEntries(META);
135
+ }
136
+ }
137
+ static deleteMeta(key) {
138
+ META.delete(key);
139
+ _dispatch("meta");
140
+ }
141
+ static clearMeta() {
142
+ META.clear();
143
+ _dispatch("meta");
144
+ }
145
+ }
146
+ return StateHandler;
147
+ };
148
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAE3D,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,cAAc,SAAS,CAAA;AACvB,cAAc,kBAAkB,CAAA;AAEhC,MAAM,IAAI,GAAG,CAAK,GAAwB,EAAc,EAAE;IACtD,OAAO;QACH,GAAG,GAAG;QACN,GAAG,EAAE,GAAG,CAAC,GAAG,IAAI,IAAI,EAAE;QACtB,QAAQ,EAAE,GAAG,CAAC,QAAQ,IAAI,OAAO,EAAE;KAC/B,CAAA;AACZ,CAAC,CAAA;AAED,MAAM,OAAO,GA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C,GAAG,CAAC,IAAI,GAAG,CAAA;YAC/B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,GAAG,CAAA;YAC/B,CAAC;QACL,CAAC;QAED,MAAM,CAAC,UAAU;YACb,IAAI,CAAC;gBACD,OAAO,CAAC,MAAM,CAAC,CAAA;gBACf,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAc,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,CAAA;YAC1F,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAc,CAAA;YAChD,CAAC;QACL,CAAC;QAED,MAAM,CAAC,UAAU,CAA4B,GAAM;YAC/C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YAChB,SAAS,CAAC,MAAM,CAAC,CAAA;QACrB,CAAC;QAED,MAAM,CAAC,SAAS;YACZ,IAAI,CAAC,KAAK,EAAE,CAAA;YACZ,SAAS,CAAC,MAAM,CAAC,CAAA;QACrB,CAAC;KACJ;IAED,OAAO,YAA6C,CAAA;AACxD,CAAC,CAAA"}
@@ -0,0 +1,28 @@
1
+ import { FinderArgsType, QueryType } from "./Finder";
2
+ export type StateDataType = "state" | "meta";
3
+ export type RowPredefinedFields = {
4
+ _id: string;
5
+ _index: number;
6
+ _observe: number;
7
+ };
8
+ export type RowType<Row> = Row & RowPredefinedFields;
9
+ export type WhereType<Row> = QueryType<RowType<Row>>;
10
+ export type ArgsType<Row> = FinderArgsType<RowType<Row>>;
11
+ export interface IStateHandler<Row, MetaProps> {
12
+ create(row: Row): RowType<Row>;
13
+ createMany(rows: Row[]): void;
14
+ update(row: Partial<Row>, where: WhereType<Row>, args?: ArgsType<Row>): void;
15
+ updateAll(row: Partial<Row>): void;
16
+ delete(where: WhereType<Row>, args?: ArgsType<Row>): void;
17
+ clearState(): void;
18
+ getAll(args?: ArgsType<Row>): RowType<Row>[];
19
+ find(where: WhereType<Row>, args?: ArgsType<Row>): RowType<Row>[];
20
+ findFirst(where: WhereType<Row>): RowType<Row>;
21
+ findById(_id: string): RowType<Row>;
22
+ move(oldIdx: number, newIdx: number): void;
23
+ setMeta<T extends keyof MetaProps>(key: T, value: MetaProps[T]): void;
24
+ getMeta<T extends keyof MetaProps>(key: T, def?: any): MetaProps[T];
25
+ getAllMeta(): MetaProps;
26
+ deleteMeta<T extends keyof MetaProps>(key: T): void;
27
+ clearMeta(): void;
28
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,61 +1,38 @@
1
- {
2
- "version": "0.1.0",
3
- "license": "MIT",
4
- "main": "dist/index.js",
5
- "typings": "dist/index.d.ts",
6
- "files": [
7
- "dist",
8
- "src"
9
- ],
10
- "engines": {
11
- "node": ">=10"
12
- },
13
- "scripts": {
14
- "start": "tsdx watch",
15
- "build": "tsdx build",
16
- "test": "tsdx test --passWithNoTests",
17
- "lint": "tsdx lint",
18
- "prepare": "tsdx build",
19
- "size": "size-limit",
20
- "analyze": "size-limit --why"
21
- },
22
- "peerDependencies": {
23
- "react": ">=16"
24
- },
25
- "husky": {
26
- "hooks": {
27
- "pre-commit": "tsdx lint"
28
- }
29
- },
30
- "prettier": {
31
- "printWidth": 80,
32
- "semi": true,
33
- "singleQuote": true,
34
- "trailingComma": "es5"
35
- },
36
- "name": "react-rock",
37
- "author": "Naxrul Ahmed",
38
- "module": "dist/react-rock.esm.js",
39
- "size-limit": [
40
- {
41
- "path": "dist/react-rock.cjs.production.min.js",
42
- "limit": "10 KB"
43
- },
44
- {
45
- "path": "dist/react-rock.esm.js",
46
- "limit": "10 KB"
47
- }
48
- ],
49
- "devDependencies": {
50
- "@size-limit/preset-small-lib": "^11.0.2",
51
- "@types/react": "^18.2.55",
52
- "@types/react-dom": "^18.2.18",
53
- "husky": "^9.0.10",
54
- "react": "^18.2.0",
55
- "react-dom": "^18.2.0",
56
- "size-limit": "^11.0.2",
57
- "tsdx": "^0.14.1",
58
- "tslib": "^2.6.2",
59
- "typescript": "^3.9.10"
60
- }
61
- }
1
+ {
2
+ "version": "3.0.2",
3
+ "name": "react-rock",
4
+ "author": "Naxrul Ahmed",
5
+ "module": "dist/index.js",
6
+ "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "typings": "dist/index.d.ts",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "engines": {
13
+ "node": ">=10"
14
+ },
15
+ "scripts": {
16
+ "start": "parcel --no-cache index.html",
17
+ "build": "npx rimraf ./dist && npx tsc",
18
+ "prepare": "npm run build"
19
+ },
20
+ "peerDependencies": {
21
+ "react": ">=16"
22
+ },
23
+ "prettier": {
24
+ "printWidth": 80,
25
+ "semi": true,
26
+ "singleQuote": true,
27
+ "trailingComma": "es5"
28
+ },
29
+ "devDependencies": {
30
+ "@size-limit/preset-small-lib": "^11.1.4",
31
+ "@types/react": "^18.3.3",
32
+ "@types/react-dom": "^18.3.0",
33
+ "parcel": "^2.12.0",
34
+ "react": "^18.3.1",
35
+ "react-dom": "^18.3.1",
36
+ "typescript": "^5.5.3"
37
+ }
38
+ }
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
-
7
- // Delete me
8
- var Thing = function Thing() {
9
- return React.createElement("div", null, "the snozzberries taste like snozzberries");
10
- };
11
-
12
- exports.Thing = Thing;
13
- //# sourceMappingURL=react-rock.cjs.development.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-rock.cjs.development.js","sources":["../src/index.tsx"],"sourcesContent":["import * as React from 'react';\n\n// Delete me\nexport const Thing = () => {\n return <div>the snozzberries taste like snozzberries</div>;\n};\n"],"names":["Thing","React"],"mappings":";;;;;;AAEA;IACaA,KAAK,GAAG,SAARA,KAAKA;EAChB,OAAOC,4EAAmD;AAC5D;;;;"}
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");exports.Thing=function(){return e.createElement("div",null,"the snozzberries taste like snozzberries")};
2
- //# sourceMappingURL=react-rock.cjs.production.min.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-rock.cjs.production.min.js","sources":["../src/index.tsx"],"sourcesContent":["import * as React from 'react';\n\n// Delete me\nexport const Thing = () => {\n return <div>the snozzberries taste like snozzberries</div>;\n};\n"],"names":["React"],"mappings":"yGAGqB,WACnB,OAAOA"}
@@ -1,9 +0,0 @@
1
- import { createElement } from 'react';
2
-
3
- // Delete me
4
- var Thing = function Thing() {
5
- return createElement("div", null, "the snozzberries taste like snozzberries");
6
- };
7
-
8
- export { Thing };
9
- //# sourceMappingURL=react-rock.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-rock.esm.js","sources":["../src/index.tsx"],"sourcesContent":["import * as React from 'react';\n\n// Delete me\nexport const Thing = () => {\n return <div>the snozzberries taste like snozzberries</div>;\n};\n"],"names":["Thing","React"],"mappings":";;AAEA;IACaA,KAAK,GAAG,SAARA,KAAKA;EAChB,OAAOC,sEAAmD;AAC5D;;;;"}
package/src/index.tsx DELETED
@@ -1,6 +0,0 @@
1
- import * as React from 'react';
2
-
3
- // Delete me
4
- export const Thing = () => {
5
- return <div>the snozzberries taste like snozzberries</div>;
6
- };