@ydant/context 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 cwd-k2
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # @ydant/context
2
+
3
+ Context API for Ydant.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @ydant/context
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Context API
14
+
15
+ ```typescript
16
+ import { mount, type Component } from "@ydant/core";
17
+ import { createBasePlugin, div, text } from "@ydant/base";
18
+ import { createContext, provide, inject, createContextPlugin } from "@ydant/context";
19
+
20
+ // Create context with optional default value
21
+ const ThemeContext = createContext<"light" | "dark">("light");
22
+
23
+ // Parent component provides value
24
+ const App: Component = () =>
25
+ div(function* () {
26
+ yield* provide(ThemeContext, "dark");
27
+ yield* ChildComponent();
28
+ });
29
+
30
+ // Child component injects value
31
+ const ChildComponent: Component = () =>
32
+ div(function* () {
33
+ const theme = yield* inject(ThemeContext);
34
+ yield* text(`Theme: ${theme}`); // "dark"
35
+ });
36
+
37
+ // Mount with base and context plugins
38
+ mount(App, document.getElementById("app")!, {
39
+ plugins: [createBasePlugin(), createContextPlugin()],
40
+ });
41
+ ```
42
+
43
+ > **Note:** `@ydant/base` is required for DOM rendering.
44
+
45
+ ## API
46
+
47
+ ### createContext
48
+
49
+ ```typescript
50
+ function createContext<T>(defaultValue?: T): Context<T>;
51
+ ```
52
+
53
+ Creates a context object that can be provided and injected.
54
+
55
+ ### provide
56
+
57
+ ```typescript
58
+ function provide<T>(context: Context<T>, value: T): ContextProvide;
59
+ ```
60
+
61
+ Provides a value to all descendant components. Use with `yield*`.
62
+
63
+ ### inject
64
+
65
+ ```typescript
66
+ function inject<T>(context: Context<T>): ContextInject;
67
+ ```
68
+
69
+ Retrieves the value from the nearest ancestor provider. Use with `yield*`. Returns the default value if no provider is found.
70
+
71
+ ### createContextPlugin
72
+
73
+ ```typescript
74
+ function createContextPlugin(): Plugin;
75
+ ```
76
+
77
+ Creates a plugin that handles `provide` and `inject`. Must be passed to `mount()`.
78
+
79
+ The context plugin extends `RenderContext` and `PluginAPI`:
80
+
81
+ ```typescript
82
+ // RenderContext extensions
83
+ interface RenderContextExtensions {
84
+ contextValues: Map<symbol, unknown>;
85
+ }
86
+
87
+ // PluginAPI extensions
88
+ interface PluginAPIExtensions {
89
+ getContext<T>(id: symbol): T | undefined;
90
+ setContext<T>(id: symbol, value: T): void;
91
+ }
92
+ ```
93
+
94
+ ## Module Structure
95
+
96
+ - `context.ts` - Context creation and provide/inject
97
+ - `plugin.ts` - Plugin implementation
@@ -0,0 +1,62 @@
1
+ import { Tagged, Primitive } from '@ydant/core';
2
+ /** Context オブジェクト */
3
+ export interface Context<T> {
4
+ /** Context の一意な識別子 */
5
+ readonly id: symbol;
6
+ /** デフォルト値 */
7
+ readonly defaultValue: T | undefined;
8
+ }
9
+ /** Context Provider 型(Tagged Union) */
10
+ export type ContextProvide = Tagged<"context-provide", {
11
+ context: Context<unknown>;
12
+ value: unknown;
13
+ }>;
14
+ /** Context Inject 型(Tagged Union) */
15
+ export type ContextInject = Tagged<"context-inject", {
16
+ context: Context<unknown>;
17
+ }>;
18
+ /** Context から値を取得する DSL プリミティブの戻り値型 */
19
+ type Accessor<T> = Generator<ContextInject, T, T>;
20
+ /**
21
+ * Context を作成する
22
+ *
23
+ * @param defaultValue - inject 時に provider が見つからない場合に使用される値
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const ThemeContext = createContext<"light" | "dark">("light");
28
+ * const UserContext = createContext<User | null>(null);
29
+ * ```
30
+ */
31
+ export declare function createContext<T>(defaultValue?: T): Context<T>;
32
+ /**
33
+ * Context に値を提供する
34
+ *
35
+ * このジェネレーターを yield* すると、その子孫コンポーネントで
36
+ * inject() を使ってこの値を取得できるようになる。
37
+ *
38
+ * @param context - 提供する Context
39
+ * @param value - 提供する値
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * yield* provide(ThemeContext, "dark");
44
+ * ```
45
+ */
46
+ export declare function provide<T>(context: Context<T>, value: T): Primitive<ContextProvide>;
47
+ /**
48
+ * Context から値を取得する
49
+ *
50
+ * 親コンポーネントで provide された値を取得する。
51
+ * provider が見つからない場合は defaultValue を返す。
52
+ *
53
+ * @param context - 取得する Context
54
+ * @returns Context の値
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const theme = yield* inject(ThemeContext);
59
+ * ```
60
+ */
61
+ export declare function inject<T>(context: Context<T>): Accessor<T>;
62
+ export {};
@@ -0,0 +1,32 @@
1
+ import { ContextProvide, ContextInject } from './context';
2
+ declare module "@ydant/core" {
3
+ // RenderContext に context プラグイン用のプロパティを追加
4
+ interface RenderContextExtensions {
5
+ /** Context の値を保持するマップ */
6
+ contextValues: Map<symbol, unknown>;
7
+ }
8
+
9
+ // PluginAPI に context プラグインのメソッドを追加
10
+ interface PluginAPIExtensions {
11
+ /** Context から値を取得 */
12
+ getContext<T>(id: symbol): T | undefined;
13
+ /** Context に値を設定 */
14
+ setContext<T>(id: symbol, value: T): void;
15
+ }
16
+
17
+ // context の DSL 型を Child に追加
18
+ interface PluginChildExtensions {
19
+ ContextProvide: ContextProvide;
20
+ ContextInject: ContextInject;
21
+ }
22
+
23
+ // inject の戻り値を ChildNext に追加
24
+ interface PluginNextExtensions {
25
+ ContextValue: unknown;
26
+ }
27
+
28
+ // inject の戻り値を ChildReturn に追加
29
+ interface PluginReturnExtensions {
30
+ ContextValue: unknown;
31
+ }
32
+ }
@@ -0,0 +1,4 @@
1
+ /// <reference path="./global.d.ts" />
2
+ export type { Context, ContextProvide, ContextInject } from './context';
3
+ export { createContext, provide, inject } from './context';
4
+ export { createContextPlugin } from './plugin';
@@ -0,0 +1,46 @@
1
+ import { isTagged as c } from "@ydant/core";
2
+ function x(t) {
3
+ return {
4
+ id: Symbol("context"),
5
+ defaultValue: t
6
+ };
7
+ }
8
+ function* a(t, e) {
9
+ yield {
10
+ type: "context-provide",
11
+ context: t,
12
+ value: e
13
+ };
14
+ }
15
+ function* i(t) {
16
+ return yield {
17
+ type: "context-inject",
18
+ context: t
19
+ };
20
+ }
21
+ function s() {
22
+ return {
23
+ name: "context",
24
+ types: ["context-provide", "context-inject"],
25
+ dependencies: ["base"],
26
+ initContext(t, e) {
27
+ const n = e?.contextValues;
28
+ t.contextValues = n ? new Map(n) : /* @__PURE__ */ new Map();
29
+ },
30
+ extendAPI(t, e) {
31
+ const n = e.contextValues;
32
+ t.getContext = (o) => n.get(o), t.setContext = (o, r) => {
33
+ n.set(o, r);
34
+ };
35
+ },
36
+ process(t, e) {
37
+ return c(t, "context-provide") ? (e.setContext(t.context.id, t.value), {}) : c(t, "context-inject") ? { value: e.getContext(t.context.id) ?? t.context.defaultValue } : {};
38
+ }
39
+ };
40
+ }
41
+ export {
42
+ x as createContext,
43
+ s as createContextPlugin,
44
+ i as inject,
45
+ a as provide
46
+ };
@@ -0,0 +1 @@
1
+ (function(t,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@ydant/core")):typeof define=="function"&&define.amd?define(["exports","@ydant/core"],o):(t=typeof globalThis<"u"?globalThis:t||self,o(t.YdantContext={},t.YdantCore))})(this,(function(t,o){"use strict";function c(e){return{id:Symbol("context"),defaultValue:e}}function*r(e,n){yield{type:"context-provide",context:e,value:n}}function*d(e){return yield{type:"context-inject",context:e}}function a(){return{name:"context",types:["context-provide","context-inject"],dependencies:["base"],initContext(e,n){const i=n?.contextValues;e.contextValues=i?new Map(i):new Map},extendAPI(e,n){const i=n.contextValues;e.getContext=u=>i.get(u),e.setContext=(u,x)=>{i.set(u,x)}},process(e,n){return o.isTagged(e,"context-provide")?(n.setContext(e.context.id,e.value),{}):o.isTagged(e,"context-inject")?{value:n.getContext(e.context.id)??e.context.defaultValue}:{}}}}t.createContext=c,t.createContextPlugin=a,t.inject=d,t.provide=r,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
@@ -0,0 +1,5 @@
1
+ import { Plugin } from '@ydant/core';
2
+ /**
3
+ * Context プラグインを作成する
4
+ */
5
+ export declare function createContextPlugin(): Plugin;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@ydant/context",
3
+ "version": "0.1.0",
4
+ "description": "Context API for Ydant",
5
+ "keywords": [
6
+ "context",
7
+ "dependency-injection",
8
+ "state",
9
+ "ydant"
10
+ ],
11
+ "homepage": "https://github.com/cwd-k2/ydant#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/cwd-k2/ydant/issues"
14
+ },
15
+ "license": "MIT",
16
+ "author": "cwd-k2",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/cwd-k2/ydant.git",
20
+ "directory": "packages/context"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "LICENSE",
25
+ "README.md"
26
+ ],
27
+ "main": "./dist/index.umd.js",
28
+ "module": "./dist/index.es.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "@ydant/dev": {
34
+ "types": "./src/index.ts",
35
+ "default": "./src/index.ts"
36
+ },
37
+ "import": "./dist/index.es.js",
38
+ "require": "./dist/index.umd.js"
39
+ }
40
+ },
41
+ "peerDependencies": {
42
+ "@ydant/base": "0.1.0",
43
+ "@ydant/core": "0.1.0"
44
+ },
45
+ "scripts": {
46
+ "build": "vite build",
47
+ "typecheck": "tsc --noEmit"
48
+ }
49
+ }