generated from klientjs/open-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Marcachips
authored and
Marcachips
committed
Jun 12, 2023
1 parent
2efd0ca
commit 4061a1a
Showing
27 changed files
with
386 additions
and
100 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
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
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
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 {}; |
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,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; |
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,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 {}; |
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,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; |
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,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; |
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,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; |
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
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
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 {}; |
Oops, something went wrong.