Skip to content

Commit

Permalink
fix: bug
Browse files Browse the repository at this point in the history
  • Loading branch information
weixiangmeng521 committed Dec 18, 2023
1 parent 4ebc4d3 commit a5244af
Show file tree
Hide file tree
Showing 19 changed files with 596 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.DS_Store
lib/
node_modules/
# package-lock.json

Expand Down
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ src
tsconfig.json
tslint.json
.prettierrc
package-lock.json
.github/*
package-lock.json
jestconfig.json
3 changes: 3 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./internal/teleport";
export * from "./internal/service";
export * from "./internal/types";
19 changes: 19 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./internal/teleport"), exports);
__exportStar(require("./internal/service"), exports);
__exportStar(require("./internal/types"), exports);
23 changes: 23 additions & 0 deletions lib/internal/observer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ObserverInterface } from "./types";
/**
* Generic observer class that implements the ObserverInterface.
* The observer is notified of updates through the provided handler function.
* @template T - The type of data that the observer will receive.
*/
export declare class Observer<T> implements ObserverInterface<T> {
/**
* The function called when the observer is updated with new data.
*/
protected handler: (arg: T) => void;
/**
* Constructs an instance of the Observer class with the provided handler function.
* @param {function} handler - The function to be called when the observer is updated.
*/
constructor(handler: (arg: T) => void);
/**
* Updates the observer with new data.
* Invokes the handler function provided during construction.
* @param {T} data - The data to be passed to the observer.
*/
next(data: T): void;
}
30 changes: 30 additions & 0 deletions lib/internal/observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Observer = void 0;
/**
* Generic observer class that implements the ObserverInterface.
* The observer is notified of updates through the provided handler function.
* @template T - The type of data that the observer will receive.
*/
class Observer {
/**
* Constructs an instance of the Observer class with the provided handler function.
* @param {function} handler - The function to be called when the observer is updated.
*/
constructor(handler) {
/**
* The function called when the observer is updated with new data.
*/
this.handler = () => { };
this.handler = handler;
}
/**
* Updates the observer with new data.
* Invokes the handler function provided during construction.
* @param {T} data - The data to be passed to the observer.
*/
next(data) {
this.handler(data);
}
}
exports.Observer = Observer;
Empty file.
37 changes: 37 additions & 0 deletions lib/internal/property_decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
// import { TeleportSingleton } from "./teleport";
// /**
// * Example usage:
// * @class DataReceiver
// */
// class DataReceiver {
// @ReceiveData('exampleEvent')
// public receivedData: string | undefined;
// // ... other properties and methods
// }
// export function TeleportReceive(eventName?: string | symbol): PropertyDecorator {
// return function (target: Object, propertyKey: string | symbol): void {
// const teleportSingleton = TeleportSingleton.getInstantce();
// // Save the original property value
// let value: any;
// // Subscribe to the specified event and update the property with received data
// teleportSingleton.receive<any>(eventName || propertyKey, (data: any) => {
// value = data;
// });
// // Getter function to retrieve the property value
// const getter = function () {
// return value;
// };
// // Setter function to update the property value and subscribe to the event
// const setter = function (newValue: any) {
// value = newValue;
// };
// // Define the property with the getter and setter
// Object.defineProperty(target, propertyKey, {
// get: getter,
// set: setter,
// enumerable: true,
// configurable: true,
// } as PropertyDescriptor); // 明确指定类型为 PropertyDescriptor
// } as PropertyDecorator; // 明确指定类型为 PropertyDecorator
// }
45 changes: 45 additions & 0 deletions lib/internal/service.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TeleportSingleton } from './teleport';
/**
* The `Teleport` class provides a simplified interface to interact with the underlying `TeleportSingleton` instance.
* It encapsulates the logic for emitting and receiving events.
*/
export declare class Teleport {
/**
* The underlying `TeleportSingleton` instance used for event management.
*/
private _teleportSingleton;
/**
* Constructs a new instance of the `Teleport` class.
*/
constructor();
/**
* Emits an event with the specified name and data.
* @template T - The type of data to be emitted.
* @param {string | symbol} name - The name or symbol of the event.
* @param {T} data - The data to be emitted with the event.
* @param {() => void} [callback] - Optional callback to be executed after emitting the event.
* @returns {TeleportSingleton} - The TeleportSingleton instance for chaining.
*/
emit<T>(name: string | symbol, data: T, callback?: () => void): TeleportSingleton;
/**
* Registers a handler function for the specified event.
* @template T - The type of data received by the event.
* @param {string | symbol} name - The name or symbol of the event.
* @param {(data: T) => void} handler - The handler function to process the event data.
*/
receive<T>(name: string | symbol, handler: (data: T) => void): void;
/**
* Removes a specific event handler for the specified event.
* @param {string | symbol} name - The name or symbol of the event.
*/
removeHandle(name: string | symbol): void;
/**
* Removes all event handlers.
*/
removeAllHandlers(): void;
/**
* Clears any internal state related to emitted events.
* This method should be adapted based on the specific requirements of your application.
*/
clear(): void;
}
60 changes: 60 additions & 0 deletions lib/internal/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Teleport = void 0;
const teleport_1 = require("./teleport");
/**
* The `Teleport` class provides a simplified interface to interact with the underlying `TeleportSingleton` instance.
* It encapsulates the logic for emitting and receiving events.
*/
class Teleport {
/**
* Constructs a new instance of the `Teleport` class.
*/
constructor() {
/**
* The underlying `TeleportSingleton` instance used for event management.
*/
this._teleportSingleton = teleport_1.TeleportSingleton.getInstance();
}
/**
* Emits an event with the specified name and data.
* @template T - The type of data to be emitted.
* @param {string | symbol} name - The name or symbol of the event.
* @param {T} data - The data to be emitted with the event.
* @param {() => void} [callback] - Optional callback to be executed after emitting the event.
* @returns {TeleportSingleton} - The TeleportSingleton instance for chaining.
*/
emit(name, data, callback) {
return this._teleportSingleton.emit(name, data, callback);
}
/**
* Registers a handler function for the specified event.
* @template T - The type of data received by the event.
* @param {string | symbol} name - The name or symbol of the event.
* @param {(data: T) => void} handler - The handler function to process the event data.
*/
receive(name, handler) {
this._teleportSingleton.receive(name, handler);
}
/**
* Removes a specific event handler for the specified event.
* @param {string | symbol} name - The name or symbol of the event.
*/
removeHandle(name) {
this._teleportSingleton.removeHandle(name);
}
/**
* Removes all event handlers.
*/
removeAllHandlers() {
this._teleportSingleton.removeAllHandlers();
}
/**
* Clears any internal state related to emitted events.
* This method should be adapted based on the specific requirements of your application.
*/
clear() {
this._teleportSingleton.clear();
}
}
exports.Teleport = Teleport;
50 changes: 50 additions & 0 deletions lib/internal/subject.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Observer } from "./observer";
import { ObserverInterface, SubjectInterface } from "./types";
/**
* Represents a generic subject class that implements the SubjectInterface.
* The subject is responsible for managing a list of observers and notifying them of updates.
* @template T - The type of data that the subject will broadcast to its observers.
*/
export declare class Subject<T> implements SubjectInterface<T> {
/**
* Array to store the observers subscribed to the subject.
*/
private _observers;
/**
* Adds an observer to the list of observers.
* @param {Observer<T>} observer - The observer to be added.
* @returns {void}
*/
protected addObserver(observer: Observer<T>): void;
/**
* Removes an observer from the list of observers.
* @param {Observer<T>} observer - The observer to be removed.
* @returns {void}
*/
removeObserver(observer: Observer<T>): void;
/**
* Notifies all observers with the provided data.
* @param {any} data - The data to be broadcasted to observers.
* @returns {void}
*/
protected notify(data: any): void;
/**
* Subscribes an observer to the subject.
* @param {(value: T) => void} event - The observer function to be subscribed.
* @returns {{ unsubscribe: () => void }} - An object containing the unsubscribe function.
*/
subscribe(obj: ObserverInterface<T>): {
unsubscribe: () => void;
};
/**
* Unsubscribes all observers from the subject.
* @returns {void}
*/
unsubscribe(): void;
/**
* Notifies all observers with the provided value.
* @param {T} value - The value to be broadcasted to observers.
* @returns {void}
*/
next(value: T): void;
}
76 changes: 76 additions & 0 deletions lib/internal/subject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subject = void 0;
const observer_1 = require("./observer");
/**
* Represents a generic subject class that implements the SubjectInterface.
* The subject is responsible for managing a list of observers and notifying them of updates.
* @template T - The type of data that the subject will broadcast to its observers.
*/
class Subject {
constructor() {
/**
* Array to store the observers subscribed to the subject.
*/
this._observers = [];
}
/**
* Adds an observer to the list of observers.
* @param {Observer<T>} observer - The observer to be added.
* @returns {void}
*/
addObserver(observer) {
this._observers.push(observer);
}
/**
* Removes an observer from the list of observers.
* @param {Observer<T>} observer - The observer to be removed.
* @returns {void}
*/
removeObserver(observer) {
const index = this._observers.indexOf(observer);
if (index !== -1) {
this._observers.splice(index, 1);
}
}
/**
* Notifies all observers with the provided data.
* @param {any} data - The data to be broadcasted to observers.
* @returns {void}
*/
notify(data) {
this._observers.forEach(observer => {
observer.next(data);
});
}
/**
* Subscribes an observer to the subject.
* @param {(value: T) => void} event - The observer function to be subscribed.
* @returns {{ unsubscribe: () => void }} - An object containing the unsubscribe function.
*/
subscribe(obj) {
const observer = new observer_1.Observer(obj.next);
this.addObserver(observer);
return {
unsubscribe: () => {
this.removeObserver(observer);
},
};
}
/**
* Unsubscribes all observers from the subject.
* @returns {void}
*/
unsubscribe() {
this._observers = [];
}
/**
* Notifies all observers with the provided value.
* @param {T} value - The value to be broadcasted to observers.
* @returns {void}
*/
next(value) {
this.notify(value);
}
}
exports.Subject = Subject;
Loading

0 comments on commit a5244af

Please sign in to comment.