Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Reactivity #230

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/alpine-ie11.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/alpine-ie11.js.map

Large diffs are not rendered by default.

1,602 changes: 1,601 additions & 1 deletion dist/alpine.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/alpine.js.map

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@babel/preset-env": "^7.8.4",
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-replace": "^2.3.1",
"@testing-library/dom": "^6.12.2",
"@testing-library/jest-dom": "^4.2.4",
"@webcomponents/template": "^1.4.1",
Expand All @@ -32,6 +33,7 @@
"element-remove": "^1.0.4",
"jest": "^24.8.0",
"jsdom-simulant": "^1.1.2",
"observable-membrane": "^0.26.1",
"proxy-polyfill": "^0.3.0",
"rollup": "^1.31.0",
"rollup-plugin-babel": "^4.3.3",
Expand Down
10 changes: 3 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import babel from 'rollup-plugin-babel';
import filesize from 'rollup-plugin-filesize';
import { terser } from "rollup-plugin-terser";
import resolve from "rollup-plugin-node-resolve"
import stripCode from 'rollup-plugin-strip-code';
import replace from '@rollup/plugin-replace';

export default {
input: 'src/index.js',
Expand All @@ -13,14 +13,10 @@ export default {
sourcemap: true,
},
plugins: [
// 'observable-membrane' uses process.env. We don't have that...
replace({ "process.env.NODE_ENV": "'production'" }),
resolve(),
filesize(),
terser({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you're dropping this?

The built alpine.js asset is now unminified

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it only saves like .1KB

Seems not worth it to me. Unminified means way easier debugging.

Copy link
Contributor

@the94air the94air Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's a good idea to leave it there for when the source code grows bigger. But we can add it later anyways. It's fine. Although @calebporzio this will break the code for people who used the CDN links previously. Should be removed starting from the next major release.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing terser shouldn't remove code so not really a breaking change

Have you tried it with mangle: true, seems like that would save a bunch more size by renaming variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @HugoDF. Thanks for your comment. For some reason, I thought we already had a minified version of the alpine in the "dist" folder. But we actually didn't.

This was the "dist" content before merging this pull request.
https://github.com/alpinejs/alpine/tree/a6496895b4ddea560244c43e4e83407861814a70/dist

JSDeliver supports auto minifying (by adding ".min" before the file extension) so it's not actually needed at all. Sorry for my misleading statement @calebporzio

mangle: false,
compress: {
drop_debugger: false,
},
}),
babel({
exclude: 'node_modules/**'
}),
Expand Down
40 changes: 17 additions & 23 deletions src/component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { walk, saferEval, saferEvalNoReturn, getXAttrs, debounce, deepProxy } from './utils'
import { walk, saferEval, saferEvalNoReturn, getXAttrs, debounce } from './utils'
import { handleForDirective } from './directives/for'
import { handleAttributeBindingDirective } from './directives/bind'
import { handleShowDirective } from './directives/show'
import { handleIfDirective } from './directives/if'
import { registerModelListener } from './directives/model'
import { registerListener } from './directives/on'
import ObservableMembrane from 'observable-membrane'

export default class Component {
constructor(el, seedDataForCloning = null) {
Expand All @@ -25,7 +26,9 @@ export default class Component {
/* IE11-ONLY:END */

// Construct a Proxy-based observable. This will be used to handle reactivity.
this.$data = this.wrapDataInObservable(this.unobservedData)
let { membrane, data } = this.wrapDataInObservable(this.unobservedData)
this.$data = data
this.membrane = membrane

// After making user-supplied data methods reactive, we can now add
// our magic properties to the original data for access.
Expand Down Expand Up @@ -65,28 +68,25 @@ export default class Component {
}

getUnobservedData() {
let rawData = {}
let unwrappedData = this.membrane.unwrapProxy(this.$data)
let copy = {}

Object.keys(this.unobservedData).forEach(key => {
Object.keys(unwrappedData).forEach(key => {
if (['$el', '$refs', '$nextTick'].includes(key)) return

rawData[key] = this.unobservedData[key]
copy[key] = unwrappedData[key]
})

return rawData
return copy
}

wrapDataInObservable(data) {
var self = this

const proxyHandler = {
set(obj, property, value) {
// Set the value converting it to a "Deep Proxy" when required
// Note that if a project is not a valid object, it won't be converted to a proxy
const setWasSuccessful = Reflect.set(obj, property, deepProxy(value, proxyHandler))

let membrane = new ObservableMembrane({
valueMutated(target, key) {
// Don't react to data changes for cases like the `x-created` hook.
if (self.pauseReactivity) return setWasSuccessful
if (self.pauseReactivity) return

debounce(() => {
self.updateElements(self.$el)
Expand All @@ -96,19 +96,13 @@ export default class Component {
self.nextTickStack.shift()()
}
}, 0)()

return setWasSuccessful
},
get(target, key) {
// Provide a way to determine if this object is an Alpine proxy or not.
if (key === "$isAlpineProxy") return true
})

// Just return the flippin' value. Gawsh.
return target[key]
}
return {
data: membrane.getProxy(data),
membrane,
}

return deepProxy(data, proxyHandler)
}

walkAndSkipNestedComponents(el, callback, initializeComponentCallback = () => {}) {
Expand Down
36 changes: 0 additions & 36 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,42 +361,6 @@ export function transition(el, stages) {
});
}

export function deepProxy(target, proxyHandler) {
// If target is null, return it.
if (target === null) return target;

// If target is not an object, return it.
if (typeof target !== 'object') return target;

// If target is a DOM node (like in the case of this.$el), return it.
if (target instanceof Node) return target

// If target is already an Alpine proxy, return it.
if (target['$isAlpineProxy']) return target;

// Otherwise, proxy the properties recursively.
// This enables reactivity on setting nested data.
for (let property in target) {
// Don't Proxy readonly properties.
if (propertyIsReadOnly(target, property)) continue

target[property] = deepProxy(target[property], proxyHandler)
}

return new Proxy(target, proxyHandler)
}

function propertyIsReadOnly(target, property) {
let propertyDescriptor = Object.getOwnPropertyDescriptor(target, property)

while (typeof propertyDescriptor === 'undefined' && target !== null) {
target = Object.getPrototypeOf(target)
propertyDescriptor = Object.getOwnPropertyDescriptor(target, property);
}

return propertyDescriptor && ! propertyDescriptor.writable
}

function isNumeric(subject){
return ! isNaN(subject)
}