forked from livewire/livewire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path$wire.js
216 lines (168 loc) · 6.51 KB
/
$wire.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { dispatch, dispatchSelf, dispatchTo, listen } from '@/features/supportEvents'
import { generateEntangleFunction } from '@/features/supportEntangle'
import { closestComponent, findComponent } from '@/store'
import { requestCommit, requestCall } from '@/request'
import { WeakBag, dataGet, dataSet } from '@/utils'
import { on, trigger } from '@/events'
import Alpine from 'alpinejs'
import { removeUpload, upload, uploadMultiple } from './features/supportFileUploads'
let properties = {}
let fallback
function wireProperty(name, callback, component = null) {
properties[name] = callback
}
function wireFallback(callback) {
fallback = callback
}
// For V2 backwards compatibility...
// And I actually like both depending on the scenario...
let aliases = {
'on': '$on',
'el': '$el',
'id': '$id',
'get': '$get',
'set': '$set',
'call': '$call',
'commit': '$commit',
'watch': '$watch',
'entangle': '$entangle',
'dispatch': '$dispatch',
'dispatchTo': '$dispatchTo',
'dispatchSelf': '$dispatchSelf',
'upload': '$upload',
'uploadMultiple': '$uploadMultiple',
'removeUpload': '$removeUpload',
}
export function generateWireObject(component, state) {
return new Proxy({}, {
get(target, property) {
if (property === '__instance') return component
if (property in aliases) {
return getProperty(component, aliases[property])
} else if (property in properties) {
return getProperty(component, property)
} else if (property in state) {
return state[property]
} else if (! ['then'].includes(property)) {
return getFallback(component)(property)
}
},
set(target, property, value) {
if (property in state) {
state[property] = value
}
return true
},
})
}
function getProperty(component, name) {
return properties[name](component)
}
function getFallback(component) {
return fallback(component)
}
Alpine.magic('wire', (el, { cleanup }) => {
// Purposely initializing an empty variable here is a "memo"
// so that a component is lazy-loaded when using $wire from Alpine...
let component
// Override $wire methods that need to be cleaned up when
// and element is removed. For example, `x-data="{ foo: $wire.entangle(...) }"`:
// we would want the entangle effect freed if the element was removed from the DOM...
return new Proxy({}, {
get(target, property) {
if (! component) component = closestComponent(el)
if (['$entangle', 'entangle'].includes(property)) {
return generateEntangleFunction(component, cleanup)
}
return component.$wire[property]
},
set(target, property, value) {
if (! component) component = closestComponent(el)
component.$wire[property] = value
return true
},
})
})
wireProperty('__instance', (component) => component)
wireProperty('$get', (component) => (property, reactive = true) => dataGet(reactive ? component.reactive : component.ephemeral, property))
wireProperty('$el', (component) => {
return component.el
})
wireProperty('$id', (component) => {
return component.id
})
wireProperty('$set', (component) => async (property, value, live = true) => {
dataSet(component.reactive, property, value)
return live
? await requestCommit(component)
: Promise.resolve()
})
wireProperty('$call', (component) => async (method, ...params) => {
return await component.$wire[method](...params)
})
wireProperty('$entangle', (component) => (name, live = false) => {
return generateEntangleFunction(component)(name, live)
})
wireProperty('$toggle', (component) => (name, live = true) => {
return component.$wire.set(name, ! component.$wire.get(name), live)
})
wireProperty('$watch', (component) => (path, callback) => {
let firstTime = true
let oldValue = undefined
Alpine.effect(() => {
// JSON.stringify touches every single property at any level enabling deep watching
let value = dataGet(component.reactive, path)
JSON.stringify(value)
if (! firstTime) {
// We have to queue this watcher as a microtask so that
// the watcher doesn't pick up its own dependencies.
queueMicrotask(() => {
callback(value, oldValue)
oldValue = value
})
} else {
oldValue = value
}
firstTime = false
})
})
wireProperty('$refresh', (component) => component.$wire.$commit)
wireProperty('$commit', (component) => async () => await requestCommit(component))
wireProperty('$on', (component) => (...params) => listen(component, ...params))
wireProperty('$dispatch', (component) => (...params) => dispatch(component, ...params))
wireProperty('$dispatchSelf', (component) => (...params) => dispatchSelf(component, ...params))
wireProperty('$dispatchTo', (component) => (...params) => dispatchTo(...params))
wireProperty('$upload', (component) => (...params) => upload(component, ...params))
wireProperty('$uploadMultiple', (component) => (...params) => uploadMultiple(component, ...params))
wireProperty('$removeUpload', (component) => (...params) => removeUpload(component, ...params))
let parentMemo = new WeakMap
wireProperty('$parent', component => {
if (parentMemo.has(component)) return parentMemo.get(component).$wire
let parent = closestComponent(component.el.parentElement)
parentMemo.set(component, parent)
return parent.$wire
})
let overriddenMethods = new WeakMap
export function overrideMethod(component, method, callback) {
if (! overriddenMethods.has(component)) {
overriddenMethods.set(component, {})
}
let obj = overriddenMethods.get(component)
obj[method] = callback
overriddenMethods.set(component, obj)
}
wireFallback((component) => (property) => async (...params) => {
// If this method is passed directly to a Vue or Alpine
// event listener (@click="someMethod") without using
// parens, strip out the automatically added event.
if (params.length === 1 && params[0] instanceof Event) {
params = []
}
if (overriddenMethods.has(component)) {
let overrides = overriddenMethods.get(component)
if (typeof overrides[property] === 'function') {
return overrides[property](params)
}
}
return await requestCall(component, property, params)
})