Skip to content

Commit

Permalink
chore(release): v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcachips authored and Marcachips committed Jun 12, 2023
1 parent 2efd0ca commit 4061a1a
Show file tree
Hide file tree
Showing 27 changed files with 386 additions and 100 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

# [1.5.0](https://github.com/klientjs/core/compare/1.4.0...1.5.0) (2023-06-12)


### Features

* allow listen for changes made on parameters and bag objects ([2efd0ca](https://github.com/klientjs/core/commit/2efd0ca9bd2c5e1645fc6f150e9151fc5646f292))

# [1.4.0](https://github.com/klientjs/core/compare/1.3.1...1.4.0) (2023-06-11)


Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Klient from './klient';
export { AxiosError } from 'axios';
export { default as Extensions } from './extensions';
export { default as Bag } from './services/bag';
export { default as Bag } from './services/bag/bag';
export { default as Dispatcher } from './services/dispatcher/dispatcher';
export { default as RequestFactory } from './services/request/factory';
export { default as Request } from './services/request/request';
Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var axios_1 = require("axios");
Object.defineProperty(exports, "AxiosError", { enumerable: true, get: function () { return axios_1.AxiosError; } });
var extensions_1 = require("./extensions");
Object.defineProperty(exports, "Extensions", { enumerable: true, get: function () { return extensions_1.default; } });
var bag_1 = require("./services/bag");
var bag_1 = require("./services/bag/bag");
Object.defineProperty(exports, "Bag", { enumerable: true, get: function () { return bag_1.default; } });
var dispatcher_1 = require("./services/dispatcher/dispatcher");
Object.defineProperty(exports, "Dispatcher", { enumerable: true, get: function () { return dispatcher_1.default; } });
Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/klient.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Bag from './services/bag';
import Bag from './services/bag/bag';
import Dispatcher from './services/dispatcher/dispatcher';
import RequestFactory from './services/request/factory';
import type Event from './events/event';
Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/klient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bag_1 = require("./services/bag");
const bag_1 = require("./services/bag/bag");
const dispatcher_1 = require("./services/dispatcher/dispatcher");
const factory_1 = require("./services/request/factory");
const extensions_1 = require("./extensions");
Expand Down
10 changes: 0 additions & 10 deletions dist/cjs/services/bag.d.ts

This file was deleted.

36 changes: 0 additions & 36 deletions dist/cjs/services/bag.js

This file was deleted.

17 changes: 17 additions & 0 deletions dist/cjs/services/bag/bag.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Watchable, WatchCallback } from './watch';
declare type BagItems = Record<string, unknown>;
export default class Bag implements BagItems, Watchable {
[x: string]: unknown;
constructor(items?: BagItems);
get watchers(): Record<string, {
callback: WatchCallback<unknown, unknown>;
deep: boolean;
}[]>;
get(path: string): any;
all(): import("../../toolbox/object").AnyObject;
set(path: string, value: unknown): this;
merge(...items: BagItems[]): this;
watch(path: string, onChange: WatchCallback, deep?: boolean): this;
unwatch(path: string, onChange: WatchCallback): this;
}
export {};
45 changes: 45 additions & 0 deletions dist/cjs/services/bag/bag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const deepmerge = require("deepmerge");
const objectPath = require("object-path");
const object_1 = require("../../toolbox/object");
const watch_1 = require("./watch");
class Bag {
constructor(items = {}) {
Object.keys(items).forEach((key) => {
this[key] = items[key];
});
}
get watchers() {
return (0, watch_1.getWatchers)(this);
}
get(path) {
return objectPath.get(this.all(), path);
}
all() {
return (0, object_1.softClone)(this);
}
set(path, value) {
const prevState = this.all();
objectPath.set(this, path, value);
return (0, watch_1.invokeWatchers)(this, this.all(), prevState);
}
merge(...items) {
const prevState = this.all();
const nextState = deepmerge.all([this.all(), ...items], {
arrayMerge: (_destinationArray, sourceArray) => sourceArray,
isMergeableObject: (o) => (0, object_1.isPlainArray)(o) || (0, object_1.isPlainObject)(o)
});
Object.keys(nextState).forEach((key) => {
this[key] = nextState[key];
});
return (0, watch_1.invokeWatchers)(this, nextState, prevState);
}
watch(path, onChange, deep = false) {
return (0, watch_1.watch)(this, path, onChange, deep);
}
unwatch(path, onChange) {
return (0, watch_1.unwatch)(this, path, onChange);
}
}
exports.default = Bag;
11 changes: 11 additions & 0 deletions dist/cjs/services/bag/watch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export declare type Watchable = object;
export declare type WatchCallback<T = unknown, Z = T> = (next: T, prev: Z) => void;
declare type WatcherItem = {
callback: WatchCallback;
deep: boolean;
};
export declare function getWatchers(wachtable: Watchable): Record<string, WatcherItem[]>;
export declare function watch<T extends Watchable>(watchable: T, path: string, onChange: WatchCallback, deep: boolean): T;
export declare function unwatch<T extends Watchable>(watchable: T, path: string, onChange: WatchCallback): T;
export declare function invokeWatchers<T extends Watchable>(watchable: T, next: object, prev: object): T;
export {};
87 changes: 87 additions & 0 deletions dist/cjs/services/bag/watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.invokeWatchers = exports.unwatch = exports.watch = exports.getWatchers = void 0;
const objectPath = require("object-path");
const deepDiff = require("deep-diff");
const instances = {};
const watchers = {};
function getInstanceId(wachtable) {
const ids = Object.keys(instances);
let id = null;
for (let i = 0, len = ids.length; i < len; i += 1) {
if (instances[ids[i]] === wachtable) {
id = ids[i];
break;
}
}
if (id === null) {
id = Math.random().toString(36).substring(2);
instances[id] = wachtable;
}
return id;
}
function getWatchers(wachtable) {
const id = getInstanceId(wachtable);
if (!watchers[id]) {
watchers[id] = {};
}
return watchers[id];
}
exports.getWatchers = getWatchers;
function watch(watchable, path, onChange, deep) {
const id = getInstanceId(watchable);
watchers[id] = getWatchers(watchable);
const collection = watchers[id][path] || [];
for (let i = 0, len = collection.length; i < len; i += 1) {
if (collection[i].callback === onChange) {
return watchable;
}
}
collection.push({ callback: onChange, deep });
watchers[id][path] = collection;
return watchable;
}
exports.watch = watch;
function unwatch(watchable, path, onChange) {
const id = getInstanceId(watchable);
watchers[id] = getWatchers(watchable);
const collection = watchers[id][path] || [];
let index = null;
for (let i = 0, len = collection.length; i < len; i += 1) {
if (collection[i].callback === onChange) {
index = i;
break;
}
}
if (index === null) {
return watchable;
}
collection.splice(index, 1);
watchers[id][path] = collection;
return watchable;
}
exports.unwatch = unwatch;
function invokeWatchers(watchable, next, prev) {
var _a;
const watched = getWatchers(watchable);
const watchedPaths = Object.keys(watched);
if (watchedPaths.length === 0) {
return watchable;
}
const changedPaths = (_a = deepDiff(prev, next)) === null || _a === void 0 ? void 0 : _a.map((diff) => diff.path.join('.'));
const invoked = [];
changedPaths === null || changedPaths === void 0 ? void 0 : changedPaths.forEach((path) => {
watchedPaths.forEach((targetPath) => {
if (!path.startsWith(targetPath))
return;
watched[targetPath].forEach((watcher) => {
if ((path === targetPath || watcher.deep) && !invoked.includes(watcher)) {
watcher.callback(objectPath.get(next, targetPath), objectPath.get(prev, targetPath));
invoked.push(watcher);
}
});
});
});
return watchable;
}
exports.invokeWatchers = invokeWatchers;
4 changes: 4 additions & 0 deletions dist/cjs/toolbox/object.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare type AnyObject = Record<string, unknown>;
export declare function isPlainObject(value: unknown): boolean;
export declare function isPlainArray(value: unknown): boolean;
export declare function softClone(obj: AnyObject): AnyObject;
28 changes: 28 additions & 0 deletions dist/cjs/toolbox/object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.softClone = exports.isPlainArray = exports.isPlainObject = void 0;
function isPlainObject(value) {
return value !== null && typeof value === 'object' && value.constructor.name === 'Object';
}
exports.isPlainObject = isPlainObject;
function isPlainArray(value) {
return Array.isArray(value) && value.constructor.name === 'Array';
}
exports.isPlainArray = isPlainArray;
function softClone(obj) {
const values = {};
Object.keys(obj).forEach((prop) => {
const value = obj[prop];
if (isPlainObject(value)) {
values[prop] = softClone(value);
}
else if (isPlainArray(value)) {
values[prop] = value.map((b) => (isPlainObject(b) ? softClone(b) : b));
}
else {
values[prop] = value;
}
});
return values;
}
exports.softClone = softClone;
2 changes: 1 addition & 1 deletion dist/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Klient from './klient';
export { AxiosError } from 'axios';
export { default as Extensions } from './extensions';
export { default as Bag } from './services/bag';
export { default as Bag } from './services/bag/bag';
export { default as Dispatcher } from './services/dispatcher/dispatcher';
export { default as RequestFactory } from './services/request/factory';
export { default as Request } from './services/request/request';
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Klient from './klient';
export { AxiosError } from 'axios';
export { default as Extensions } from './extensions';
export { default as Bag } from './services/bag';
export { default as Bag } from './services/bag/bag';
export { default as Dispatcher } from './services/dispatcher/dispatcher';
export { default as RequestFactory } from './services/request/factory';
export { default as Request } from './services/request/request';
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/klient.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Bag from './services/bag';
import Bag from './services/bag/bag';
import Dispatcher from './services/dispatcher/dispatcher';
import RequestFactory from './services/request/factory';
import type Event from './events/event';
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/klient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Bag from './services/bag';
import Bag from './services/bag/bag';
import Dispatcher from './services/dispatcher/dispatcher';
import RequestFactory from './services/request/factory';
import Extensions from './extensions';
Expand Down
10 changes: 0 additions & 10 deletions dist/esm/services/bag.d.ts

This file was deleted.

33 changes: 0 additions & 33 deletions dist/esm/services/bag.js

This file was deleted.

17 changes: 17 additions & 0 deletions dist/esm/services/bag/bag.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Watchable, WatchCallback } from './watch';
declare type BagItems = Record<string, unknown>;
export default class Bag implements BagItems, Watchable {
[x: string]: unknown;
constructor(items?: BagItems);
get watchers(): Record<string, {
callback: WatchCallback<unknown, unknown>;
deep: boolean;
}[]>;
get(path: string): any;
all(): import("../../toolbox/object").AnyObject;
set(path: string, value: unknown): this;
merge(...items: BagItems[]): this;
watch(path: string, onChange: WatchCallback, deep?: boolean): this;
unwatch(path: string, onChange: WatchCallback): this;
}
export {};

0 comments on commit 4061a1a

Please sign in to comment.