Skip to content

Commit

Permalink
Finally have some reactivity on components
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Sep 23, 2023
1 parent e880d2a commit 1c3fa6d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 38 additions & 13 deletions packages/reactivity/src/lib/components.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createApp, reactive, nextTick } from 'petite-vue';
import { getMappableProperties, createReactiveStore, mapReactiveStore } from './scope';
import {
getMappableProperties,
createReactiveStore,
mapReactiveStore,
updateReactiveStore,
} from './scope';
import { getTemplate } from './template';

/**
Expand Down Expand Up @@ -60,16 +65,25 @@ function prepareComponents(parent, mountElement) {
}

function updateDataStore(scope) {
const mappable = getMappableProperties.call(this);
const componentMappable = getMappableProperties.call({ ...scope });

this.$data = reactive(createReactiveStore.call(this, {
...mappable,
...componentMappable,
}));

mapReactiveStore.call(this, mappable);
mapReactiveStore.call(this, componentMappable);
nextTick(() => {
const mappable = getMappableProperties.call(this);
const componentMappable = getMappableProperties.call({ ...scope });

if (!Object.keys(this.$data).length) {
this.$data = reactive(createReactiveStore.call(this, {
...mappable,
...componentMappable,
}));

mapReactiveStore.call(this, mappable);
mapReactiveStore.call(this, componentMappable);
} else {
updateReactiveStore.call(this, {
...mappable,
...componentMappable,
});
}
});
}

/**
Expand All @@ -80,8 +94,12 @@ function updateDataStore(scope) {
*/
function linkBlock(oldMount, newMount) {
const block = this.$parent.$context.ctx.blocks.find((b) => b.template === oldMount);
block.template = newMount;
updateDataStore.call(this, block.ctx.scope);

if (block) {
block.template = newMount;
updateDataStore.call(this, block.ctx.scope);
this.$block = block;
}
}

/**
Expand All @@ -94,6 +112,13 @@ function componentCleanup(context, parent) {
if (componentIndex !== -1) {
parent.$components[context.exp].splice(componentIndex, 1);
}

// Trigger data update on remaining components
Object.values(parent.$components).forEach((components) => {
components.forEach((component) => {
updateDataStore.call(component, component.$context.ctx.scope);
});
});
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/reactivity/src/lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function getMappableProperties() {
'detach',
'template',
'mountTo',
'components',
'$app',
'$nextTick',
'$reactive',
'$mount',
'$el',
Expand Down Expand Up @@ -161,8 +164,17 @@ function mapReactiveStore(mappable) {
});
}

function updateReactiveStore(mappable) {
Object.entries(mappable).forEach(([key, prop]) => {
if (prop.type === 'value') {
this.$data[key] = prop.value;
}
});
}

export {
getMappableProperties,
createReactiveStore,
mapReactiveStore,
updateReactiveStore,
};

0 comments on commit 1c3fa6d

Please sign in to comment.