Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Use dependency injection instead of circular references
Browse files Browse the repository at this point in the history
…to work around Vite bug: vitejs/vite#2491
  • Loading branch information
aral committed Apr 28, 2021
1 parent 6ae5eba commit 519552c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 9 additions & 4 deletions .kit/src/lib/JSDB/IncompleteQueryProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import QueryOperators from './QueryOperators.js'
import QueryProxy from './QueryProxy.js'

export default class IncompleteQueryProxy {

constructor (table, data, property) {
constructor (table, data, property, QueryProxy) {
this.table = table
this.data = data
this.query = property

// Injecting a reference to the QueryProxy class
// since Vite (and thus SvelteKit) cannot handle
// circular references in SSR apps right now.
// (See https://github.com/vitejs/vite/issues/2491)
this.QueryProxy = QueryProxy

// Note: we return a proxy instance; not an instance of DataProxy. Use accordingly.
return new Proxy({}, { get: this.getHandler.bind(this) })
}
Expand All @@ -50,7 +55,7 @@ export default class IncompleteQueryProxy {
return (function (value) {
if (isNaN(value)) value = `'${value}'`
const updatedQuery = `${this.query} ${QueryOperators.RELATIONAL_OPERATORS[property]} ${value}`
return new QueryProxy(this.table, this.data, updatedQuery)
return new this.QueryProxy(this.table, this.data, updatedQuery, IncompleteQueryProxy)
}).bind(this)
} else if (QueryOperators.FUNCTIONAL_OPERATORS.includes(property)) {
return (function (value) {
Expand All @@ -66,7 +71,7 @@ export default class IncompleteQueryProxy {
// so that it’s correct JavaScript.
property = property.replace('CaseInsensitive', '')
updatedQuery += `.${property}(${value})`
return new QueryProxy(this.table, this.data, updatedQuery)
return new this.QueryProxy(this.table, this.data, updatedQuery, IncompleteQueryProxy)
}).bind(this)
}
else {
Expand Down
13 changes: 9 additions & 4 deletions .kit/src/lib/JSDB/QueryProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import QuerySanitiser from './QuerySanitiser.js'
import IncompleteQueryProxy from './IncompleteQueryProxy.js'

export default class QueryProxy {
cachedResult = null

constructor (table, data, query) {
constructor (table, data, query, IncompleteQueryProxy) {
this.table = table
this.data = data
this.query = query

// Injecting a reference to the IncompleteQueryProxy class
// since Vite (and thus SvelteKit) cannot handle
// circular references in SSR apps right now.
// (See https://github.com/vitejs/vite/issues/2491)
this.IncompleteQueryProxy = IncompleteQueryProxy

return new Proxy({}, {
get: this.getHandler.bind(this),
set: this.setHandler.bind(this),
Expand Down Expand Up @@ -71,12 +76,12 @@ export default class QueryProxy {
//
if (property === 'and') return (function (connectiveProperty) {
const incompleteQuery = `${this.query} && valueOf.${connectiveProperty}`
return new IncompleteQueryProxy(this.table, this.data, incompleteQuery)
return new this.IncompleteQueryProxy(this.table, this.data, incompleteQuery)
}).bind(this)

if (property === 'or') return (function (connectiveProperty) {
const incompleteQuery = `${this.query} || valueOf.${connectiveProperty}`
return new IncompleteQueryProxy(this.table, this.data, incompleteQuery)
return new this.IncompleteQueryProxy(this.table, this.data, incompleteQuery)
}).bind(this)

// If nothing else matches, just do the default.
Expand Down

0 comments on commit 519552c

Please sign in to comment.