-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wire decorator for wire storepart to store
- Loading branch information
1 parent
8ec30a5
commit b69bd29
Showing
32 changed files
with
736 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"name": "@react-store/core", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"main": "dist/index.js", | ||
"repository": "https://github.com/amirqasemi74/react-store.git", | ||
"repository": "https://github.com/sahabpardaz/react-store.git", | ||
"author": "Amir Hossein Qasemi Moqaddam <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
|
@@ -32,22 +32,22 @@ | |
"@types/react-dom": "^17.0.11", | ||
"@types/styled-components": "^5.1.19", | ||
"@types/testing-library__jest-dom": "^5.14.2", | ||
"@typescript-eslint/eslint-plugin": "^5.8.1", | ||
"@typescript-eslint/parser": "^5.8.1", | ||
"@typescript-eslint/eslint-plugin": "^5.9.0", | ||
"@typescript-eslint/parser": "^5.9.0", | ||
"@zerollup/ts-transform-paths": "^1.7.18", | ||
"babel-jest": "^27.4.5", | ||
"babel-jest": "^27.4.6", | ||
"babel-plugin-transform-typescript-metadata": "^0.3.2", | ||
"eslint": "^8.5.0", | ||
"eslint": "^8.6.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"html-webpack-plugin": "^5.5.0", | ||
"husky": "^7.0.4", | ||
"jest": "^27.4.5", | ||
"jest": "^27.4.7", | ||
"lint-staged": ">=12", | ||
"prettier": "^2.5.1", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"react-refresh-typescript": "^2.0.3", | ||
"rollup": "^2.62.0", | ||
"rollup": "^2.63.0", | ||
"styled-components": "^5.3.3", | ||
"ts-loader": "^9.2.6", | ||
"ttypescript": "^1.5.13", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
import { Effect, StorePart } from "@react-store/core"; | ||
import ThemeStore from "sampleApp/theme.store"; | ||
import { UserService } from "sampleApp/toDos/services/userService"; | ||
|
||
@StorePart() | ||
export class FormValidator { | ||
hasAnyError = false; | ||
|
||
constructor(private form: any) {} | ||
form?: any; | ||
|
||
@Effect<any>("form", true) | ||
constructor(protected theme: ThemeStore, protected userService: UserService) {} | ||
|
||
@Effect("form", true) | ||
validate() { | ||
new Promise((res) => res(1)).then(() => { | ||
this.hasAnyError = !this.form.value; | ||
this.hasAnyError = !this.form?.value; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
import ThemeStore from "../theme.store"; | ||
import { Effect, Store } from "@react-store/core"; | ||
import { AutoWire, Effect, Observable, Store, Wire } from "@react-store/core"; | ||
import { FormValidator } from "sampleApp/libs/formValidator"; | ||
import { ToDoService } from "sampleApp/toDos/services/todos.service"; | ||
|
||
@Store() | ||
export class BaseStore { | ||
todo = new ToDo(); | ||
|
||
@AutoWire() | ||
readonly validator: FormValidator; | ||
|
||
constructor(public theme: ThemeStore, public todoService: ToDoService) {} | ||
|
||
@Effect<BaseStore>([]) | ||
@Effect([]) | ||
onMount() { | ||
this.validator.form = this.todo; | ||
console.log("mounted"); | ||
} | ||
} | ||
|
||
@Observable() | ||
class ToDo { | ||
value = ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Wire } from ".."; | ||
|
||
export function AutoWire(): PropertyDecorator { | ||
return function (target, propertyKey) { | ||
const type = Reflect.getOwnMetadata("design:type", target, propertyKey); | ||
if (!type) { | ||
throw new Error( | ||
`AutoWire for ${ | ||
target.constructor.name | ||
}.${propertyKey.toString()} can't detect type. use \`@Wire(...) instead.\'` | ||
); | ||
} | ||
Wire(type)(target, propertyKey); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ClassType } from "src/types"; | ||
|
||
export function Wire(type: Function): PropertyDecorator { | ||
return function (target, propertyKey) { | ||
WireMetadataUtils.set(target.constructor, propertyKey, type); | ||
}; | ||
} | ||
|
||
export class WireMetadataUtils { | ||
private static readonly KEY = Symbol(); | ||
|
||
static set(target: Function, propertyKey: PropertyKey, type: Function) { | ||
const wires = this.getAllOwn(target); | ||
wires.push({ propertyKey, type }); | ||
Reflect.defineMetadata(this.KEY, wires, target); | ||
} | ||
|
||
static getAllOwn(target: Function): WireDetail[] { | ||
return Reflect.getOwnMetadata(this.KEY, target) || []; | ||
} | ||
|
||
static getAll(target: Function): WireDetail[] { | ||
let wires = this.getAllOwn(target); | ||
const parentClass = Reflect.getPrototypeOf(target) as ClassType; | ||
if (parentClass) { | ||
wires = wires.concat(this.getAll(parentClass)); | ||
} | ||
return wires; | ||
} | ||
|
||
static is(target: Function, propertyKey: PropertyKey) { | ||
const wires = this.getAll(target); | ||
return wires.some(({ propertyKey: _p }) => _p === propertyKey); | ||
} | ||
} | ||
|
||
interface WireDetail { | ||
type: Function; | ||
propertyKey: PropertyKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
export { getFromContainer } from "./container/container"; | ||
export { Injectable, Scope } from "./container/decorators/Injectable"; | ||
export { Injector } from "./container/Injector"; | ||
export { useStore } from "./react/hooks/useStore"; | ||
export { Store } from "./decorators/store"; | ||
export { connect } from "./react/store/connect"; | ||
export { StoreProvider } from "./react/store/StoreProvider"; | ||
export { Props } from "./decorators/props"; | ||
export { UseContext } from "./decorators/useContext"; | ||
export { connectStore } from "./react/store/connectStore"; | ||
export { StoreProvider } from "./react/store/StoreProvider"; | ||
export { Effect } from "./decorators/effect"; | ||
export { Store } from "./decorators/store"; | ||
export { Inject } from "./container/decorators/inject"; | ||
export { StorePart } from "./decorators/storePart"; | ||
export { Injector } from "./container/Injector"; | ||
export { Action } from "./decorators/action"; | ||
export { Observable } from "./decorators/observable"; | ||
export { Wire } from "./decorators/wire"; | ||
export { AutoWire } from "./decorators/autoWire"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.