Skip to content

Commit

Permalink
feature: first iteration done
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-TheGentleman committed May 3, 2024
1 parent 8311d5b commit da7adae
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
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
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
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}}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
75 changes: 75 additions & 0 deletions README.md
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! 🚀🔔
8 changes: 8 additions & 0 deletions build.mjs
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()]
})
Binary file added bun.lockb
Binary file not shown.
30 changes: 30 additions & 0 deletions package.json
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"
}
}
1 change: 1 addition & 0 deletions src/domain/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./signals-adapter";
15 changes: 15 additions & 0 deletions src/domain/adapters/signals-adapter.ts
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);
}
}
2 changes: 2 additions & 0 deletions src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./adapters";
export * from "./signals-domain";
13 changes: 13 additions & 0 deletions src/domain/signals-domain.ts
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;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./init";
15 changes: 15 additions & 0 deletions src/init.ts
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;
}
35 changes: 35 additions & 0 deletions src/signals_manager.ts
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);
}
}
29 changes: 29 additions & 0 deletions test/index.test.ts
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);
});
});
16 changes: 16 additions & 0 deletions tsconfig.json
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
}
}

0 comments on commit da7adae

Please sign in to comment.