Skip to content

Commit

Permalink
Fixing inheritence and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bcameron1231 committed Jan 4, 2024
1 parent e6de448 commit a81acb0
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 7 deletions.
9 changes: 2 additions & 7 deletions packages/graph/places/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,18 @@ export const RoomLists = graphInvokableFactory<IRoomlists>(_RoomLists);
* Room
*/
export class _Room extends _GraphInstance<IRoomType> {}
export interface IRoom extends _Rooms { }
export interface IRoom extends _Room { }
export const Room = graphInvokableFactory<IRoom>(_Room);

/**
* Rooms
*/
@defaultPath("microsoft.graph.room")
@getById(Room)
export class _Rooms extends _GraphCollection<IRoom> {}
export class _Rooms extends _GraphCollection<IRoomType[]> {}
export interface IRooms extends _Rooms, IGetById<IRoom> { }
export const Rooms = graphInvokableFactory<IRooms>(_Rooms);

export interface IPlacesType {
readonly rooms: IRoomType[];
readonly roomLists: IRoomListType[];
}

export interface IUpdatePlaceProps extends IRoomType, IRoomListType {
"@odata.type": string;
}
97 changes: 97 additions & 0 deletions packages/logging/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
export * from "./listeners.js";
/**
* A set of logging levels
*/
export var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["Verbose"] = 0] = "Verbose";
LogLevel[LogLevel["Info"] = 1] = "Info";
LogLevel[LogLevel["Warning"] = 2] = "Warning";
LogLevel[LogLevel["Error"] = 3] = "Error";
LogLevel[LogLevel["Off"] = 99] = "Off";
})(LogLevel || (LogLevel = {}));
const _subscribers = [];
let _activeLogLevel = 2 /* Warning */;
/**
* Class used to subscribe ILogListener and log messages throughout an application
*
*/
export class Logger {
/**
* Gets or sets the active log level to apply for log filtering
*/
static get activeLogLevel() {
return _activeLogLevel;
}
static set activeLogLevel(value) {
_activeLogLevel = value;
}
/**
* Adds ILogListener instances to the set of subscribed listeners
*
* @param listeners One or more listeners to subscribe to this log
*/
static subscribe(...listeners) {
_subscribers.push(...listeners);
}
/**
* Clears the subscribers collection, returning the collection before modification
*/
static clearSubscribers() {
const s = _subscribers.slice(0);
_subscribers.length = 0;
return s;
}
/**
* Gets the current subscriber count
*/
static get count() {
return _subscribers.length;
}
/**
* Writes the supplied string to the subscribed listeners
*
* @param message The message to write
* @param level [Optional] if supplied will be used as the level of the entry (Default: LogLevel.Info)
*/
static write(message, level = 1 /* Info */) {
Logger.log({ level: level, message: message });
}
/**
* Writes the supplied string to the subscribed listeners
*
* @param json The json object to stringify and write
* @param level [Optional] if supplied will be used as the level of the entry (Default: LogLevel.Info)
*/
static writeJSON(json, level = 1 /* Info */) {
Logger.write(JSON.stringify(json), level);
}
/**
* Logs the supplied entry to the subscribed listeners
*
* @param entry The message to log
*/
static log(entry) {
if (entry !== undefined && Logger.activeLogLevel <= entry.level) {
_subscribers.map(subscriber => subscriber.log(entry));
}
}
/**
* Logs an error object to the subscribed listeners
*
* @param err The error object
*/
static error(err) {
Logger.log({ data: err, level: 3 /* Error */, message: err.message });
}
}
export function PnPLogging(activeLevel) {
return (instance) => {
instance.on.log(function (message, level) {
if (activeLevel <= level) {
_subscribers.map(subscriber => subscriber.log({ level, message }));
}
});
return instance;
};
}
101 changes: 101 additions & 0 deletions packages/logging/listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
export function ConsoleListener(prefix, colors) {
return new _ConsoleListener(prefix, colors);
}
function withColor(msg, color, logMethod) {
if (typeof color === "undefined") {
logMethod(msg);
}
else {
logMethod(`%c${msg}`, `color:${color}`);
}
}
/**
* Formats the message
*
* @param entry The information to format into a string
*/
function entryToString(entry, prefix) {
const msg = [];
if (prefix.length > 0) {
msg.push(`${prefix} -`);
}
msg.push(entry.message);
if (entry.data !== undefined) {
try {
msg.push("Data: " + JSON.stringify(entry.data));
}
catch (e) {
msg.push(`Data: Error in stringify of supplied data ${e}`);
}
}
return msg.join(" ");
}
// index order matters, this is a lookup table based on the corresponding LogLevel value
const colorProps = ["verbose", "info", "warning", "error"];
/**
* Implementation of LogListener which logs to the console
*
*/
class _ConsoleListener {
/**
* Makes a new one
*
* @param prefix Optional text to include at the start of all messages (useful for filtering)
* @param colors Optional text color settings
*/
constructor(_prefix = "", _colors = {}) {
this._prefix = _prefix;
this._colors = _colors;
}
/**
* Any associated data that a given logging listener may choose to log or ignore
*
* @param entry The information to be logged
*/
log(entry) {
let logMethod = console.log;
switch (entry.level) {
case 3 /* Error */:
logMethod = console.error;
break;
case 2 /* Warning */:
logMethod = console.warn;
break;
case 0 /* Verbose */:
logMethod = console.debug;
break;
case 1 /* Info */:
logMethod = console.info;
break;
default:
logMethod = console.log;
}
withColor(entryToString(entry, this._prefix), this._colors[colorProps[entry.level]], logMethod);
}
}
export function FunctionListener(impl) {
return new _FunctionListener(impl);
}
/**
* Implementation of LogListener which logs to the supplied function
*
*/
class _FunctionListener {
/**
* Creates a new instance of the FunctionListener class
*
* @constructor
* @param method The method to which any logging data will be passed
*/
constructor(method) {
this.method = method;
}
/**
* Any associated data that a given logging listener may choose to log or ignore
*
* @param entry The information to be logged
*/
log(entry) {
this.method(entry);
}
}

0 comments on commit a81acb0

Please sign in to comment.