Skip to content

Commit

Permalink
Automatic!
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikb committed Sep 3, 2020
1 parent 7d5ecbb commit d87b91e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
7 changes: 5 additions & 2 deletions data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ListenerCallback, Listeners, ImmediateListeners } from './listeners.ts'
import { Pathifier } from './pathifier.ts';
import { clean } from './paths.ts';
import { LooseObject, ToCall } from './types.ts';

export * from './types.ts';

function isProbablyPlainObject(obj: any) {
Expand Down Expand Up @@ -274,8 +275,8 @@ export class Data {
const results = listeners.get(path);
let resultValue;
for (let res of results) {
const { path, fullPath } = res;
const listeners = res._;
res.value = value;
for (let [ref, listener] of listeners) {
const refPath = ref + res.path;
if (listener && !refPaths.has(refPath)) {
Expand All @@ -287,7 +288,9 @@ export class Data {
const valIsObject = isProbablyPlainObject(val);
resultValue = listener(val, {
target,
path: res.path,
subPath: fullPath.slice(path.length),
path,
fullPath,
...res.keys,
...(valIsObject
? {
Expand Down
2 changes: 2 additions & 0 deletions data/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { LooseObject } from './types.ts';
export type ListenerCallback<T> = (
value: T,
props: {
subPath: string;
fullPath: string;
path: string;
[key: string]: any;
}
Expand Down
15 changes: 10 additions & 5 deletions data/pathifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ export class Pathifier<T> {
} else {
const subPath = target.slice(this.cleanFrom.length + 1);
const updated = this.set(subPath, this.data.get(target));
if (updated && this._then) this._then(this.cache, { path: subPath });
if (updated && this._then)
this._then(this.cache, { path, fullPath: target, subPath, target });
}
}),
this.data.on(`- ${this.from}`, (_, { target }) => {
this.data.on(`- ${this.from}`, (_, { path, fullPath, target }) => {
const subPath = target.slice(this.cleanFrom.length + 1);
const updated = this.unset(subPath);
if (updated && this._then) {
this._then(this.cache, { path: subPath });
this._then(this.cache, { path, fullPath, subPath, target: fullPath });
}
})
);
Expand Down Expand Up @@ -178,7 +179,7 @@ export class Pathifier<T> {
this.unset(bb);
}
if (updated && this._then) {
this._then(this.cache, { path: '' });
this._then(this.cache, { path: '', fullPath: '', subPath: '' });
}
}

Expand All @@ -200,7 +201,11 @@ export class Pathifier<T> {
const origValue = value;
if (this._map) {
const path = this.keys(this.cleanFrom, k);
value = this._map(this.data.get(path), { path });
value = this._map(this.data.get(path), {
path,
fullPath: path,
subPath: '',
});
key = k;
}

Expand Down
11 changes: 5 additions & 6 deletions domdom/hodor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,18 @@ export class Hodor<T = any> implements Mountable {
if (this._sort) this.pathifier.sort(this._sort);
if (this._sortOn)
this.pathifier.sortOn(this._sortOn.path, this._sortOn.sorterOn);
const self = this;
this.pathifier.toArray({
or(_: number, __: any): void {},

add(value: any, subIndex: number, _?: number, path?: string) {
add: (value: any, subIndex: number, _?: number, path?: string) => {
if (value instanceof Element) {
(value as any).path = path;
(value as any).path = [this.path, path].join('.');
}

self._stower?.add(value, self.index!, subIndex, path);
this._stower?.add(value, this.index!, subIndex, path);
},
remove(value: any, subIndex: number, _: number, path: string) {
self._stower?.remove(value, self.index!, subIndex, path);
remove: (value: any, subIndex: number, _: number, path: string) => {
this._stower?.remove(value, this.index!, subIndex, path);
},
});
}
Expand Down

0 comments on commit d87b91e

Please sign in to comment.