unwrapped 0.0.1 → 0.1.1

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,106 @@
1
+ import { computed, onUnmounted, ref, toRef, triggerRef, watch, type Ref, type WatchSource } from "vue";
2
+ import { AsyncResult, type FlatChainFunction, type Result } from "unwrapped/core";
3
+
4
+ export function useAsyncResultRef<T, E>(asyncResult: AsyncResult<T, E>) {
5
+ const state = ref<AsyncResult<T, E>>(asyncResult) as Ref<AsyncResult<T, E>>;
6
+
7
+ const unsub = asyncResult.listen(() => {
8
+ triggerRef(state);
9
+ });
10
+
11
+ onUnmounted(() => {
12
+ unsub();
13
+ });
14
+
15
+ return state;
16
+ }
17
+
18
+ export function useReactiveResult<T, E, Inputs>(source: WatchSource<Inputs>, pipe: FlatChainFunction<Inputs, T, E>, options:{ immediate: boolean } = { immediate: true }): Ref<AsyncResult<T, E>> {
19
+ const result = new AsyncResult<T, E>();
20
+ const resultRef = useAsyncResultRef(result);
21
+
22
+ let unsub: (() => void) | null = null;
23
+
24
+ watch(source, (newInputs) => {
25
+ unsub?.();
26
+ unsub = result.mirror(pipe(newInputs));
27
+ }, { immediate: options.immediate });
28
+
29
+ onUnmounted(() => {
30
+ unsub?.();
31
+ });
32
+
33
+ return resultRef;
34
+ }
35
+
36
+ export function useAsyncResultRefFromPromise<T, E>(promise: Promise<Result<T, E>>) {
37
+ return useAsyncResultRef(AsyncResult.fromResultPromise(promise));
38
+ }
39
+
40
+ export type Action<T,E> = () => Promise<Result<T, E>>;
41
+ export function useImmediateAction<T, E>(action: Action<T, E>): Ref<AsyncResult<T, E>> {
42
+ return useAsyncResultRefFromPromise(action());
43
+ }
44
+ export interface LazyActionReturn<T, E> {
45
+ resultRef: Ref<AsyncResult<T, E>>;
46
+ trigger: () => void;
47
+ }
48
+
49
+ export function useLazyAction<T, E>(action: Action<T, E>): LazyActionReturn<T, E> {
50
+ const result = new AsyncResult<T, E>();
51
+ const resultRef = useAsyncResultRef(result);
52
+
53
+ const trigger = () => {
54
+ result.updateFromResultPromise(action());
55
+ }
56
+
57
+ return { resultRef, trigger };
58
+ }
59
+
60
+ export function useReactiveAction<I, O, E>(input: I | Ref<I> | (() => I), pipe: FlatChainFunction<I, O, E>, options:{ immediate: boolean } = { immediate: true }): Ref<AsyncResult<O, E>> {
61
+ const source = typeof input === 'function' ? computed(input as () => I) : toRef(input)
62
+
63
+ const outputRef = ref<AsyncResult<O, E>>(new AsyncResult()) as Ref<AsyncResult<O, E>>;
64
+ let unsub: (() => void) | null = null;
65
+
66
+ watch(source, () => {
67
+ unsub?.();
68
+ const newOutput = pipe(source.value);
69
+ unsub = newOutput.listen((newState) => {
70
+ outputRef.value.setState(newState.state);
71
+ triggerRef(outputRef);
72
+ });
73
+ }, { immediate: options.immediate });
74
+
75
+ onUnmounted(() => {
76
+ unsub?.();
77
+ });
78
+
79
+ return outputRef;
80
+ }
81
+
82
+ export function useGenerator<T>(generatorFunc: () => Generator<AsyncResult<any, any>, T, any>): Ref<AsyncResult<T, any>> {
83
+ const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));
84
+ return resultRef;
85
+ }
86
+
87
+ export function useLazyGenerator<T>(generatorFunc: () => Generator<AsyncResult<any, any>, T, any>): { resultRef: Ref<AsyncResult<T, any>>, trigger: () => void } {
88
+ const result = new AsyncResult<T, any>();
89
+ const resultRef = useAsyncResultRef(result);
90
+
91
+ const trigger = () => {
92
+ result.runInPlace(generatorFunc);
93
+ }
94
+
95
+ return { resultRef, trigger };
96
+ }
97
+
98
+ export function useReactiveGenerator<T, E, Inputs>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs) => Generator<AsyncResult<any, any>, T, any>, options:{ immediate: boolean } = { immediate: true }): Ref<AsyncResult<T, E>> {
99
+ const resultRef = useAsyncResultRef(new AsyncResult<T, E>());
100
+
101
+ watch(source, (newInputs) => {
102
+ resultRef.value.runInPlace(() => generatorFunc(newInputs));
103
+ }, { immediate: options.immediate });
104
+
105
+ return resultRef;
106
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./composables"
2
+ export * from "./components/asyncResultLoader"
@@ -1,8 +0,0 @@
1
- # Changesets
2
-
3
- Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4
- with multi-package repos, or single-package repos to help you version and publish your code. You can
5
- find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6
-
7
- We have a quick list of common questions to get you started engaging with this project in
8
- [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
@@ -1,11 +0,0 @@
1
- {
2
- "$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json",
3
- "changelog": "@changesets/cli/changelog",
4
- "commit": false,
5
- "fixed": [],
6
- "linked": [],
7
- "access": "restricted",
8
- "baseBranch": "main",
9
- "updateInternalDependencies": "patch",
10
- "ignore": []
11
- }
@@ -1,19 +0,0 @@
1
- name: CI
2
- on:
3
- push:
4
- branches:
5
- - "**"
6
-
7
- jobs:
8
- build:
9
- runs-on: ubuntu-latest
10
- steps:
11
- - uses: actions/checkout@v3
12
-
13
- - uses: actions/setup-node@v3
14
- with:
15
- node-version: 16.x
16
- cache: "npm"
17
-
18
- - run: npm ci
19
- - run: npm run lint && npm run build
@@ -1,35 +0,0 @@
1
- name: Publish
2
- on:
3
- workflow_run:
4
- workflows: [CI]
5
- branches: [main]
6
- types: [completed]
7
-
8
- concurrency: ${{ github.workflow }}-${{ github.ref }}
9
-
10
- permissions:
11
- contents: write
12
- pull-requests: write
13
-
14
- jobs:
15
- publish:
16
- if: ${{ github.event.workflow_run.conclusion == 'success' }}
17
- runs-on: ubuntu-latest
18
- steps:
19
- - uses: actions/checkout@v3
20
-
21
- - uses: actions/setup-node@v3
22
- with:
23
- node-version: 16.x
24
- cache: "npm"
25
-
26
- - run: npm ci
27
-
28
- - name: Create Release Pull Request or Publish
29
- id: changesets
30
- uses: changesets/action@v1
31
- with:
32
- publish: npm run release
33
- env:
34
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35
- NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
package/dist/index.d.mts DELETED
@@ -1,3 +0,0 @@
1
- declare function test(): void;
2
-
3
- export { test };
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- declare function test(): void;
2
-
3
- export { test };
package/dist/index.js DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- test: () => test
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
- function test() {
27
- console.log("test");
28
- }
29
- // Annotate the CommonJS export names for ESM import in node:
30
- 0 && (module.exports = {
31
- test
32
- });
package/dist/index.mjs DELETED
@@ -1,7 +0,0 @@
1
- // index.ts
2
- function test() {
3
- console.log("test");
4
- }
5
- export {
6
- test
7
- };
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- /* Base Options: */
4
- "esModuleInterop": true,
5
- "skipLibCheck": true,
6
- "target": "es2022",
7
- "verbatimModuleSyntax": true,
8
- "allowJs": true,
9
- "resolveJsonModule": true,
10
- "moduleDetection": "force",
11
- /* Strictness */
12
- "strict": true,
13
- "noUncheckedIndexedAccess": true,
14
- /* If NOT transpiling with TypeScript: */
15
- "moduleResolution": "Bundler",
16
- "module": "ESNext",
17
- "noEmit": true,
18
- /* If your code runs in the DOM: */
19
- "lib": ["es2022", "dom", "dom.iterable"],
20
- }
21
- }