-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
8311d5b
commit da7adae
Showing
16 changed files
with
301 additions
and
0 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,37 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest] | ||
fail-fast: false | ||
|
||
steps: | ||
- id: checkout | ||
name: Checkout | ||
uses: actions/checkout@v3 | ||
- id: setup-bun | ||
name: Setup Bun | ||
uses: oven-sh/setup-bun@v1 | ||
with: | ||
bun-version: latest | ||
- id: install-deps | ||
name: Install dependencies | ||
run: | | ||
bun install | ||
- id: test | ||
name: Run test | ||
run: | | ||
bun test |
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,22 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16.x | ||
|
||
- run: npx changelogithub | ||
env: | ||
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} |
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,2 @@ | ||
node_modules | ||
dist |
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,75 @@ | ||
# Signals Manager | ||
|
||
Hey there! 👋 Welcome to the Signals Manager documentation. This README will guide you through using the `SignalsManager` class and its related modules. | ||
|
||
## Introduction | ||
|
||
The Signals Manager is a powerful utility for managing signals within your application. Signals, also known as events or messages, allow components or modules to communicate and react to changes or actions in a decoupled manner. | ||
|
||
## Installation | ||
|
||
To use the Signals Manager in your project, follow these steps: | ||
|
||
1. Clone or download the repository. | ||
2. Install the required dependencies. | ||
3. Import the necessary modules into your project. | ||
|
||
## Usage | ||
|
||
### `SignalsManager` Class | ||
|
||
The `SignalsManager` class provides methods for creating, retrieving, and updating signals. Here's how you can use it: | ||
|
||
```typescript | ||
import { SignalsManager } from "./signals_manager"; | ||
|
||
// Define your default state | ||
const defaultState = { | ||
// Define your state properties here with their default values | ||
count: 0, | ||
}; | ||
|
||
// Initialize the Signals Manager | ||
const signalsManager = new SignalsManager(defaultState); | ||
|
||
// Now you can add, retrieve, and update signals as needed | ||
// But !! only the signals declared at the "defaultState" object, if you try to use any key that is not already provided an error will occur by typescript | ||
``` | ||
|
||
### Initialization | ||
|
||
You can initialize the Signals Manager using the `initSignalsManager` function: | ||
|
||
```typescript | ||
import { initSignalsManager } from "./init"; | ||
|
||
// Define your default state | ||
const defaultState = { | ||
// Define your state properties here | ||
}; | ||
|
||
// Initialize the Signals Manager | ||
const signalsManager = initSignalsManager(defaultState); | ||
|
||
// Now you can start using the Signals Manager | ||
``` | ||
|
||
### Retrieving a Signal | ||
|
||
To retrieve a signal from the manager, use the `getSignal` method: | ||
|
||
```typescript | ||
const mySignal = signalsManager.getSignal("propertyName"); | ||
``` | ||
|
||
### Updating a Signal | ||
|
||
To update a signal in the manager, use the `updateSignal` method: | ||
|
||
```typescript | ||
signalsManager.updateSignal("propertyName", payload); | ||
``` | ||
|
||
## Conclusion | ||
|
||
And that's it! You're now ready to start using the Signals Manager in your project. If you have any questions or need further assistance, feel free to reach out. Happy coding! 🚀🔔 |
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,8 @@ | ||
import dts from 'bun-plugin-dts' | ||
|
||
await Bun.build({ | ||
entrypoints: ['./src/index.ts'], | ||
outdir: './dist', | ||
minify: true, | ||
plugins: [dts()] | ||
}) |
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,30 @@ | ||
{ | ||
"name": "my-lib", | ||
"version": "0.0.0", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"description": "", | ||
"scripts": { | ||
"build": "bun run build.mjs", | ||
"prepublishOnly": "bun run build" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"bun" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://github.com/wobsoriano/pkg-name#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/wobsoriano/pkg-name.git" | ||
}, | ||
"bugs": "https://github.com/wobsoriano/pkg-name/issues", | ||
"author": "Robert Soriano <[email protected]>", | ||
"devDependencies": { | ||
"bun-plugin-dts": "^0.2.1", | ||
"@types/bun": "^1.0.0", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
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 @@ | ||
export * from "./signals-adapter"; |
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 { DomainSignal, SignalsDomain } from "../signals-domain"; | ||
|
||
export type Signal<T> = DomainSignal<T>; | ||
|
||
export class SignalsAdapter<T extends { [K in keyof T]: any }> { | ||
constructor(private signalsDomain: SignalsDomain<T>) {} | ||
|
||
createSignal(payload: T[keyof T]) { | ||
return this.signalsDomain.createSignal(payload); | ||
} | ||
|
||
updateSignal(signal: Signal<T[keyof T]>, payload: T[keyof T]) { | ||
this.signalsDomain.updateSignal(signal, payload); | ||
} | ||
} |
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,2 @@ | ||
export * from "./adapters"; | ||
export * from "./signals-domain"; |
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,13 @@ | ||
import { signal, Signal } from "@preact/signals-core"; | ||
|
||
export type DomainSignal<T> = Signal<T>; | ||
|
||
export class SignalsDomain<T extends { [K in keyof T]: any }> { | ||
createSignal(payload: T[keyof T]) { | ||
return signal(payload); | ||
} | ||
|
||
updateSignal<U extends T[keyof T]>(signal: DomainSignal<U>, payload: U) { | ||
signal.value = payload; | ||
} | ||
} |
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 @@ | ||
export * from "./init"; |
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 { SignalsManager } from "./signals_manager"; | ||
|
||
let signalsManager: SignalsManager<any>; | ||
export type SignalsState<T = {}> = { [K in keyof T]: any }; | ||
|
||
export function initSignalsManager<T extends SignalsState<T>>( | ||
defaultState: T, | ||
): SignalsManager<T> { | ||
if (signalsManager) { | ||
throw new Error("SignalsManager already initialized"); | ||
} | ||
|
||
signalsManager = new SignalsManager<T>(defaultState); | ||
return signalsManager; | ||
} |
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,35 @@ | ||
import { Signal, SignalsAdapter, SignalsDomain } from "./domain"; | ||
|
||
export class SignalsManager<T extends { [K in keyof T]: any }> { | ||
signalsCollection = new Map<keyof T, Signal<T[keyof T]>>(); | ||
signalsAdapter: SignalsAdapter<T>; | ||
|
||
constructor(defaultState: T) { | ||
this.signalsAdapter = new SignalsAdapter(new SignalsDomain<T>()); | ||
|
||
for (const key in defaultState) { | ||
if (defaultState.hasOwnProperty(key)) { | ||
this.signalsCollection.set( | ||
key, | ||
this.signalsAdapter.createSignal(defaultState[key]), | ||
); | ||
} | ||
} | ||
} | ||
|
||
getSignal<U extends T[keyof T]>(key: keyof T): Signal<U> { | ||
const foundSignal = this.signalsCollection.get(key); | ||
|
||
if (!foundSignal) { | ||
throw new Error(`Signal ${String(key)} not found`); | ||
} | ||
|
||
return foundSignal; | ||
} | ||
|
||
updateSignal(key: keyof T, payload: T[keyof T]) { | ||
const foundSignal = this.getSignal(key); | ||
|
||
this.signalsAdapter.updateSignal(foundSignal, payload); | ||
} | ||
} |
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,29 @@ | ||
import { describe, it, expect } from "bun:test"; | ||
import { initSignalsManager } from "../src"; | ||
|
||
describe("should", () => { | ||
const signalsManager = initSignalsManager({ count: 0, test: "" }); | ||
|
||
it("should create a new instance of SignalsManager", () => { | ||
expect(signalsManager).toBeDefined(); | ||
}); | ||
|
||
it("should throw an error if the instance already exists", () => { | ||
try { | ||
expect(initSignalsManager({ count: 0 })).toThrow( | ||
"SignalsManager already initialized", | ||
); | ||
} catch (error) {} | ||
}); | ||
|
||
it("should have been initialized with the default state", () => { | ||
const countSignal = signalsManager.getSignal("count"); | ||
expect(countSignal.value).toBe(0); | ||
}); | ||
|
||
it("should update the signal value", () => { | ||
signalsManager.updateSignal("count", 1); | ||
const countSignal = signalsManager.getSignal("count"); | ||
expect(countSignal.value).toBe(1); | ||
}); | ||
}); |
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,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2020", | ||
"module": "esnext", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"skipLibCheck": true, | ||
"noUnusedLocals": true, | ||
"noImplicitAny": true, | ||
"allowJs": true, | ||
"noEmit": true, | ||
"outDir": "dist", | ||
"resolveJsonModule": true | ||
} | ||
} |