├── .gitignore ├── .nvmrc ├── LICENSE.txt ├── README.md ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── _ringduck │ ├── README.md │ ├── indeps-ringduck.ts │ └── index-ringduck.ts ├── examples │ ├── examples-lookduck │ │ ├── duckpond │ │ │ ├── DuckPondApp.tsx │ │ │ ├── cmd-start-duckpond.sh │ │ │ ├── duckpond-index.html │ │ │ └── duckpond-store.ts │ │ └── todopond │ │ │ ├── TodoPondApp.tsx │ │ │ ├── cmd-start-todopond.sh │ │ │ ├── todopond-index.html │ │ │ └── todopond-store.ts │ ├── examples-multi │ │ ├── README.md │ │ ├── chatter │ │ │ ├── ChatterRoot.tsx │ │ │ ├── chatter-client.html │ │ │ ├── chatter-server.ts │ │ │ ├── chatter-shared.ts │ │ │ ├── chatter-store.ts │ │ │ ├── cmd-start-chatter-server.sh │ │ │ ├── cmd-watch-chatter-client.sh │ │ │ └── package.json │ │ └── frontonly │ │ │ ├── FrontonlyRoot.tsx │ │ │ ├── cmd-start-frontonly.sh │ │ │ ├── frontonly-client.html │ │ │ ├── nameStore.ts │ │ │ └── package.json │ ├── examples-rainduck │ │ └── rain-todo-eg │ │ │ ├── cmd-start-rain-todo-eg.sh │ │ │ ├── rain-todo-eg-index.html │ │ │ ├── rain-todo-eg-main.ts │ │ │ └── rain-todo-eg-store.ts │ └── examples-tapiduck │ │ ├── tapi-isofetch-eg │ │ ├── cmd-start-consumer.sh │ │ ├── cmd-start-producer.sh │ │ ├── consumer-app.ts │ │ ├── isofetch-shared.ts │ │ ├── package-lock.json │ │ ├── package.json │ │ └── producer-api.ts │ │ └── tapi-todo-eg │ │ ├── cmd-start-client.sh │ │ ├── cmd-start-server.sh │ │ ├── package.json │ │ ├── tapi-eg.test.ts │ │ ├── todo-client-rain.ts │ │ ├── todo-client.html │ │ ├── todo-client.ts │ │ ├── todo-server.ts │ │ └── todo-shared.ts ├── index-monoduck.ts ├── lookduck │ ├── README.md │ ├── atom.test.ts │ ├── atom.ts │ ├── computed.test.ts │ ├── computed.ts │ ├── indeps-lookduck.ts │ ├── index-lookduck.ts │ ├── lookable.ts │ ├── observable.test.ts │ ├── observable.ts │ ├── observableIdMap.test.ts │ ├── observableIdMap.ts │ ├── pubsubable.test.ts │ ├── pubsubable.ts │ ├── react-hook.ts │ └── store.ts ├── patchduck │ ├── README.md │ ├── indeps-patchduck.ts │ ├── index-patchduck.ts │ ├── patchduck-readme.test.ts │ └── patchduck.test.ts ├── quack.test.ts ├── rainduck │ ├── indeps-rainduck.ts │ ├── index-rainduck.ts │ ├── rainEvent.ts │ ├── rainHtml.test.ts │ ├── rainHtml.ts │ └── rainRender.ts ├── roqsduck │ ├── README.md │ ├── indeps-roqsduck.ts │ ├── index-roqsduck.ts │ ├── qs-base-tracker.ts │ ├── qs-parsing.test.ts │ └── qs-react-integ.ts ├── sqlduck │ ├── README.md │ ├── indeps-sqlduck.ts │ ├── index-sqlduck.ts │ └── sqlduck.test.ts ├── tapiduck │ ├── README.md │ ├── indeps-tapiduck.ts │ ├── index-tapiduck.ts │ ├── jsend.ts │ ├── tapiEndpoint.ts │ ├── tapiFetcher.ts │ ├── tapiRouter.ts │ ├── tapiSockpoint.ts │ └── tapiToOpenApi.ts └── utilduck │ ├── README.md │ ├── index-utilduck.ts │ └── utilduck.test.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.generated.* 3 | .parcel-cache/ 4 | node_modules/ 5 | dist/ 6 | flagcmd/ 7 | tmp/ 8 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | lts/* 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Monoduck 2 | 3 | ### What is it? 4 | 5 | - Monoduck is a Typescript-first, monolithic repository of subpackages. 6 | - To avoid bloat, it has absolutely no `dependencies`, and all `peerDependencies` are _optional_. 7 | - And to keep things tree-shakable, only named exports are used. 8 | 9 | ### Production Readiness 10 | 11 | - Monoduck is not generally ready for production use, but it's getting there. 12 | - However, it should be stable enough for use in internal apps, and POCs. 13 | 14 | ### What's included? 15 | 16 | - [**Lookduck**](/src/lookduck/README.md): React state manager with automatic dependency management for derived state. 17 | - [**Tapiduck**](/src/tapiduck/README.md): End-to-end type-safe APIs with fullstack intillisense. Simple and restful-ish. 18 | - [**Patchduck**](/src/patchduck/README.md): TS-first, React-friendly utility for deep-patching (deep-spreading) objects. 19 | - [**Utilduck**](/src/utilduck/README.md): Commonly used, Underscore-style, typed utils like `_.map`, `_.deepClone`, etc. 20 | - [**Sqlduck**](/src/sqlduck/README.md): A thin Sequelize wrapper. Plays well with Zod. Prioritizes simplicity & type-safety. 21 | - [**Roqsduck**](/src/roqsduck/README.md): Intentionally simple, query-string based, client-side routing for React apps. 22 | 23 | ### Code Style & Preferences 24 | 25 | Monoduck uses `ts-standard` for code-styling and linting. This way, one needn't manually configure lint rules. The codebase also has a few additional conventions, listed below. While most of these conventions are not strict requirements, the codebase tries to follow all of them. 26 | 27 | - Prefer functions over classes. 28 | - Mildly prefer interfaces over types, but prefer types for functions. 29 | - Avoid `this`. If unavoidable, go with `const self = this` and use `self`. 30 | - Prefer `function` over `=>`. (Latter is OK for one-liners like `x => x + 1`) 31 | - Prefer named exports over default exports, and avoid `*` imports. 32 | - Prefer `unknown` over `any`. For return types, prefer `void` over `unknown`. 33 | - Prefer `const` over `let`, and don't use `var`. 34 | - Bool vars should begin with `is`, `has`, etc. (eg. `isAdmin`, `hasAccess`) 35 | - Functions that return bools should end with `is`, `has`, etc. (eg. `oddIs`) 36 | - Strictly prefer `===` over `==`. Never use `==`, not even `== null`. 37 | - Prefer `if`..`else` over `switch`. (Latter is OK if each `case` just returns.) 38 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ 2 | module.exports = { 3 | preset: 'ts-jest', 4 | testEnvironment: 'node' 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "monoduck", 3 | "version": "0.2.3-dev", 4 | "description": "", 5 | "source": "src/index-monoduck.ts", 6 | "main": "dist/index-monoduck.js", 7 | "types": "dist/index-monoduck.d.ts", 8 | "files": [ 9 | "dist/" 10 | ], 11 | "scripts": { 12 | "test": "jest src/", 13 | "test-utilduck": "jest src/utilduck/", 14 | "test-sqlduck": "jest src/sqlduck/", 15 | "test-lookduck": "jest src/lookduck/", 16 | "test-patchduck": "jest src/patchduck/", 17 | "test-tapiduck-eg": "jest src/examples/examples-tapiduck", 18 | "test-rainduck": "jest src/rainduck/", 19 | "lint": "ts-standard --fix", 20 | "build": "rm -rf dist && tsc && rm -rf dist/examples", 21 | "prepack": "npm run lint && npm run test && npm run build" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "git+https://github.com/sumukhbarve/monoduck.git" 26 | }, 27 | "keywords": [], 28 | "author": "Sumukh Barve", 29 | "license": "Apache-2.0", 30 | "bugs": { 31 | "url": "https://github.com/sumukhbarve/monoduck/issues" 32 | }, 33 | "homepage": "https://github.com/sumukhbarve/monoduck#readme", 34 | "devDependencies": { 35 | "@parcel/packager-ts": "^2.3.2", 36 | "@parcel/transformer-typescript-types": "^2.3.2", 37 | "@types/cors": "^2.8.12", 38 | "@types/express": "^4.17.13", 39 | "@types/jest": "^27.4.1", 40 | "@types/node-fetch": "^2.6.2", 41 | "@types/react": "^17.0.40", 42 | "@types/react-dom": "^17.0.13", 43 | "buffer": "^6.0.3", 44 | "cors": "^2.8.5", 45 | "express": "^4.17.3", 46 | "jest": "^27.5.1", 47 | "node-fetch": "^2.6.7", 48 | "parcel": "^2.3.2", 49 | "process": "^0.11.10", 50 | "react": "^17.0.2", 51 | "react-dom": "^17.0.2", 52 | "sequelize": "^6.17.0", 53 | "socket.io": "^4.4.1", 54 | "socket.io-client": "^4.4.1", 55 | "sqlite3": "^5.0.8", 56 | "ts-jest": "^27.1.3", 57 | "ts-node-dev": "^2.0.0", 58 | "ts-standard": "^11.0.0", 59 | "typescript": "^4.6.2", 60 | "zod": "^3.14.1", 61 | "zod-to-json-schema": "^3.20.4" 62 | }, 63 | "peerDependencies": { 64 | "express": "^4.17.3", 65 | "node-fetch": "^2.6.7", 66 | "react": "^17.0.2", 67 | "sequelize": "^6.17.0", 68 | "zod": "^3.14.1", 69 | "zod-to-json-schema": "^3.20.4" 70 | }, 71 | "peerDependenciesMeta": { 72 | "express": { 73 | "optional": true 74 | }, 75 | "node-fetch": { 76 | "optional": true 77 | }, 78 | "react": { 79 | "optional": true 80 | }, 81 | "sequelize": { 82 | "optional": true 83 | }, 84 | "zod": { 85 | "optional": true 86 | }, 87 | "zod-to-json-schema": { 88 | "optional": true 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/_ringduck/README.md: -------------------------------------------------------------------------------- 1 | # `_ringduck` 2 | 3 | - Meant for internal use only. 4 | - Helps accept React as an INJected dependency throughout Monoduck. 5 | - Ensures that different ducks don't receive different versions of React. 6 | - _rinJduck_ doesn't sound great, so _rinGduck_ instead. 7 | 8 | Other ducks can: 9 | - Expose (this duck's) `injectReact()` for accepting React, and 10 | - call (this duck's) `getInjectedReact()` for accessing React. 11 | -------------------------------------------------------------------------------- /src/_ringduck/indeps-ringduck.ts: -------------------------------------------------------------------------------- 1 | export type { VoidFn, AnyFn, JsonValue } from '../utilduck/index-utilduck' 2 | export { _ } from '../utilduck/index-utilduck' 3 | -------------------------------------------------------------------------------- /src/_ringduck/index-ringduck.ts: -------------------------------------------------------------------------------- 1 | import type { VoidFn } from './indeps-ringduck' 2 | import type { ZodSchema } from 'zod' 3 | 4 | // A 2-tuple: [injectFoo, getInjectedFoo] 5 | type DepInjectionTup = [(dep: T) => void, () => T] 6 | 7 | const buildInjectionTup = function (depName: string): DepInjectionTup { 8 | let _dep: T | null = null 9 | let didInject = false 10 | const injectDep = function (dep: T): void { 11 | if (!didInject) { 12 | _dep = dep 13 | didInject = true 14 | } else if (_dep !== dep) { 15 | throw new Error(`Monoduck: Another ${depName} was previously injected.`) 16 | } 17 | } 18 | const retrieveDep = function (): T { 19 | if (!didInject) { 20 | throw new Error(`Monoduck: ${depName} not injected. Please inject it.`) 21 | } 22 | return _dep as T 23 | } 24 | return [injectDep, retrieveDep] 25 | } 26 | 27 | interface Reacty { 28 | useState: (initVal: T) => [T, (cb: (val: T) => T) => void] 29 | useEffect: (effect: () => VoidFn, deps?: unknown[]) => void 30 | useRef: (initVal: T) => {current: T} 31 | unstable_batchedUpdates?: (fn: VoidFn) => void 32 | // Note the signature of: React.createElement(component, props, ...children) 33 | createElement: 34 | (tag: string, props: Record, ...children: any) => any 35 | // Unused: 36 | // useCallback: (callback: VoidFn, deps: unknown[]) => VoidFn 37 | // createRoot?: AnyFn 38 | } 39 | const [injectReact, getInjectedReact] = buildInjectionTup('React') 40 | 41 | type FetchyFn = (url: string, data: Record) => Promise<{ 42 | status: number 43 | text: () => Promise 44 | }> 45 | const [injectFetch, getInjectedFetch] = buildInjectionTup('fetch') 46 | 47 | // On the browser, we auto-inject window.fetch: 48 | if (globalThis.fetch !== undefined) { 49 | injectFetch(globalThis.fetch) 50 | } 51 | 52 | type ZodyToJsonSchemaFn = 53 | (zodSchema: ZodSchema, options: {target: 'openApi3', $refStrategy: 'none'}) 54 | => Record 55 | 56 | const [injectZodToJsonSchema, getInjectedZodToJsonSchema] = 57 | buildInjectionTup('zodToJsonSchema') 58 | 59 | export type { Reacty, FetchyFn, ZodyToJsonSchemaFn } 60 | export { 61 | injectReact, getInjectedReact, 62 | injectFetch, getInjectedFetch, 63 | injectZodToJsonSchema, getInjectedZodToJsonSchema 64 | } 65 | -------------------------------------------------------------------------------- /src/examples/examples-lookduck/duckpond/DuckPondApp.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { store, useStore } from './duckpond-store' 3 | import ReactDOM from 'react-dom' 4 | 5 | import { lookduck } from '../../../index-monoduck' 6 | 7 | const DuckCountHeading: React.FC = function () { 8 | const { duckCount, duckNoun } = useStore('duckCount', 'duckNoun') 9 | return

Duck Count: {duckCount} {duckNoun}

10 | } 11 | 12 | const DuckAdder: React.FC<{delta: number, label: string}> = function (props) { 13 | const { delta, label } = props 14 | const onAdd = (): void => store.duckCount.set(store.duckCount.get() + delta) 15 | return 16 | } 17 | 18 | const LowCountWarning: React.FC = function () { 19 | const { isTooLow } = useStore('isTooLow') 20 | return isTooLow ?

Warning: There are too few ducks. Add some!

: null 21 | } 22 | 23 | const countAtom = lookduck.atom(0) 24 | const doubleCountAtom = lookduck.atom(get => get(countAtom) * 2) 25 | 26 | const Counter: React.FC = function () { 27 | const [count, setCount] = lookduck.useAtom(countAtom) 28 | const [doubleCount] = lookduck.useAtom(doubleCountAtom) 29 | const onIncrement = React.useCallback(() => { 30 | setCount(count + 1) 31 | }, [count]) 32 | return ( 33 | <> 34 |
35 |

Unrelated Counter:

36 |

The count is {count}, and twice that is {doubleCount}.

37 | 38 | 39 | ) 40 | } 41 | 42 | const DuckPondApp: React.VFC = function () { 43 | return ( 44 | <> 45 | 46 | {' '} 47 | {' '} 48 | {' '} 49 | 50 | 51 | 52 | ) 53 | } 54 | 55 | ReactDOM.render(, document.querySelector('#root')) 56 | -------------------------------------------------------------------------------- /src/examples/examples-lookduck/duckpond/cmd-start-duckpond.sh: -------------------------------------------------------------------------------- 1 | rm -rf dist && npx parcel serve duckpond-index.html 2 | -------------------------------------------------------------------------------- /src/examples/examples-lookduck/duckpond/duckpond-index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Lookduck Counter Exmaple 5 | 6 | 7 | 8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /src/examples/examples-lookduck/duckpond/duckpond-store.ts: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { lookduck } from '../../../index-monoduck' 3 | 4 | const MIN_EXPECTED_DUCK_COUNT = 5 5 | 6 | export const store = { 7 | duckCount: lookduck.observable(0), 8 | duckNoun: lookduck.computed(function (): string { 9 | return store.duckCount.get() === 1 ? 'duck' : 'ducks' 10 | }), 11 | isTooLow: lookduck.computed(function (): boolean { 12 | return store.duckCount.get() < MIN_EXPECTED_DUCK_COUNT 13 | }) 14 | } 15 | 16 | export const useStore = lookduck.makeUseStore(store, React) 17 | -------------------------------------------------------------------------------- /src/examples/examples-lookduck/todopond/TodoPondApp.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Todo, store, useStore } from './todopond-store' 3 | import ReactDOM from 'react-dom' 4 | 5 | const Header: React.VFC = function () { 6 | const { hideDone } = useStore('hideDone') 7 | const onToggle = React.useCallback(() => store.hideDone.set(!hideDone), [hideDone]) 8 | return ( 9 |
10 |

Todos

11 | 15 |
16 |
17 | ) 18 | } 19 | 20 | const SingleTodo: React.VFC<{todo: Todo}> = function ({ todo }) { 21 | const currentId = todo.id 22 | const onToggle = React.useCallback(function () { 23 | store.allTodos.set(store.allTodos.get().map( 24 | (todo) => todo.id === currentId ? { ...todo, done: !todo.done } : todo 25 | )) 26 | }, [currentId]) 27 | const onRemove = React.useCallback(function () { 28 | store.allTodos.set(store.allTodos.get().filter( 29 | (todo) => todo.id !== currentId 30 | )) 31 | }, [currentId]) 32 | return ( 33 |

34 | 38 | 39 | (x) 40 | 41 |

42 | ) 43 | } 44 | 45 | const TodoList: React.VFC = function () { 46 | const { visibleTodos } = useStore('visibleTodos') 47 | return ( 48 |
49 | {visibleTodos.map(todo => ())} 50 |
51 | ) 52 | } 53 | 54 | const TodoAdder: React.VFC = function () { 55 | const [text, setText] = React.useState('') 56 | const onAdd = React.useCallback(function (event: React.FormEvent) { 57 | event.preventDefault() 58 | if (text !== '') { 59 | const newTodo: Todo = { id: Date.now(), text: text, done: false } 60 | store.allTodos.set([...store.allTodos.get(), newTodo]) 61 | setText('') 62 | } 63 | }, [text]) 64 | return ( 65 |
66 | setText(event.target.value)} 70 | /> 71 | 72 |
73 | ) 74 | } 75 | 76 | const Footer: React.FC = function () { 77 | return ( 78 |
79 |
80 | 81 |
82 | ) 83 | } 84 | 85 | const TodoApp: React.VFC = function () { 86 | return ( 87 | <> 88 |
89 | 90 | 91 |