-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
240 additions
and
177 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@dreamkit/site": patch | ||
--- | ||
|
||
Update examples |
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
48 changes: 32 additions & 16 deletions
48
packages/site/src/pages/api/create-action/_examples/basic-usage.tsx
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,19 +1,35 @@ | ||
// title: Basic usage | ||
import { $route, createAction } from "dreamkit"; | ||
import { $api, $route, createAction } from "dreamkit"; | ||
|
||
export default $route.path("/").create(() => { | ||
const start = createAction( | ||
() => | ||
new Promise<number>((resolve) => { | ||
setTimeout(() => resolve(Date.now()), 1000); | ||
}), | ||
); | ||
return ( | ||
<> | ||
<p>{start.result}</p> | ||
<button onClick={start} disabled={start.running}> | ||
Start | ||
</button> | ||
</> | ||
); | ||
export const start = $api.title("Start").create(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
const id = Date.now(); | ||
if (id % 2) throw new Error("Random error"); | ||
return id; | ||
}); | ||
|
||
export default $route | ||
.path("/") | ||
.api({ start }) | ||
.create(({ api }) => { | ||
const start = createAction(api.start); | ||
return ( | ||
<> | ||
<ul> | ||
<li>state: {start.state}</li> | ||
<li>result: {start.result}</li> | ||
<li>error: {start.error?.message}</li> | ||
</ul> | ||
<button | ||
onClick={start} | ||
disabled={start.running} | ||
children={api.start.title} | ||
/> | ||
<button | ||
onClick={start.abort} | ||
disabled={!start.running} | ||
children="abort" | ||
/> | ||
</> | ||
); | ||
}); |
55 changes: 38 additions & 17 deletions
55
packages/site/src/pages/api/create-action/_examples/predefined-params.tsx
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,21 +1,42 @@ | ||
// title: Predefined params | ||
import { createAction, Input } from "dreamkit"; | ||
import { $api, $route, createAction, Input, s } from "dreamkit"; | ||
import { createEffect, createSignal } from "solid-js"; | ||
|
||
function remove(key: string) { | ||
console.log("key removed", key); | ||
} | ||
const remove = $api | ||
.title("Remove") | ||
.params({ | ||
key: s.title("Key").string(), | ||
}) | ||
.create(({ key }) => { | ||
console.log("Received", { key }); | ||
}); | ||
|
||
export default function App() { | ||
const [id, setKey] = createSignal(""); | ||
const $remove = createAction(remove).with(() => id()); | ||
createEffect(() => $remove.state === "success" && $remove.clear()); | ||
return ( | ||
<> | ||
<Input value={id} onChange={setKey} /> | ||
<button onClick={$remove} disabled={$remove.running}> | ||
Remove | ||
</button> | ||
</> | ||
); | ||
} | ||
export default $route | ||
.path("/") | ||
.api({ remove }) | ||
.create(({ api }) => { | ||
const [key, setKey] = createSignal(""); | ||
const remove = createAction(api.remove).with(() => ({ key: key() })); | ||
createEffect(() => { | ||
if (remove.state === "success") { | ||
setKey(""); | ||
remove.clear(); | ||
} | ||
}); | ||
return ( | ||
<> | ||
<p> | ||
<Input | ||
placeholder={api.remove.params.key.options.title} | ||
value={key} | ||
onChange={setKey} | ||
/> | ||
</p> | ||
<button | ||
onClick={remove} | ||
disabled={remove.running} | ||
children={api.remove.title} | ||
/> | ||
</> | ||
); | ||
}); |
12 changes: 8 additions & 4 deletions
12
packages/site/src/pages/api/input/_examples/controlled-checkbox.tsx
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,9 +1,13 @@ | ||
// title: Controlled number | ||
// title: Controlled checkbox | ||
import { $route, Input } from "dreamkit"; | ||
import { createEffect, createSignal } from "solid-js"; | ||
import { createSignal } from "solid-js"; | ||
|
||
export default $route.path("/").create(() => { | ||
const [bool, setBool] = createSignal(false); | ||
createEffect(() => console.log("bool", bool())); | ||
return <Input type="checkbox" value={bool} onChange={setBool} />; | ||
return ( | ||
<> | ||
<p>value: {JSON.stringify(bool())}</p> | ||
<Input type="checkbox" value={bool} onChange={setBool} /> | ||
</> | ||
); | ||
}); |
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
48 changes: 23 additions & 25 deletions
48
packages/site/src/pages/api/ioc/_examples/basic-usage.tsx
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,33 +1,31 @@ | ||
// title: Basic usage | ||
import { context, IocClass, ServiceClass } from "dreamkit"; | ||
import { $route, context, IocClass } from "dreamkit"; | ||
|
||
class Child extends IocClass({}) { | ||
process(value: string) { | ||
return value; | ||
} | ||
abstract class ThirdModel { | ||
abstract fetch(): string; | ||
} | ||
|
||
class CustomChild extends Child { | ||
override process(value: string): string { | ||
return value.toUpperCase(); | ||
class MyModel extends IocClass({ ThirdModel }) { | ||
fetch() { | ||
return this.thirdModel.fetch(); | ||
} | ||
} | ||
|
||
class Parent extends IocClass({ Child }) { | ||
process(value: string) { | ||
return this.child.process(value); | ||
export default $route.path("/").create(() => { | ||
class ThirdModelImpl extends ThirdModel { | ||
override fetch() { | ||
return "resolved"; | ||
} | ||
} | ||
} | ||
|
||
export class TestService extends ServiceClass({ Parent }) { | ||
onStart() { | ||
const value0 = this.parent.process("hello"); | ||
const value1 = new Parent({ child: new Child() }).process("hello"); | ||
const value2 = context | ||
.fork() | ||
.register(Child, { useClass: CustomChild }) | ||
.resolve(Parent) | ||
.process("world"); | ||
console.log([value0, value1, value2]); // ["hello", "WORLD"] | ||
} | ||
} | ||
const manualModel = new MyModel({ thirdModel: new ThirdModelImpl() }); | ||
const autoModel = context | ||
.fork() | ||
.register(ThirdModel, { value: new ThirdModelImpl() }) | ||
.resolve(MyModel); | ||
return ( | ||
<> | ||
<p>{manualModel.fetch()}</p> | ||
<p>{autoModel.fetch()}</p> | ||
</> | ||
); | ||
}); |
Oops, something went wrong.