-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move query proxy to initializer function
- Loading branch information
1 parent
b53c58a
commit b6700be
Showing
2 changed files
with
30 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type Query from './index'; | ||
|
||
const TRAPS = { | ||
get(target: Query, key: string, receiver: Proxy): ?mixed | void { | ||
if (target.model.hasScope(key)) { | ||
const scope = target.model.scopes[key]; | ||
|
||
return (...args) => { | ||
let { snapshots } = scope.apply(target.model, args); | ||
snapshots = snapshots.map(snapshot => [...snapshot, key]); | ||
|
||
target.snapshots.push(...snapshots); | ||
return receiver; | ||
}; | ||
} else { | ||
return target[key]; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @private | ||
*/ | ||
export default function initialize(instance: Query): Query { | ||
return new Proxy(instance, TRAPS); | ||
} |